整型
二进制:0b100 = 4
八进制:0o100 = 64
十进制:100
十六进制:0x100 = 256
浮点数
123.456 = 1.23456e2
字符串
‘’’
aaaa
aaaa
‘’’
=
\naaaa\naaaa\n
复数型
3+5j = 数学中(3+5i)
变量使用
// 取整
变量转换
chr(65) = ‘A’
ord(‘A’) = 65
键盘输入(没系统学,真不知道这个)
In [16]: input(‘a = ‘)
a = 1
Out[16]: ‘1’
循环
range(1, 100, 2)可以产生一个1到99的奇数序列,其中2是步长,即数值序列的增量。
字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| str1 = 'hello, world!'
print(str1.capitalize())
print(str1.title())
print(str1.find('or')) print(str1.find('shit'))
print(str1.index('or')) print(str1.index('shit'))
print(str1.center(50, '*'))
print(str1.rjust(50, ' ')) str2 = 'abc123456'
print(str2.isdigit())
print(str2.isalpha())
print(str2.isalnum()) str3 = ' jackfrued@126.com '
|
格式化
1 2
| a, b = 5, 10 print(f'{a} * {b} = {a * b}')
|
列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| list2 = ['hello'] * 3 print(list2)
for index, elem in enumerate(list1): print(index, elem)
list1 += [1000, 2000]
fruits3 = fruits[:] print(fruits3)
fruits5 = fruits[::-1] print(fruits5)
|
set
discard:不会报错,如果元素不存在
remove:会报错,如果元素不存在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| print(set1 & set2)
print(set1 | set2)
print(set1 - set2)
print(set1 ^ set2)
print(set2 <= set1)
print(set3 <= set1)
print(set1 >= set2)
print(set1 >= set3)
|
zip && unzip
dict(zip([‘a’, ‘b’, ‘c’], ‘123’))
{‘a’: ‘1’, ‘b’: ‘2’, ‘c’: ‘3’}
list(zip([‘a’, ‘b’, ‘c’], ‘123’))
[(‘a’, ‘1’), (‘b’, ‘2’), (‘c’, ‘3’)]
list(zip(*zip([‘a’, ‘b’, ‘c’], ‘123’)))
[(‘a’, ‘b’, ‘c’), (‘1’, ‘2’, ‘3’)]