python学习日志--day2

1、序列大概有六种形式:列表,元祖,字符串,Unicode字符串,buffer对象,xrange对象

2、索引

从左往右,下标从0开始递增。

从右往左,下标从-1开始递减。(负数索引)

3、分片


扩展分片(第三个数为步长)



4、列表、字典

三级菜单小程序

data = {
    '北京':{
        "昌平":{
            "沙河":["oldboy","test"],
            "天通苑":["链家地产","我爱我家"]
        },
        "朝阳":{
            "望京":["奔驰","陌陌"],
            "国贸":{"CICC","HP"},
            "东直门":{"Advent","飞信"},
        },
        "海淀":{},
    },
    '山东':{
        "德州":{},
        "青岛":{},
        "济南":{}
    },
    '广东':{
        "东莞":{},
        "常熟":{},
        "佛山":{},
    },
}
exit_flag = False

while not exit_flag:
    for i in data:
        print(i)
    choice = input("选择进入1>>:")
    if choice in data:
        while not exit_flag:
            for i2 in data[choice]:
                print("\t",i2)
            choice2 = input("选择进入2>>:")
            if choice2 in data[choice]:
                while not exit_flag:
                    for i3 in data[choice][choice2]:
                        print("\t\t", i3)
                    choice3 = input("选择进入3>>:")
                    if choice3 in data[choice][choice2]:
                        for i4 in data[choice][choice2][choice3]:
                            print("\t\t",i4)
                        choice4 = input("最后一层,按b返回>>:")
                        if choice4 == "b":
                            pass
                        elif choice4 == "q":
                            exit_flag = True
                    if choice3 == "b":
                        break
                    elif choice3 == "q":
                        exit_flag = True
            if choice2 == "b":
                break
            elif choice2 == "q":
                exit_flag = True

输出结果



购物车程序

需求

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
  4. 可随时退出,退出时,打印已购买商品和余额
    product_list = [
        ('Iphone',5800),
        ('Mac Pro',9800),
        ('Bike',800),
        ('Watch',10600),
        ('Coffee',31),
        ('Alex Python',120),
    ]
    shopping_list = []
    salary = input("Input your salary:")
    if salary.isdigit():
        salary = int(salary)
        while True:
            for index,item in enumerate(product_list):
                #print(product_list.index(item),item)
                print(index,item)
            user_choice = input("选择要买嘛?>>>:")
            if user_choice.isdigit():
                user_choice = int(user_choice)
                if user_choice < len(product_list) and user_choice >=0:
                    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,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) )
                    else:
                        print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m" % salary)
                else:
                    print("product code [%s] is not exist!"% user_choice)
            elif user_choice == 'q':
                print("--------shopping list------")
                for p in shopping_list:
                    print(p)
                print("Your current balance:",salary)
                exit()
            else:
                print("invalid option")

输出结果



5、字符串操作


name = "my \tname is {name} and i am {year} old"

print(name.capitalize())   #首字母大写
print(name.count("a"))   #计算字符串中a出现的次数
print(name.center(50,"-"))   #将字符串长度变成50,不足用-补充,字符串放在中间
print(name.endswith("ex"))   #判断字符串是否以ex结尾
print(name.expandtabs(tabsize=30))   #将tab键转成30个空白字符
print(name[name.find("name"):])   #查找从name开始的字符串,相当于切片
print(name.format(name='alex',year=23))   #格式化输出
print(name.format_map(  {'name':'alex','year':12}  ))   #格式化输出(不常用)
print('ab23'.isalnum())   #是否只包含阿拉伯数字和字母
print('abA'.isalpha())   #是否只包含字母
print('11'.isdecimal())   #是否是十六进制
print('1A'.isdigit())   #是否只包含阿拉伯数字
print('a 1A'.isidentifier()) #判断是不是一个合法的标识符
print('33A'.isnumeric())   #判断是否只包含数字
print('My Name Is  '.istitle())   #是否是title形式(每个单词首字母大写)
print('My Name Is  '.isprintable()) #tty file ,drive file
print('My Name Is  '.isupper())   #是否全是大写
print('+'.join( ['1','2','3'])  )   #将字符以+号连接
print( name.ljust(50,'*')  )   #字符串长度为50,不足在右边补*
print( name.rjust(50,'-')  )   #字符串长度为50,不足在左边补-
print( 'Alex'.lower()  )  #大写变小写
print( 'Alex'.upper()  )   #小写变大写
print( '\nAlex'.lstrip()  )   #删除左边的空白字符
print( 'Alex\n'.rstrip()  )   #删除右边的空白字符
print( '    Alex\n'.strip()  )
p = str.maketrans("abcdefli",'123$@456')
print("alex li".translate(p) )   #返回字符的对应编码

print('alex li'.replace('l','L',1))  #把l替换成L
print('alex lil'.rfind('l'))   #从左往右找最右边的对应字符的下标
print('1+2+3+4'.split('+'))   #将字符串按+号划分
print('1+2\n+3+4'.splitlines())   #自动识别不同系统的换行
print('Alex Li'.swapcase())   #大写变小写,小写变大写
print('lex li'.title())   #将字符串变成title形式(单词首字母大写)
print('lex li'.zfill(50))   #字符串长度变为50,不足用0补充

print( '---')

输出结果








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值