教程
任务
启航,Python基础
学习时间
5/13
笔记
hello, world
相比于C,python第一行代码简单多了
print("hello world")
//print的名字来自历史上的打孔计算机
hello world,检测运行环境的简单手段
Brian Wilson Kernighan,C语言之父
最初的hello world C代码,没有#include和return 0
注释
单行注释,#开头
多行注释,‘’’ or “”"包裹起来
//# magic comment
会被当作代码处理
程序员讨厌别人的代码不写注释,也讨厌给自己的代码写注释
BASIC CONSOLE OUTPUT
编程中的函数类似数学上的函数
print("Datawhale")# 将填入的内容显示在Consolde,默认换行
print("Datawhale", end="")
print("Data", "whale")# 也可以一次输出多个内容,默认以空格分隔
print("Data", "whale", sep="*")
print("Data"+"whale"+"!!"*2)
# Datawhale!!!!
print(f"a easy problem:\"{x} + {y} = ?\",答案是 {x+y}!")
# f-strings, amazing function
# use f"..." and {...}
# print many lines at one time
print("""
hell
o, w
orld
""")
//python确实是可以对字符串方便操作,当初学C反而不习惯
错误ERROR
- 语法错误Syntax Errors
- 运行时错误Runtime Errors(Crash)
- 逻辑错误Logical Errors
BASIC CONSOLE INPUT
name = input("输入你的名字:")
print("あなたの名前は", name, "です")
# python uses Unicode, support any character
# amazing
# 自己实践,发现vscode输出乱码,不支持UTF8,新建了一个系统环境变量,成功了
x = int(input("输入一个数字"))
print(x, "的一半等于", x/2)
# input more than one variable in one line
a, b = input().split(",")
print(f"a = {a}, b = {b}")
库Library
import math# import a library before using it!
print(math.factorial(20))
print(math.e)
math.gcd(12, 36)
课后思考
0.1+0.2≠0.3?? 无可避免的浮点误差:https://www.bilibili.com/video/BV1xq4y1D7Ep