python-1.列表、元组操作 2.字符串操作 3.字典操作 4.集合操作 5. 文件操作 6.字符编码与转码 7.内置函数

#1列表和元组
#(1列表)#存储很多信息,上面字符串的形式取不出来一个单独的名字,可以使用列表的形式取出来名字
'''names = ['zhangsan','lisi','wangmazi']
print(names[0],names[2])
print(names[1:3])#切片
print(names[-1])#取列表中最后一个值
print(names[-2:])#取列表中最后两个值'''

'''names = ['zhangsan','lisi','wangmazi']
#
想在名字列表里继续放值 append的意思是追加
names.append('shasha')
print(names[1:3])'''



'''#名字随意插入列表里 把哈哈插入lisi名字前面
names = ['zhangsan','lisi','wangmazi']
names.insert(1,'haha')#增加语句
print(names)'''

'''#列表切换名字  把Lisi的名字换成haha
names = ['zhangsan','lisi','wangmazi']
names[1] = 'haha'#替换语句
print(names)'''

#列表中删除名字 删除lisi
'''names = ['zhangsan','lisi','wangmazi']
#names.remove('lisi')#
第一种删除语句
del names[1]#第二种删除语句
names.pop()#删除列表中最后一个人的名字
print(names)'''

#查找一个人名字的位置
'''names = ['zhangsan','lisi','wangmazi']
print(names.index('wangmazi'))#
查找语句
print(    names[names.index('wangmazi')]   )#打印出该名字'''

#查找列表中有几个一样的名字
'''names = ['zhangsan','lisi','wangmazi','wangmazi']
print(names.count('wangmazi'))#
查找语句

#反转列表中的名字
names = ['zhangsan','lisi','wangmazi']
names.reverse()#反转语句
print(names)

#列表排序语句
names = ['zhangsan','lisi','wangmazi']
names.sort()#列表排序语句
print(names)

#合并语句
names = ['zhangsan','lisi','wangmazi']
names2 = [1,2,3,4]
names.extend(names2)
print(names,names2)'''

#复制语句
'''names = ['zhangsan','lisi','wangmazi']
name2 = names.copy()
names[2] = '
王麻子'
print(names)
print(name2)'''

#深copy
'''import copy
names = ['zhangsan','lisi','wangmazi']
names2 = copy.deepcopy(names)#
浅copy的话是把deep单词去掉
names[2] = '王麻子'
print(names)
print(names2)

#列表循环语句
names = ['zhangsan','lisi','wangmazi']
for i in names :
    print(i)'''

#步长切片
'''names = ['zhangsan','lisi','wangmazi','hahah','xixi','yaya']
print (names[0:-1:2])
print (names[::2])#
翻转 0和-1可以省略掉 该语句和上面的语句表达效果是一样的'''


#(2元组tuple)
#元组和列表都是存一组数 创建不可修改 所以叫只读列表 只有两个方法一个是count 一个是index
name = ('shasha','haha')
#程序练习
'''程序:购物车程序
需求:1启动程序后,让用户输入工资,然后打印商品列表
2允许用户根据商品编号购买商品
3用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4可随时退出,退出时,打印已购买商品和余额'''
'''product_list = [
    ('iphone', 5800),
    ('bike',800),
    ('watch',10600),
    ('coffee',31),
    ('alex python',120),
]
shopping_list = []#购物车
salary = input ('input your salary:')#输入工资
if salary.isdigit():#判断工资是否为数字
    salary = int(salary)#如果是数字int一下 进入循环
    while True:
        for index,item in enumerate(product_list):
            print(index,item)#enumerate是索引序列 取出来商品下标 打印商品列表 index,item是项目索引
#找下标的另一语法for item in product_list:
        #print(product_list.index(item),item)
        user_choice = input('你选择要买什么>>>:')#用户选择买的商品
        if user_choice.isdigit():#判断用户输入的商品必须是数字类型
            user_choice = int(user_choice)
            if user_choice < len(product_list) and user_choice >=0:#用户输入商品后int一下判断商品是否在序号内
                p_item = product_list[user_choice]#通过下标把商品取出来
                if p_item[1] <= salary: #买的起
                    shopping_list.append(p_item)#买的起就添加到商品列表里
                    salary -= p_item[1]#扣钱
                    print('Added %s into shopping cart,你的余额还剩 
33[31;m%s33[0m'%(p_item,salary))#告知卡里剩余钱数
                else:
                    print('
33[31;1m你的余额还剩 [%s]33[0m'% salary) #买不起
            else:
                print('选择商品[%s] 不存在'%user_choice)#输入的商品序号不存在
        elif user_choice == 'q':#退出 打印购买商品列表及剩余钱数
             print('---------shopping list---------')
             for p in shopping_list:
                 print(p)
                 print('你的工资卡余额:',salary)
                 exit()
        else:
            print('invalid option')#无效选项


#2字符串常用操作
name = 'alex'
print(name.capitalize())#capitalize首字母大写的意思
name = 'my name is alex'
print(name.count('a'))#count是统计name字符串中有几个字母a
name = 'my name is alex'
print(name.center(50,'-'))#在name字符串中一共打印50个字符 如果不够用-填补
name = 'my name is alex'
print(name.endwith('ex'))#判断字符串以什么结尾
name = 'my
name is alex'
print(name.expandtabs(tabsize=30))#
able是空格键
name = 'my name is alex'
print(name.find('name'))#找到字符串的索引
name = 'my name is alex'
print(name[name.find('name'):])#字符串切片
name = 'my name is {name} and iam  {year} old'
print (name.format(name='alex',year=23))#format格式化
name = 'my name is {name} and iam  {year} old'
print (name.format_map(  {'name':'alex','year':23}))#字典格式化
print('ab23'.isalnum())#验证英文和阿拉伯
print('abA'.isalpha())#验证英文字母
print('1'.isdecimal())#验证数字
print('1'.isdigit())#判断是否是整数
print('1a'.isidentifier())#判断是一个合法的变量名
print('a'.islower())#判断是不是小写
print('22'.isnumeric())#判断是不是只有数字在里面
print(' '.isspace())#判断是否是一个空格
print('My Name Is'.istitle())#判断是否是个标题
print('My Name Is'.upper())#判断是否是大写
print('My Name Is'.join(['1','2','3']))#join把列表换成字符串
print('My Name Is'.ljust(50,'*'))#字符串长度为50 不够*补齐后面补
print('My Name Is'.rjust(50,'*'))#前面补齐
print('Alex'.lower())#字符串变小写
print('Alex'.upper())#字符串变大写
print('Alex
'.lstrip())#去左边的空格 回车|n是空格
print('Alex'.strip())#两边空格都去
p = str.maketrans('abcd','1234')
print('alex li'.translate(p))#翻译一一对应
print('Alex'.replace('l','L',1))#替换
print('Alex li'.rfind('l'))#找到最右边值的下标
print('Alex'.split())#字符串按照空格分成列表
print('Alex'.swapcase())#大小写转换'''

#3字典操作 字典key-value类型
'''info = {
    'stu1101':'tenglan wu',
    'stu1102':'longze luola',
    'stu1103':'xiaoze maliya',
}
#print(info['stu1101'])#
查找
print(info.get('stu1103'))#安全查找不报错
print('stu1104'in info)#判断是否在字典里
info['stu1101'] = '武藤兰'#存在就修改
info['stu1104'] ='cangjingkong'#不存在就创建
del info['stu1101']#删除
info.pop('stu1101')#标准删除
info.popitem()#随意删
print(info)'''
#多级字典列表嵌套
#update 更新赋值 创建
#items 字典变成列表

#字典循环
'''info = {
    'stu1101':'tenglan wu',
    'stu1102':'longze luola',
    'stu1103':'xiaoze maliya',
}
for i in info:
    print(i,info[i])'''
date = {
    '北京':{
       '昌平':{
           '沙河':['oldboy','test'],
           '天通苑':['链家房产','我爱我家']
       },
       '朝阳':{
            '望京':['奔驰','陌陌'],
            '国贸':{'CICC','HP'},
            '东直门':{'Advent','飞信'},
       },
       '海淀':{
       },

    },
}
exit_flag = False#标志位
while not exit_flag:
    for i in date:
        print(i)#打印第一层
   
choice = input('选择进入1:')
    if choice in date:#判断是否在date里面
     
while not exit_flag:
          for i2 in date[choice]:
              print(' ',i2)#打印第二层
         
choice2 = input('你选择进入2:')
          if choice2 in date[choice]:#根据上一层选择继续进行选择
             
while not exit_flag:
                  for i3 in date[choice][choice2]:#进行第三层
                     
print(' ', i3)
                  choice3 = input('你选择进入3:')
                  if choice3 in date[choice][choice2]:#第四层
                     
for i4 in date[choice][choice2][choice3]:
                          print(' ',i4)
                      choice4 = input('最后一层,按B返回:')
                      if choice4 == 'b':
                        pass
                      elif
choice4 =='q':#判断是否要退出
                         
exit_flag = True#如果退出把exit_flag设置成true
             
if choice3 == 'b':
                  break
              elif
choice3 =='q':
                  exit_flag = True
#需求
#购物车:用户入口1商品信息存在文件里2已购商品,余额记录
# 商家入口2可以添加商品修改商品价格3

原文地址:https://www.cnblogs.com/iamshasha/p/10864853.html