if "小甲鱼"=="小姐姐":
print("小甲鱼是小姐姐")
else:
print("小甲鱼不是小姐姐")
score=input("请输入分数")
score=int(score)
if 0<=score<60:
print("D")
elif 60<=score<80:
print("C")
else:
print("A")
#99乘法表
i=1
while i<=9:
j=1
while j<=i:
print(j,"*",i,"=",j*i,end=" ")
j+=1
else:
print() #默认换行
i+=1
for each in "fish": #正确
print(each)
for each in 10000: #错误
print(each)
(1)range 整数数列
range(stop)---【0到stop)
for each in range(11):
print(each)
range(start,stop)
for each in range(5,10):
print(each)
range(start,stop,step) step为跨度
for each in range(5,10,2):
print(each,end=" ")
for each in range(5,10,-2):
print(each,end=" ")
(2)小应用---------找出10以内所有素数
for n in range(2,11):
for x in range(2,n):
if n%x==0:
break
else:
print(n,"是一个素数")#for else
a=[1,2,3,4,5,"上山打老虎"]
for each in a:
print(each)
a[0] a[1] a[2]
1.
a[0:3]#------[1,2,3]
a[:3]#---------[1,2,3]
a[3:]#-------------[4,5,"上山打老虎"]
a[:]#-------------[1,2,3,4,5,"上山打老虎"]
a[0:6:2]#-------------[1.3.5]
a[::2]#--------------[1,3,5]
b=a[::-2]#----------------[5,3,1]
切片并非原地翻转而是导出一个新对象
2.
a[-6] a[-5] a[-4] a[-3] a[-2] a[-1]
1. str1.append()
heros=["钢铁侠","绿巨人"]
heros.append("黑寡妇") #一次加一个
2.extend()
heros.extend(["鹰眼","灭霸","雷神"])#一次加多个
3.用切片的方式
s=[1,2,3,4,5]
s[len(s):]=[6,7,8,9]
print(s) #结果 [1,2,3,4,5,6,7,8,9]
4.插入 s.insert(pos,item)
s=[1,2,3,4,5]
s.insert(len(s),6)
s.insert(0,0)
print(s)
1.remove
heros=["钢铁侠","绿巨人","鹰眼","灭霸","雷神"]
heros.remove("灭霸")#有相同元素时,只会删去下标最小的那一个
print(heros)
2.pop()
heros=["钢铁侠","绿巨人","鹰眼","灭霸","雷神"]
print(heros.pop(2))#----鹰眼
3.clear()
heros=["钢铁侠","绿巨人","鹰眼","灭霸","雷神"]
print(heros.clear())
1.下标直接改
heros=["钢铁侠","绿巨人","鹰眼","灭霸","雷神"]
heros[4]="钢铁侠"
2.切片
heros=["钢铁侠","绿巨人","鹰眼","灭霸","雷神"]
heros[3:]=["武松","林冲"]
1.升序
nums=[3,1,9,6,8,3,5,3]
nums.sort()
print(nums)
2.降序
nums=[3,1,9,6,8,3,5,3]
nums.sort()
nums.reverse()
print(nums)
1.a.count(item)#item出现次数
2.a.index(item,start,end)#返回item下标,相同时,默认下标最小的那一个
nums=[3,1,9,6,8,3,5,3]
print(nums.count(3))#查找3出现次数
heros=["钢铁侠","绿巨人","鹰眼","灭霸","雷神"]
print(heros.index("绿巨人"))
heros[heros.index("绿巨人")]="神奇女侠"
print(heros)
1.str1.copy()
nums=[3,1,9,6,8,3,5,3]
nums_1=nums.copy() #直接赋值两个列表会相互影响 nums_1=nums引用
print(nums_1)
2.切片
nums=[3,1,9,6,8,3,5,3]
nums_2=nums[:]
print(nums_2)
3.嵌套拷贝
浅拷贝
x=[[1,2,3],[4,5,6],[7,8,9]]
y=x.copy() # y=x
浅拷贝(用模块)
import copy
x=[[1,2,3],[4,5,6],[7,8,9]]
y=copy.copy(x)
x[1][1]=0
print(x)
print(y)
#[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
#[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
深拷贝(用模块)
import copy
x=[[1,2,3],[4,5,6],[7,8,9]]
y=copy.deepcopy(x)
x[1][1]=0
print(x)
print(y)
#[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
#[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
s=[1,2,3]
t=[4,5,6]
print(s+t)
a[[1,2,3],[4,5,6][7,8,9]
s=[[0]*3 for i in range(3)]#列表里面放什么元素
print(s)
#[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
s=[i for i in range(10) if i%2==0]
print(s)
words=["great","fishc","brilliant","excellent","fantastic"]
f_word=[i for i in words if i[0]=='f']
print(f_word)
#['fishc', 'fantastic']
#嵌套的列表推导式
max=[[1,2,3],[4.5.6],[7,8,9]]
s=[col for row in max for col in row]
a(1,2,3,4,5)
1.a.count(item)#item出现次数
2.a.index(item,start,end)#返回item下标,相同时,默认下标最小的那一个
t=(123,"fishc",3.14)
x,y,z=t
print(x)
print(y)
print(z)
#123
#fishc
#3.14
同时也适用于任何序列类型
列表
t=[123,"fishc",3.14]
x,y,z=t
print(x)
print(y)
print(z)
#123
#fishc
#3.14
字符串
a,b,c,d,e="fishc"
print(a)
print(b)
print(c)
print(d)
print(e)
#f
#i
#s
#h
#c
案例---判断字符串是否是回文数
分析:字符串也是序列,用切片的方式让其反转,判断与原来的是否相等
x="12321"
y=x[::-1]
print("是回文数") if x==y else print("不是回文数")
(1)count()---元素出现次数
x="上海自来水来自上海"
print(x.count("海"),0,5)
(2)find()----从左往右寻找
rfind()-----从右往左寻找
(3)a.index(item,start,end)#返回item下标,相同时,默认下标最小的那一个
lstrip()
rstrip()
strip()
(1)+
(2)join()
print("".join(("f","ish","c")))
year=2010
print("鱼c工作室成立于{}年".format(year))
print("{1}看到{0}就很激动".format("小甲鱼","漂亮的小姐姐"))
#漂亮的小姐姐看到小甲鱼就很激动
1.都可以以索引
2.拼接
判断同一性
x is y#---true(同一个对象)
x=[[1,2,3],[4,5,6],[7,8,9]]
y=x
print(x is not y)#---false
3.in / not in (序列中是否包含某个元素)
print("鱼"in"鱼c")--true
4.删除
x=[1,2,3,4,5]
del x[1:4]#[1,5]
x=[1,2,3,4,5]
x[1:4]=[]#[1:5]
5.min() / max()
print(max([1,2,3,4,5]))
print(min("fishc"))
6.sum()
s=[1,0,0,8,6]
sum(s)#15
7.d=sorted(s)#生成一个全新的列表
s=[1,2,3,4,5,6]
d=sorted(s.reverse=True)
print(d)#[6, 5, 4, 3, 2, 1]
s.sort()#原地翻转
8.
print(list(reversed("fishc")))#必须reverse可迭代对象
9.迭代器
vx=[1,2,3,4,5]
y=iter(x)
for eact in x:
print(next(y,"没了"),end=" ")
def myfunc(name):
for i in range(3):
print(f"i love {name}")
myfunc("python")
#i love python
#i love python
#i love python
注:f"...... "=".......".format()
参数
def myfunc(a,b,c):
return "".join((a,b,c))
print(myfunc("小甲鱼","打了","我"))
关键字参数(位置参数必须在关键字参数之前)优先放在右边
def myfunc(a,b,c):
return "".join((a,b,c))
print(myfunc(c="小甲鱼",b="打了",a="我"))
默认参数
def myfune(a,b="苹果",c="小甲鱼")
return "".join((c,a,b))
print(myfune("拱了"))
#小甲鱼拱了苹果
元组作为实参
#打包
def myfune(*arges):
print(f"有{len(arges)}个参数")
print("第2个参数是{}".format(arges[1]))
print(myfune("小甲鱼是","笨蛋"))
#解包
arges=(1,2,3,4)
def myfune(a,b,c,d)
print(a,b,c,d)
myfune(*arges)