Python选修

1
print("")
1
2
eval
a,b,c=eval(input("输入数字🔢:"))
Turtle Graphics
1
import turtle as t
设置画布大小
1
t.setup(600,300)
设置画笔颜色
1
t.pencolor("red")
设置画笔大小
1
t.pensize(3)
设置填充颜色
1
t.fillcolor("red")
填充起点
1
t.begin_fill()
抬笔
1
t.penup()
下笔
1
t.pendown()
角度
1
t.seth(30)
0905课堂练习
1
2
3
4
5
6
7
8
9
10
11
12
import turtle as t
t.penup()
t.goto(200,100)
t.pendown()
t.pencolor("blue")
t.pensize(4)
t.fillcolor("yellow")
t.begin_fill()
t.seth(45)
t.fd(100)
t.endfill()
t.ht()
And
1
8 and 7
Or
1
0 or 5
Not
1
6 not 
Is
1
8 is 5
Is not(身份运算)
1
6 is not 4
In

example:

1
2
3
5 in [1,3,4,5,6]
输出结果:
True
引用Math第三方库函数
pow
1
2
3
4
5
6
pow(2,3)
输出结果:
8
# 该函数位于math第三方库,应正确引用使用
math.pow(2,3)
math.sqrt(9)
random库
重命名
1
import random as rd
生成10-50的任意一个数字
1
2
rd.randint(10,50)
21
1
rd.random()
1

1

0912课堂练习
1
2
3
4
5
6
7
8
9
10
11
#random库练习
import random as rd
import turtle as t
for i in range(20):
t.pensize(rd.randint(1,5))
r,g,b=(rd.random(),rd.random(),rd.random())
t.pencolor((r,g,b))
t.penup()
t.goto(rd.randint(-350,350),rd.randint(-580,580))
t.pendown()
t.circle(rd,randint(5,20))
1
2
3
4
5
6
7
8
9
10
11
#七彩通道
import random as rd
import turtle as t
for i in range(100):
t.pencolor(rd.random(),rd.random(),rd.random())
#规定几种颜色
# ls=["red","blue","pink"]
# t.pencolor(rd.choice(ls))
t.fd(10+i)
t.right(90)
t.ht()
Type
1
2
3
4
5
6
a="lele"
s='5983'
type(s)
<class'str'>
type(a)
<class'str'>
s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
s=lele's blog
s.index("lele")
s=0
s="青海民族大学"
s[-1]
'学'
s[0]
'青'
s[0::5]
'青海民族大学'
s[::]
'青海民族大学'
s[2:4:1] //切片
'民族'
s="090805030717"
s.count("8") //统计“8”一共出现了几次
1
s.count("0717") //统计“0717”一共出现了几次
1
s.index("8")
0
max(s)
'9'
min(s)
'0'
a="123ngf*/"
max(a)
'n'
b="123'hhh'4[1,2,3"



len(s)
14
len("hgj")
3
s="LeJin"
s.upper() //全部转换大写
"LEJIN"
s.lower() //全部转换小写
"lejin"
s="123123123"
s.replace("2","fff") //替换
1fff31fff31fff3
" ". join(s)
'1 2 3 1 2 3 1 2 3'
",". join(s)
'1,2,3,1,2,3,1,2,3'
print("%s"%s) //居中对齐
print("%10s"%s) //右对齐
print("%-10s"%s) //左对齐
print("{:<10}".format(s)) //左对齐
print("{:>10}".format(s)) //右对齐
print("{:^10}".format(s)) //居中对齐
print("{:.2f}".format(s)) //保留两位小数
print(f"{x}, {c}") //输出x,c
s="* 5 8f6 * *"
s.strip(" *")
'5 8f6'
0919课堂练习
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#验证码.py
import random as rd
s="AfEh147369"
yzm=""
for i in range(4):
yzm+=rd.choice(s)
print("验证码为:",yzm)
x=input("请输入验证码:")
#把x中的空格删除
x=x.replace(" ","")
#不区分大小写
if x.upper()==yzm.upper():
print("complete!")
else:
print("Error!")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#随机输出年月日
import random as rd
y=rd.randint(2000,2005)
m=rd.randint(1,12)
if m in [1,3,5,7,8,10,12]:
d=rd.randint(1,31)
elif m in [4,6,9,11]:
d=rd.randint(1,30)
else:
if y%4=0 and y%100!=0 or y%400==0:
d=rd.randint(1,29)
else:
d=rd.randint(1,28)
print(str(y)+"年"+str(m)+"月"+str(d)+"日")
0926课堂练习
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#选择结构.py
#单分支结构
'''
s=float(input("请输入成绩:"))
if s>=60:
print("合格")
'''
#双分支结构
'''
s=float(input("请输入成绩:"))
if s>=60:
print("合格")
else:
print("不合格")
print("运行结束")
'''
#多分支结构
s=float(input("请输入成绩:"))
if s>=90:
print("优秀")
elif s>=75:
print("良好")
elif s>=60:
print("合格")
else:
print("不合格")
print("运行结束")
#案例1
s=float(input("请输入消费金额:"))
if s>=10000:
print("支付金额",s*0.7)
elif s>=8000:
print("支付金额",s*0.8)
elif s>=6000:
print("支付金额",s*0.9)
else:
print("支付金额",s)
print("运行结束")
#案例2
import random as rd
x,y,z=rd.randint(30,50),rd.randint(30,50),rd.randint(30,50)
print(x,y,z)
a,b,c=eval(input("输入数字🔢:"))
if a==x and b==y and c==z:
print("很荣幸能够看到现在的樂乐")
elif a==x and b==y or b==y and c==z or a==x and c==z:
print("很幸运能再次遇见瑾昀")
elif a==x or b==y or c==z:
print("无他,仅能通过瑾谦博客的方式来记录回忆")
else:
print("很遗憾,瑾已擦肩而过╮( •́ω•̀ )╭")