基础很sb但必须熟练掌握:
第一天就是Python的简单介绍与简单的接触。基本都是概念性的东西,需要知道一个非常便捷的IDE工具:pycharm,以后的编程就靠它了。还有就是%s和%d的格式化输出、运算符、及流程控制:if...else...;while...else...;for i in seq...else...
第二天的主要讲的是数据类型:数字,字符串,列表,元组,字典,集合及字符编码的详细介绍。
数字:转换为数字: int(10);转换为浮点数:float(10.3) ;是否全部是数字:msg.isdigit()
字符串:切片:msg[1:7];长度:len(msg);成员运算(in or not in):print('a' in msg);查看索引: print(msg.index(18));移除空白:msg.strip();切分split:msg.split('|');以...开头/结尾(startswith/endswith):print(msg.startswith('alex'));替换replace:print(msg.replace('a','B'));格式化输出%和format: print('my name is %s my age is %s' %('egon',18)) print('my name is {} my age is {}'.format('egon',18));合成join:print(':'.join(l));是否全部由字母组成:print(msg.isalpha())
列表:列表可变:msg[0]='abc';切片:msg[1:4];长度:len(msg);成员运算(in or not in):print('a' in msg);追加:msg.append('abc');删除:del msg[1] msg.remove('abc') msg.pop(1);插入:msg.insert(index,'abc');扩展添加:msg.extend([1,2,3,4]);清空列表:msg.clear();复制:msg.copy();反转:msg.reverse()
元组:切片:msg[1:7];长度:len(msg);成员运算(in or not in):print('a' in msg);查看索引: print(msg.index(18));统计元素个数: print(ages.count('abc'))
字典:增加: info['hobbies']=['read','music','play','sleep','eat'] ;长度:len(msg);key运算(in or not in):print('a' in msg);删除:print(info.pop('name')); 键keys(): print(info.keys());值values(): print(info.values());键值对items():print(info.items());获取值:print(info.get('name123',123));删除键值对: print(info.popitem());没有则新增,有则返原值:print(info.setdefault('hobbies',['read','music']));更新字典(字典):info.update(info_new);
集合:长度:len(msg);成员运算(in or not in):print('a' in msg);并集:|;交集:&;差集:-;对称差集:^;父集,子集:>,>= ,<,<=
字符编码: unicode =====encode ======>gbk # 存文件,unicode可以转化成任意编码 gbk====decode====>unicode
1 #打印三级菜单,打印下级时,记录到列表中;打印上级时,用pop取出,然后再打印最后字典的key 2 menu = { 3 '北京':{ 4 '海淀':{'五道口':{'soho':{},'网易':{},'google':{}}, 5 '中关村':{'爱奇艺':{},'汽车之家':{},'youku':{}}, 6 '上地':{'百度':{},}, 7 }, 8 '昌平':{'沙河':{'老男孩':{},'北航':{},}, 9 '天通苑':{},'回龙观':{}, 10 }, 11 '朝阳':{}, 12 '东城':{}, 13 }, 14 '上海':{ 15 '闵行':{"人民广场":{'炸鸡店':{}} 16 }, 17 '闸北':{'火车战':{'携程':{}} 18 }, 19 '浦东':{ 20 }, 21 }, 22 '山东':{ 23 }, 24 } 25 26 exit_flag = False 27 current_layer = menu 28 29 layers = [menu] 30 31 while not exit_flag: 32 for k in current_layer: 33 print(k) 34 choice = input(">>:").strip() 35 if choice == "b": 36 current_layer = layers[-1] 37 #print("change to laster", current_layer) 38 layers.pop() 39 elif choice not in current_layer:continue 40 else: 41 layers.append(current_layer) 42 current_layer = current_layer[choice]
#打印购物车程序 以列表的索引为主
1 #!/usr/bin/env python2 2 import sys 3 salary = int(raw_input('please input you salary:')) 4 products = [ 5 ['Iphone',5800], 6 ['MacPro',12000], 7 ['NB Shoes',680], 8 ['Cigarate',48], 9 ['MX',2500] 10 ] 11 shopping_list = [] 12 13 while True: 14 for p in products: 15 print products.index(p),p[0],p[1] 16 choice = raw_input("