1.循环语句
(1)for循环
for x in (10,12,13):
print(x*2)
d = {'name':'selene','age':22}
for x in d:
print(x)
for x in d.keys():
print(x)
for x in d.values():
print(x)
for x in d.items():
print(x)
sum = 0
sum_even = 0
sum_odd = 0
for i in range(21):
sum = sum+i
print(sum)
for i in range(1,20,2):
sum_even = sum_even +i
print(sum_even)
for i in range(0,21,2):
sum_odd = sum_odd +i
print(sum_odd)
sum = 0
sum_even = 0
sum_odd = 0
for i in range(21):
sum = sum+i
if i%2==1:
sum_even = sum_even + i
else:
sum_odd = sum_odd + i
print('总和:{0},奇数和:{1},偶数和:{2}'.format(sum,sum_even,sum_odd))
(2)嵌套循环
重点关注第三个打印表
'''
打印
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
'''
for i in range(5):
for j in range(5):
print(i,end='\t')
print()
for i in range(1,10):
for j in range(1,10):
if i>=j:
print('{0}*{1}={2}'.format(i,j,i*j),end='\t')
print()
tb = []
r1 = dict(name = '高小一',age = 18,salary=3000,city='北京')
r2 = dict(name = '高小二',age = 19,salary=8000,city='上海')
r3 = dict(name = '高小三',age = 20,salary=15000,city='重庆')
tb = [r1,r2,r3]
for i in tb:
print(i)
for i in tb:
if i.get('salary')>10000:
print(i)
(3)break
while True:
a = input('输入一个字母:')
if a.upper() == "Q":
break
else:
print(a)
```(4)continue
![在这里插入图片描述](https://img-blog.csdnimg.cn/c2fc7ddd46cf46778d6d25754e8f310c.png)
```python
r ={}
i = 0
while True:
name = input('输入员工的姓名:')
s = input('输入员工的薪资(按q或Q退出):')
if name.upper() =='Q':
print('录入完成!')
break
else:
if int(s)<0:
continue
else:
r[i] ={name:int(s)}
i+=1
print('共录入{0}个人的工资信息'.format(i))
for x in r.items() :
print(x,end='\t')
(4)else while和for配套的else,在循环语句正常执行结束后执行,但由break强行跳出后不会执行
while i in range:
print:('apple')
else:
print('end')
(5)循环代码优化
import time
start = time.time()
for i in range(1000):
result = []
for j in range(10000):
result.append(i*1000+j*100)
end = time.time()
print('耗时时间:{0}'.format(end-start))
start = time.time()
for i in range(1000):
result = []
x =1*1000
for j in range(10000):
result.append(x + j * 100)
end = time.time()
print('耗时时间:{0}'.format(end - start))
结果: 其他优化方法: 连接多个字符串,用join(),不用+ 列表进行元素插入和删除时,尽量在尾部不在中间
2. zip()进行并行迭代
name = ('aa','bb','cc')
age = (11,22,33)
job = ('doc','stu','pro')
for name,age,job in zip(name,age,job):
print('{0}--{1}--{2}'.format(name,age,job))
for i in range(3):
print('{0}--{1}--{2}'.format(name[i],age[i],job[i]))
3. 推导式
(1)列表推导式
(2)字典推导式
(3)集合推导式
集合本身与字典的区别就在于没有value b = {i for i in range(10) if i%2==0}
(4)生成器推导式(生成元组)
生成器可循环遍历,但是也是只能一次
4.综合练习
画同心圆 import turtle
t = turtle.Pen()#定义画笔 color = [‘yellow’,‘blue’,‘red’,‘pink’,‘green’]
t.width(3)#画笔宽度 t.speed(0)#速度很快 for i in range(100): t.penup() t.goto(0,-i10) t.pendown() t.color(color[i%len(color)]) t.circle(10i+10)
turtle.done()#设置程序执行完,窗口仍存在
5.函数基础知识
(1)函数分类
(2)函数定义
def test01():
'''
打印一排分隔符(这部分是函数的文档字符串)help
'''
print('*'*10)
test01()
test01()
print('该函数的id:{0}'.format(id(test01)) )
help(test01._doc_)
结果;
(3)形参和实参
这里函数定义时括号里a,b是形参,调用时的10,20是实参 形参和实参必须一一对应
(4)函数的返回值
def add(a,b):
print('计算两个数的和:{0},{1},{2}'.format(a,b,a+b))
return a+b
def test02():
print('aaa')
print('bbb')
return
print('ccc')
def test03(a,b,c):
return [a*2,b*2,c*2]
c = add(3,8)
test02()
print(test03(1,2,3))
结果:
|