sum1=0
a=0
b=0
for i in range(101):
sum1+=i
if i%2==1:
a+=i
else:
b+=i
print("总和为{0}:,奇数和为{1}:,偶数和为{2}:".format(sum1,a,b))
#循环嵌套测试
for y in range(5):
for x in range(5):
print(y,end="\t")
print()
#打印乘法表
for m in range(1,10):
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,salary=20000,city="北京")
r2=dict(name="丽丽",age=18,salary=12000,city="北京")
r3=dict(name="安安",age=18,salary=20003,city="北京")
tb=[r1,r2,r3]
for x in tb:
if x.get("salary")>15000:
print(x)
#break和con
while True:
h=input("请输入一个字符(输入Q或者q时退出:)")
if h=="q" or h=="Q":
print("循环结束")
break
else:
print(h)
empNum=0
salaryNum=0
salary=[]
while True:
p=input("输入薪资,(按Q或者q结束)")
if p.upper()=='Q':
print("录录完成,退出")
break
if float(p)<0:
continue
empNum+=1
salary.append(float(p))
salaryNum+=float(p)
print("员工数{0}".format(empNum))
print("薪资统计",salary)
print("平均薪资{0}".format(salaryNum/empNum))
输出为
总和为5050:,奇数和为2500:,偶数和为2550:
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
11=1
21=2 22=4
31=3 32=6 33=9
41=4 42=8 43=12 44=16
51=5 52=10 53=15 54=20 55=25
61=6 62=12 63=18 64=24 65=30 66=36
71=7 72=14 73=21 74=28 75=35 76=42 77=49
81=8 82=16 83=24 84=32 85=40 86=48 87=56 88=64
91=9 92=18 93=27 94=36 95=45 96=54 97=63 98=72 9*9=81
{‘name’: ‘泽泽’, ‘age’: 18, ‘salary’: 20000, ‘city’: ‘北京’}
{‘name’: ‘安安’, ‘age’: 18, ‘salary’: 20003, ‘city’: ‘北京’}
empNum=0
salaryNum=0
salary=[]
while True:
p=input("输入薪资,(按Q或者q结束)")
if p.upper()=='Q':
print("录录完成,退出")
break
if float(p)<0:
continue
empNum+=1
salary.append(float(p))
salaryNum+=float(p)
print("员工数{0}".format(empNum))
print("薪资统计",salary)
print("平均薪资{0}".format(salaryNum/empNum))
#zip()并行迭代
names=("高一","高二","高三","高四")
ages=(16,12,14,17)
jobs=("程序员","老师","演员")
# for name,age,job in zip(names,ages,jobs):
# print("{0}--{1}--{2}".format(name,age,job))
for i in range(3):
print("{0}--{1}--{2}",names[i],ages[i],jobs[i],)
#推导式
#列表推导式
y=[x*2 for x in range(1,50)]
print(y)
o=[]
for v in range(1,50):
if v%5==0:o.append(v*2)
print(o)
cells=[(row,col)for row in range(1,10)for col in range(1,10)]
print(cells)
#字典推导式
my_text="i love you,i love sxt,i lov gaoqi"
char_count={c:my_text.count(c)for c in my_text}
print(char_count)
#集合推导式
str1={x for x in range(1,100) if x%9==0}
print(str1)
#生成器推导式(元组推导式)生成器只能用一次
gnt=(x for x in range(4))
print(gnt)
#print(tuple(gnt))
for x in gnt:
print(x,end=",")
print(tuple(gnt))
输出为
输入薪资,(按Q或者q结束)100
输入薪资,(按Q或者q结束)200
输入薪资,(按Q或者q结束)300
输入薪资,(按Q或者q结束)-10
输入薪资,(按Q或者q结束)Q
录录完成,退出
员工数3
薪资统计 [100.0, 200.0, 300.0]
平均薪资200.0
{0}–{1}–{2} 高一 16 程序员
{0}–{1}–{2} 高二 12 老师
{0}–{1}–{2} 高三 14 演员
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
[10, 20, 30, 40, 50, 60, 70, 80, 90]
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)]
{‘i’: 4, ’ ': 6, ‘l’: 3, ‘o’: 5, ‘v’: 3, ‘e’: 2, ‘y’: 1, ‘u’: 1, ‘,’: 2, ‘s’: 1, ‘x’: 1, ‘t’: 1, ‘g’: 1, ‘a’: 1, ‘q’: 1}
{99, 36, 72, 9, 45, 81, 18, 54, 90, 27, 63}
0,1,2,3,()
print("hellow")
def test01():
print("*"*10)
print("@"*10)
for i in range(10):
test01()
#形参和实参的基本用法
def printMax(a,b):
'''比较两个数的最大值'''
if a>b:
print("较大值",a)
else:
print("较大值是",b)
printMax(20,30)
help(printMax.__doc__)
#函数的返回值
def add(a,b):
print("计算两个数的和{0},{1},{2}".format(a,b,(a+b)))
return a+b
def test02():
print("bbbb")
print("nnn")
return
print("hello")
def text03(x,y,z):
return [x*10,y*10,z*10]
c=add(30,40)
print(c)
test02()
print(text03(10,20,30))
输出为
hellow
@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@
较大值是 30
No Python documentation found for ‘比较两个数的最大值’.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.
计算两个数的和30,40,70
70
bbbb
nnn
[100, 200, 300]
import turtle
my_color=("red","green","blue","black")
t=turtle.Pen()
t.width(4)
t.speed(10)
for i in range(10):
t.penup()
t.goto(0,-i*10)
t.pendown()
t.color(my_color[i%len(my_color)])
t.circle(10+i*10)
turtle.done()