目录
提供了一种手段,不依赖索引取值
for 变量名(会拿到容器类元素的每一个值,没有了就结束循环) in 容器类元素:
? print(变量名)
for + break
for i in range(50,101,3): # 顾头不顾尾,3表示步长
if i == 53:
break # 中断循环
print(i)
for + continue
for i in range(50,101,3): # 顾头不顾尾,2表示步长
if i == 53:
continue # 跳出本次循环,不执行下面的代码
for +else(仅作了解)
for i in range(50,101,3):
if i == 1000:
break
print(i)
else:u
print('如果没有被break终止我就打印')
作用:年龄/id
定义方式:x=10
使用方法: +,-,*,/,%,//,**,<,>,=,!=,==,>=,<=
有序or无序(有索引的就有序,无索引的就无序):没这一说
可变or不可变(值变id不变叫可变,值变id变叫不可变):整型不可变
x = 10
print(id(x))
x += 1
print(id(x))
lt = [1, 2, 3]
print(id(lt))
lt.append(4)
print(lt)
print(id(lt))
作用:薪资
定义方式:x = 10.1
使用方法: +,-,*,/,%,//,**,<,>,=,!=,==,>=,<=
有序or无序: 压根没有有序无序一说
可变or不可变:不可变
x = 10.1
print(id(x))
x +=1
print(id(x))
作用:存储多个值
定义方式:[]内用逗号隔开多个元素
使用方法
索引取值/索引修改:
it = [1,2,3,4]
print(it[1])
# 2
it[1] = 3
print(it)
# [1, 3, 3, 4]
切片:
it = [1,2,3,4]
print(it[:])
# [1,2;,3,4]
print(lt[1:4:2]) # 2表示步数
# [3,4]
for循环:
it = [1,2,3,4]
for i in it:
print(i ** 2)
# 1 4 9 16
成员运算:
it = [1,2,3,4]
print(1 in it)
# True
print(5 in it)
# False
len:
it = [1,2,3,4]
print(len(it))
# 4
append:
it = [1,2,3,4]
it.append(5)
print(lt)
# [1, 2, 3, 4, 5]
del删除:
it = [1,2,3,4]
print(lt)
del lt[0]
print(lt)
# [2, 3, 4]
insert
lt = [1,2,3,4,5]
lt.insert(0.0) # 往前插入
print(lt)
# [0, 1, 2, 3, 4, 5]
pop
lt = [11,22,33,44,55]
lt.pop(0) # 按索引值删除
print(lt)
# [22, 33, 44, 55]
remove
lt = [11,22,33,44,55]
lt.remove(22) # 按照值删除
print(li)
# [11,33,44,55]
count
lt = [11,11,11,44,55]
print(lt.count(11)) #统计个数
# 3
index
lt = [11,11,11,44,55]
print(index(11)) # 寻找值的索引
# 0
clear
lt = [11,11,11,44,55]
lt.clear
print(lt) # 清空列表
# []
copy
lt = [1, 2, 3, 4]
lt1 = lt.copy() # 拷贝列表
print(lt1)
# lt1 = [1, 2, 3, 4]
extend
lt1 = [1, 2, 34]
lt2 = [1, 1, 2, ]
lt1.extend(lt2) # 扩展列表
print(lt1)
# [1, 2, 34, 1, 1, 2]
reverse
lt = [1, 2, 3, 4]
lt.reverse() # 反转列表
print(lt)
# [4, 3, 2, 1]
sort
lt = [2, 3, 1, 0, 4]
lt.sort() # 排序
print(lt)
# [0, 1, 2, 3, 4]
lt.sort(reverse=True) # 反转列表
print(lt)
# [4, 3, 2, 1, 0]
s ='hello world'
print(s[3]) # l
print(s[-1]) # d
s ='hello world'
print(s[::-1] ) # dlrow olleh
print(s[4:5]) # o
s ='hello world'
print(len(s)) # 11个长度
s ='hello world'
print('nice' in s) # False
print('nice' not in s) # Ture
s ='hello world'
print(s.strip('h')) # 两边开始移除
#ello world )
s ='hello world'
print(s.split('w')) # 随意一个字符进行切割,在全部转换为列表形式
# ['hello ', 'orld']
s ='hello world'
for i in s:
print(i,end='') #一次打印出变量名中的字符
lstrip&rstrip
lower&uppre
startswith&endswith
rsplit
join
replace
sdigit
s ='Hello : world***'
d = 'hello teacher'
print(s.rstrip('*')) # 右边开始去
# Hello : world
print(s.lstrip('He')) # 左边开始去
# llo : world***
s ='Hello : world***'
d = 'hello teacher'
print(s.lower()) # 将大写字母小写
# hello : world***
print(s.upper()) #将小写字母大写
# HELLO : WORLD***
s ='Hello : world***'
d = 'hello teacher'
print(s.startswith('Hello')) # 以....开始
# True
print(s.endswith('***')) # 以....结束
# True
s ='Hello : world***'
d = 'hello teacher'
print(s.rsplit(':')) # 切割不需要的字符在转换为列表
# ['Hello ', ' world***']
s ='Hello : world***'
d = 'hello teacher'
it = s.split(':') # 用变量名 接收s的值,并且切割出':',转换为列表
print(it)
# ['Hello ', ' world***']
print(''.join(it)) # 在将重新定义的变量名进行拼接
# Hello world***
s ='Hello : world***'
d = 'hello teacher'
print( s.replace('Hello','morning')) #取出需要代替的值,和输入代替的值
# morning : world***
s ='Hello : world***'
d = 'hello teacher'
print(d.isdigit()) # False
age = input('age')
if age.isdigit():
print('good')
else:
print('bad')
原文:https://www.cnblogs.com/hj59988326/p/11519812.html