三元函数:
a,b,c = 1,2,3
d = a if a>b else c
print(d)
#list 用法:
lst = [1,2,3,4,5]
print(lst[0:3])
print(lst[-1])
print(lst[-3:]) #从右边往左数4位然后向右输出
print(lst[0:-1:2]) = print(lst[::2]) #跳着输出
lst.append()#直接加入
lst.extend()#拆开一个列表加入
lst.insert() # 更换
lst.sort()#排序 1、特殊符号 2、数字 3、大写 4、小写
del lst
lst.pop() #默认删除最后一项
#list copy的几种用法
#浅copy
list.copy
list[:]
copy.copy(list)
#深copy
list.deepcopy
str的用法
str.lower #全部转换为小写
str.upper #全部转换为大写
str.capitalize # 首字母大写
str.swapcase# 大小写翻转
str.center(num,‘ ’) # 打印num个字符,将str居中,不够的用‘ ’来替代
str.endwish() #判定以什么结尾
str.find()# 返回索引值
str.format
str.isalnum #是否为阿拉伯数字,包括字母
str.isdigit #是否数字为整数
str.isalpha #是否为纯英文
str.isidentifier() # 判定是否为一个正确的变量名
print(' '.join(str)) #将字符串以' '来连接
str.strip() #将str的空格与回车给去掉
p = str.maketrans('12345','abcde') # 将str1与str2一一对应
print('11321'.translate(p)) #将str根据p的翻译规则翻译
str.replace
print(str.replace(str[old],str[new],count)) #替换原来str中的元素,count为数量
str.split(' ')将字符串以‘ ’分割
#字典dictionary
dic.setdefault # 如果能在字典里面找到这个key,就return,如果找不到就添加key-value
dic.fromkeys #初始化一个字典 这里的dic不是已有的字典
dic.update(dic_1) #刷新字典,原有的值被刷新 ,没有的加入
购物车程序:
product_list = (
['Iphone',5999],
['Mac',12000],
['Python',2999],
['Dynamo',120],
['Bike',5000]
)
salary = int(input('salary:'))
shop_list = []
while True:
#for index , item in enumeric(product_list):
#print(index,item)
for item in product_list:
print(item,item[1])
usr_choice = input(‘请输入物品的编号:’)
if usr_choice.isdigit:
usr_choice = int(usr_choice)
if usr_choice < len(produce_list) and usr_choice>= 0:
p_item = produce_list[item]
if p_item[1] < = salary:
salary -= p_item[1]
print('Added %s in your shopping list ,and your current balance is \033[31;1m%s\033[0m'%(p_item,salary))
shop_list.append(p_item)
else:
print('\033[41;1myour balance not enough buy it,please recharge\033[0m')
else:
print('valid option')
elif usr_choice == 'q':
exit()
elif usr_choice == 'b':
break
else:
print('valid option')