2.字符串操作、组、元祖、集合、字典、身份判断、循环、工程结构


little tip : python 自带学习文档  

在控制台输入 python3 -m pydoc -p 8888

2.字符串操作、组、元祖、集合、字典、身份判断、循环、工程结构

2.字符串操作、组、元祖、集合、字典、身份判断、循环、工程结构

 


  • 字符串操作

1、单个访问字符串中的字符

        s = "hello world"

        print(s[2])

        输出:l

2、访问字符串子串 切片 左闭右开

        s = "hello world"

        print(s[2:5])  

        输出:llo

3、字符串相加

        s1 = "hello"

        s2 = " world"

        print(s1 + s2)

        输出:hello world

4、字符串更新操作        

        s1 = "hello string"

        s2 = "python"

        print(s1[:6] + s2)

        输出:hello python

5、字符串成员运算  包含in / 不包含not in

        s1 = "hello world"

        s2 = "llo"

        print(s2 in s1)

        输出:True

6、转义字符

        print("\")

        输出:

    类似的有 \t  \r  \n  

7、字符串的格式化输出

        s1 = "小明"

        s2 = "小美"

        print("你好,%s,%s!" % (s1, s2))

        输出:你好,小明,小美!

8、查找子串

        s = "hello world"

        print(s.find("world"))

        输出:6

9、大小写转换

        s = "heLLo wOrlD"

        print(s.upper())

        print(s.lower())  

        打印:HELLO WORLD

                 hello world  

10、求长度

        s = "hello world"

        print(s.__len__())

        打印:11    

11、判断是否是空格

        s = "   "

        print(s.isspace())

        打印:True

12、字符串替换

        s = "hello world"

        print(s.replace('o', 'a'))

        打印:hella warld

 

and so on...

 


  • 组 list

1、输出

    list1 = ['apple', 8, 'cherry', 50, 'banana', 5]

    print(type(list1))

    print(list1)

    输出:<class 'list'>

             ['apple', 8, 'cherry', 50, 'banana', 5]

 

2、列表元素访问  单个 切片

    list1 = ['apple', 8, 'cherry', 50, 'banana', 5]

    print(list1[2])

    print(list1[2:])

    print(list1[2:4]) 

    输出:cherry

             ['cherry', 50, 'banana', 5]

             ['cherry', 50]

3、列表更新    

    list1 = ['apple', 8, 'cherry', 50, 'banana', 5]

    list1[3] = ‘40'

    print(list1)

    输出:['apple', 8, 'cherry', 40, 'banana', 5]

4、列表添加操作

    list1 = ['apple', 8, 'cherry', 50, 'banana', 5]

    list1.append('strawberry')

    list1.append(15)

    print(list1)

    输出:['apple', 8, 'cherry', 50, 'banana', 5, 'strawberry', 15]

5、列表删除操作

    list1 = ['apple', 8, 'cherry', 50, 'banana', 5]

    del list1[4]

    print(list1)

    输出:['apple', 8, 'cherry', 50, 5]

6、嵌套列表

    list1 = [['apple', 8], ['cherry', 50], ['banana', 5]]

    print(list1)

    print(list1[0])

    print(list1[0][0])

    输出:[['apple', 8], ['cherry', 50], ['banana', 5]]

               ['apple', 8]

               apple

7、列表个数

    list1 = [['apple', 8], ['cherry', 50], ['banana', 5]]

    count = len(list1)

    print(count)

    输出:3

8、移除列表中元素 并返回该值

    list1 = [['apple', 8], ['cherry', 50], ['banana', 5]]

    mem = list1.pop(0)

    print(mem)

    print(list1)

    输出:['apple', 8]

               [['cherry', 50], ['banana', 5]]

9、列表排序

    list1 = [['apple', 8], ['cherry', 50], ['banana', 5]]

    list1.sort()

    print(list1)

    输出:[['apple', 8], ['banana', 5], ['cherry', 50]]

10、查找元素 返回第一个匹配值的索引

    list1 = [1, 33, 5, 21, 0, -8, 21, 35, 4]

    index = list1.index(21)

    print(index)

    输出:3

 


  • 元祖 tuple 不可修改

   基本与列表相同....区别是,列表可以修改,但是元祖不可以修改增删改都不行

    num = (1, 2, 3)

    print(type(num))

    print(num[1])

    print(num)

    输出:<class 'tuple'>

               2

               (1, 2, 3)

 


  • 集合 set 一个无序的不重复元素序列

1、基本操作

     因为集合是无序的,所以每次输出结果不确定,不能查找索引、切片,只能循环遍历

     可以用len获取集合长度、add和update增、remove删,clear清空,用in判断集合是否存在指定键的元素

    set_param = {'apple', 'orange', 'peach', 'banana', 'apple}

    print(set_param)

    输出:{'peach', 'apple', 'orange', 'banana’}

2、集合间操作  &交集,|并集,^对称差分,

    set1 = set('abcdefg')

    set2 = set('abcdxyz')

    print(set1, set2)

    print(set1 & set2)

    print(set1 | set2)

    print(set1 ^ set2)

    输出:{'c', 'g', 'b', 'd', 'f', 'e', 'a'} {'x', 'y', 'c', 'b', 'd', 'z', 'a'}

               {'b', 'a', 'c', 'd'}

               {'x', 'y', 'c', 'g', 'b', 'd', 'f', 'z', 'e', 'a'}

               {'x', 'y', 'g', 'z', 'f', 'e’}

 


  • 字典 dict 键值对

        基本操作

       d1 = {'小明': 15, '小美': 20, '小王': 17}

       print(type(d1))    

       print(d1)

       print(len(d1))

       print(d1['小明'])

       d1['小王'] = 18

       print(d1)

       d2 = {'小张': 16}

       d1.update(d2)

       print(d1)

       d1.pop('小明')

       print(d1)

       print('小明' in d1)  

       d1.clear()

       print(d1) 

       输出:<class 'dict'>

                 {'小明': 15, '小美': 20, '小王': 17}

                 3

                 15

                 {'小明': 15, '小美': 20, '小王': 18}

                 {'小明': 15, '小美': 20, '小王': 18, '小张': 16}

                 {'小美': 20, '小王': 18, '小张': 16}

                 False

                 {}  

 


  •    身份判断  is  ==

is判断地址, ==判断内容

    i = [1, 2]

    j = [1, 2]   

    print(id(i))

    print(id(j))

    print(i == j)

    print(i is j)

    输出:4302302024

              4338265224

              True

              False


  • 循环

1、while循环

    n = 1

    count = 0

    while n <= 100:

        count += n

        n += 1

    print(count)

    输出:5050

2、for循环

    list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    count = 0

    for i in list1:

        count += i

    print(count)

    count = 0

    for i in range(1, 101):   //左闭右开

        count += i

    print(count)

    输出:55

             5050

3、continue语句:跳过本次循环后面的代码,结束本次循环,开始下一次循环

4、break语句:直接结束当前循环,如果外层还有循环,还要继续执行外层循环

 

 


  • Python工程结构

2.字符串操作、组、元祖、集合、字典、身份判断、循环、工程结构