Python_(1)数据类型及其常见使用方法(图文)

python学习笔记

一. 变量类型及其常见函数用法:数值型(int float complex) 字符串 (str) 列表 (list)元组 (tuple) 字典(dict)

  • (1)数值

  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)
  • (2)字符串

str="abc";str1="def"; 
print(a+b);                             #字符串拼接
print(a*3);                             #字符串乘法
结果:abcdef  abcabcabc
  • (3)元组 (元组只要确定就不能修改 增加 删除任何元素)

       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用科学计数法定制浮点数

  • (4)列表 类似于链表

    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]                         # 访问倒数第二个元素 tractor
print(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"]

常用列表方法

IMG_20180409_100012

  • (5)字典值

    • 字典键值对表示方法:dict = ["key1","value1","key2","value2" ...]

    • 字典的常用方法

      8e25d07545765df7db1182198cf359c21be71b87

你可能感兴趣的:(Python_(1)数据类型及其常见使用方法(图文))