strs ='12345'for i in strs:print(i)for i inrange(1,6,2):print(i)print(len(strs))for i inrange(len(strs)):print(strs[i])
结果:
12345
六、循环1~100
import time
n =0while n<100:
n = n+1print(n)
七、计算器
1.可不可以连续计算
whileTrue:
c =float(input('请输入第一个数字'))print("""
1 +
2 -
3 *
4 /
""")
f =input('请输入符号的序号')
c1 =float(input('请输入第二个数字'))if f=='1':print(c+c1)elif f=='2':print(c - c1)elif f=='3':print(c * c1)elif f=='4':print(c / c1)else:print('error')2.功能好的计算器
strs=''whileTrue:
c =input('请输入数字')
strs = strs + c
if c!=strs:print(strs,'=',eval(strs))
f =input('请输入符号,按q清零')if f=='q':
strs=''continue
strs=strs+f
print(strs)
八、== 列表:list基础知识 ==
lists =[1,2,3,4,5,6]print(id(lists))1.修改
lists[0]='hello'print(lists,id(lists))2.增加
(1)append
lists =[1,2,3,4,5,6]
lists.append(123)print(lists)(2)+ 会创建新的内存空间
lists =[1,2,3,4,5,6]print(id(lists))
a = lists+[123]print(a,id(a))(3)extend
lists =[1,2,3,4,5,6]
lists.extend([123])print(lists)(4)insert
lists =[1,2,3,4,5,6]
lists.insert(1,'hello')print(lists)3.删除
(1)pop
lists =[1,2,3,4,5,6]
a = lists.pop(-2)print(lists,a)(2)remove
lists =[1,2,3,4,5,6]
lists.remove(lists[-2])print(lists)(3)del
lists =[1,2,3,4,5,6]del lists[-2]print(lists)(4)clear
lists =[1,2,3,4,5,6]
lists.clear()print(lists)4.查询
lists =[1,2,3,4,5,6]for i in lists:print(i)for i inrange(len(lists)):#要填长度print(lists[i])#注意i要加[]
九、课后题:提取列表里面的字母
方法一
lists =['hello',123,'wo','#er%wew$','1231sgtwe$@#']
strs =''for i inrange(65,91):
strs= strs+chr(i)
words = strs+strs.lower()
ll =[]for i in lists:
i =str(i)print('++++++++++++++++++')
flag =1for ii in i:if ii notin words:
flag=0breakif flag:
ll.append(i)print(ll)
结果:['hello','wo']
方法二
import re
lists =['hello',123,'wo','#er%wew$','1231sgtwe$@#']
ll =' '.join(map(lambda x:re.search('^([a-zA-Z]+)$',x).groups(0)[0]if re.search('^[a-zA-Z]+$',x)else'',[str(x)for x in lists]))print(ll)
结果:lists =['hello',123,'wo','#er%wew$','1231sgtwe$@#']
十、课后题斐波那契数列
lists =[0,1,1,2,3,5,8]
x1 =0
y1 =1for i inrange(20):# 实现原理、两两互换# x = y1# y = x1 + y1# print(y)# x1 = x# y1 = y
x1,y1 = y1,x1+y1
print(y1)
十一、课后题水仙花数
for x inrange(100,1000):
s =str(x)
bai =int(s[0])
shi =int(s[1])
ge =int(s[2])if x == bai**3+ shi**3+ ge**3:print(x)