python学习之路2

1. 列表、元组操作

  列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作

  定义列表

1 cars = ["Camry","Accord","Teana"]

  访问列表中的元素,方式如下

#列表第一个
>>>cars[0]
Camry
#列表最后一个
>>>cars[-1]
Teana
#列表第二个
>>>cars[1]
Accord

  切片取元素

 1 >>> cars = ["Camry","Accord","Teana","Q50","ES","320i","S60L"]
 2 #取第二到第三个元素
 3 >>> cars[1:3]
 4 ['Accord', 'Teana']
 5 #取第一到最后一个元素但不包含最后一个元素
 6 >>> cars[0:-1]
 7 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', '320i']
 8 #取第一到最后一个元素
 9 >>> cars[0:]
10 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', '320i', 'S60L']
11 #第一到最后一个元素,每隔三个取一个
12 >>> cars[0::3]
13 ['Camry', 'Q50', 'S60L']

  添加

1 >>> cars
2 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', '320i', 'S60L']
3 >>> cars.append("A3")
4 >>> cars
5 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', '320i', 'S60L', 'A3']

  插入

1 >>> cars
2 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', '320i', 'S60L', 'A3']
3 >>> cars.insert(3,"Corolla")
4 >>> cars
5 ['Camry', 'Accord', 'Teana', 'Corolla', 'Q50', 'ES', '320i', 'S60L', 'A3']

  修改

1 >>> cars
2 ['Camry', 'Accord', 'Teana', 'Corolla', 'Q50', 'ES', '320i', 'S60L', 'A3']
3 >>> cars[-1] = "A4"
4 >>> cars
5 ['Camry', 'Accord', 'Teana', 'Corolla', 'Q50', 'ES', '320i', 'S60L', 'A4']

  删除

 1 >>> cars
 2 ['Camry', 'Accord', 'Teana', 'Corolla', 'Q50', 'ES', '320i', 'S60L', 'A4']
 3 #删除第四个元素
 4 >>> del cars[3]
 5 >>> cars
 6 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', '320i', 'S60L', 'A4']
 7 #指定名称删除元素320i
 8 >>> cars.remove("320i")
 9 >>> cars
10 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L', 'A4']
11 #弹出最后一个元素
12 >>> cars.pop()
13 'A4'
14 >>> cars
15 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L']

  合并

1 >>> brands = ["Toyota","Nissan","Honda"]
2 >>> cars
3 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L']
4 #将cars与brands合并
5 >>> cars.extend(brands)
6 >>> cars
7 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L', 'Toyota', 'Nissan', 'Honda']
8 >>> brands
9 ['Toyota', 'Nissan', 'Honda']

  拷贝

1 >>> cars
2 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L', 'Toyota', 'Nissan', 'Honda']
3 >>> cars_copy = cars.copy()
4 >>> cars_copy
5 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L', 'Toyota', 'Nissan', 'Honda']

  统计

1 >>> cars
2 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L', 'Toyota', 'Nissan', 'Honda']
3 >>> cars.count("ES")
4 1

  排序&逆序

1 >>> cars
2 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L']
3 >>> cars.sort()
4 >>> cars
5 ['Accord', 'Camry', 'ES', 'Q50', 'S60L', 'Teana']
6 >>> cars.reverse()
7 >>> cars
8 ['Teana', 'S60L', 'Q50', 'ES', 'Camry', 'Accord']

  获取下标

1 >>> cars
2 ['Teana', 'S60L', 'Q50', 'ES', 'Camry', 'Accord']
3 >>> cars.index("ES")
4 3

元祖

元组与列表类似,也是存一组数,只是一旦被创建,便不能再修改,所以又叫只读列表

1 >>> cars = ['Teana', 'S60L', 'Q50', 'ES', 'Camry', 'Accord']

它只有2个方法,一个是count,一个是index。

程序练习 

程序:购物车程序

需求:

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
  4. 可随时退出,退出时,打印已购买商品和余额
 1 product_list = [
 2     ("Iphone",5000),
 3     ("Mac Pro",13000),
 4     ("Iwatch",3000),
 5     ("Bike",1000),
 6     ("Book",80),
 7     ("Coffee",30),
 8 ]
 9 shopping_list = []
10 salary = input("input you salary:")
11 if salary.isdigit():
12     salary = int(salary)
13     while True:
14         for item in product_list:
15             print(product_list.index(item),item)
16         user_choice = input("选择要买啥?>>>:")
17         if user_choice.isdigit():
18             user_choice = int(user_choice)
19             if user_choice < len(product_list) and user_choice >=0:
20                 p_item = product_list[user_choice]
21                 if p_item[1] <= salary: #买得起
22                     shopping_list.append(p_item)
23                     salary -= p_item[1]
24                     print("added %s into shopping cart,your current balance is [%s]"% (item,salary))
25                 else:
26                     print("你的余额只剩[%s]啦,买不起啦"% (salary))
27             else:
28                 print("product code [%s] is not exist!"% user_choice)
29         elif user_choice == "q":
30             print("--------shopping list--------")
31             for p in shopping_list:
32                 print(p)
33             print("your currnt balance:",salary)
34             exit()
35         else:
36             print("invalid option")

二、字符 

 1  name.capitalize()  首字母大写
 2  name.casefold()   大写全部变小写
 3  name.center(50,"-")  输出 '---------------------xxx----------------------'
 4  name.count('x') 统计 x出现次数
 5  name.encode()  将字符串编码成bytes格式
 6  name.endswith("x")  判断字符串是否以x结尾
 7  "xx\tx".expandtabs(10) 输出'xx      x', 将\t转换成多长的空格 
 8  name.find('A')  查找A,找到返回其索引, 找不到返回-1 
 9   
10  format :
11      >>> msg = "my name is {}, and age is {}"
12      >>> msg.format("xxx",26)
13      'my name is xxx, and age is 26'
14      >>> msg = "my name is {1}, and age is {0}"
15      >>> msg.format("xxx",26)
16      'my name is 26, and age is xxx'
17      >>> msg = "my name is {name}, and age is {age}"
18      >>> msg.format(age=26,name="xxx")
19      'my name is xxx, and age is 26'
20  format_map
21      >>> msg.format_map({'name':'xxx','age':26})
22      'my name is xxx, and age is 26'
23  
24  
25 msg.index('x')  返回x所在字符串的索引
26  
27  
28 '9'.isdigit() 是否整数
29 name.isnumeric  
30 name.isprintable
31 name.isspace
32 name.istitle
33 name.isupper
34 "|".join(['xxx','jack','rain'])
35 'xxx|jack|rain'
36  
37  
38 maketrans
39     >>> intab = "aeiou"  #This is the string having actual characters. 
40     >>> outtab = "12345" #This is the string having corresponding mapping character
41     >>> trantab = str.maketrans(intab, outtab)
42     >>> 
43     >>> str = "this is string example....wow!!!"
44     >>> str.translate(trantab)
45     'th3s 3s str3ng 2x1mpl2....w4w!!!'
46 
47 msg.swapcase 大小写互换
48  
49  
50 >>> msg.zfill(40)
51 '00000my name is {name}, and age is {age}'
52  
53  
54 
55 >>> n4.ljust(40,"-")
56 'Hello 2orld-----------------------------'
57 >>> n4.rjust(40,"-")
58 '-----------------------------Hello 2orld'
59  
60  
61 >>> b="ddefdsdff_哦" 
62 >>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
63 True

猜你喜欢

转载自www.cnblogs.com/breeze-24/p/9098253.html