x = 1
Python_is_第1名 = True
python_is_第1名 = False # 与上面的变量不是一个
# 错误案例
1_fruit = "apple"
my fruit = "apple"
for = True
a = [17, 18, 19] # 看不出用途
age_of_students = [17, 18, 19] # 一目了然
AgeOfStudents
MAX_ITERATION = 1000
x = 1+2
print(x)
3
x = 10
x += 10 # 加减乘除等皆可 效果相当于x = x + 10
print(x)
10
x, y = 1, 2
print(x, y)
1 2
x, y = y, x
print(x, y)
2 1
x = input("请输入一个数字:")
y = input("请输入一个数字:")
print(x)
print(y)
print(x + y)
print(type(x))
请输入一个数字:4
请输入一个数字:3.5
4
3.5
43.5
x = eval(input("请输入一个数字:"))
y = eval(input("请输入一个数字:"))
print(x)
print(y)
print(x + y)
请输入一个数字:4
请输入一个数字:3.5
4
3.5
7.5
print("我是一颗小星星")
print(1234)
我是一颗小星星
1234
x = 1024
print(x)
1024
print(1)
print(2)
1
2
print(123,end=" ")
print(456)
123 456
PI = 3.1415926
E = 2.71828
print("PI = ", PI, "E = ", E)
PI = 3.1415926 E = 2.71828
"字符{0}字符{1}字符".format(v0,v1)
PI = 3.1415926
E = 2.71828
print("PI = {0},E = {1}".format(PI, E))
print("PI = {1},E = {0}".format(PI, E))
print("PI = {},E = {}".format(PI, E)) # 不写数字就按顺序填入
print("PI = {0},E = {0}".format(PI, E)) # 很多位置需要相同变量时会很方便
PI = 3.1415926,E = 2.71828
PI = 2.71828,E = 3.1415926
PI = 3.1415926,E = 2.71828
PI = 3.1415926,E = 3.1415926
PI = 3.1415926
print("{0:*^20}".format(PI))
*****3.1415926******
print("{0:,}".format(10000000))
10,000,000
print("{0:&>20,}".format(10000000))
&&&&&&&&&&10,000,000
print("{0:.2}".format("abcde"))
ab
PI = 3.1415926
print("{0:.2f}".format(PI))
3.14
print("{0:.1%}".format(0.818727))
81.9%
print("{0:.2e}".format(0.818727))
8.19e-01
print("二进制{0:b},Unicode码{0:c},十进制{0:d},八进制{0:o},十六进制{0:x},十六进制{0:X}".format(450))
二进制111000010,Unicode码ǂ,十进制450,八进制702,十六进制1c2,十六进制1C2
"字符{0:修饰}字符{1:修饰}字符".format(v0 ,v1)
: | 填充 | 对齐 | 宽度 | , | .精度 | 类型 |
---|---|---|---|---|---|---|
引导符号 | 用于填充的字符,如* _等 | < 左对齐、> 右对齐、^ 居中对齐 | 设定输出的宽度 | 数字千分位分隔符,适用于整数和浮点数 | 浮点数:小数部分精度;字符串:最大输出长度 | 整数类型:b,c,d,o,x,X;浮点类型:e,E,f,% |
for i in [1, 2, 3]:
print(i)
print("打印结束")
x = 2 # 赋值
x += 4 # 增量
6 > 2 # 比较
x = x*2 - 1
z = x*x + y*y
c = (a+b) * (a-b)
x, y = 1, 2
ls = [1, 2, 3]
x = 2 # 空格一个就够了,不应过多
def fun(n=1, m=2):
print(n, m)
几点建议:
a=1 # 我是单行注释
"""
1
2
3
4
"""