本文为6月30日Python基础学习笔记,分为五个章节:
d = {"name": "小李", "age": 18, "job": "工人"}
for x in d:
print(x)
>>> name
>>> age
>>> job
for x in d.keys():
print(x)
>>> name
>>> age
>>> job
for x in d.values():
print(x)
>>> 小李
>>> 18
>>> 工人
for x in d.items():
print(x)
>>> ('name', '小李')
>>> ('age', 18)
>>> ('job', '工人')
break 语句用来结束整个循环。 当有嵌套循环时,break 语句只能跳出最近一层的循环。
continue 语句用于结束本次循环,继续下一次。 多个循环嵌套时,continue 应用于最近的一层循环。
如果 for、while 语句没有被 break 语句结束,则会执行 else 子句。
遵守下面三个原则,以避免不必要的低效计算:
# 循环代码优化测试
import time
start = time.time()
for i in range(1000):
result = []
for m in range(10000):
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))
>>> 耗时:0.9497463703155518
>>> 耗时:0.8246762752532959
zip() 函数在最短序列用完时就会停止。
names = ("高淇","高老二","高老三","高老四")
ages = (18,16,20,25)
jobs = ("老师","程序员","公务员")
for name, age, job in zip(names, ages, jobs):
print("{0}**{1}**{2}".format(name, age, job))
>>> 高淇**18**老师
高老二**16**程序员
高老三**20**公务员
语法:
[x*2 for x in range(1,20) if x%5
>>> [10, 20, 30]
cells = [(row,col) for row in range(1,10) for col in range(1, 10)]
>>> [(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)]
语法如下:
my_text = ' i love you, i love sxt, i love g'
char_ccount = {c: my_text.count(c) for c in my_text}
char_count
char_ccount
>>> {' ': 9, 'i': 3, 'l': 3, 'o': 4, 'v': 3, 'e': 3, 'y': 1, 'u': 1, ',': 2, 's': 1, 'x': 1, 't': 1, 'g': 1}
元组没有推导式。 只能通过生成器生成。
scq = (x for x in range(1, 100) if x%9 == 0)
for x in scq:
print(x, end=' ')
>>> 9 18 27 36 45 54 63 72 81 90 99
score = int(input("输入分数:"))
grade = "ABCDE"
if score>100 or score<0:
print("!!!")
else:
num = score//10
if num<6:
num = 5
print("分数:{0},等级:{1}".format(score,grade[9 - num]))
>>> 输入分数:80
分数:80,等级:B
num = 0
sum = 0
sum_odd = 0
sum_even = 0
while num in range(101):
sum = sum + num
if num%2 != 0:
sum_odd += num
else:
sum_even += num
num += 1
print("1-100的和为:{0}, 奇数和:{1}, 偶数和:{2}".format(sum, sum_odd, sum_even))
>>> 1-100的和为:5050, 奇数和:2500, 偶数和:2550
sum_all = 0
sum_odd = 0
sum_even = 0
for x in range(101):
sum_all += x
if x%2 != 0:
sum_odd += x
else:
sum_even += x
print("1-100累加和{0}, 奇数和{1}, 偶数和{2}".format(sum_all, sum_odd, sum_even))
>>> 1-100累加和5050, 奇数和2500, 偶数和2550
for x in range(5):
for y in range(5):
print(x, end='\t')
print() # print 一行后换行
>>> 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 m in range(1, 10):
for n in range(1, m+1):
print("{0}*{1} = {2}".format(m, n, (m*n)), end='\t')
print()
>>> 1*1 = 1
2*1 = 2 2*2 = 4
3*1 = 3 3*2 = 6 3*3 = 9
4*1 = 4 4*2 = 8 4*3 = 12 4*4 = 16
5*1 = 5 5*2 = 10 5*3 = 15 5*4 = 20 5*5 = 25
6*1 = 6 6*2 = 12 6*3 = 18 6*4 = 24 6*5 = 30 6*6 = 36
7*1 = 7 7*2 = 14 7*3 = 21 7*4 = 28 7*5 = 35 7*6 = 42 7*7 = 49
8*1 = 8 8*2 = 16 8*3 = 24 8*4 = 32 8*5 = 40 8*6 = 48 8*7 = 56 8*8 = 64
9*1 = 9 9*2 = 18 9*3 = 27 9*4 = 36 9*5 = 45 9*6 = 54 9*7 = 63 9*8 = 72 9*9 = 81
tb = []
row1 = dict(name = "高小一", age = 18, salary = 30000, city = "北京")
row2 = dict(name = "高小二", age = 19, salary = 20000, city = "上海")
row3 = dict(name = "高小三", age = 20, salary = 10000, city = "深圳")
tb = [row1, row2, row3]
for x in tb:
if x.get("salary")>15000:
print(x.values())
>>> dict_values(['高小一', 18, 30000, '北京'])
dict_values(['高小二', 19, 20000, '上海'])
empNum = 0
salarySum = 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))
salarySum +=float(s)
print("员工数:{0}".format(empNum))
print("薪资统计:", salarys)
print("平均薪资:{0}".format(salarySum/empNum))
>>> 输入员工的薪资(以 Q 或 q 结束):10000
输入员工的薪资(以 Q 或 q 结束):20000
输入员工的薪资(以 Q 或 q 结束):-8000
输入员工的薪资(以 Q 或 q 结束):-7000
输入员工的薪资(以 Q 或 q 结束):30000
输入员工的薪资(以 Q 或 q 结束):q
录入完成,结束
员工数:3
薪资统计: [10000.0, 20000.0, 30000.0]
平均薪资:20000.0
empNum = 0
salarySum = 0
salarys = []
for i in range(4):
s = input("输入4名员工的薪资(以 Q 或 q 结束):")
if s.upper() == 'Q':
print("录入完成,退出")
break
if float(s)<0:
continue
empNum += 1
salarys.append(float(s))
salarySum += float(s)
else:
print("已录入4名员工的工资")
print("薪资统计:", salarys)
print("平均薪资{0}".format(salarySum/empNum))
>>> 输入4名员工的薪资(以 Q 或 q 结束):10000
输入4名员工的薪资(以 Q 或 q 结束):20000
输入4名员工的薪资(以 Q 或 q 结束):30000
输入4名员工的薪资(以 Q 或 q 结束):40000
已录入4名员工的工资
薪资统计: [10000.0, 20000.0, 30000.0, 40000.0]
平均薪资25000.0
import turtle
t = turtle.Pen()
my_colors = ("red", "green", "yellow", "black")
t.width(4)
t.speed(1)
for i in range(5):
t.penup()
t.goto(0, -i*30) # 0, -30, -60, -90, -120
t.pendown()
t.color(my_colors[i%len(my_colors)])
t.circle(100 + i*30) # 100, 130, 160, 190, 220
turtle.done() # 执行完后保留窗口
import turtle as t
for m in range(19):
t.penup()
t.goto(0, -m*10)
t.pendown()
t.forward(180)
t.speed(8)
t.right(90)
for n in range(19):
t.penup()
t.goto(n*10, 0)
t.pendown()
t.forward(180)
t.hideturtle()
t.done()
可重复使用的程序代码块。
def printMax(a, b):
"实现两个数的比较,并返回较大的值"
if a>b:
print(a, '较大值')
else:
print(b, '较大值')
printMax(10, 20)
>>> 20 较大值
def printMax(a, b):
"实现两个数的比较,并返回较大的值"
if a>b:
print(a, '较大值')
else:
print(b, '较大值')
help(printMax)
>>> Help on function printMax in module __main__:
>>> printMax(a, b)
实现两个数的比较,并返回较大的值