Python自学,Day2-Python基础

python

本节内容

  1.sys模块

  2.列表、元组操作

  3.字符串操作

  4.字典操作

1、模块初识                                              

Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎任何功能都有相应的Python库支持(看着老NB了哈......)

  • sys  系统模块

 

1 import sys

2 print(sys.path)

 

含义:打印系统环境变量列表

  • os  系统模块

1 import os

2

3 cmd_res=os.system("df -h")#打印到屏幕上,不保存结果

4

5 print("打印结果:",cmd_res)#0代表命令执行的结果为成功

ps:没有搞明白这个具体有什么用.....

 

调用外部非标准的模块

规则:

1、调用的非标准模块时,先为当前路径变量寻找,如果找不到则从全局变量中寻找;

2、如果从当前路径变量和全局环境变量中均无该变量,则程序会提示:No module named \'userloging\'  #\'userloging\'为调用的外部变量

3、解决方案:

1)、将需调用的模块copy至当前环境变量中

2)、将需调用的模块copy至全局环境变量中

3)、程序调用时,声明调用模块的路径

 

2. what is .pyc?                                            

简述Python的运行过程

PyCodeObject和pyc文件:PyCodeObject则是Python编译器真正编译成的结果;pyc是预编译后的字节码文件;

当python程序运行时,编译的结果则是保存在位于内存中的PyCodeObject中,当Python程序运行结束时,Python解释器则将PyCodeObject写回到pyc文件中。

当python程序第二次运行时,首先程序会在硬盘中寻找pyc文件,如果找到,则直接载入,否则就重复上面的过程。

所以我们应该这样来定位PyCodeObject和pyc文件,我们说pyc文件其实是PyCodeObject的一种持久化保存方式。

 

此处跳过一堆的数据和数据运算相关的内容。。。

 

3、列表、元组操作                                           

列表的含义与数组相同;元组为只读数组,不可更改。

(1)、定义列表

names=["shang","quan","xin","ace"]

数组角标从左至右依次为0、1、2...

(2)、获取列表中元素

  1)、单个元素

1 names=["shang","quan","xin","ace"]

2

3 print(names[1])

输出结果:quan

  当取元素较多,直接取最后一个元素时

1 names=["shang","quan","xin","ace"]

2 print(names[-1])

输出结果:ace  备注:-1代表从最后一个元素开始读取

  2)、多个元素

1 names=["shang","quan","xin","ace"]

2 print(names[1],names[3])

输出结果:quan ace

  3)、连续多个元素(切片)

1 names=["shang","quan","xin","ace"]

2 print(names[1:3])

输出结果:[\'quan\', \'xin\']  遵循左闭右开的原则

  从第一位开始获取

1 names=["shang","quan","xin","ace"]

2 print(names[0:1])

输出结果:[\'shang\']  备注:0可以不写,结果相同

1 names=["shang","quan","xin","ace"]

2 print(names[:1])

  倒叙切片

1 names=["shang","quan","xin","ace"]

2 print(names[-3:-1])

输出结果:[\'quan\', \'xin\']  备注:因左闭右开规则,-1位的元素并无法取到

  可以取到最后一位元素

1 names=["shang","quan","xin","ace"]

2 print(names[-3:])

输出结果:

[\'quan\', \'xin\', \'ace\']  

(3)、添加元素

  1)、最后一位添加(append)

1 names=["shang","quan","xin","ace"]

2 names.append("pm")

3 print(names)

输出结果:[\'shang\', \'quan\', \'xin\', \'ace\', \'pm\']

  2)、任意位置添加(insert)

1 names=["shang","quan","xin","ace"]

2 names.insert(1,"pm")

3 print(names)

输出结果:[\'shang\', \'pm\', \'quan\', \'xin\', \'ace\']  其中“1”代表需要插入的位置

(4)、改元素

  改任意位置元素

1 names=["shang","quan","xin","ace"]

2 names[3]="Ace"

3 print(names)

输出结果:[\'shang\', \'quan\', \'xin\', \'Ace\']  直接改变数组中参数的值

(5)、删除

  1)、del 删除

1 names=["shang","quan","xin","ace"]

2 del names[3]

3 print(names)

输出结果:[\'shang\', \'quan\', \'xin\']

  2)、remove删除

1 names=["shang","quan","xin","ace"]

2 names.remove("xin")

3 print(names)

输出结果:[\'shang\', \'quan\', \'ace\']

  3)、pop删除

1 names=["shang","quan","xin","ace"]

2 names.pop()

3 print(names)

输出结果:[\'shang\', \'xin\', \'ace\']  不输入角标时,默认删除最后一个元素

  设置角标时,则同del

1 names=["shang","quan","xin","ace"]

2 names.pop(1)

3 print(names)

输出结果:[\'shang\', \'xin\', \'ace\']

(6)、获取元素角标  index

1 names=["shang","quan","xin","ace"]

2 index=names.index("xin")

3 print(index)

输出结果:2  使用index时获取元素角标

(7)、统计数组中元素的个数  count

1 names=["shang","quan","xin","ace","xin"]

2 count=names.count("xin")

3 print(count)

输出结果:2  使用count进行统计

(8)、清空数组  clear

1 names=["shang","quan","xin","ace","xin"]

2 names.clear()

3 print(names)

输出结果:[]

(9)、反转元素顺序  reverse

1 names=["shang","quan","xin","ace","xin"]

2 names.reverse()

3 print(names)

输出结果:[\'xin\', \'ace\', \'xin\', \'quan\', \'shang\']

(10)、排序  sort

1 names=["shang","quan","xin","ace","xin"]

2 names.sort()

3 print(names)

 输出结果:[\'ace\', \'quan\', \'shang\', \'xin\', \'xin\']

备注:排序优先级为:特殊符号>数字>大写字母>小写字母(阿斯玛值排序)

(11)、合并  extend

names=["shang","quan","xin","ace","xin"]

names2=["gome"]

names.extend(names2)

print(names)

输出结果:[\'shang\', \'quan\', \'xin\', \'ace\', \'xin\', \'gome\']

备注:name2依然存在

(12)、删除数组

 1 del names2 

(13)、复制 copy

   1)、浅copy

1 names=["shang","quan","xin",["pm","python","pycharm"],"ace","xin"]

2 names2=names.copy()

3 print(names)

4 print(names2)

输出结果:

[\'shang\', \'quan\', \'xin\', [\'pm\', \'python\', \'pycharm\'], \'ace\', \'xin\']
[\'shang\', \'quan\', \'xin\', [\'pm\', \'python\', \'pycharm\'], \'ace\', \'xin\']

  修改names2中元素

1 names=["shang","quan","xin",["pm","python","pycharm"],"ace","xin"]

2 names2=names.copy()

3 names2[2]="XIN"

4 print(names)

5 print(names2)

输出结果:

[\'shang\', \'quan\', \'xin\', [\'pm\', \'python\', \'pycharm\'], \'ace\', \'xin\']
[\'shang\', \'quan\', \'XIN\', [\'pm\', \'python\', \'pycharm\'], \'ace\', \'xin\']

  修改name2数组中数组元素

1 names=["shang","quan","xin",["pm","python","pycharm"],"ace","xin"]

2 names2=names.copy()

3 names2[3][0]="PM"

4 print(names)

5 print(names2)

输出结果:

[\'shang\', \'quan\', \'xin\', [\'PM\', \'python\', \'pycharm\'], \'ace\', \'xin\']
[\'shang\', \'quan\', \'xin\', [\'PM\', \'python\', \'pycharm\'], \'ace\', \'xin\']

备注:copy只复制了第一层元素,第二层元素则时复制的空间内存地址,当空间内容修改,原时的数组names也修改

  数组直接赋值

1 names=["shang","quan","xin",["pm","python","pycharm"],"ace","xin"]

2 names2=names

3 names[2]="鑫"

4 names2[3][0]="PM"

5 print(names)

6 print(names2)

输出结果:

[\'shang\', \'quan\', \'鑫\', [\'PM\', \'python\', \'pycharm\'], \'ace\', \'xin\']
[\'shang\', \'quan\', \'鑫\', [\'PM\', \'python\', \'pycharm\'], \'ace\', \'xin\']

备注 :数组赋值,只是数组的内存地址赋值,两个数组指向的是同一个内存地址

  其他写法

 1 import copy

2

3 names=["shang","quan",["pm","python","pycharm"],"ace","xin","python"]

4 names2=copy.copy(names)

5 names3=names[:]

6 names4=list(names)

7

8 names[4]="鑫"

9 names2[2][0]="PM"

10 names3[2][1]="PYTHON"

11 names4[2][2]="PYCHARM"

12 print(names)

13 print(names2)

14 print(names3)

15 print(names4)

输出结果:

[\'shang\', \'quan\', [\'PM\', \'PYTHON\', \'PYCHARM\'], \'ace\', \'鑫\', \'python\']
[\'shang\', \'quan\', [\'PM\', \'PYTHON\', \'PYCHARM\'], \'ace\', \'xin\', \'python\']
[\'shang\', \'quan\', [\'PM\', \'PYTHON\', \'PYCHARM\'], \'ace\', \'xin\', \'python\']
[\'shang\', \'quan\', [\'PM\', \'PYTHON\', \'PYCHARM\'], \'ace\', \'xin\', \'python\']

备注:引用copy模块,name2是用copy函数下的浅copy,name3是用完全切片,name4是使用工厂函数

  2)、深copy

1 import copy

2

3 names=["shang","quan","xin",["pm","python","pycharm"],"ace","xin"]

4 names2=copy.deepcopy(names)

5 names[2]="鑫"

6 names2[3][0]="PM"

7 print(names)

8 print(names2)

输出结果:

[\'shang\', \'quan\', \'鑫\', [\'pm\', \'python\', \'pycharm\'], \'ace\', \'xin\']
[\'shang\', \'quan\', \'xin\', [\'PM\', \'python\', \'pycharm\'], \'ace\', \'xin\']

备注:引用copy模块,使用copy.deepcopy(),两个独立的数组元素

  3)、列表循环打印

1 names=["shang","quan","xin",["pm","python","pycharm"],"ace","xin"]

2 names2=copy.deepcopy(names)

3 names[2]="鑫"

4 names2[3][0]="PM"

5 print(names)

6 print(names2)

7

8 for i in names2:

9 print(i)

输出结果:

[\'shang\', \'quan\', \'鑫\', [\'pm\', \'python\', \'pycharm\'], \'ace\', \'xin\']
[\'shang\', \'quan\', \'xin\', [\'PM\', \'python\', \'pycharm\'], \'ace\', \'xin\']
shang
quan
xin
[\'PM\', \'python\', \'pycharm\']
ace
xin

备注:循环打印,只需打印索引角标即可,ps,如此简单.....

(14)、定义元组

1 names=(\'shang\',\'quan\',\'xin\')

2 print(names)

输出:(\'shang\', \'quan\', \'xin\')

备注:元素不可更改,所以无可编辑功能

 

程序练习 

请闭眼写出以下程序。

程序:购物车程序

需求:

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
  4. 可随时退出,退出时,打印已购买商品和余额

 

 1 product_list=[

2 (\'iphone\',5800),

3 (\'mac pro\',12000),

4 (\'starbucks\',37),

5 (\'python\',10000),

6 (\'book\',45),

7 (\'apple\',6),

8 (\'bike\',800)

9 ]

10 product_neeeded=[]

11 while True:

12 salary = input("Input your salary:")

13 if salary.isdigit():#判断是否是数字

14 salary=int(salary)

15 break

16 else:

17 print("Please input the number!")

18 while True:

19 for item in product_list:

20 print(product_list.index(item), item)

21 shopping=input("Input the product number of you need: ")

22 if shopping.isdigit():

23 shopping=int(shopping)

24 if shopping >=0 and shopping<len(product_list):

25 price=product_list[shopping][1]

26 if price<=salary:

27 salary-=price

28 product_neeeded.append(product_list[shopping])

29 for added in product_neeeded:

30 print(added)

31 print("remind banlance:",salary)

32 shopping_reslut=input("do you want continue? Y/N:")

33 if shopping_reslut=="Y":

34 continue

35 else:

36 break

37 else:

38 print("the salary is not enough!remind banlance:",salary)

39 shopping_reslut = input("do you want continue? Y/N:")

40 if shopping_reslut == "Y":

41 continue

42 else:

43 break

44 else:

45 print("Please input the right number of product!")

46 else:

47 print("Please input the right number of product!")

48 print("------------shopping_list------------")

49 for added in product_neeeded:

50 print(added)

51 print("remind banlance:", salary)

View Code

备注:第一个自己码的代码,如有不妥的地方,欢迎大神开喷~(心境跟第一次做C课程设计一样儿一样儿的)

 4、字符串操作 string                                            

  • capitalize  首字母大写

1 name="ace shang"

2 print(name.capitalize())

输出结果:Ace shang

  • count  统计个数

1 name="ace shang"

2 print(name.count("a"))

输出结果:2  统计string中a的个数

  • center  将输入的字符串剧中

1 name="ace shang"

2 print(name.center(50,"-"))

输出结果:--------------------ace shang---------------------  备注:输出50个字符,不足则以“-”补充,并将name居中

  • endswith  校验字符串结尾

1 name="aceshang@qq.com"

2 print(name.endswith(".com"))

输出结果:True  可以用于校验输入内容格式是否满足要求,例如邮箱结尾

  • expandtabs  将\t转换成要求的空格数输出

1 name="ace\tshang@qq.com"

2 print(name.expandtabs(3))

输出结果:ace   shang@qq.com

  • find   查询输入项在字符串中位置

1 name="aceshang@qq.com"

2 print(name.find("a"))

输出结果:0

1 name="aceshang@qq.com"

2 print(name.find("p"))

输出结果:-1  不存在时,默认-1

1 name="aceshang@qq.com"

2 print(name.find("ace"))

输出结果:0  若输入项是一串,按第一个字符查询,

  • format  构造格式

1 name="my name is {} and my age is {}"

2 print(name.format("ace",29))

输出结果:my name is ace and my age is 29

  角标定义位置

1 name="my name is {0} and my age is {1}"

2 print(name.format("ace",29))

输出结果:my name is ace and my age is 29

  按定义的角标与输入项对应

1 name="my name is {1} and my age is {0}"

2 print(name.format("ace",29))

输出结果:my name is 29 and my age is ace

  参数定义位置

1 name="my name is {name} and my age is {age}"

2 print(name.format(name="ace",age=29))

输出结果:my name is ace and my age is 29

  • format_map  字典形式构造格式

1 name="my name is {name} and my age is {age}"

2 print(name.format_map({\'name\':\'ace\',\'age\':30}))

输出结果:my name is ace and my age is 30  数据字典:{\'name\':\'ace\',\'age\':30}

  • index  查询索引值

1 name="my name is {name} and my age is {age}"

2 print(name.index("m"))

输出结果:0

  • isalnum  是否是阿拉伯数字或者英文字母

1 name="abc1A"

2 print(name.isalnum())

输出结果:True

  如果有特殊字符,则为False

1 name="abc1A$#"

2 print(name.isalnum())

输出结果:False

  • isalpha  是否是纯英文字符

1 name="abcQ"

2 print(name.isalpha())

输出结果:True

  • isdecimal  是否是十进制

1 name="1222"

2 print(name.isdecimal())

输出结果:True

  • isdigit  是否是整数

1 name="12"

2 print(name.isdigit())

输出结果:True

  • isidentifier  判读是否是合法的标示符

1 name="ab12"

2 print(name.isidentifier())

输出结果:True  备注:有空格、特殊字符为不合法

  • islower  是否是小写
  • isupper  是否是大些
  • join  链接

1 name=["a","b","c"]

2 print("|".join(name))

输出结果:a|b|c  用指定符号将字符串拼接

  • ljust  左对其
  • rjust  右对其

1 name="my name is ace"

2 print(name.ljust(50,"*"))

3 print(name.rjust(50,"-"))

输出结果:

my name is ace************************************   备注:总长度50,不足用定义字符补充 
------------------------------------my name is ace

  • lower  变为小写
  • upper  变为大些

1 name="My Name Is Ace"

2 print(name.lower())

3 print(name.upper())

输出结果:

my name is ace
MY NAME IS ACE

  • lstrip  去掉左边的空格和换行(\n)
  • rstrip  去掉右边的空格和换行
  • strip  去调两边的空格和换行

 1 name="     My Name Is Ace\n"

2 print(name.lstrip())

3 print(name.rstrip())

4 print(name.strip())

5

6 #输出结果

7 My Name Is Ace

8

9 My Name Is Ace

10 My Name Is Ace

  • maketrans  作字符转换编码
  • translate  字符变换

1 intab="abcdefghijklmn"

2 outtab="!@#$%^&*()_+QW"

3 incode=str.maketrans(intab,outtab)

4 print("my name is ace".translate(incode))

5

6 #输出结果

7 Qy W!Q% (s !#%

备注:先用maketrans编码,然后使用translate进行字符转换

  • replace  替换

1 name=\'Ace Angle\'

2 print(name.replace(\'A\',\'a\',1))#将A替换成a,替换个数为1个

3

4 #输出结果

5 ace Angle

  • rfind  查询最右一位指定内容的角标

1 name=\'Ace Angle\'

2 print(name.rfind(\'e\'))

3

4 #输出结果

5 8

 备注:最后一个e的角标值

  • split  按照分隔符将字符串分割成数组

1 1 name=\'Alce Angle\'

2 2 print(name.split())

3 3 print(name.split(\'l\'))

4 4

5 5 #输出结果

6 6 [\'Alce\', \'Angle\'] #默认空格分割

7 7 [\'A\', \'ce Ang\', \'e\'] #按照\'l\'进行分割

  • swapcase  大小字母互换

1 name=\'Ace Angle\'

2 print(name.swapcase())

3

4 #输出结果

5 aCE aNGLE

  • title  将每个字符的首字母大写

1 name=\'ace angle\'

2 print(name.title())

3

4 #输出结果

5 Ace Angle

5、字典操作                                                 

 1 #定义字典

2 info={

3 \'no101\':"Ace Shang",

4 \'no102\':"Baobao Feng",

5 \'no103\':"Chulan Zhang",

6 \'no104\':"Zhenqiu Wang",

7 }

8

9 #打印资料

10 print("1:",info) #打印是无序的

11

12 #精确查找,明确知道key值

13 print("2:",info["no102"]) #当没有改key时,程序报错

14

15 print("3:",info.get(\'no106\')) #没有该key时,返回none

16 print("4:",info.get(\'no103\')) #有该key时,则返回对应的value

17

18 print("5:",\'no109\' in info) #判断no109是否在info的字典中

19

20 #修改字典内容

21 info["no101"]="Ye Wang"

22 print("6:",info)

23

24 #新增

25 info["no105"]="Qing Zhuge"

26 print("7:",info)

27

28 #删除

29 del info["no104"]

30 print("8:",info)

31

32 info.pop("no105")

33 print("9:",info)

34

35 #删除随机项(尽量不用)

36 info.popitem()

37 print("10:",info)

38

39

40 #输出结果

41 1: {\'no101\': \'Ace Shang\', \'no102\': \'Baobao Feng\', \'no103\': \'Chulan Zhang\', \'no104\': \'Zhenqiu Wang\'}

42 2: Baobao Feng

43 3: None

44 4: Chulan Zhang

45 5: False

46 6: {\'no101\': \'Ye Wang\', \'no102\': \'Baobao Feng\', \'no103\': \'Chulan Zhang\', \'no104\': \'Zhenqiu Wang\'}

47 7: {\'no101\': \'Ye Wang\', \'no102\': \'Baobao Feng\', \'no103\': \'Chulan Zhang\', \'no104\': \'Zhenqiu Wang\', \'no105\': \'Qing Zhuge\'}

48 8: {\'no101\': \'Ye Wang\', \'no102\': \'Baobao Feng\', \'no103\': \'Chulan Zhang\', \'no105\': \'Qing Zhuge\'}

49 9: {\'no101\': \'Ye Wang\', \'no102\': \'Baobao Feng\', \'no103\': \'Chulan Zhang\'}

50 10: {\'no101\': \'Ye Wang\', \'no102\': \'Baobao Feng\'}

  • 多级字典嵌套及操作

 1 #多级嵌套

2 under_one_person={

3 "NaDouTong":{

4 "Chulan Zhang":["不摇碧莲","无形大贱","炁体源流","月下观鸟"],

5 "Baobao Feng":["社会我宝儿姐","机智一逼","阿威十八式","冈本零点零一"],

6 "Fangxu Zhao":["公司大Boss","心思细腻","知道宝儿姐身份","威望极高"]

7 },

8 "Region":{

9 "Northeast":["leader:高廉","temporary:二壮(高钰珊)"],

10 "North China":["leader:徐四","temporary:冯宝宝"],

11 "East China":["leader:窦乐","temporary:肖自在"],

12 "Central China":["leader:任菲","temporary:黑管儿"],

13 "Westeast":["leader:华风","temporary:老孟"],

14 "Westsouth":["leader:郝意","temporary:王震球"]

15 },

16 "8Buff":{

17 "张怀义":["甲申之乱的罪魁祸首","挂:炁体源流","不摇碧莲的爷爷"],

18 "无根生":["公认的天下第一搅屎棍","挂:神明灵","全性掌门"],

19 "风天养":["凉山大觋","挂:拘灵遣将","被王家控制,截胡buff"],

20 "郑子步":["茅山上清","挂:通天箓","陆瑾的好友"],

21 "周圣":["武当弟子","挂:风后奇门","下落不明"],

22 "马本在":["炼器师","挂:神机百炼","老马的的曾祖父"],

23 "阮丰":["干城章嘉峰上修行","挂:六库仙贼","传授于巴伦"],

24 "谷畸亭":["全性中人","挂:大罗洞观","不详"],

25 "端木瑛":["不详","挂:双全手","不详"]

26 }

27 }

28

29 for i in under_one_person:

30 print(i) #打印key

31

32 #变更value值

33 under_one_person["8Buff"]["周圣"][2]="也总已习得该技能"

34 print(under_one_person["8Buff"]["周圣"])

35

36 #输出结果

37 NaDouTong

38 Region

39 8Buff

40 [\'武当弟子\', \'挂:风后奇门\', \'也总已习得该技能\']

View Code

  • 字典的其他操作

 1 info={

2 \'no101\':"Ace Shang",

3 \'no102\':"Baobao Feng",

4 \'no103\':"Chulan Zhang",

5 \'no104\':"Zhenqiu Wang",

6 }

7

8 #打印字典values

9 print("1:",info.values())

10

11 #打印字典的key

12 print("2:",info.keys())

13

14 #setdefault

15 print("3:",info.setdefault("no104","Ye Wang")) #当key存在时,则获取key值对应的value,并输出此value

16 print("4:",info.setdefault("no105","Si Xu")) #当key不存时,则新增key值并赋相应的value,同时输出新赋值的value

17 print("5:",info)

18

19 #update

20 new_info={1:2,"no101":"Zhiwei Zhang",4:5,}

21 info.update(new_info) #将new_info合并至info,并且将已经存在key的value更新为新的value

22 print("6:",info)

23

24 #item

25 print("7:",info.items()) #将字典转化成列表

26

27 #循环dict

28 for key in info:

29 print(key,info[key]) #key输出字典的key值,info[key]输出key对应的value

30

31 #循环dict之item

32 for k,v in info.items(): #将字典转换成列表,别按照列表对应的关系进行输出;当数据量大时,勿用该方法

33 print(k,v)

34

35 #输出结果

36 1: dict_values([\'Ace Shang\', \'Baobao Feng\', \'Chulan Zhang\', \'Zhenqiu Wang\'])

37 2: dict_keys([\'no101\', \'no102\', \'no103\', \'no104\'])

38 3: Zhenqiu Wang

39 4: Si Xu

40 5: {\'no101\': \'Ace Shang\', \'no102\': \'Baobao Feng\', \'no103\': \'Chulan Zhang\', \'no104\': \'Zhenqiu Wang\', \'no105\': \'Si Xu\'}

41 6: {\'no101\': \'Zhiwei Zhang\', \'no102\': \'Baobao Feng\', \'no103\': \'Chulan Zhang\', \'no104\': \'Zhenqiu Wang\', \'no105\': \'Si Xu\', 1: 2, 4: 5}

42 7: dict_items([(\'no101\', \'Zhiwei Zhang\'), (\'no102\', \'Baobao Feng\'), (\'no103\', \'Chulan Zhang\'), (\'no104\', \'Zhenqiu Wang\'), (\'no105\', \'Si Xu\'), (1, 2), (4, 5)])

43 no101 Zhiwei Zhang

44 no102 Baobao Feng

45 no103 Chulan Zhang

46 no104 Zhenqiu Wang

47 no105 Si Xu

48 1 2

49 4 5

50 no101 Zhiwei Zhang

51 no102 Baobao Feng

52 no103 Chulan Zhang

53 no104 Zhenqiu Wang

54 no105 Si Xu

55 1 2

56 4 5

View Code

  • fromkey 初始化生成字典

 1 #初始化生成字典    fromkey

2 info=dict.fromkeys([1,2,3],"temp")

3 info2=dict.fromkeys([4,5,6],["冯宝宝",{"name":"张楚岚"},"王也"])

4 print(info)

5 print(info2)

6

7 #修改

8 info[2]="ace"

9 print(info)

10

11 info2[4]="宝儿姐"

12 info2[5][1]["name"]="不摇碧莲"

13 #同浅copy,对于字典赋值的只是路径,改变一个key值对应的value,则其他key中对应value也变更

14 print(info2)

15

16 #输出结果

17 {1: \'temp\', 2: \'temp\', 3: \'temp\'}

18 {4: [\'冯宝宝\', {\'name\': \'张楚岚\'}, \'王也\'], 5: [\'冯宝宝\', {\'name\': \'张楚岚\'}, \'王也\'], 6: [\'冯宝宝\', {\'name\': \'张楚岚\'}, \'王也\']}

19 {1: \'temp\', 2: \'ace\', 3: \'temp\'}

20 {4: \'宝儿姐\', 5: [\'冯宝宝\', {\'name\': \'不摇碧莲\'}, \'王也\'], 6: [\'冯宝宝\', {\'name\': \'不摇碧莲\'}, \'王也\']}

  • 三级菜单 略

因涉及到较多重复代码,后期学到函数时再进行编码....

 

以上是 Python自学,Day2-Python基础 的全部内容, 来源链接: utcz.com/z/388727.html

回到顶部