遍历列表
list = [11,22,33,44,55]
for num in list:
print(num)
列表遍历 + break + else
list = [11,22,33,44,55]
for num in list:
if(num === 22)
print(num)
break # 提前终止遍历
else: # 条件前的都在此打印
print(num)
列表添加元素,append,append 不会返回任何结果
list = [11,22,33]
list1 = [45,78]
list2 = list.append(list1) # list2 是none,在python中,none表示没有任何东西
print(list) # [11, 22, 33, [45, 78]]
元组 Tuple
与列表相似,不同之处,元组的元素不可修改(只支持看)。元组用小括号,列表用中括号
aTuple = ('xcs',78,45)
type(aTuple) # <class 'tuple'>
eg:
# 元组赋值也是引用
a = (1, 3)
b = a
print(b) # (1,3)
# 取值(解构)
c,d = a # c=1 d=3
字典
由 key 和 value 键值对组成,key 必须要加单引号或者双引号
info = {'name': 'xcs', 'age':18}
遍历字典
info = {'name': 'xcs', 'age': '18'}
# keys
for temp in info.keys():
print(temp) # 显示 name age
# values
for temp in info.values():
print(temp) # 显示 xcs 18
# items
for temp in info.items():
print('key=%s,value=%s'%(temp[0],temp[1]))
# items 或者该写成: print('key=%s,value=%s'%(temp[0],temp[1]))
for A,b in info.items():
print('key=%s,value=%s'%(A,b)) # key=name,value=xcs key=age,value=18
函数定义
def fnName(a, b):
result = a + b
c = a - b
print('这是定义函数%d+%d=%d'%(a,b,result))
return result,c # 返回值(可以一次返回多个)
d,e = fnName(23, 43) # 调用函数
print(d) # 打印返回值 125
print(e) # 打印返回值 -35
全局变量在函数中的使用
num = 5 # 若方法中使用到该变量,则必须在该方法调用前定义此全局变量
def fnName(a, b):
global num # 声明此处的num是全局变量,若没有此处的声明,则num是局部变量(字典和列表,不加声明也不会报错,但为了增加可读性,建议加上)
num = a * 2 # 这里会改变全局变量num的值
result = a + b + num
print('这是定义函数%d+%d=%d'%(a,b,result))
fnName(10, 20) # 调用函数
print(num) # 打印返回值
help 查看函数的功能
help(print)
# 输入结果如下:
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
help 使用于自定义函数
def fnName():
'''这是自定义函数fnName''' # 通过help可以查看这个定义(单引号或双引号才有效)
print('hello')
fnName() # hello
help(fnName)
# 如下输出结果
Help on function fnName in module __main__:
fnName()
这是自定义函数
函数 缺省参数
缺省参数如果没有传入,则会使用默认值
def test(a, b=22): # b是缺省值,若不传,则默认是22
res = a + b
print('res=%d'%res)
test(20) # res=42
函数 缺省参数 命名参数-绕过某参数
def test(a, b=22, c=33): # b是缺省值,若不传,则默认是22
res = a + b + c
print('res=%d'%res)
test(11, c=22) # 这里直接绕过b,直接设置c参数。此处的c是命名参数
test(d=10, c=22, a = 33) # 若都是命名参数,这里是按名字对应,而非按位置对应传递
函数 缺省参数 *args
*args 会把对应剩下的参数,会以元组的形式都给 args,
def test(a, b=22, c=33, *args):
res = a + b + c
print('res=%d'%res)
print(args) # (44, 55)
test(11,22,33,44,55)
函数 缺省参数 *args **kwargs
**kwargs 会把对应剩下的参数,会以字典的形式都给 **kwargs
def test(a, b=22, c=33, *args, **kwargs):
res = a + b + c
print('res=%d'%res)
print(args) # (44, 55)
print(kwargs) # {'task': 88, 'done': 99}
test(11,22,33, 44,55, task=88,done=99)
#也可以这么写 拆包
A = (44,55)
B = {'task': 88, 'done': 99}
test(11,22,33, *A, **B)
id 方法
可以获取某个变量的地址
abc = 123
ddd = abc
id(abc) # 1638961647664
id(ddd) # 1638961647664 地址一样,说明他们对应同一个值
在 python 中,字符串,数字,元组,都是不可变的
字符串,数字,元组,都可以作为字典的 key,但列表不可以