• 【Python基础】lpthw


      1.列表和字符串操作的混合练习

     1 ten_things = "apples  oranges crows telephone light sugar"
     2 
     3 print("Wait there are not 10 things in that list. Let's fix it.")
     4 
     5 stuff = ten_things.split()
     6 more_stuff = ["day","night","song","frisbee","corn","banana","girl","boy"]
     7 
     8 while len(stuff) != 10:
     9     next_one = more_stuff.pop()
    10     print("Adding:",next_one)
    11     stuff.append(next_one)
    12     print(f"There are {len(stuff)} items now.")
    13 
    14 print("There we go:",stuff)
    15 
    16 print("Let's do some things with stuff")
    17 
    18 print(stuff[1])
    19 print(stuff[-1])
    20 print(stuff.pop())
    21 print(' '.join(stuff))
    22 print('#'.join(stuff[3:5]))

      输出

    Wait there are not 10 things in that list. Let's fix it.
    Adding: boy
    There are 7 items now.
    Adding: girl
    There are 8 items now.
    Adding: banana
    There are 9 items now.
    Adding: corn
    There are 10 items now.
    There we go: ['apples', 'oranges', 'crows', 'telephone', 'light', 'sugar', 'boy', 'girl', 'banana', 'corn']
    Let's do some things with stuff
    oranges
    corn
    corn
    apples oranges crows telephone light sugar boy girl banana
    telephone#light
    View Code

      2. 列表的索引和操作

      参考:聊聊python中的细节--左闭右开区间

      python在设计索引下标的时候都是采用左闭右开区间的形式,是个值得注意的小细节。

    1 list = ['a','b','c','d']
    2 x1 = list[0]      # x1 = a
    3 x2 = list[-1]     # x2 = d 此时 list = ['a','b','c','d']
    4 x3 = list.pop()  # x3 = d 此时 list = ['a','b','c']
    5 x4 = list[1:2]    # x4 = b 注意python中索引时[]为左闭右开区间

      list.pop()表示取出列表最后一个元素返回给当前被赋值的变量。

      3. str.join(sequence)

      返回被str连接的sequence序列,如

    1 str = "-"
    2 sequence = ["a","b","c"]
    3 print(str.join(sequence))

      输出为

      a-b-c

      一般sequence是一个列表,如果它是一个字符串,则将字符串中每个字符视为一个元素并用str去连接它们,如

    1 str = "-"
    2 sequence = "test"
    3 print(str.join(sequence))

      输出为

      t-e-s-t

      4.进一步理解python的列表(作为一种常见的数据结构)

      有序;

      可随机访问,也可线性访问(使用索引);

      查找时需要依次检索

      5.巩固练习-概念理解

      面向对象编程,object oriented programming(OOP)

      python中的类(class)

      函数式编程,functional programming

  • 相关阅读:
    Python heapq 模块的实现
    使用Python在2M内存中排序一百万个32位整数
    heapq
    将不确定变成确定~Uri文本文件不用浏览器自动打开,而是下载到本地
    说说设计模式~组合模式(Composite)
    JS~字符串长度判断,超出进行自动截取(支持中文)
    DDD~基础设施层~续
    谈谈设计模式~原型模式(Prototype)
    Study note for Continuous Probability Distributions
    Spring——AOP配置时的jar包异常
  • 原文地址:https://www.cnblogs.com/cry-star/p/10649124.html
Copyright © 2020-2023  润新知