基于一个GitHub项目学习

Day 1

1、环境配置
python 3.10

2、输入import this运行程序

输出:
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

3、turtle库的使用

4、变量与类型

使用type()检查变量的类型
使用input()函数获取键盘输入(字符串)
使用int()将一个数值或字符串转换成整数,可以指定进制。
使用float()将一个字符串转换成浮点数。
使用str()将指定的对象转换成字符串形式,可以指定编码。
使用chr()将整数转换成该编码对应的字符串(一个字符)。
使用ord()将字符串(一个字符)转换成对应的编码(整数)

5、占位符
%d、%.nf(n表示n位小数)、%s
如:

1
2
print("对应的摄氏温度为:%.6f" % b)
print(f'对应的摄氏温度为:{b:.6f}')

Day 2

1、print("内容", end=' ')
end=''是设置print()打印结束添加的字符,如打印九九乘法表:

1
2
3
4
for i in range(1, 10):
for j in range(1, i+1):
print("%d * %d = %d" % (j, i, i * j), end='\t')
print()

2、执行代码模块__main__

__name__python中一个隐含的变量它代表了模块的名字,只有被python解释器直接执行的模块的名字才是__main__

1
2
if __name__ == '__main__':
# add code

3、变量的作用域
global关键字来指示函数中的变量来自于全局作用域,如果全局作用域中没有该变量,那么下面一行的代码就会定义该变量并将其置于全局作用域。
同理,如果我们希望函数内部的函数能够修改嵌套作用域中的变量,可以使用nonlocal关键字来指示变量来自于嵌套作用域。
如:
1
2
3
4
5
6
7
8
9
def func():
# global a
a = 20
print(a) # 20

if __name__ == '__main__':
a = 2
func()
print(a) # 2

1
2
3
4
5
6
7
8
9
def func():
global a
a = 20
print(a) # 20

if __name__ == '__main__':
# a = 2
func()
print(a) # 20

1
2
3
4
5
6
7
8
9
def func():
global a
a = 20
print(a) # 20

if __name__ == '__main__':
a = 2
func()
print(a) # 20

Day 3

1、breakcontinue的用法
break能且只能终止它所在的那个循环,continue可以用来放弃本次循环后续的代码直接让循环进入下一轮,而不是跳出循环,这一点在使用嵌套的循环结构需要引起注意。