# 1: enumerate:下标和数据同时用  经常用于循环
my_list = ["a", "b", "c"]
for index, value in enumerate(my_list):
    print(index, value)

#输出结果:
# 0 a
# 1 b
# 2 c

my_str = "abc"
for index, value in enumerate(my_str):
    print(index, value)

#输出结果:
# 0 a
# 1 b
# 2 c

# 2:定义函数  并且  查看文档说明:格式:help(函数名)
help(max)
# Help on built-in function max in module builtins:
#
# max(...)
#     max(iterable, *[, default=obj, key=func]) -> value
#     max(arg1, arg2, *args, *[, key=func]) -> value
#
#     With a single iterable argument, return its biggest item. The
#     default keyword-only argument specifies an object to return if
#     the provided iterable is empty.
#     With two or more arguments, return the largest argument.


def maxleng(a, b):
    """文档说明"""
    if a > b:
        return a
    else:
        return b

returnmark = maxleng(10, 20)
print(returnmark)
help(maxleng)
#Help on function maxleng in module __main__:

# maxleng(a, b)
    # 文档说明


# 3:在函数内对全局变量进行修改
a = 100

def test():
    global a
    a = 200
    print(a)

test()
print("全局变量中的a:%d" % a)

#输出结果
#200
# 全局变量中的a:%d 200


# 4:缺省参数
def sum_num(num1, num2=10):
    return num1 + num2
# 缺省参数的后面不能再定义非缺省参数

# 传参的三种方式: 位置传参,关键字传参,位置--关键字传参
def show_info(name, age, sex):
    """姓名性别年龄描述"""

    print("您的姓名是:%s" % name)
    print("您的年龄是:%d" % age)
    print("您的性别是:%s" % sex)

#d位置传参:
show_info("任善文", 23, "男")
print("------------------")

#按关键字传参
show_info(name="任善文", age=23, sex='男')
print("_____________________")

# 位置参数和关键字参数混合使用:位置参数必须在关键字参数之前
show_info("任善文", sex="男", age=24)

# 5:字典的items属性为元组类型
my_dict = {'name': "任善文", "age": 23, 'sex': "男"}
for value in my_dict.items():
    print(value)
    print(type(value))

#运行结果为:

#('name', '任善文')
# <class 'tuple'>
# ('age', 23)
# <class 'tuple'>
# ('sex', '男')
# <class 'tuple'>

print("-----------------")
# 6:不定长参数的使用:不定长位置参数(*args)(**kwargs),不定长关键字参数
def show_info(*args):
    print("args:", args, type(args))


show_info(1, 2, 3)
#args: (1, 2, 3) <class 'tuple'>

# :show_info只能接受位置参数,如果show_info(1, a= 10)则会报错

print("----------------")
# 7:打包成字典再进行传参
def show_msg(**kwargs):
    print("kwargs:", kwargs, type(kwargs))

show_msg(a=1, b=2, c=3 )
# kwargs: {'a': 1, 'b': 2, 'c': 3} <class 'dict'>

print("-----------------------")
# 8 :复核使用
def show(*args, **kwargs):
    print("args:", args, type(args))
    print("kwargs", kwargs, type(kwargs))

show(1, 2, 3)
print("-------------------")
# args: (1, 2, 3) <class 'tuple'>
# # kwargs {} <class 'dict'>


show(a=1, b=2, c=3)
print("-------------------")
# args: () <class 'tuple'>
# kwargs {'a': 1, 'b': 2, 'c': 3} <class 'dict'>

show(1, 2, a=2, b=3)
print("--------------------")

# args: (1, 2) <class 'tuple'>
# kwargs {'a': 2, 'b': 3} <class 'dict'>

# 9:演练:
# 定义一个函数,用于计算传入数据的和:提示:传入数据的个数不确定
def sum_num (*args ,**kwargs):
    result = 0
    for value in args:
        result += value
    for value in kwargs.values():
        result += value
    return result

my_data = sum_num(1, 3, a=1, b=2)
print(my_data)



# 10:缺省参数存在的问题:
def showinfo(name, age = 18, *args):
    print("name:", name, "age:", age, "args:", args)

showinfo("王五", 20, 1, 3, 5)
# 缺点:缺省参数无法使用。


#改正方法:把缺省参数放在不定长位置参数的后面
def show_info(name, *args, age = 18):
    print("name:", name, "age:", age, "args:", args )

show_info("任善文", 1, 2, 3, age = 20)
show_info("李健健", 1, 2, 3)
#运行结果:
name: 任善文 age: 20 args: (1, 2, 3)
name: 李健健 age: 18 args: (1, 2, 3)

#背:顺序:参数,不定长位置参数,缺省参数,不定长关键字参数

#不定长参数的扩展:
# 将元组中的数据按照位置参数方式传参
#将字典中的数据按照关键字的参数进行传参
def show_info(*args, **kwargs):
    print("args:", args, type(args))
    print("kwargs:", kwargs, type(kwargs))

my_tuple = (1, 2, 3, 4)
my_dict = {"name": "李四", "age": 23, "sex": "男"}
show_info(*my_tuple, **my_dict)

# name: 王五 age: 20 args: (1, 3, 5)
# name: 王五 age: 18 args: (1, 3, 5)
# args: (1, 2, 3, 4) <class 'tuple'>
# kwargs: {'name': '李四', 'age': 23, 'sex': '男'} <class 'dict'>



# 11:拆包:容器类型数据全部交给变量保存(字符串 ,range, 列表,元组,字典,集合)
# 变量的数据必须和容器内保持一致
my_str = "abc"
v1, v2, v3 = my_str
print(v1, v2, v3)


value1, value2 = range(1, 5, 2)
print(value1, value2)

#默认对key进行拆包
my_dict = {"name": "张飞", "age": 23}
data1, data2 = my_dict
print(data1, data2)

#如果对数据进行拆包
data1, data2 = my_dict.values()
print(data1, data2)


# 12:交换两个变量

#方法一:
a = 1
b = 2
a, b = b, a
print(a, b)

# 方法二:
a = 1
b = 2

c = a
a = b
b = c
print(a,b)


#方法三:
a = 1
b = 2

a = a + b
b = a - b
a = a - b
print(a, b)


# 13: id 获取变量在内存中的地址
#对于数字,元组,字符串,地址相同,对于列表,字典,变量不同
num1 = 1
num2 = 1
print(id(num1) == id(num2))


list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(id(list1) == id(list2))

tuple1 = (1, 2, 3)
tuple2 = (1, 2, 3)
print(id(tuple1) == id(tuple2))

str1 = "abc"
str2 = "abc"
print(id(str1) == id(str2))

dict1 = {"name": "任善文"}
dict2 = {"name": "任善文"}
print(id(dict1) == id(dict2))

# 14:不可变类型:数字,字符串,元组

# 1>:无法使用del关键字删除内部值
my_str = "abc"
# del my_str[0]  :错误用法

# 2>重新赋值,内存地址改变
print(id(my_str))
my_str = "abcd"
print(id(my_str))

# 3>:不能修改内部值
# my_str[0] = 'b'  :错误用法

# 4>:实际上replace只是返回一个新的字符串
value = my_str.replace("b", '>>>')
print(value, id(value))

# 15:可变类型:列表,字典,集合
my_list = [1, 2, 3]
print(id(my_list))
my_list[0] = 0
print(id(my_list))

# id 发现是相同的