第一周
课后练习
NO.1
1 | str1 = input("请输入一个人的名字:") |
str.format(*args, **kwargs)
Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.
NO.2
1 | n = input("请输入整数N:") |
NO.3
1 | for i in range(1,10): |
- end = ‘’ 为末尾end传递一个空字符串,这样print函数不会在字符串末尾添加一个换行符,而是添加一个空字符串
- {:2} 前置两个空格
NO.4
1 | sum,tmp = 0,1 |
NO.5
猴子吃桃问题。猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个;第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半多一个。到第五天 早上想再吃时,见只剩下一个桃子了。请编写程序计算猴子第一天共 摘了多少桃子。1
2
3
4n = 1
for i in range(5,0,-1):
n = (n+1)<<1
print(n)
- range range(start, stop[, step])
- 在数字没有溢出的前提下,对于正数和负数,左移一位都相当于乘以2的1次方,左移n位就相当于乘以2的n次方。
NO.6
健康食谱输出。列出 5 种不同的食材,请输出它们可能组成的所 有菜式名称。1
2
3
4diet = ['西红柿', '花椰菜', '黄瓜', '牛排', '虾仁'] for x in range(0, 5):
for y in range(0, 5):
if not(x == y):
print("{}{}".format(diet[x], diet[y]))
NO.7
五角星的绘制1
2
3
4
5
6
7
8
9from turtle import *
fillcolor("red")
begin_fill()
while True:
forward(200)
right(144)
if abs(pos()) < 1:
break
end_fill()
NO.8
太阳花的绘制1
2
3
4
5
6
7
8
9
10from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
turtle.color(*args)
Return or set pencolor and fillcolor.Several input formats are allowed. They use 0 to 3 arguments as follows:
color()
Return the current pencolor and the current fillcolor as a pair of color specification strings or tuples as returned by pencolor() and fillcolor().color(colorstring), color((r,g,b)), color(r,g,b)
Inputs as in pencolor(), set both, fillcolor and pencolor, to the given value.color(colorstring1, colorstring2), color((r1,g1,b1), (r2,g2,b2))
Equivalent to pencolor(colorstring1) and fillcolor(colorstring2) and analogously if the other input format is used.
If turtleshape is a polygon, outline and interior of that polygon is drawn with the newly set colors.
NO.9
螺旋线绘制1
2
3
4
5
6
7
8import turtle
import time
turtle.speed("fastest")
turtle.pensize(2)
for x in range(100):
turtle.forward(2*x)
turtle.left(90)
time.sleep(3)
turtle.speed(speed=None)
Parameters: speed – an integer in the range 0..10 or a speedstring (see below)
Set the turtle’s speed to an integer value in the range 0..10. If no argument is given, return current speed.If input is a number greater than 10 or smaller than 0.5, speed is set to 0. Speedstrings are mapped to speedvalues as follows:
“fastest”: 0
“fast”: 10
“normal”: 6
“slow”: 3
“slowest”: 1
Speeds from 1 to 10 enforce increasingly faster animation of line drawing and turtle turning.Attention: speed = 0 means that no animation takes place. forward/back makes turtle jump and likewise left/right make the turtle turn instantly.
NO.10
1 | import turtle |
turtle.tracer(n=None, delay=None)
Parameters:
n – nonnegative integer
delay – nonnegative integerTurn turtle animation on/off and set delay for update drawings. If n is given, only each n-th regular screen update is really performed. (Can be used to accelerate the drawing of complex graphics.) When called without arguments, returns the currently stored value of n. Second argument sets delay value (see delay()).
第二周
2.1 Python语法元素入门
- 缩进 必须
- 注释 单行:# 多行 ‘’‘ ’‘’
- 变量 不需定义
- 空格
- 表示缩进关系的空格(4个)不能改变
- 空格不能将一个命名分割
- 表达式
- 如果val = “28C”
- val[-1]是最后一个字符”C”
- val[0:2] [0,2)的区间的子串
- 同步赋值:先运算右侧N个表达式,然后同步将表达式结果赋值给左侧
- t=x;x=y;y=t; <===> x,y=y,x
- 循环 for i in range (<计数值>)
- inital-print模板
- 初始变量:运算需要的初始值
- 运算部分:根据算法实现
- 结果输出
- 函数 def
eg.1
2
3
4
5
6
7
8
9val = input("请输入温度值,如32C")
if val[-1] in ['C','c']:
f = 1.8 * float(val[0:-1])+32
print("转换后的温度为:%.2fF"%f)
elif val[-1] in ['F','f']:
c = (float(val[0,-1])-32)/1.8
print("转换后的温度为:%.2fC"%c)
else:
print("输入有误")
课后练习
1 | val = input("请输入温度值,如32C") |
eval()和float()的区别1
2
3
4
5x = 1
print(eval("x+1")) // 1
x = 1
print(float(x+1)) // 1.0
第三周
- 圆周率的算法
- 随机点分布
- 计算到原点的距离小于radius的点个数
- 计算概率
- format()
{<参数序号:格式控制标记>}
第4周
异常处理
try…except…
else 无异常执行
finally 无论是否发生异常执行
第5周
函数的调用执行过程
- 运行到函数调用,暂停程序
- 程序转而执行该函数,将实参赋值给形参,相当于执行了一条赋值语句
- 执行函数体
return 语句
退出该函数,并返回到函数被调用的地方
将返回的值传给调用程序
return = return None = 无 return
返回多个值 ,分隔