[ 初识Python] Day2~~

Python Day Two


 1.列表

names = ["smk","kevin","qwer","lasl",["alex","jack"]]

1>查找

#查找
print(names[2])
print(names[0:2])
print(names[-1])
print(names[-3:])

#查找下标
print(names.index("kevin"))

2>删除

#删除
names.remove("kevin")
del names[1]

names.pop()#删除最后一个元素

names.pop[1]

#删除所有元素
names.clear() 

3>修改

#修改 只能用下标改
names[1] = "凯文"

4>插入

#插入

names.insert(1,"kkk")

names.append("ffff")

names2 = [1,2,3,4,5]
names.extend(names2)

5>统计出现次数

print(names.count("kevin"))

6>反转

names.reverse()

7>排序

排序优先级:特殊字符>数字>大写字母>小写字母

names.sort()

8>复制

#复制列表 只能copy第一层浅copy
names3=names.copy()
print(names3)

#深copy 全部copy,独立
import copy
names4 = copy.deepcopy(names)
print(names4)

 

2.课后作业 

#购物车程序
#1.启动后输入工资,打印商品列表
#2.允许用户根据编号购买商品
#3.用户选择商品后,检测余额是否够,够直接扣款,不够则提醒
#4.可随时退出,退出时,打印已购买的商品和余额

#购物车程序
#1.启动后输入工资,打印商品列表
#2.允许用户根据编号购买商品
#3.用户选择商品后,检测余额是否够,够直接扣款,不够则提醒
#4.可随时退出,退出时,打印已购买的商品和余额

#Author : Kevin

product_list = [
                ('IphoneX',10000),
                ('Apple',10),
                ('Dell',200),
                ('lenovo',1050),
                ('girlfriend',9999999)]
user_list=[]
salary = input("Please Input your salary : ")

if salary.isdigit:
    salary = int(salary)
    while True:
        for item in product_list:
               print(product_list.index(item),item)
        #for index,item in enumerate(product_list):#enumerate可直接获取下标
          #  print(index,item)
        choice = input("choice product to shop:")
        if choice.isdigit():
            if product_list[int(choice)][1] <= salary:
                salary = salary - product_list[int(choice)][1]
                user_list.append(product_list[int(choice)])
                print("Added %s into shopping cart,your current balance is %s" %(product_list[int(choice)],salary) )
            else:
                print("you have no money !")
                
        elif choice =='q':
            print("----------- Shopping List------------")
            for l in user_list:
                print("\t",l)
            print("your banlance:%s"%(salary))
            s = input("按任意键退出...")
            exit()
elif salary =='q':
    print("----------- Shopping List------------")
    for l in user_list:
        print("\t",l)
    print("your banlance:%s"%(salary))
    s = input("按任意键退出...")
    exit()

 

name = "my name is kevin age = {age}"

print(name.capitalize())#首字母大写
print(name.count("n"))#统计个数
print(name.center(50,"-"))#补全,并把字符串放到中间形如-----xxx-----
print(name.endswith("in"))#判断字符串以什么结尾
print(name.expandtabs(tabsize=30))#把tab转成tabsize个空格
print(name.find("name"))#查找字符串起始位置
print(name[name.find("name"):])#切片
print(name.format(age = 20))
print(name.format_map({'age':20}))
print('abc132'.isalnum())#是否为阿拉伯数字或者英文字母
print('abA'.isalpha())#是否为英文字母
print('1A'.isdigit())#是否为数字
print('asd12'.isidentifier())#是否为合法标识符
print('123132'.isspace())#是否是空格
print('My Name Is'.istitle())#是否每个单词首字母大写
print('ADAS'.isupper())#是否全是大写字母
print('+'.join(['1','2','3']))#把join中的每个字符用前面的字符间隔开
print(name.ljust(50,'*'))#补全,在右边补
print(name.rjust(50,'*'))#补全,在左边补
print('Alex'.lower())
print('Alex'.upper())
print('\nAlex'.lstrip())#去掉左边开头的换行或空格
print('Alex\n'.rstrip())
print('\nAlex\n'.strip())

#加密
p = str.maketrans("abcdef",'123456')
print("afcd".translate(p))

#替换,换n个,默认都换
print("afcdaa".replace('a','A',2))
print('alex lill'.rfind('l'))#查找最右边的位置
print('alsadll asdasdas asd asd'.split(' '))#字符串分割
print('Alex Li'.swapcase())#大写转小写,小写转大写
print('lex li'.title()) #单词首字母大写
print('132'.zfill(10))#不够的用0填充

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值