#1-100的累加
num2 = 0
sum =0
while num2<=100:
sum =sum +num2
num2 +=1
print('累加:',sum)
##for
for x in(10,20,30):
print(x*30)
for y in "abcdef":
print(y)
d = {'name':'高琪','age':18,'job':'程序猿'}
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_all =0
sum_odd =0#100内的奇数和
sum_even =0 #100以内的偶数和
for x in range(101):
sum_all += x
if x%2==1 :
sum_odd +=x
else:
sum_even +=x
print('1-100累加{0},奇数{1},偶数{2}'.format(sum_all,sum_odd,sum_even))
#嵌套循环
for x in range(5):
for y in range(5):
print(x,end='\t') #不一个数一个数换行
print() #换行作用
#乘法表
for m in range(1,10):
s = ''
for n in range(1,m+1):
print('{0}*{1}={2}'.format(m,n,(m*n)),end='\t')
print() #换行
#使用列表和字典储存表格的数据
tb = []
r1 = dict(name='高小一',age =18,slary=30000,city='北京')
r2 = dict(name='高小二',age =19,slary=20000,city='上海')
r3 = dict(name='高小三',age =20,slary=10000,city='广东')
tb =[r1,r2,r3]
for x in tb:
if x.get('slary')>15000:
print(x)
##break中断
##continue 结束本次循环
while True:
a = input('请输入一个字符(输入Q或Q时推出):')
if a == 'q' or a=='Q':
print('循环结束,退出')
break
else:
print(a)
##print(*******)
empnum =0
salsrysum =0
salarys =[]
while True:
s = input('请输入员工的薪资(按Q或q结束)')
if s.upper() =='Q':
print('录入完成,退出')
break
if float(s) <0:
continue
empnum += 1
salarys.append(float(s))
salsrysum += float(s)
print('员工数{0}',format(empnum))
print('录入薪资',salarys)
print('平均薪资{0}',format(salsrysum/empnum))
##else 语句
salsrysum =0
salarys =[]
for i in range(4):
s = input('请输入员工的薪资(按Q或q结束)')
if s.upper() =='Q':
print('录入完成,退出')
break
if float(s) <0:
continue
salarys.append(float(s))
salsrysum += float(s)
else:
print('您已经全部录入4名员工的薪资')
print('录入薪资',salarys)
print('平均薪资',format(salsrysum/4))
##循环代码的优化
import time
start = time.time()
for i in range(1000):
result = []
for n in range(1000):
result.append(i*1000+m*100)
end = time.time()
print('耗时:{0}'.format(end-start))
start2 =time.time()
for i in range(1000):
result =[]
c = i*1000
for m in range(10000):
result.append(c+m*100)
end2 =time.time()
print('耗时:{0}'.format((end2-start2)))
##zip()并行迭代---有问题
for i in [1,2,3]:
print(i)
name = ('高琪','高老二','高老三','高老四')
ages = (18,16,20,25)
jobs = ('老师','程序猿','公务员')
for name,ages,job in zip(name,ages,jobs):
print('{0}-{1}-{2}'.format(name,ages,jobs))
for i in range(3):
print('{0}-{1}-{2}'.format(name[i],ages[i],jobs[i]))
##测试推导式
y = [x*2 for x in range(1,5) if x%5==0]
print(y)
y=[]
for x in range(1,50):
if x%5 ==0:y.append(x*2)
print(y)
cells =[(row,col) for row in range(1,10) for col in range(1,10)]
print(cells)
#字典推导式char_count 计算字符出现的次数
my_text = 'I love you, i loce sxt ,i loe gaoqi'
char_count = {c:my_text.count(c) for c in my_text}
print(char_count)
##输入普通循环,实现上诉推导式字数出现次数的实现
###集合推导式
b={x for x in range(1,100) if x%9 ==0}
print(b)
##生成器推导/元组推导
gnt =(x for x in range(10))
print(tuple(gnt))
for x in gnt: #gnt是从生成器对象,可循环
print(x,end=',')
print(tuple(gnt))
#画园
import turtle
t = turtle.pen()
for i in range(5):
t.penump() ##提笔
t.goto(0,-1*10)
t.penump()
t.circle(15+i*10)
turtle.done() #程序执行完,窗口存在
##
import turtle
t = turtle.pen()
t.goto(0,0)
t.circle(10)
t.goto(0,-10)
t.circle(20)
t.goto(0,-20)
t.circle(30)
##
import turtle
t = turtle.pen()
my_colors = ('red','green','yellow','black')
t.width(4) #线的粗细
t.speed(0) #运行的速度
for i in range(10):
t.penup() #提笔
t.goto(0,-i*10)
t.pendom
t.color(my_colors[i%len(my_colors)]) # 颜色
t.circle(15+i*10)
#画棋盘
import turtle
width=30 #线的粗细
num =18
x1 = [(-400,100),(-400+width*num,400)]
x1 = [(-400,100),(-400,400+width*num,)]
t =turtle.pen()
for i in range(0,19):
a.penup() #提笔
a.goto(x1[0][0],x1[0][1]-30*i)
a.pendom
a.goto(x1[0][0],x1[0][1]-30*i)
for i in range(0,19):
a.penup() #提笔
a.goto(y1[0][0],y1[0][1]-30*i)
a.pendom
a.goto(y1[0][0],y1[0][1]-30*i)
#函数 function
#def 函数名(参数列表)
def test01():
print('*'*10)
print('@'*10)
test01()
test01()
test01()
for i in range(10):
test01()
#形参和实参
#形参就是定义函数
def printmax(a,b):
'''用于比较两个数的大小,打印较大的值'''
if a > b:
print(a,'较大值')
else:
print(b, '较大值')
printmax(10,20)
printmax(200,300)
help(printmax.__doc__) #显示备注
#返回值
def add(a,b):
print('计算两个数的和:{0},{1},{2}'.format(a,b,(a+b)))
return a+b
c = add(30,40)
print(add(30,40)*10)
def test02():
print('sxt')
print('gao')
return #return 1.返回值 2.结束函数执行
d = test02()
print(d)
def test03(x,y,z):
return[x*10,y*10,z*10]
print(test03(4,5,3))
第6天打卡-for,while循环,迭代,画棋盘
最新推荐文章于 2024-08-15 01:55:01 发布