num += 1 就没有必要了
# 计算1-100的总和、奇数和、偶数和
sum_all = 0
sum_even = 0
sum_odd = 0
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))
# 打印9*9乘法表
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()
# 使用列表和字典存储表格的数据
r1 = {"name": "高小一", "age": 18, "salary": 30000, "city": "北京"}
r2 = {"name": "高小二", "age": 19, "salary": 20000, "city": "上海"}
r3 = {"name": "高小五", "age": 20, "salary": 10000, "city": "深圳"}
tb = [r1, r2, r3]
for x in tb:
if x.get("salary")>15000:
print(x)
# 推导式创建序列
# 列表推导式
y = [x for x in range(10)]
print(y)
y = [x**2 for x in range(1, 11)]
print(y)
y = [x*2 for x in range(1, 50) if x % 5 == 0]
print(y)
y = [a for a in 'abcdefg']
print(y)
cells = [(line, col) for line in range(5) for col in range(5)]
print(cells)
for cell in cells:
print(cell)
# 字典推导式
# 统计文本中字符出现的次数
My_text = 'I love you, I love the world, I love study, I love life'
text_count = {c: My_text.count(c) for c in My_text}
print(text_count)
综合练习-绘制不同颜色的多个同心圆-绘制棋盘
# 绘制同心圆
import turtle
t = turtle.Pen()
my_colors = ('red', 'blue', 'yellow', 'black', 'green', 'cyan')
t.width(3)
t.speed(0)
for i in range(10): # 0, 1, 2, 3, 4,,,,
t.penup()
t.goto(0, -10*i) # 0,-10,-20,-30,,,
t.pendown()
t.color(my_colors[i % len(my_colors)])
t.circle(50+10*i) # 50,60,70,80,,,
turtle.done()
# 绘制棋盘
import turtle
t = turtle.Pen()
# t.goto(180, 0)
# t.goto(0, -10)
# t.goto(180, -10)
# t.goto(0, -20)
# t.goto(180, -20)
for i in range(19): # 0, 1, 2, 3, 4, 5, 6, 7,,,,
t.penup()
t.goto(0, -10*i) # 0, -10, -20, -30
t.pendown()
t.goto(180, -10*i) # 0,-10,-20,,,
for i in range(19): # 0, 1, 2, 3, 4, 5, 6, 7,,,,
t.penup()
t.goto(i*10, 0) # 0, 10, 20, 30,,,
t.pendown()
t.goto(i*10, -180) # 0, 10, 20, 30,,,
turtle.done()
# 函数
def test01(a, b):
'''计算a与b的平均值'''
print("{0}与{1}的平均值为:{2}".format(a, b, (a+b)/2))
test01(5, 8)
def test02():
print("bsjy")
print("Python")
return
test02()
def test03(a, b, c):
return [a+b, a+c, (a+b+c)/2]
print(test03(1, 2, 3))