DAY1
学习内容:
今天是培训班开班第一天,第一天,了解了python的历史,发展前景,也喝了Alex老师的一大碗鸡汤。鸡汤是鲜的,然后学习还是要学得。
今天写了python的第一个程序,hello world。突然想起大学的时候学习c语言的时候,写的也是这个。此时此刻,新增所想确实,hello python,i‘m coming。
笔记:大写代表常量,例如PIE;
ASCII的起源,以及gb2312、gbk、gb18031和utf-8等编码的出生;
注释:单行用#,多行用‘‘‘ ‘‘‘;
定于多行变量用‘‘‘ ‘‘‘;
python默认所有输入均为字符串;
getpass模块可以使用户输入的密码不可见。
DAY2
学习内容:
第二天的继续按部就班的学习。今天学习了if while循环,写了一些上课的例子:
guess_answer = 56
count = 0
while count<3:
guess = int(input("what‘my age,please gusess:"))
if guess == guess_answer :
print(‘you got it‘)
break
elif guess > guess_answer :
print(‘Thinking less!!‘)
else :
print(‘Thinking lager!!‘)
count +=1
else:
print(‘you guess too many‘)
for i in range(3):
guess = int(input("what‘my age,please gusess:"))
if guess == guess_answer :
print(‘you got it‘)
break
elif guess > guess_answer :
print(‘Thinking less!!‘)
else :
print(‘Thinking lager!!‘)
else:
print(‘you guess too many‘)
while count <3:
guess = int(input("what‘my age,please gusess:"))
if guess == guess_answer :
print(‘you got it‘)
break
elif guess > guess_answer :
print(‘Thinking less!!‘)
else :
print(‘Thinking lager!!‘)
count +=1
if count == 3:
continue_guess = input(‘Do you want guessing again?‘)
if continue_guess != ‘n‘:
count = 0
上面的这个小程序是用来猜答案,第一次动手写一个小程序,感觉收获很大。今天的作业是登陆程序和三级菜单,都顺利的完成了。
笔记:
continue:跳出本次循环,进入下一次循环;
break:打断整个循环;
pass:跳过
os模块:
os.system("dir") 执行命令,不保存结果
os.popen("dir").read() 前面一个执行命令,有存储结果,后面的read()读取结果
os.mkdir("文件名") 创建目录
计算机中能表示(存储)的最小单位,是一个二进制(bit)
8bit = byte(字节)
1024byte = 1kbyte
60 & 13 结果是12 (and)
60 | 13 结果是61(or)
三元运算:
a,b,c = 1,3,5
d = a if a < b else c if条件成立,d = a ,否则 d = c
encode 编码
decode 解码
string <-----> bytes
DAY3
学习内容:
今天学习了列表,元祖和字典。
names = ["dzk","pxm","zd","zm"] print(names[0]) names.append(23) print(names) print(names[1:2]) print(names[-3:-1])
上面是字典的一些用法。还写了购物车作业:
import yaml #导入yaml模块
f = open(‘shoping_list‘,"rb")
goods_dict = yaml.load(f) #利用yaml模块将信息文件转为商品列表
print(goods_dict)
dict_user = {} #定义老用户名单字典
file =open("user","r",encoding="utf-8")
for line in file:
key,vaule = line.strip().split(":")
dict_user[key] = vaule #将老用户文件内容转成字典
file.close()
buy_list = [] #定义已购商品列表
dict_olduser_info = {} #定义老用户已购买信息
print("wlecome to shoping mall") #输出欢迎信息
username = input("please input your name>>>")
password = input("please input your password>>>")
def exit_shoping(): #定义程序退出函数
print("--------you have bought \033[32;1m %s\033[0m-------" % buy_list)
exit()
def shoping(user_wage): #定义用户购买商品程序函数,方便是老用户的调用
while True:
print(goods_dict)
number = input("please chose the number(q to exit):")
if number.isdigit(): #判断用户输入的商品编号是否是数字
buy_number = int(number)
if buy_number in goods_dict: #判断用户输入的商品编号是否存在
if user_wage >= goods_dict[buy_number][1]: #用户的工资够买商品
user_wage -= goods_dict[buy_number][1]
buy_list.append(goods_dict[buy_number][0])
print("you chose \033[32;1m %s \033[0m,you left \033[32;1m %s \033[0m" %(goods_dict[buy_number][0],user_wage))
else:
print("you haven‘t enough money") #用户工资不够买
exit_shoping()
else:
print("The goods list don‘t have this number")
elif number == ‘q‘:
with open("buy_info", "a+", encoding="utf-8") as file2: #用户退出时,将用户余额存入文件,方便下次调用
file2.write("\n"+username+":"+str(user_wage))
with open("user", "a+", encoding="utf-8") as file1: #用户退出时,将用户名存入文件,方便下次调用
file1.write("\n"+username+":"+ password )
exit_shoping()
else:
print("wrong number,please input again")
if username not in dict_user: #检验用户是新用户
user_wage = int(input("please input your wage:"))
shoping(user_wage)
elif username in dict_user: #用户是老用户
with open("buy_info", "r+", encoding="utf-8") as file3:
for line in file3:
key, vaule = line.strip().split(":")
dict_olduser_info[key] = vaule
left_wage = int(dict_olduser_info[username])
print("you have \033[32;1m %s \033[0m money"%(dict_olduser_info[username]))
shoping(left_wage)
笔记
列表
1、切片
2、插入:insert(1," ") 1代表插入位置
3、删除 a >> names.remove(" ")
b >> del name [ ] #删除指定位置
c >> names.pop(" ") #可指定下标,否则默认删除最后一个
4、查找 names.index(" ")
5、 统计个数 names.count(" ")
6、反转 names.reverse(" ")
7、合并 names.extend(name2)
8、浅复制 names.copy() = name2
9、深复制 import copy
name2 = copy.deepcopy(names)
元祖(不可改、删、加)
1、index #索引
2、count #统计个数
字典 (key - value)
1、字典是 无序的,没有下标
2、删除 del info["key"]
info.pop["key"]
info.popitem() #随机删
3、setdefault("key","value") #创建一个新值
4、items() #把字典转换成列表
python-第一块,笔记整理和学习内容复习(day1 - day2 - day3)
原文:http://www.cnblogs.com/japhi/p/6832596.html