目录
# 局部变量
# 形参也是局部变量
def func_1(num_1,num_2):
# result在函数内部定义的,因此是局部变量
result = num_1 + num_2
print(result)
func_1(1,2)
# 3
# 全局变量
print("-"*10)
num = 1
def func_1(num_1,num_2):
global num
print(num)
# 1
num = num_1 + num_2
print(num)
# 3
func_1(1,2)
print(num)
# 3
# 位置参数
def func_1(num_1,num_2):
print(num_1)
print(num_2)
func_1(1,2)
# 1,2
print("-"*20)
# 关键字参数
def func_1(num_1,num_2):
print(num_1)
print(num_2)
func_1(num_1=1,num_2=2)
func_1(num_2=1,num_1=2)
print("-"*20)
# 参数默认值
def func_1(num_1,num_2=10):
print(num_1)
print(num_2)
func_1(num_1=1)
func_1(num_1=1,num_2=2)
# *args 元组不定长参数:传递的元素个数不确定
# 计算多个数字的和
def func_1(*args):
print(args,type(args))
print(sum(args))
func_1(1)
func_1(1,2,3)
print("-"*20)
def func_1(num_1,num_2):
print(num_1)
print(num_2)
return num_1+num_2
result = func_1(func_1(1,2),3)
print(result)
# **kwargs 字典不定长参数
def func_1(**kwargs):
print(kwargs,type(kwargs))
func_1() # 空字典
func_1(a=1)
func_1(a=1,b=2)
# func_1(1,2)
# func_1({'a': 1, 'b': 2}) 不能给字典不定长直接传字典
print("-"*20)
# 四类参数的顺序:普通参数、元组不定长、默认值参数、字典不定长
def func_1(a, *args, b=666,**kwargs):
print(a)
print(args,type(args))
print(b)
print(kwargs,type(kwargs))
# func_1(1,2,3,4,5,6,7,8,9,10)
func_1(1,2,3,4,5,b=6,c=7,d=8,e=9,f=10)
# func_1(1,2,3,4,b=5,6,c=7,d=8,e=9,f=10)
def func_1(my_dict):
print(my_dict)
func_1({'a': 1, 'b': 2})
# 先组包再拆包
a, b, c = 1, 2, 3
print(a, b, c)
# 交换变量
a, b = 10, 20
print(f'交换前a={a},b={b}')
a, b = b, a
print(f'交换后a={a},b={b}')
# 交换前a=10,b=20 ,交换后 a=20,b=10
# 函数多个返回值
def many_return():
return 10, 20, 30
a, b, c = many_return()
print(a, b, c)
# 10 20 30
# 遍历字典
dict1 = {'a': '老大', 'b': '老三', 'c': '老三'}
for index, (k, v) in enumerate(dict1.items()):
print(index, k, v)
# 0 a 老大
# 1 b 老三
# 2 c 老三
a = 10
b = a
c = a
print(a, id(a))
print(b, id(b))
print(c, id(c))
# 10 2249041013328
# 10 2249041013328
# 10 2249041013328
a = 'hello'
b = 'world'
c = a + b
print(a, id(a))
print(b, id(b))
print(c, id(c))
# hello 2249042057648
# world 2249042057968
# helloworld 2249046609456
print("-" * 10)
num_1 = 10
print("函数运行前", num_1, id(num_1))
# 函数运行前 10 2249041013328
def func_1(num):
print("函数运行时", num, id(num))
return num
# 函数运行时 10 2249041013328
result = func_1(num=num_1)
print("函数运行结束后", result, id(result))
# 函数运行结束后 10 2249041013328
#
# 可变和不可变类型
# 可变
my_list = [1, 2]
print(my_list, id(my_list))
# [1, 2] 2249048768384
my_list.append(3)
print(my_list, id(my_list))
# [1, 2, 3] 2249048768384
# 不可变
my_tuple = (1, 2)
print(my_tuple, id(my_tuple))
# (1, 2) 2249042045824
my_tuple = my_tuple + (3,)
print(my_tuple, id(my_tuple))
# (1, 2, 3) 2249046608576 不可变类型,所以这个拼接操作相当于生成了新的元组
list1 = [1, 2, [3, 4]]
list2 = list1.copy()
# 复制了过来
print('list1:', list1, id(list1))
print('list2:', list2, id(list2))
# list1: [1, 2, [3, 4]] 2280092815936
# list2: [1, 2, [3, 4]] 2280092900160
# id不一样
list1[2][0] = 5
print('修改了list1中的某个值')
print("list1:", list1, id(list1))
print("list2:", list2, id(list2))
# list1: [1, 2, [5, 4]] 2193288286528
# list2: [1, 2, [5, 4]] 2193288370752
# 利用copy函数,复制的对象id发生了改变,原列表的值改变,新列表也跟着变
from copy import deepcopy # 导包使用deppcopy深度拷贝函数
list1 = ['bwj', 'lhl', 'lxz']
list2 = deepcopy(list1)
print("list1:", list1, id(list1))
print("list2:", list2, id(list2))
# list1: ['bwj', 'lhl', 'lxz'] 1730845841792
# list2: ['bwj', 'lhl', 'lxz'] 1730845757888
print('修改list1中的某个值')
list2[0] = 'hwj'
print('list1:', list1, id(list1))
print('list2:', list2, id(list2))
# list1: ['bwj', 'lhl', 'lxz'] 1794599590400
# list2: ['hwj', 'lhl', 'lxz'] 1794599506432
# range 范围
ret1 = range(1, 100, 20)
print(type(ret1), ret1)
# <class 'range'> range(1, 100, 20)
print(list(ret1))
# [1, 21, 41, 61, 81]
# range函数默认从0开始,默认步长为1
ret2 = range(0, 10, 1)
print(list(ret2))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 依然是左闭右开
new_list = [i for i in range(0, 100, 20)]
print(new_list)
# [0, 20, 40, 60, 80]
# range函数+列表推导式
print("-"*20)
my_list = []
for i in range(1,11):
my_list.append(i)
print(my_list)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_list_2 = [i for i in range(1,11)]
print(my_list_2)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_list_2 = [i for i in range(1,11) if i%2==0]
# 求偶数
print(my_list_2)
# [14, 28, 42, 56, 70]