英文的大小写是可以命名作区分的
关键字:
标识符命名还尽量做到:
见名知意
简洁
下划线分割进行命名
运算符
/ 代表除法
// 代表整除
andy="A"
Andy="B"
1
2
False, None, True, and, as, assert, async, await, break, class, continue, def, del, elif,
else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass,
raise, return, try, while, with, yield
1
first_num=1
student_name="zhangsan"
1
2
print(3/2) #1.5
print(4/2) #2.0
print(4//2) #2
print((2+3j)*(4+5j)) #(-7+22j) 复数在Python中使用j表示
print("ab"*3) #ababab
print([1,2,3]*3) #[1, 2, 3, 1, 2, 3, 1, 2, 3]
print((1,2,3)*3) #(1, 2, 3, 1, 2, 3, 1, 2, 3)
print([1,2,3]+[4,5,6]) #[1, 2, 3, 4, 5, 6]
# 字符串不能与非字符串进行拼接
print("hello"+" "+"world") #hello world
1
2
3
4
5
6
7
8
9
10
11
12
13
14
表达式
位运算符
1. 与(&):对应位上的两个操作数都为1时,结果为1,否则为0
2. 或(|):对应位上的两个操作数只要有一个为1,结果就为1
3. 非(~):对操作数的每个位取反,即将0变为1,将1变为0
4. 异或(^):对应位上的两个操作数相同时,结果为0;不同时,结果为1
5. 左移(<<):将操作数的二进制表示向左移动指定的位数,右边用0填充
6. 右移(>>):将操作数的二进制表示向右移动指定的位数,左边用原来的最高位填充
常用的内置函数
a=0b00111100
b=0b00001101
print(bin(a&b)) #0b1100
print(bin(a|b)) #0b111101
print(bin(a^b)) #0b110001
print(bin(~a)) #-0b111101
print(bin(a<<2)) #0b11110000
print(bin(a>>2)) #0b1111
1
2
3
4
5
6
7
8
print(pow(2, 3)) # 输出:8,计算 2 的 3 次方
print(abs(-33)) # 输出:33,计算 -33 的绝对值
print(round(3.7)) # 输出:4,对 3.7 四舍五入
print(round(3.3)) # 输出:3,对 3.3 四舍五入
from cmath import sin
print(sin(3.14)) # 输出:(0.0015926529164868282-0j),计算 sin(3.14)
1
2
3
4
5
6
7
8
9
10
11
12
进制转化
format
基本用法
print(-3.2) # 输出:-3.2,直接打印 -3.2
print(float('3.5')) # 输出:3.5,将字符串 '3.5' 转换为浮点数
print(ord('a')) # 输出:97,获取字符 'a' 的 ASCII 码值
print(chr(65)) # 输出:A,将 ASCII 码值 65 转换为字符
print(max(2, 7)) # 输出:7,获取最大值
print(sum([1, 2, 3, 4])) # 输出:10,计算列表中所有元素的总和
print(len("love you")) # 输出:8,计算字符串的长度(要算空格)
print(bin(5)) # 输出:0b101,将整数 5 转换为二进制表示形式
print(oct(55)) # 输出:0o67,将整数 55 转换为八进制表示形式
print(hex(555)) # 输出:0x22b,将整数 555 转换为十六进制表示形式
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#各进制转化为十进制
print(int("0b11010101",2)) #213
print(int("0o43124",8)) #18004
print(int("0xFF12E",16)) #1044782
#十进制转换为各进制
print(bin(432)) #0b110110000
print(oct(432)) #0o660
print(hex(432)) #0x1b0
#各进制之间的转化
print(bin(0x1b0)) #0b110110000
print(hex(0b110110000)) #0x1b0
#各进制转化为十进制的简单形式
print(0x1b0) #432
print(0b110110000)
print(0o660)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
name = "Alice"
age = 30
print("Name: {}, Age: {}".format(name, age))
# 按索引替换
print("Name: {1}, Age: {0}".format(age, name))
1
2
3
4
5
6
对齐
+
精度
+
科学计数法
这些代码使用了字符串的 format() 方法来格式化输出。下面逐行解释:
1. 第一行:
'{0:,}':代表用逗号将数字格式化为千位分隔符的形式,{0} 表示第一个参数,即 123456。
'{0:#x}':代表将数字格式化为十六进制的形式,{0} 表示第一个参数,即 123456。
'{1:#o}':代表将数字格式化为八进制的形式,{1} 表示第二个参数,即 789。
所以,输出结果为:
2. 第二行:
与第一行类似,只不过参数的顺序调换了,因此输出结果也对应调换了参数的顺序:
3. 第三行:
'{name:>20}':将 name 右对齐,并且总宽度为20个字符。
# 添加格式说明符
price = 19.99
print("Product: {:10s}, Price: ${:.2f}".format("Apple", price))
# Name: Alice, Age: 30
# Name: Alice, Age: 30
# Product: Apple , Price: $19.99
7
8
9
10
11
12
13
14
x=1234.56789
print(format(x,'0.2f')) #1234.57
print(format(x,'>12.1f')) # 1234.6(总共12个字符右对齐)
print(format(x,'<12.2f'),12) #12个字符左对齐 后面接一个12
print(format(x,'0>12.1f')) #0000001234.6 右对齐,左边用0补全
print(format(x,'0<12.2f')) #1234.5700000
print(format(x,"0^12.1f")) #0001234.6000中间对齐
print(format(x,",.2f")) #1,234.57 加上千分位逗号
# 科学计数法
print(format(x,'e')) #1.234568e+03
print(format(x,'0.2e')) #1.23e+03 e前面保留两位小数的科学计数法
# 百分数写法
a=0.25
print(format(a,'.0%'))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
print('The number {0:,} in hex is:{0:#x},the number {1} in oct is {1:#o}'.format(123456, 789))
print('The number {1:,} in hex is:{1:#x},the number {0} in oct is {0:#o}'.format(123456, 789))
print('my name is {name:>20},my age is {age}.'.format(name='yjg', age=22))
1
2
3
The number 123,456 in hex is:0x1e240,the number 789 in oct is 0o14251
The number 789 in hex is:0x315,the number 123,456 in oct is 0o3611001