吴振虎

导航

day 6 元组、字典、字符串

本节内容 :
  1,元组
  2,字典
  3,字符串
作业
 1 produce = [('mac', 9000),
 2            ('bicycle', 800),
 3            ('computer', 8000),
 4            ('book', 50),
 5            ('car', 100)]
 6 money = input('please input your money')
 7 for i, v in enumerate(produce, 1):  # enumarate,可以输出列表内容,并显示行号,行号可以设置从1开始
 8     print(i, '>>>', v)
 9 shopping_list = []
10 if money.isdigit():
11     money = int(money)
12     while True:
13         choice = input("input you choice shopping line")
14         if choice.isdigit():
15             choice = int(choice)
16             if 0 < choice < len(produce):
17                 item = produce[choice-1]
18                 if item[1] < money:
19                     shopping_list.append(item)
20                     money -= item[1]
21 
22                     print("您已经将\"%s\"加入购物车,价格:%s" % item)
23                 else:
24                     print("-----余额不足---------")
25             else:
26                 print("-------输入序号商品不存在---------")
27         elif choice == "quit":
28             print("------------您购买的商品有----------")
29             for i in shopping_list:
30                 print(i)
31             print("---您的余额-------")
32             print(money)
33             break
34         else:
35             print("---请输入quit或者商品码---")

1,元组

  a = ("1","2","3","4","5")   # 使用的是()

  a = ("1",)      # 当只有一个成员的时候最好加上一个逗号,标识是元组

  其他的切片查看和列表的相同,但是不能进行修改删除等操作

 

2,字典

   字典的两大特点,第一:键唯一,

           第二:键是不可修改类型

  不可变类型 :整型,字符串,元组

  可变类型:列表,字典

 1 dic1 = {'name': 'wuzhenhu'}
 2 print(dic1)
 3 
 4 # 修改和增加
 5 dic1["age"] = 18  # 有则修改,无则新增
 6 print(dic1)
 7 dic1["age"] = 26
 8 print(dic1)
 9 ret_setdefault = dic1.setdefault("age", "28")  # 有则默认并返回,无则新增并返回
10 print(dic1, "\n", ret_setdefault)
11 ret_setdefault1 = dic1.setdefault("hobby", "girl")
12 print(dic1, "\n", ret_setdefault1)
13 dic2 = {"sex": "man", "hobby": "beautiful girl"}
14 dic1.update(dic2)  # 将dic2在dic1中有则修改,无则新增
15 print(dic1)
16 
17 # 删除
18 ret_pop = dic1.pop("hobby")
19 # dic1.popitem()  # 随机删除一对, 我看到是最后一个,有返回值,是元组形式返回
20 print(dic1, "\n", ret_pop)
21 del dic1["age"]
22 print(dic1)
23 dic1.clear()  # 清空dict
24 print(dic1)
25 del dic1  # 删除整个字典
26 
27 # 其他
28 dic3 = {"sex": "man", "hobby": "beautiful girl"}
29 print(dic3.keys())  # 获得所有的键,得到的值是dict_keys类型,
30 print(list(dic3.keys()))  # 可以使用list(dic3.keys())转化为list
31 print(dic3.values())  # 将所有键值对以元组的形式列出
32 a = sorted(dic3.items(), reverse=True)  # 根据键排序,整型从小到大,使用reverse为True实现了倒序
33 print(a)

 

3,字符串

 1 a = "abc"
 2 b = "123"
 3 c = ''.join((a, b))
 4 print(c)
 5 c = '--->>>---'.join((a, b))  # 字符串拼接
 6 print(c)
 7 # 重点记忆
 8 st = "hello kitty {{}} {name} {age}"
 9 print(st.count("tt"))  # 统计元素个数
10 print(st.capitalize())  # 首字母大写
11 print(st.center(50, "#"))  # 以st内容为中心,#填充组合50个
12 print(st.endswith("ty"))  # 返回是否ty结尾
13 print(st.startswith("he"))  # 返回是否以he开头
14 print(st.find("tty"))  # 找到匹配的字符串,并返回第一个元素的索引,匹配不到会返回-1
15 print(st.format(name='wuzhenhu', age=26))  # 格式化输出, {{}}可以实现输出{}而不需要传值
16 print("1234".isdigit())  # 判断是否是数字
17 print("ASD".lower())  # 大写变小写
18 print("asd".upper())  # 小写变大写
19 print("  my title  \n ".strip())  # 去掉前后空格和换行符
20 print("my title".replace("title", "lesson"))  # 替换所有相同的,第三个是参数替换几次
21 print("my title title".split(" "))  # 将字符串分割,从左向右
22 
23 # 不常用
24 print("23aa".isidentifier())  # 判断是不是非法变量
25 print("Abc".islower())  # 是不是全小写
26 print("aBc".isupper())  # 是不是全大写
27 print(" ".isspace())  # 是不是全空格
28 print("My Title".istitle())  # 是不是每个单词首字母大写
29 print('My Title'.swapcase())  # 反转大小写
30 print("My Title".ljust(50, "*"))  # My
31 print("  my title  \n ".lstrip())  # 去掉前空格和换行符
32 print("  my title  \n ".rstrip())  # 去掉后空格和换行符
33 print("ok")
34 print("my title".rfind("ti"))  # 从右向左查找返回索引
35 print("my title title".rsplit(" ", 1))  # 将字符串分割,从右向左,最多分割一次
36 print("my title title".title)  # 将字符串的每个单词首字母大写
37 print(st.isalnum())  # 判断是否时字符串或者数字
38 print(st.isdecimal())  # 判断是否是十进制的
39 print("234".isnumeric())  # 和isdigit的用法相同
40 print(st.format_map({"name": "wuzhenhu", "age": 26}))  # 格式化输出
41 print(st.index("it"))  # 返回匹配的字符串的第一元素索引,匹配不到会报错

 

posted on 2017-11-29 16:54  长生帝君  阅读(147)  评论(0编辑  收藏  举报