这周时间充裕,把第一周的两个作业登陆验证和三级菜单做完后又用零零散散的时间看完了第二周的课程,不得不说老男孩这个教育方式感觉还是不错的,其实说白了就是花钱找个人监督自己学习呗,而且还强行让我们养成一些好习惯,博客也好,流程图也好,还有ReadMe的一些规范,希望自己也可以坚持下去。
下面简单回顾一下第二周的课程的知识点:
一、模块初识
只讲了两个基础模块
sys:
#Author:Ivor import sys
s = sys.path #调用python获取模块的路径
print(s)
print(sys.argv) #脚本自身的相对路径位置,不过在pycharm里显示的是绝对路径,因为pycharm里边存储的就是绝对路径,在cmd中就会显示相对路径
print(sys.argv[1]) #可以从脚本外取值,相当于shell里边的$1 $2,[1]获取第一个参数
os:
#Author:Ivor import os #os.system('dir') #执行命令,dir是windows下显示当前目录文件的命令,直接在屏幕打印出来
cmd_rst = os.popen('dir') #popen可以将打印的信息存储起来,返回存储数据的内存地址
cmd_rst = os.popen('dir').read() #read方法可以直接把打印数据读出来
print(cmd_rst) #os.mkdir("new_file") #在当前目录新建一个文件夹
二、pyc的概念
自己的理解,python在运行之前会像java一样生成一个预编译的文件*.pyc,但是预编译的程度可能只有20%不到,java可以达到50%左右,当python执行程序时会先找有没有该程序的pyc,再按照pyc和程序的时间进行对比,哪个更新就用哪个。再引用一下Alex写的。
我们在硬盘上看到的pyc自然不必多说,而其实PyCodeObject则是Python编译器真正编译成的结果。我们先简单知道就可以了,继续向下看。
当python程序运行时,编译的结果则是保存在位于内存中的PyCodeObject中,当Python程序运行结束时,Python解释器则将PyCodeObject写回到pyc文件中。
当python程序第二次运行时,首先程序会在硬盘中寻找pyc文件,如果找到,则直接载入,否则就重复上面的过程。
所以我们应该这样来定位PyCodeObject和pyc文件,我们说pyc文件其实是PyCodeObject的一种持久化保存方式。
三、python数据类型
int(整型)
"hello world"
- 移除空白
- 分割
- 长度
- 索引
- 切片
基本操作:
- 索引
- 切片
- 追加
- 删除
- 长度
- 切片
- 循环
- 包含
ages
=
(
11
,
22
,
33
,
44
,
55
)
或
ages
=
tuple
((
11
,
22
,
33
,
44
,
55
))
person
=
{
"name"
:
"mr.wu"
,
'age'
:
18
}
或
person
=
dict
({
"name"
:
"mr.wu"
,
'age'
:
18
})
#Author:Ivor msg='*' print(msg.encode(encoding='GBK')) mss=b'\xcc\xec\xb0\xb2\xc3\xc5' print(mss.decode(encoding='GBK'))
五、列表的使用
列表有许多自带的内置方法
#Author:Ivor
#list使用 names = ["zZhangsan","lisi","wangwu","xiaoliu"] #names.clear() #清空列表
#names.pop() #删除最后一个
#names.append("xxxxx") #在最后添加
#print(names.count("Zhangsan")) #计数
#names2=[1,2,3,4,5];names.extend(names2) #将names2添加到names中
#print(names[names.index("lisi")]) #寻找下标,并打印出这个值
#names.insert(1,"aaaa") #在哪个位置插入
#names.reverse() #反向排序
#names.remove("lisi") #删除
#names.sort() #排序,按ascii码
print(names)
列表的另一些用法
#Author:Ivor
import copy person = ["name",['saving',1000]] #浅copy的三种方式
names2 = copy.copy(person)
names2 = person[:]
names2 = list(person) #列表指向同一块内存
#names2 = names #可以用作联合账号
p1 = person[:]
p2 = list(person) p1[0]='alex'
p2[0]='fengjie' p1[1][1]=500 print(p1)
print(p2)
六、元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
语法
names = ("alex","jack","eric")
它只有2个方法,一个是count,一个是index,完毕。
七、字典
字典应该是和列表一起,最最常用的数据类型
这里mark一个迭代时候可以用的方法,enumerate,可以获取字典的下标
语法:
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}
增加:
>>> info["stu1104"] = "苍井空"
>>> info
{'stu1102': 'LongZe Luola', 'stu1104': '苍井空', 'stu1103': 'XiaoZe Maliya', 'stu1101': 'TengLan Wu'}
修改:
>>> info['stu1101'] = "武藤兰"
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1101': '武藤兰'}
删除:
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1101': '武藤兰'}
>>> info.pop("stu1101") #标准删除姿势
'武藤兰'
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
>>> del info['stu1103'] #换个姿势删除
>>> info
{'stu1102': 'LongZe Luola'}
>>>
>>>
>>>
>>> info = {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} #随机删除
>>> info.popitem()
('stu1102', 'LongZe Luola')
>>> info
{'stu1103': 'XiaoZe Maliya'}
查找:
>>> info = {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
>>>
>>> "stu1102" in info #标准用法
True
>>> info.get("stu1102") #获取
'LongZe Luola'
>>> info["stu1102"] #同上,但是看下面
'LongZe Luola'
>>> info["stu1105"] #如果一个key不存在,就报错,get不会,不存在只返回None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'stu1105'
其他用法:
#values
>>> info.values()
dict_values(['LongZe Luola', 'XiaoZe Maliya']) #keys
>>> info.keys()
dict_keys(['stu1102', 'stu1103']) #setdefault
>>> info.setdefault("stu1106","Alex")
'Alex'
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
>>> info.setdefault("stu1102","龙泽萝拉")
'LongZe Luola'
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'} #update
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'}
>>> b = {1:2,3:4, "stu1102":"龙泽萝拉"}
>>> info.update(b)
>>> info
{'stu1102': '龙泽萝拉', 1: 2, 3: 4, 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'} #items
info.items()
dict_items([('stu1102', '龙泽萝拉'), (1, 2), (3, 4), ('stu1103', 'XiaoZe Maliya'), ('stu1106', 'Alex')]) #通过一个列表生成默认dict,有个没办法解释的坑,少用吧这个
>>> dict.fromkeys([1,2,3],'testd')
{1: 'testd', 2: 'testd', 3: 'testd'}
八、之后是按照视频要求写了两个小实例,三级菜单和购物车
三级菜单:
#Authon Ivor dic_list = {
"北京":{
"东城": {},
"崇文": ['虹桥市场','北京游乐园','百荣商场'],
"西城": {},
"宣武": {}
},
"上海":{},
"广州":{}
} while True:
for i in dic_list:
print(i)
choice = input("Please input your choice exit(q):")
if choice == "q":
print("exit the program..")
exit()
while True:
if choice in dic_list.keys():
for i in dic_list[choice]:
print(i)
choice2 = input("Please input your choice2 exit(q) return(r):")
if choice2 == "q":
print("exit the program..")
exit()
elif choice2 == 'r':
break
if choice2 in dic_list[choice]:
while True:
for i in dic_list[choice][choice2]:
print(i)
choice3 = input("Please input your choice3 exit(q) return(r):")
if choice3 == "q":
print("exit the program..")
exit()
elif choice3 == 'r':
break
购物车:
#Author:Ivor
product_list = [
["iphone", 5999],
["bike", 899],
["book", 120],
["coffee", 31]
]
goods =[]
salary = input("please input your salary:")
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
print(index,item)
user_choice = input(">>>")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice >= 0 and user_choice < len(product_list):
if salary > product_list[user_choice][1]:
salary -= product_list[user_choice][1]
goods.append(product_list[user_choice])
print("your balance is:",salary)
else:
print("your money is not enough..")
else:
print("in valid number!")
elif user_choice == 'q':
print("your balance is ",salary)
print("your shop list is ",goods)
break
else:
print("invalid choice")
else:
print("invalid value!")