python 笔记
第一周2016/9/11
Python 2.0和3.0的区别
3.0 的模块名改了和之前的2.0 不一样
#!/usr/bin/env python
# -*- coding:utf-8 -*- 加 utf8
>>> user="123" 2.0打印
>>> print user
123
Print(user) 3.0打印
变量
Name=”hanwei” #姓名
name=”hanwei” #姓名
注释 # ‘’’ ‘’’
2.0
>>> a=raw_input("your name:")
your name:123
>>> print a
123
3.0
>>> a=input("your name:")
your name:123
>>> print a
123
格式化字符串
#!/usr/bin/env python
name=input("your name:")
age=int(input("your age:")) %d代表小数必须像这样
job=input("your job:")
%s 小数或字符串 %f 浮点数 %d 小数
msg='''
Name:%s
Age:%d
Job:%s
'''%(name,age,job)
print(msg)
ASCI码叫阿克斯码
Ascl 本身没有加入utf8 加入utf8才有中文
8个bi等于一个字节
1024个字节等于1kb
#####################
Ascl 查询 规律
Ab-----69----------ascl码
输入密码不显示密码
import getpass
username = input("username:")
password = getpass.getpass()
print(username,password)
判断次数输入密码
user = "hanwei"
passwd = "789"
username = input("username")
password = input("password")
if user == username:
print("username is corerct....")
if password == passwd:
print("Wlecome login....")
else:
print("password is invalid...
else:
print("no no no")
判断输入密码器
user = "hanwei"
passwd = "789"
username = input("username:")
password = input("password:")
if username == user and password == passwd:
print("Welcome login")
else:
print("mima huo yonghu mong cuowu")
猜数字
#!/usr/bin/env python
# -*- coding:utf-8 -*-
age = 22
guess_num = int(input("input your guess num:")) init 转为数字
if guess_num == age:
print("you git lt")
elif guess_num > age:
print("da le")
else:
print("xiaole")
猜数字2
age = 22
for i in range(10):
if i <3:
guess_num = int(input("input your guess num:"))
if guess_num == age:
print("you git lt")
break
elif guess_num > age:
print("da le")
else:
print("xiaole")
else:
print("cishi taiduo")
break
列表---其他叫做数组
定义数组
name=["zhangsan","lisi","wangwu","liu","xie","zhou"]
打印
name[1:3][1][3]
name[1:3]
name[-2:]
name[-1]
name[4]
name[:2]
修改
name[4] = "xietingfeng"
插入
name.insert(4,'hanwei')
删除
name.remove("liu")
打印所有
>>> name
['zhangsan', 'lisi', 'wangwu', 'hanwei', 'xietingfeng', 'zhou', 'alix']
if 9 in name:
num = name.count(9) 统计9的个数
pos = name.index(9) 找出索引
name[pos] = 999 改
print(name)
循环改改所有
name = ["zhangsan","lisi","wangwu","sunliu","liu0",1,1,3,5,4,8,98,9,10,11,9,9,9]
for i in range(name.count(9)):
ele = name.index(9)
name[ele] = 9999999999
print name
扩展
name = ["zhangsan","lisi","wangwu","sunliu","liu0"]
name2 = ["hanweiu","lini"]
name.extend(name2)
print(name)
打印嵌套
name = ["zhangsan","lisi",["li","a"],"wangwu","sunliu","liu0"]
print name [2][0]
反向排序
name = ["zhangsan","lisi",["li","a"],"wangwu","sunliu","liu0"]
name.reverse()
print name
排序
name = ["zhangsan","lisi",["li","a"],"wangwu","sunliu","liu0"]
name.sort()
print name
考被列表
name = ["zhangsan","lisi",["li","a"],"wangwu","sunliu","liu0"]
name2 = name.copy()
print(name2)
删除
name = ["zhangsan","lisi",["li","a"],"wangwu","sunliu","liu0"]
name.pop(2)
print name
不长 (比如 1.3.5排序)
name = ["zhangsan","lisi",["li","a"],"wangwu","sunliu","liu0"]
print (name[::2])
删除
name = ["zhangsan","lisi",["li","a"],"wangwu","sunliu","liu0"]
del name[1]
print (name[::2])
完全拷贝
name = ["zhangsan","lisi","wangwu","sunliu","liu0"]
name4 = copy.deepcopy(name)
print(name4)
猜数字2
age = 22
count = 0
for i in range(10):
if count<3:
print(count)
guess_num = int(input("input your guess num:"))
if guess_num == age:
print("you git lt")
break
elif guess_num > age:
print("da le")
else:
print("xia ole")
else:
con = input("hai yao ji xu ma:")
if con == 'y':
count = 0
continue
else:
print("exit")
break
count += 1
字符串处理
name = "alex,jack,rain"
name2 = name.split(",") 转换成列表
print(name2)
print("|".join(name2)) 换成 |
删掉多余前后空格
username=input("user:")
if username.strip() == 'alex':
print("welcome")'''
name = "alex li"
print(name[2:4]) 打印指定字符
print(name.center(40,'-')) 打印40不满40 前后加—
print(name.find("li")) 查找
print('' in name) 查找空格
print(name.capitalize()) 第一个字母换成大写
变量
msg="Hello, {name}, it's been along {age} since last tiem...."
msg2=msg.format(name='mimng hu',age='33')
print(msg2)
变量
msg2="hahah {0}, dddd {1}"
print(msg2.format('alex','aa'))
避免输入的不是数字报错
age = input("your age:")
'''if age. isdigit(): 避免
age = int(age)
else:
print("nono")'''
name='alex3sdf'
print(name.isalnum()) 判断是否是字母或者数字
print(name.endswith('f')) 判断结尾是否是f
print(name.startswith('a')) 判断开头是否是a
print(name.upper().lower()) 大小写转换
While 死循环
count=0
while True:
count+=1
if count>50 and count<60: 50-60之间退出循环
continue
print("你是风儿我是沙")
print(count)
if count == 100:
print("穿上裤子走天涯")
print(count)
break
字典
id_db = {
412728199004125331:{
'name':"hanwei",
'age': 18,
'addr':'henan'
},
41272819900512331: {
'name':"shanpao",
'age':24,
'addr':'shandong'
},
}
#print(id_db[412728199004125331]) 打印k值
#id_db[41272819900512331]['name']="minghu" 修改
#del id_db[41272819900512331] 删除
#id_db[41272819900512331].pop("addr") 删除
#v=id_db.get(41272819900512331) 判断是否存在
#v=id_db[41272819900512331] 负值k值
#print(id_db)
dic2={
'name':'hanhanhan',
222012000:{'name':'wang'},
}
#print(dic2)
#id_db.update(dic2) 更新如果只存在覆盖不存在追加
#print(id_db)
#print(id_db) # 打印字典
#print(id_db.items()) 转换成元祖
#print(id_db.values()) 打印values
#print(id_db.keys()) 打印keys
#41272819900512331 in id_db 判断一个值是否存在
#print(id_db.setdefault(41272819900412533,"hhh")) 判断一个不存在就追加
循环字典
for key in id_db:
print(key,id_db[key])
购物车
salary=input("input your salery:") if salary.isdigit(): salary=int(salary) else: exit("shu ru cuo wu") welcom_msg=,'-') print(welcom_msg) exit_flag = False product_list=[ (), (), (), ('xiaomi 2',19.9), (), (), (), (), ] shop_car=[] while exit_flag is not True: print(,'-')) for item in enumerate(product_list): index=item[] p_name=item[][] p_price=item[][] print(index,'.',p_name,p_price) user_choice = input("[q=quit,c=check],what do your want to buy:") if user_choice.isdigit(): user_choice=int(user_choice) if user_choice <len(product_list): p_item=product_list[user_choice] ] <=salary: shop_car.append(p_item) salary -= p_item[] print("added[%s]into shop car,you current balance is \033[31:1m[%s]" %( p_item,salary)) else: print("your balance is [%s], nono.." % salary) else: if user_choice == 'q' or user_choice == 'quit': print(,'*')) for item in shop_car: print(item) print(,'*')) print("nide yu ge shi [%s]" % salary) print("Bye") exit_flag = True elif user_choice == 'c' or user_choice == 'check': print(, '*')) for item in shop_car: print(item) print(, '*')) print("nide yu ge shi [%s]" % salary) print("Bye")
Set
s1={11,22,33}
s2={22,33,44}
#s3=s1.difference(s2) a中存在b中不存在
#s3=s1.symmetric_difference(s2) a中不存在和b中不存在
#s1.difference_update(s2) a中存在b中不存在跟新到a中
#s3=s1.intersection(s2) 找出a和b相同的 叫做交集
#s1.symmetric_difference_update(s2) a和b中不存在德跟新到a中
#s1.discard(11) 删除不存在不报错
#s1.remove(22) 删除不存在报错
#s1.pop() 随机删除
#s1.intersection_update(s2) 把a和b相同的更新到a中
#s3=s1.union(s2) 并集
#{33, 22, 11, 44}
添加
#s1.add(99)
#li=[88,66,99]
#s1.update(li)
练习题
old_dict={
"#1":8,
"#2":4,
"#4":2,
}
new_dict={
"#1":4,
"#2":4,
"#3":2,
}
拿到keys
new_set=set(new_dict.keys())
old_set=set(old_dict.keys())
remove_set=old_set.difference(new_set) 需要删除的
add_set=new_set.difference(old_set) 需要添加的
update_set=old_set.intersection(new_set) 需要更新的
#!/usr/bin/env python http://www.cnblogs.com/spykids/p/5163108.html shopping_list = [ [], [], [], [], [], [],] salary = total = shop_list = [] while True: welcom_1 = "XXX购物商城欢迎你" we_1 = welcom_1.center(,'*') print(we_1) choice_1 = "1.注册 2.登录 q。退出" ch_1 = choice_1.center(,'*') exit_1 = "谢谢使用,欢迎下次光临" ex_1 = exit_1.center(,'*') error_1 = "你输入的用户已存在,请重新输入" e_1 = error_1.center(,'*') error_2 = "密码不能为空,请重新输入" e_2 = error_2.center(,'*') error_3 = "输入的密码太短,请重新输入" e_3 = error_3.center(,"*") error_4 = "你输入有误,请重新输入" e_4 = error_4.center(,'*') error_5 = "你的账号已锁定,请联系管理员" e_5 = error_5.center(,'*') print(ch_1) sr_1 = input("Please input:") ': while True: with open('ming.txt','r')as r_1: temp = r_1.readlines() tlist = [] for tline in temp: tline = tline.strip().split(':') tlist.append(tline[]) useradd = input("Please create user:") s_1 = '成功创建用户:%s' %(useradd) if useradd in tlist: print(e_1) elif useradd == 'exit': break else: passwd = input('Please create a password:') lenth = len(passwd) : print(e_2) elif lenth > : with open('ming.txt','a')as r_3: w_1 = '%s:%s:0\n ' %(useradd,passwd) r_3.write(w_1) s_1 = s_1.center(,'*') print(s_1) break else: print(e_3) elif sr_1 == ': flag = False while True: username = input("Please enter a user name:") l = open('lock.txt','r') for lline in l.readlines(): lline = lline.strip() if username == lline: print("账号已经锁定") flag = True l.close() break if flag == True: break u = open('ming.txt','r') for uline in u.readlines(): user,password,mony = uline.strip().split(':') if username == user: i = : passwd = input('Please enter a password:') i += if passwd == password: print('你好欢迎登陆购物平台') flag = True u.close() break else: : with open('lock.txt','a') as l_2: l_2.write(username + '\n') l_2.close() exit("错误过多,账号已锁定,请联系管理员") print( - i)) break else: print("用户输入错误,请重新输入") break while True: print("1.购物 2.查看购物车 3.查看余额 4.充值 b.返回 q.退出") print("--------------------------------------------------") choice_2 = input('请选择') flag_1 = False while True: ": while True: for index,g in enumerate(shopping_list): print(index,g[],g[]) print('--------------') print('c.查看购物车 b.返回 q.退出') choice = input('请选择商品:').strip() if choice.isdigit(): choice = int(choice) p_price = shopping_list[choice][] if p_price < salary: shop_list.append(shopping_list[choice]) total += p_price salary -= p_price print('------------------') print(],salary)) print('------------------') else: print('--------------------') print('余额不足,请充值') print('--------------------') elif choice == "c": while True: print('-------你已进入购物车-------') for k,v in enumerate(shop_list): print(k,v[],v[]) print("已消费金额为:%s"%total) print("你的余额为:%s"%salary) print('--------------------------') print("d.删除商品,b.返回 q.退出") print('--------------------------') choce_1 = input('请选择') print('--------------------------') if choce_1 == 'd': print('-------------------------') print('选择要删除的商品 b.返回购物车:') print('-------------------------') while True: choice_2 = input('请选择:') if choice_2.isdigit(): choice_2 = int(choice_2) d_price = shop_list[choice_2][] shop_list.remove(shop_list[choice_2]) total -= d_price salary += d_price print('----------------') print(],salary)) print('-----------------') elif choice_2 == 'b': break elif choice_1 == 'b': flag = True break else: print('-----购物清单------') for k,v in enumerate(shop_list): print(k,v[],v[]) print('总消费金额为:%s'%total) print('你的可用余额:%s'%salary) print('-----欢迎下次再来-----') exit() elif choice == "b": break elif choice == 'q': print('------购物清单------') for k,v in enumerate(shop_list): print(k,v[],v[]) print('消费金额为:%s'%total) print('你的可用余额为:%s'%salary) print('------欢迎再次光临------') exit() else: print('----------------') print('你输入错误,请重新输入!') print('-------------------') if flag == True: break elif choice_2 == ': print('-----购物车-------') for k,v in enumerate(shop_list): print(k,v[],v[]) print('已消费金额为:%s'%total) print('你的余额为:%s'%salary) print('--------------------') break elif choice_2 == ': with open('ming.txt','r')as m_1: mony_1 = m_1.readlines() for mline in mony_1: (user,password,mony) = mline.strip().split(':') print(salary) flag_1 = True break break elif choice_2 == ': z = : chongzhi = int(input('输入金额:')) passwd_1 = input('输入密码:') m = open('ming.txt','r+') m_2 = m.readlines() for mline in m_2: (user,password,mony) = mline.strip().split(':') if passwd_1 == password: mony_2 = (chongzhi + int(mony)) w_2 = '%s:%s:%s'%(username,password,mony_2) m.write(w_2) print('充值成功') print(mony) flag = True break continue break if flag == True: break elif choice_2 == 'b': flag = True break elif choice_2 == 'q': exit(ex_1) else: print(e_4) break break if flag == True: break break elif sr_1 == 'q': exit(ex_1) else: print(e_4) print('--------------------------------')
三级菜单
http://www.2cto.com/kf/201512/453371.html
import re memu = { '东北':{ '吉林省':{ '吉林市':['吉林市1','吉林市2'], '长春':['长春1','长春2'], }, '辽宁省':{ '沈阳':['沈阳1','沈阳2'], '大连':['大连1','大连2'], }, }, '华北':{ '河北':{ '廊坊':['廊坊1','廊坊2'], '保定':['保定1','保定2'], }, '内蒙古':{ '呼和浩特':['呼和浩特1','呼和浩特2'], '包头':['包头1','包头2'] }, }, } flag = True while flag: for i,v in enumerate(memu.keys()): print(i,v) num_1 = input('请输入一级菜单号 q.退出:') if num_1 == 'q': flag = True break if num_1.isdigit(): num_1 = int(num_1) if num_1 <= len(memu): key_1 = list(memu.keys())[num_1] while flag: for i1,v1 in enumerate(memu[key_1]): print(i1,v1) num_2 = input('请输入二级菜单号 q.退出 b.返回:') if num_2 == 'q': flag = False break if num_2 == 'b': break if num_2.isdigit(): num_2 = int(num_2) if num_2 <= len(memu[key_1]): key_2 = list(memu[key_1].keys())[num_2] while flag: for i2,v2 in enumerate(memu[key_1][key_2]): print(i2,v2) num_3 = input('请输入三级菜单号 q.退出 b.返回:') if num_3 == 'q': flas = False break if num_3 == 'b': break if num_3.isdigit(): num_3 = int(num_3) if num_3 <= len(memu[key_1][key_2]): key_3 = list(memu[key_1][key_2].keys())[num_3] while flag: print('最后一页!') for i3,v3 in enumerate(memu[key_1][key_2][key_3]): print(i3,v3) num_4 = input('q.退出 b.返回:') if num_4 == 'q': flag = False break if num_4 == 'b': break
购物车
http://www.cnblogs.com/spykids/p/5163108.html
python 笔记的更多相关文章
- Python笔记之不可不练
如果您已经有了一定的Python编程基础,那么本文就是为您的编程能力锦上添花,如果您刚刚开始对Python有一点点兴趣,不怕,Python的重点基础知识已经总结在博文<Python笔记之不可不知 ...
- boost.python笔记
boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...
- 20.Python笔记之SqlAlchemy使用
Date:2016-03-27 Title:20.Python笔记之SqlAlchemy使用 Tags:python Category:Python 作者:刘耀 博客:www.liuyao.me 一. ...
- Python笔记——类定义
Python笔记——类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...
- 13.python笔记之pyyaml模块
Date:2016-03-25 Title:13.Python笔记之Pyymal模块使用 Tags:Python Category:Python 博客地址:www.liuyao.me 作者:刘耀 YA ...
- 8.python笔记之面向对象基础
title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...
- python笔记 - day8
python笔记 - day8 参考: http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/wupeiqi/art ...
- python笔记 - day7-1 之面向对象编程
python笔记 - day7-1 之面向对象编程 什么时候用面向对象: 多个函数的参数相同: 当某一些函数具有相同参数时,可以使用面向对象的方式,将参数值一次性的封装到对象,以后去对象中取值即可: ...
- python笔记 - day7
python笔记 - day7 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 面向对象,初级篇: http://www.cnblog ...
- python笔记 - day6
python笔记 - day6 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 大纲: 利用递归,实现阶乘: Python反射 pyt ...
随机推荐
- XML 中对当前时间的引用
current_date 而不是 date 然而,在attrs 和 readonly中并不适用.
- 在cenOS下安装apache出现-bash: /etc/init.d/httpd: 没有那个文件或目录
我是在vmware上装的centos7,使用命令yum install httpd httpd-devel 安装完apache后,想要启动apache,执行了/etc/init.d/httpd sta ...
- thinkphp框架验证码验证一次
做异步验证验证码,只要验证一次结果正确,拿相同的值再次来对比,返回结果就不正确.我看到论坛中有人说,tp框架只要验证过一次正确后验证码就销毁了.确实是这个效果,但具体的还没深入了解
- Java面试题大全(一)
JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...
- 一个链接直接打开APP
http://www.cnblogs.com/jzm17173/p/4569574.html 这是IOS http://www.jianshu.com/p/af211f2a990e
- JavaScript入门篇 第三天(认识DOM)
认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面代码 ...
- php5-fpm.sock failed (13: Permission denied) error
In order to fix the php5-fpm.sock failed error follow these instructions 1) Make sure your virtual h ...
- Splay树-Codevs 1296 营业额统计
Codevs 1296 营业额统计 题目描述 Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司 ...
- SourceInsight
进入到Temp Project窗口分别可以以文件列表的方式,列出所有的文件,每个窗体下边有一排按钮,左边的窗口(secondView.cpp)从左至右分别为:按字母顺序排列所有标记.按照文件中行数顺序 ...
- SynchronousQueue 的简单应用
SynchronousQueue是这样一种阻塞队列,其中每个 put 必须等待一个 take,反之亦然.同步队列没有任何内部容量,甚至连一个队列的容量都没有. 不能在同步队列上进行 peek ...