python自动化开发-2

时间:2022-06-02 13:57:41

1.python的数据类型之列表

  列表是Python开发语言中最常见的数据类型之一,通过列表可以实现对数据的增删改等常用操作。

  列表的定义:例子

    names = ["Lucy","Lily","jack"]

  列表的常用功能

    1)取出列表中的元素:例子

      python自动化开发-2

      取出列表的第二个元素:通过下标的方式,下标从0开始计数。

        python自动化开发-2

      取出列表的最后一个元素:

        python自动化开发-2

    2)切片:例子 注意列表在切片的时候是包头不包尾。     

      例一:
      names = ["Lucy","Lily","jack","rain"]
      print (names[0:3])
      运行结果如下:
      ['Lucy', 'Lily', 'jack']
      例二:     
      datas = ["jack","john","rain","lily"]
      print(datas[-1])
      运行结果:lily
    3)追加:例子      
      datas = ["jack","john","rain","lily"]
      datas.append("新人")
      print(datas)
      运行结果:['jack', 'john', 'rain', 'lily', '新人']
    4)插入:例子      
      datas = ["jack","john","rain","lily"]
      datas.insert(1,"插入到下标为1的位置上")
      print(datas)
      运行结果:['jack', '插入到下标为1的位置上', 'john', 'rain', 'lily']
    5)修改;例子      
      datas = ["jack","john","rain","lily"]
      datas[2]="lilei"
      print(datas)
      运行结果:['jack', 'john', 'lilei', 'lily']
    6)删除:例子     
      datas = ["jack","john","rain","lily"]
      del datas[2]
      print(datas)
      运行结果:['jack', 'john', 'lily']
      
      
      datas = ["jack","john","rain","lily"]
      datas.remove("jack")
      print(datas)
      运行结果:['john', 'rain', 'lily']
    7)拷贝:例子      
      datas = ["jack","john","rain","lily"]
      data = datas.copy()
      print(data)
      运行结果:['jack', 'john', 'rain', 'lily']
    8)统计:例子      
      datas = ["jack","john","rain","lily","john"]

      print(datas.count("john"))
      运行结果:2
    9)排序和反转:例子
    排序:
      
      datas = ["jack","john","rain","lily","john","1","2"]

      datas.sort()
      print(datas)
      运行结果:['1', '2', 'jack', 'john', 'john', 'lily', 'rain']
    反转:
      
      datas = ["jack","john","rain","lily","john","1","2"]

      datas.reverse()
      print(datas)
      运行结果:['2', '1', 'john', 'lily', 'rain', 'john', 'jack']
    10)获取列表里某个元素的下标位置:例子
      
      datas = ["jack","john","rain","lily","john","1","2"]

      print(datas.index("lily"))
      运行结果:3 2.python的数据类型之元组
  元组与列表类似,也是存一组数,但是元组一旦创建,便不能再修改,所以又叫只读列表。
  元组只有两个方法,一个是count,另一个是index。
  例子1:    
    datas = ("jack","john","rain","lily","john","1","2")

    print(datas.index("rain"))
    运行结果:2
  例子2:    
    datas = ("jack","john","rain","lily","john","1","2")

    print(datas.count("john"))
    运行结果:2
3.python的数据类型之字符串
  字符串的特性就是不可修改。
  字符串的一些功能如下:
    1)首字母大写      
      datas = "far across the distance and spaces between us"

      print(datas.capitalize())
      运行结果:Far across the distance and spaces between us      2)大写全部变小写      
      datas = "Far Across The Tistance And Spaces Between Us"

      print(datas.casefold())
      运行结果:far across the tistance and spaces between us      3)输出方式      
      datas = "Far Across The Tistance And Spaces Between Us"

      print(datas.center(50,"-"))
      运行结果:--Far Across The Tistance And Spaces Between Us---      4)统计      
      datas = "Far Across The Tistance And Spaces Between Us Us"

      print(datas.count("Us"))
      运行结果:2      5)判断字符串是否以...结尾      
      datas = "Far Across The Tistance And Spaces Between Us Us"

      print(datas.endswith("s"))
      运行结果:True

     6)查找字符,找到返回其索引, 找不到返回-1      

      datas = "Far Across The Tistance And Spaces Between Us"

      print(datas.find("d"))
      运行结果:26      
      datas = "Far Across The Tistance And Spaces Between Us"

      print(datas.find("D"))
     运行结果:-1

     7)format使用方式      
      datas = "you are {} yeras old,your are {}."

      print(datas.format(25,"Lily"))
      运行结果:you are 25 yeras old,your are Lily.      
      
      datas = "you are {age} yeras old,your are {name}."

      print(datas.format(name="Lucy",age=28))
      运行结果:you are 28 yeras old,your are Lucy.
    
     8)isdigit():是否整数
       print("9".isdigit())
       运行结果:True
       
       print("9a".isdigit())
       运行结果:False
    
     9)返回某字符所在字符串的索引      
      datas = "you are 22 yeras old,your are lily."

      print(datas.index("s"))
      运行结果:15 4.python的数据类型之字典
  字典主要有两个特性:a.无序;b.key必须是唯一的。
  字典的常用功能如下:
    1)增加
     datas = {"name":"lily","age":"23"}
       datas["job"]="IT"
       print(datas)
       运行结果:{'age': '23', 'name': 'lily', 'job': 'IT'}      2)修改      
      datas = {"name":"lily","age":"23"}

      datas["name"]="jack"     
      print(datas)
     运行结果:{'age': '23', 'name': 'jack'}
    3)删除      
      datas = {"name":"lily","age":"23","job":"IT"}

      datas.pop("name")
      print(datas)
      运行结果:{'age': '23', 'job': 'IT'}
      
      
      datas = {"name":"lily","age":"23","job":"IT"}

      del datas["age"]
      print(datas)
      运行结果:{'name': 'lily', 'job': 'IT'}
    
     4)查找      
      datas = {"name":"lily","age":"23","job":"IT"}
      print("name" in datas)
      运行结果:True     
      
      datas = {"name":"lily","age":"23","job":"IT"}
      print(datas.get("age"))
      运行结果:23      5)字典的循环     
      
     datas = {"name":"lily","age":"23","job":"IT"}
      for i in datas:
      print(i,datas[i])
      运行结果:     

        job IT
        name lily
        age 23

5.文件

 1) 打开文件

    打开文件时,需要指定文件路径和以哪种方式打开文件,打开后,就可以获取该文件句柄并且把它赋值给一个变量,以后通过此文件句柄对该文件操作,对文件操作结束后,要关闭文件。

    

    打开文件的主要模式如下:

      r ,只读模式【默认】

      w,只写模式【不可读;不存在则创建;存在则清空内容;】

      x, 只写模式【不可读;不存在则创建,存在则报错】

      a, 追加模式【可读;   不存在则创建;存在则只追加内容;】

      "+" 表示可以同时读写某个文件

      r+, 读写【可读,可写】

      w+,写读【可读,可写】

      x+ ,写读【可读,可写】

      a+, 写读【可读,可写】

  2)对文件进行操作

    文件句柄 = open('文件路径''模式'):例子  

     例子1: f = open("test.txt",mode="r",encoding="utf-8")  #以只读方式打开test.txt文件
     
     例子2:
       test.txt文件内容如下:       
          abcde
          ABCDE
          opqrst         程序代码内容如下:      
          f = open("test.txt","r",encoding="utf-8") #以只读方式打开文件且获取文件句柄
          Files = f #把文件句柄赋值给变量Files
          a = Files.read() #读取文件内容,并且把它赋值给一个变量
          print(a)                    #输出或者打印文件内容
          运行结果:          

            abcde
            ABCDE
            opqrst

      例子3:        

        test.txt文件内容如下:       
          abcde
          ABCDE
          opqrst         程序代码如下:       
          #!/usr/bin/env python
          # _*_ coding:utf-8 _*_
          f = open("test.txt","r",encoding="utf-8")
          Files = f
          a = Files.readline() #读取文件的一行内容
          print(a) 
          f.close()

          运行结果如下:
            abcde