基本数据类型
数字 字符串 列表 元组 字典 格式化输出
a.数字
数字主要是用于计算用的,使用方法并不是很多
a = 1 b = 2 print(a+b) >>>3
b.字符串
msg = ''' 床前明月光 ''' print(msg) >>>输出 床前明月光
b1.字符串相加
s1 = 'hello' s2 = 'world' print(s1 + s2) >>>输出 helloworld
b2.字符串相乘
字符串相乘是字符串乘数字 print('呵呵'*3) >>>输出 呵呵呵呵呵呵
b3.索引与切片
s = 'alexwusirritian' s1 = s[0] print(s1,type(s1)) s2 = s[2] print(s2) s3 = s[-1] print(s3) s4 = s[-2] print(s4) ---------------------------- 切片: #切片 #切片 顾头不顾腚 s5 = s[0:4] s5 = s[:4] print(s5) s6 = s[4:9] print(s6) s7 = s[:] print(s7) s8 = s[:5:2] print(s8) s9 = s[-1:-5:-1] print(s9) #切片:s[起始索引:结束索引+1:步长]
c.列表
# 按照索引取值,与元素本身的数据类型一致 # 按照切片取值,取出来的是小列表。 li = [1, (2, 3), 2, 3, 4, 5, 6,] l1 = li[:3] print(l1) print(li[0],type(li[0])) print(li[1],type(li[1])) l2 = li[:] print(l2)
c1.增
c1.1追加
l = ['WuSir', 'alex', 'OldBoy', 'barry'] l.append('oldgirl') l.append(1) print(l) >>>输出 ['WuSir', 'alex', 'OldBoy', 'barry''oldgirl']
c1.2根据索引插入
l = ['WuSir', 'alex', 'OldBoy', 'barry'] l.insert(1, '葫芦') print(l) >>>输出 ['WuSir','葫芦', 'alex', 'OldBoy', 'barry']
c1.3 extend
l = ['WuSir', 'alex', 'OldBoy', 'barry'] l.extend('abc') l.extend([1, '2122', 100]) print(l) >>>输出 ['WuSir', 'alex', 'OldBoy', 'barry', 'a', 'b', 'c', 1, '2122', 100]
c2.删
c2.1按照索引删除
l = ['WuSir', 'alex', 'OldBoy', 'barry'] ret = l.pop(1) print(l) >>>输出 ['WuSir', 'OldBoy', 'barry']
c2.2按照元素删除
l = ['WuSir', 'alex', 'OldBoy', 'barry'] l.remove('alex') print(l) >>>输出 ['WuSir', 'OldBoy', 'barry']
c2.3清空列表
l = ['WuSir', 'alex', 'OldBoy', 'barry'] l.clear() print(l) del l print(l) del l[1] print(l) del l[:3 print(l)
c3.改
c3.1 按照索引改
l = ['WuSir', 'alex', 'OldBoy', 'barry'] l[0] = '日天' print(l) >>>输出 ['日天', 'alex', 'OldBoy', 'barry'] l[2] = '正恩' print(l) >>>输出 ['WuSir', 'alex', '正恩', 'barry']
c3.3 按照切片去改
l[:3] = 'abcdgfdljgkfdgjlfdlgjfdsaklfdjskladsfj' l[:3] = 'a' l[:3] = [11, 22, 33, 44] #加步长 必须一一对应 l[:3:2] = 'ab' print(l)
c4.查
c4.1 for循环遍历
l = ['WuSir', 'alex', 'OldBoy', 'barry'] for i in l: print(i) >>>输出 WuSir alex OldBoy barry
c4.2 正序、倒序、翻转
l1 = [1, 9, 8, 4, 3, 2, 6,] l1.sort() # 正序排序 print(l1) l1.sort(reverse=True) # 倒叙排序 print(l1) l1.reverse() # 翻转 print(l1)
c4.3 列表的嵌套
l1 = [1, 2, 'alex', ['WuSir', 'taibai', 99], 6] # 1,将alex该成Alex l1[2] = l1[2].capitalize() # 2,将'WuSir'变成全部大写。 l1[3][0] = l1[3][0].upper() print(l1) # 3,99让其通过数字加1的方式变成 '100'. l1[3][-1] = str(l1[3][-1] + 1) print(l1)
d.元组
#元组 只读列表。 tu = (1, 2, 33, 'alex') print(tu[1]) >>>输出 2 print(tu[:-1]) >>>输出 (1, 2, 33) for i in tu: print(i) >>>输出 1 2 33 alex tu1 = (1, 2, [1, 3,], 4) tu1[2].append(666) print(tu1) >>>输出 (1, 2, [1, 3, 666], 4)
d.1 range
print(range(100)) for i in range(100): print(i) for i in range(1,11): print(i) for i in range(2, 101, 2): print(i) for i in range(100,0,-1): print(i) l1 = [1, 2, 3, 4, 5, 'alex'] for i in range(len(l1)): print(i)
e.字典的增删改查
e.1 增
dic = { 'name': '老男孩', 'age': 10000, 'sex': '男', } dic['hobby'] = 'old_girl' # 无责增加,有责修改。 dic['name'] = 'alex' print(dic) dic.setdefault('high') # 有key则不修改,无则添加。 dic.setdefault('high', 169) dic.setdefault('name', 'wusir') print(dic)
e.2 删
dic = { 'name': '老男孩', 'age': 10000, 'sex': '男', } print(dic.pop('name')) # 返回值 dic.pop('name1',None) print(dic) #popitem 随机删除 有返回值 print(dic.popitem()) print(dic) clear dic.clear() print(dic) del dic print(dic) del dic['name'] print(dic) ''' 1,删除整个字典 2,按照键去删除键值对 '''
e.3 改
dic['name'] = 'alex' # 无责增加,有责修改。 print(dic) #update 更新信息 dic = {"name": "jin", "age": 18,"sex": "male"} dic2 = {"name": "alex", "weight": 75} dic2.update(dic) # 将dic里面的键值对覆盖并更新到dic2中,dic不变 。 print(dic) # {'name': 'jin', 'age': 18, 'sex': 'male'} print(dic2) # {'name': 'jin', 'weight': 75, 'age': 18, 'sex': 'male'}
e.4 查
print(dic['name']) print(dic['name1']) print(dic.get('name')) print(dic.get('name1','没有此键...')) print(dic.keys()) # 类似于列表的一个容器,没有索引 for i in dic.keys(): print(i) for i in dic: print(i) print(dic.values()) print(dic.items()) for i in dic.items(): print(i)
e.5 字典的嵌套及数据类型的补充
dic = {'name_list':['高猛', '于其',], 1:{ 'alex': '李杰', 'high': '175', } } # 1、['高猛', '于其',] 追加一个元素'wusir', # 2, {'alex': '李杰','high': '175' } 增加一个键值对 'sex': man, dic['name_list'].append('wusir') print(dic) print(dic[1]) dic[1]['sex'] = 'man' print(dic) l1 = [11, 22, 33, 44, 55] # 将索引为奇数对应的元素删除。 del l1[1::2] print(l1) #再循环一个列表时,不要改变列表的大小,这样会影响结果。 for i in range(len(l1)): print(i) print(l1) if i % 2 == 1: del l1[i] print(i) print(l1) print(l1) for i in range(len(l1)-1,-1,-1): if i % 2 == 1: del l1[i] print(l1) # dictionary changed size during iteration # 再循环一个字典时,不要改变字典的大小,这样会影响结果。 dic = {'k1': 'v1', 'k2': 'v2', 'k3':'v3', 'name':'alex'} for k in dic: if 'k' in k: dic.pop(k) print(dic) s1 = 'abc' print('a' in s1) l1 = [] for i in dic: if 'k' in i: l1.append(i) print(l1) for i in l1: del dic[i] print(dic) dic = dict.fromkeys('abc',666) print(dic) dic = dict.fromkeys('abc',[]) dic['a'].append(666) print(dic)
f.格式化输出
# 格式化输出 % 占位符 s d name = input('请输入名字:') age = input('请输入年龄:') job = input('请输入职业:') hobbie = input('请输入爱好:') msg = '''------------ info of %s ----------- Name : %s Age : %d job : %s Hobbie: %s ------------- end ----------------- ''' % (name, name, int(age), job, hobbie) print(msg) >>>输出 请输入名字:bowen 请输入年龄:18 请输入职业:IT 请输入爱好:sleep ------------ info of bowen ----------- Name : bowen Age : 18 job : IT Hobbie: sleep ------------- end ----------------- print('我叫%s,今年%d,学习进度2%%' % ('老男孩', 10000)) #and or not # 1,前后都是比较运算 # 优先级:() > not > and > or print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) # 2, 前后都是数值 ''' 0 是False 非0 都是True x or y if x is True,return x ''' print(3 or 4) print(1 or 4) print(0 or 4) print(-1 or 4) print(3 and 5) print(3 or 4 and 5) choice = input('>>>').strip() if not choice: pass
练习:实现一个购物车
# -*- coding:utf8 -*- goods = [ ("苹果X", 5800), ("华为P20", 6000), ("小米6", 1000)] goods_cart = [] mybudget = input("来吧,秀一秀你的总资产:") while not mybudget.isdigit(): mybudget = input("输入有误,请重新输入您的资产:") mybudget = int(mybudget); print('选购商品完成后按q推出进行结算') while True: print("商品列表".center(50, "-")) print("编号".center(8, " "), "名称".ljust(30, " "), "价格".ljust(10, " ")) for i in enumerate(goods): print(str(i[0]).center(10, " "), str(i[1][0]).ljust(31, " "), str(i[1][1]).ljust(10, " ")) user_choose = input("请输入您的选择:") if user_choose.isdigit(): user_choose = int(user_choose) if user_choose >= 0 and user_choose < len(goods): if (mybudget - goods[user_choose][1]) >= 0: goods_cart.append(goods[user_choose]) mybudget -= goods[user_choose][1] print("预算还有%d" % mybudget) else: print("钱不够了") else: print("没有此商品") elif user_choose == 'q': break else: print("您的输入有误~") print("预算还剩%d" %(mybudget)) print("购物车商品信息".center(50, "-")) for i in enumerate(goods_cart): print(str(i[0]).center(10, " "), str(i[1][0]).ljust(31, " "), str(i[1][1]).ljust(10," ")) print('不要忘了付款哦~')