zoukankan      html  css  js  c++  java
  • 【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

  • 相关阅读:
    Sql中使用With创建多张临时表
    sql(join on 和where的执行顺序)
    什么是正则化
    ETL讲解(转)
    MySQL等 SQL语句在线练习
    Sublime text 3 --html
    Sublime text 3 搭建Python3 IDE
    地区列车经过查询
    Lasso回归算法: 坐标轴下降法与最小角回归法小结
    完全卸载VMware
  • 原文地址:https://www.cnblogs.com/cry-star/p/10649124.html
Copyright © 2011-2022 走看看