2021-01-16

PythonDay05

本节内容

  • 元组
  • 购物车
  • 字典
  • 字符串

1.元组

元组:称为只读列表,即数据可以被查询,但不能被修改,所以列表的切片操作同样适用于元组

元组写在小括号()里,元素之间用逗号隔开

tup=(1,2,3,4,)   #需要在后面加逗号

print(tup[1:4])

2.购物车

shop_car=[]
product_list = [('Mac',9000),('kindle',800),('tesla',900000),('python book',60),('coffee',30)]
salary = input("your salary:")
if salary.isdigit():#判断是否为数字
    salary=int(salary)
    while True:
        #打印商品内容
        for i,v in enumerate(product_list,1):
            print(i,'、',v)
        #引导用户选择商品
        choice = input("输入购买商品的序号[退出:q]>>>:")
        #验证输入是否合法
        if choice.isdigit():
            choice=int(choice)
            if 0<choice and choice<=len(product_list):
                #取出用户选择的商品
                p_item=product_list[choice-1]
                #判断本金和商品价格的大小,判断成功加入购物车,并计算余额
                if p_item[1]<salary:
                    salary-=p_item[1]
                    shop_car.append(p_item)
                else:
                    print("余额不足,还剩%s" %salary)
                print(p_item)
            else:
                print("编号不存在")
        elif choice=='q':
            print("退出")
            print("--------您已经购买如下商品--------")
            #循环遍历购物车里已买的商品
            for i in shop_car:
                print(i)
            print("您还剩%s元" %salary)
            break
        else:
            print("非法字符")

 3.字典

字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据。字典是无序存储的,且key必须是可哈希的。可哈希表示key必须是不可变类型,如数字、字符串、元组

dic1={'name':'goto','age':24,'sex':'male','hobby':'girl','isHandsome':'True'}
print(dic1)
print(dic1['name'])

 字典特点:无序、键唯一

字典的增删改查

#增加
dic3={'name':'goto'}
dic3['age']=18
print(dic3)
#setdefault若字典中该键已有,则不改变,返回字典中相应键对应值
#键不存在,在字典中增加新的键值对,并返回相应的值
dic3.setdefault('hobby','girl')
print(dic3)
ret=dic3.setdefault('age',34)
print(ret)
dic4={'name':'goto','age':18}
#查 通过键去查找
print(dic4['name'])
#分别查键、值、键值
print(dic4.keys())  #print(list(dic4.keys()))
print(dic4.values())
print(dic4.items())
#改
dic4={'name':'goto','age':18}
dic4['age']=20
print(dic4)
dic5={'1':'111','2':'222','age':22}
dic4.update(dic5)
print(dic4)
#删
del dic4['name'] #删除指定的键值对
print(dic4)
dic4.clear() #清空字典
print(dic4)
ret=dic4.pop('age') #删除指定的键值对,并返回该键值对的值
print(ret)
a=dic4.popitem()  #随机删除某组键值对,并以元组方式返回值
print(a,dic4)
del dic4 #删除整个字典
#嵌套
catalog={
    '安徽':{
        "合肥":["科级之城","中科大"],
        "芜湖":["长江之畔","空气好"],
        "宣城":["徽商","文化底藴"]
    },
    '北京':{
        "西城区":["大","教育好"]
    },
    '上海':{
        "东方明珠":["魔都","梦幻"]
    }
}
print(catalog)
#遇到字典用键,遇到列表用位置
catalog["欧美"]["www.youporn.com"][1]="高清"
print(catalog)
#排序
dic={5:'555',2:'222',4:'444'}
print(sorted(dic))
print(sorted(dic.values()))
#字典的遍历
dic={5:'555',2:'222',4:'444'}
for i in dic:
    print(i,dic[i])
for i,v in dic.items():
    print(i,v)

#信息的存储
dic1={'zhangsan':{'age':18,'sex':'male'},
      'lisi':{'age':20,'sex':'male'},
      'xishi':{'age':30,'sex':'female'}
      }
print(dic1)

4.字符串的操作

#字符串
a="let's go"
print(a)
#重复输出字符串
print("hello"*10)
#通过索引获取字符串中的字符
print("helloworld"[2:])
#关键字 in
print('el' in 'hello')
#格式化输出
print("%s is a boy" %'goto')
#字符串拼接
a='123'
b='abc'
c=a+b
print(c)
#空字符串join拼接
d=''.join([a,b])
print(d)
#内置方法
str="hello world"
print(str.count('l'))    #统计元素个数
print(str.capitalize())  #字符串首字母大写
print(str.center(50,'-'))  #居中
print(str.endswith('ld')) #判断以某个内容结尾
print(str.startswith('he')) #判断以某个内容开头
str1="he\tllo world"
print(str1.expandtabs(tabsize=10))
print(str.find('o'))   #查找第一个元素返回索引值
str2='hello world {name} is {sex}'
print(str2.format(name='goto',sex='boy')) #格式化输出
print(str2.format_map({'name':'goto','sex':'boy'}))
print(str.index('t'))
print('abc123'.isalnum())  #判断是否是字母数字
print('AF00'.isdecimal())
print('12345'.isdigit())  #判断是否是数字
print('12ab'.isidentifier()) #判断是否是非法变量名
print('abx'.islower()) #是否全小写
print('ABC'.isupper()) #是否全大写
print('   e'.isspace())  #是否全空格
print('My Title'.istitle()) #每个单词首字母大写
print('My Title'.lower())  #大写变小写
print('My Title'.upper())  #小写变大写
print('My Title'.swapcase())  #大写变小写,小写变大写
print('     My Title\n'.strip())  #去掉换行和空格
print('My Title'.replace('Title','home'))
print('My Title title'.rfind('t'))
print('My Title title'.split('i')) #以i做分隔符把字符串分割
print('My Title title'.title())

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值