整型
1
2
3
4
5
6
7
8
9
10int_number = 5
print (int_number)
int_number = 5*6
print (int_number)
int_number = -6
print (int_number)
int_number = 0xA0F #十六进制2575
print (int_number)
int_number = 0o37 #八进制31
print (int_number)浮点型
1
2
3
4
5
6
7
8float_number = 2.5
print (float_number)
float_number = 1/4
print (float_number)
float_number = -32.54e100
print (float_number)
float_number = -1e-12-1e-12 #等价-1e(-12)+(-1e(-12))
print (float_number)复数型
1
2
3
4complex_number=3+3.14j
print(complex_number)
print(complex_number*complex_number)
print(complex_number+complex_number)数字类型转换
将数据类型作为函数名即可1
2
3
4int(x) #将x转换为一个整数
float(x) #将x转换到一个浮点数
complex(x) #将x转换到一个复数,实数部分为 x,虚数部分为 0
complex(x, y) #将 x 和 y 转换到一个复数,实数部分为 x,虚数部分为 y布尔型
1
2
3
4
5
6bool_true=True
print(bool_true)
print(int(bool_true))
bool_false=False
print(bool_false)
print(int(bool_false))列表
1
2
3
4
5
6
7
8
9
10
11
12
13
14list_1=['a','b','c',3,2]
print("list_1[4] is :"+str(list_1[4]))
print(list_1[0:2])
print(list_1[2:-1])
list_1.append('d')
print(list_1[2:-1])
list_merge=list_1+list_1
print('list_merge is :'+str(list_merge))
del list_merge[2]
print('list_merge is :'+str(list_merge))
list_2=[[1,2,3],['a','b','c','d'],['$','&']]
print(list_2[1][2])
for li in list_2:
print(li)字符串
1
2
3
4
5
6string ='Hello World!'
print(string)
print(string*2)
print(string[3])
print(string[6:-1])
print(string.center(30, '*'))元组
元组与列表类似,不同点:元组的元素不能修改。元组使用小括号,列表使用方括号。1
2
3
4
5
6tuple_1=((1,2,3),('a','bcd','f'))
for sub_tuple in tuple_1:
for sub in sub_tuple:
print (sub)
print(tuple_1[1][1])
#tuple_1[1][2]='abcd' # 报错'tuple' object does not support item assignment字典
1
2
3
4
5
6tom_dict={'name':'Tom','age':14,'number':232}
print(tom_dict)
print(tom_dict['name'])
print(tom_dict['age'])
tom_dict['age']=15
print(tom_dict['age'])