本文共 2004 字,大约阅读时间需要 6 分钟。
import math a=20;b=3.2; a**3; #结果为20*20*20《=》math.pow(20,3) print(a**3); print(round(100/3,3)); #小数点后面保留3位有效数字(保证输出格式) print(math.ceil(3.32)); #向上取整 print(math.floor(3.32)); #向下取整 print(math.radians(180)); #度数转换成弧度制 divmod(10); #取商和余数 结果:8000 33.333 4 3 3.1415926 (3,1)
str="abc";str1="def"; print(a+b); #字符串拼接print(a*3); #字符串乘法结果:abcdef abcabcabc
zoo=('wolf','dog','cat') a,b,c=zoo #相当于a=zoo[0],b=zoo[1],c=zoo[2] print("%s is 狼,%s is 狗, %s is 猫"%(a,b,c)) print("{} is 狼,{} is 狗, {} is 猫".format(a,b,c)) 结果:wolf is 狼,dog is 狗,cat is 猫
wolf is 狼,dog is 狗,cat is 猫
%c 定制字符及其ASCLL码%o 定制无符号八进制数
%s 定制字符串%x 定制无符号十六进制数
%d 定制整数
%f定制浮点数,后可指定小数点后面精度%u 定制无符号整数 %e用科学计数法定制浮点数list_name=["car","jeep","bike"] #创建列表list_name.append("tractor") #在列表的末尾追加tractor元素list_name.insert(2,"train") #在下标为2的位置插入train元素list_name.remove("jeep") #删除list_name中的jeep元素list_name.pop() #弹出list_name的最后一个元素
列表的访问
list=["car","jeep",bike","tractor","airplane"]list[-2] # 访问倒数第二个元素 tractorprint(list[1,4]) #输出标号为1,2,3的元素,及[jeep,bike,tractor]print(list[-3,-1]) #输出标号为-3,-2元素 及[bike,tractor]list1=["1","2"]list = list+list1 #list=["car","jeep",bike","tractor","airplane","1","2"]list1.extend(list) # list1=["1","2","car","jeep",bike","tractor","airplane"]list2=["5","6"]list2 +=["7"] #list2=["5","6","7"]print(list)结果: tractor [jeep,bike,tractor] [bike,tractor] ["car","jeep",bike","tractor","airplane","1","2"]
转载地址:http://myolo.baihongyu.com/