python变量
整形,浮点型
-
代码
counter=10; counter1=100.0; print (counter); print (counter1);
-
结果
10 100.0
多变量赋值
-
代码
a=b=c=1; print(a); print(b); print(c);
-
结果
1 1 1
字符串及访问
-
代码
str="iloveopencv01516"; print(str); print(str[1:5])#第二个字符到第5个字符之间 print(str[2:6])
-
结果
iloveopencv01516 love oveo
包含左边不包含右边
列表
-
代码
#列表 list=['1',1.2,'1','3'] print(list) print(list[0]) print(list[0:3])#包含左边不包含右边 print(list[0:]) #不填的话。全部
-
结果
['1', 1.2, '1', '3'] 1 ['1', 1.2, '1'] ['1', 1.2, '1', '3']
元组
-
代码
tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 ) print(tuple) print(tuple[0]) print(tuple[1:4])
-
结果
('runoob', 786, 2.23, 'john', 70.2) runoob (786, 2.23, 'john')
键值对
-
代码
tinydict={"code":'haha',"jjj":"kkkk"}; print(tinydict["code"])#取值 print(tinydict.keys())#列出所有的键 print(tinydict.values())#列出所有的值
-
结果
('runoob', 786, 2.23, 'john', 70.2) runoob (786, 2.23, 'john')
结合算数
-
代码
#集合算数 a=21; b=13; d=2; c=a/b; print("c_1的值是:",c);#精确到小数,不四舍五入;python3新特性 python2 整除整整得出整数 c=a-b; print("c_2的值是:",c); c=a*b; print("c_3的值是:",c); c=a%b; print("c_4的值是:",c);
-
结果
c_1的值是: 1.6153846153846154 c_2的值是: 8 c_3的值是: 273 c_4的值是: 8
次方
-
代码
#修改变量的值 a=1; b=2; c=a**b; print(c);#a的b次方
-
结果
1
身份运算符
-
代码
a=20; b=10; if (a is b): print("登陆") elif(a is not b): print("不登录");
-
结果
不登录