学习python:day2
1. 库
标准库:就是不用安装,直接导入的库就叫标准库。是python最常用的功能,比如sys和os模块。
1-1 sys模块
import sys
print(sys.path) #相对路径
print(sys.argv) #参数
1-2 os模块
import os
cmd_dir = os.system("dir") #将当前目录下的内容显示到屏幕,并不赋值
print('--->',cmd_dir) #输出为--->0
cmd_new = os.popen("dir").read() #将当前目录下的内容显示到屏幕,并赋值
第三方库:必须安装才能使用的库
注意:在程序中导入模块要在开头用import;
在login.py 中引用user.py程序,且这两个程序在同一个目录下,则可在login.py文件中写: import user #不用写 .py
2. 列表、元组操作
列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作
定义列表
|
1
|
names = ['Alex',"Tenglan",'Eric'] |
通过下标访问列表中的元素,下标从0开始计数
|
1
2
3
4
5
6
7
8
|
>>> names[0]'Alex'>>> names[2]'Eric'>>> names[-1]'Eric'>>> names[-2] #还可以倒着取'Tenglan' |
增删改查 ,切片练习
#names = "ZhangYang Guyun Xiangpeng XuLiangChen"
'''names = ["4ZhangYang", "#!Guyun","xXiangPeng",["alex","jack"],"ChenRonghua","XuLiangchen"]
print(names[0:-1:2])
print(names[::2])
print(names[:])
#range(1,10,2)
for i in names:
print(i)
name2 = copy.deepcopy(names)
print(names)
print(name2)
names[2] = "向鹏"
names[3][0] ="ALEXANDER"
print(names)
print(name2)
names.append("LeiHaidong")
names.insert(1,"ChenRonghua")
names.insert(3,"Xinzhiyu")
#names[2] ="XieDi"
#print(names[0],names[2])
#print(names[1:3]) #切片
#print(names[3]) #切片
#print(names[-2:]) #切片
#print(names[0:3]) #切片
#print(names[:3]) #切片
#delete
#names.remove("ChenRonghua")
#del names[1] =names.pop(1)
#names.pop(1)
print(names)
#print(names.index("XieDi"))
#print( names[names.index("XieDi")] )
#print(names.count("ChenRonghua"))
#names.clear()
#names.reverse()
#names.sort()
print(names)
names2 = [1,2,3,4]
#names
names.extend(names2)
#del names2
print(names,names2)'''
a =[1,2,3]
b = a
a[1] =555
b = [1,555,3]
三级菜单练习
data = {
'北京':{
"昌平":{
"沙河":["oldboy","test"],
"天通苑":["链家地产","我爱我家"]
},
"朝阳":{
"望京":["奔驰","陌陌"],
"国贸":{"CICC","HP"},
"东直门":{"Advent","飞信"},
},
"海淀":{},
},
'山东':{
"德州":{},
"青岛":{},
"济南":{}
},
'广东':{
"东莞":{},
"常熟":{},
"佛山":{},
},
}
exit_flag = False
while not exit_flag:
for i in data:
print(i)
choice = input("选择进入1>>:")
if choice in data:
while not exit_flag:
for i2 in data[choice]:
print("\t",i2)
choice2 = input("选择进入2>>:")
if choice2 in data[choice]:
while not exit_flag:
for i3 in data[choice][choice2]:
print("\t\t", i3)
choice3 = input("选择进入3>>:")
if choice3 in data[choice][choice2]:
for i4 in data[choice][choice2][choice3]:
print("\t\t",i4)
choice4 = input("最后一层,按b返回>>:")
if choice4 == "b":
pass
elif choice4 == "q":
exit_flag = True
if choice3 == "b":
break
elif choice3 == "q":
exit_flag = True
if choice2 == "b":
break
elif choice2 == "q":
exit_flag = True
3.字典
字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。
语法:
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}
字典的特性:
- dict是无序的
- key必须是唯一的,so 天生去重
相关练习
#key-value
'''
av_catalog = {
"欧美":{
"www.youporn.com": ["很多免费的,世界最大的","质量一般"],
"www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"],
"letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
"x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"]
},
"日韩":{
"tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"]
},
"大陆":{
"1024":["全部免费,真好,好人一生平安","服务器在国外,慢"]
}
}
av_catalog["大陆"]["1024"][1] = "可以在国内做镜像"
av_catalog.setdefault("大陆",{"www.baidu.com":[1,2]})
print(av_catalog)
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}
b ={
'stu1101': "Alex",
1:3,
2:5
}
info.update(b)
print(info )
c = dict.fromkeys([6,7,8],[1,{"name":"alex"},444])
print(c )
c[7][1]['name'] = "Jack Chen"
print(c)'''
#print(info.items() )
#info['stu1104']
#print(info.get('stu1103'))
#print('stu1103' in info) #info.has_key("1103") in py2.x
'''
#print(info["stu1101"])
info["stu1101"] ="武藤兰"
info["stu1104"] ="CangJingkong"
#del
#del info["stu1101"]
info.pop("stu1101")
info.popitem()
print(info)
'''
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}
for i in info:
print(i,info[i])
for k,v in info.items():
print(k,v)
4.一些函数
name = "my \tname is {name} and i am {year} old"
print(name.capitalize())
print(name.count("a"))
print(name.center(50,"-"))
print(name.endswith("ex"))
print(name.expandtabs(tabsize=30))
print(name[name.find("name"):])
print(name.format(name='alex',year=23))
print(name.format_map( {'name':'alex','year':12} ))
print('ab23'.isalnum())
print('abA'.isalpha())
print('1A'.isdecimal())
print('1A'.isdigit())
print('a 1A'.isidentifier()) #判读是不是一个合法的标识符
print('33A'.isnumeric())
print('My Name Is '.istitle())
print('My Name Is '.isprintable()) #tty file ,drive file
print('My Name Is '.isupper())
print('+'.join( ['1','2','3']) )
print( name.ljust(50,'*') )
print( name.rjust(50,'-') )
print( 'Alex'.lower() )
print( 'Alex'.upper() )
print( '\nAlex'.lstrip() )
print( 'Alex\n'.rstrip() )
print( ' Alex\n'.strip() )
p = str.maketrans("abcdefli",'123$@456')
print("alex li".translate(p) )
print('alex li'.replace('l','L',1))
print('alex lil'.rfind('l'))
print('1+2+3+4'.split('\n'))
print('1+2\n+3+4'.splitlines())
print('Alex Li'.swapcase())
print('lex li'.title())
print('lex li'.zfill(50))
print( '---')
浙公网安备 33010602011771号