一,字典,列表,元祖互转
#1、字典
dict = {'name': 'Zara', 'age': 7, 'class': 'First'}
#字典转为字符串,返回:<type 'str'> {'age': 7, 'name': 'Zara', 'class': 'First'}
print (type(str(dict)), str(dict))
#字典可以转为元组,返回:('age', 'name', 'class')
print (tuple(dict))
#字典可以转为元组,返回:(7, 'Zara', 'First')
print (tuple(dict.values()))
#字典转为列表,返回:['age', 'name', 'class']
print (list(dict))
#字典转为列表
print (dict.values)
#2、元组
tup=(1, 2, 3, 4, 5)
#元组转为字符串,返回:(1, 2, 3, 4, 5)
print (tup.__str__())
#元组转为列表,返回:[1, 2, 3, 4, 5]
print (list(tup))
#元组不可以转为字典
#3、列表
nums=[1, 3, 5, 7, 8, 13, 20];
#列表转为字符串,返回:[1, 3, 5, 7, 8, 13, 20]
print (str(nums))
#列表转为元组,返回:(1, 3, 5, 7, 8, 13, 20)
print (tuple(nums))
#列表不可以转为字典
#4、字符串
#字符串转为元组,返回:(1, 2, 3)
print (tuple(eval("(1,2,3)")))
#字符串转为列表,返回:[1, 2, 3]
print (list(eval("(1,2,3)")))
#字符串转为字典,返回:<type 'dict'>
print (type(eval("{'name':'ljq', 'age':24}")))
二,字符串操作
#单引号
str_single_quotes = 'blog: http://www.csdn.net/wirelessqa'
#双引号
str_double_quotes = "blog: http://www.csdn.net/wirelessqa"
print ("## 单引号: " + str_single_quotes)
print ("## 双引号: " + str_double_quotes)
#用\分行
str_multi_line = "blog:\
http://www.csdn.net/wirelessqa"
print ("## 使用\\分行: " + str_multi_line)
#用\n换行显示
str_n = "blog:\nhttp://www.csdn.net/wirelessqa"
print ("## 使用\\n换行: " + str_n)
#三引号"""显示多行
str_more_quotes = """
my
name
is
Mr.B
"""
print ("## 使用三引号\"\"\"n显示多行: " + str_more_quotes)
#用r或R显示原貌
str_r = r"我是\
帅哥"
str_R = R"我是\n帅哥"
print ("## 用r显示原貌: " + str_r)
print ("## 用R显示原貌: " + str_R)
#使用u或U使之成为Unicode字符串
str_u = u'老\u0020毕'
print ("## 使用u或U使之成为Unicode字符串: " + str_u)
#注意: 字符串是无法改变的,无论你对它做什么操作,你总是创建了一个新的字符串对象,而不是改变了原有的字符串
#
#字符串是字符的序列,所以也可以通过索引的方法访问单个字符
test_str_index = "我是帅哥"
print ("## index 0: " + test_str_index[0])
print ("## index -1: " + test_str_index[-1])
#使用切片访问字任串的一部分
print ("## [0:3]: " + test_str_index[0:3])
print ("## [2:]: " + test_str_index[2:])
print ("## [-1:]: " + test_str_index[-1:])
print ("## 遍历整个字符串: ")
for t in test_str_index:print (t)
#构建另一个序列
str_list = list(test_str_index) #['我', '是', '帅', '哥']
#字符串拼接
str_add = test_str_index + '哈哈'
print ("## 字符串拼接" + str_add)
print("## 使用乘法对字符串多次重复: " + '老毕' * 3)
#使用s.isdigit()来判断字符串是否全为数字
test_isdigit_true = '782670627'
test_isdigit_false = 'abcd123'
test_isdigit_empty = ''
if test_isdigit_true.isdigit():
print (test_isdigit_true + " 字符串都是数字")
if not test_isdigit_false.isdigit():
print (test_isdigit_false + " 字符串不都是数字")
if not test_isdigit_empty.isdigit():
print ("字符串为空")
if len(test_isdigit_empty) == 0:
print ("字符串为空")
#将字符串转换成大写
test_upper = test_isdigit_false.upper()
print(test_upper)
#将字符串转换成小写
test_lower = test_upper.lower()
print(test_lower)
#测试某个字符在字符串中出现的次数
test_count = "my name is my name"
print ("## 测试某个字符在字符串中出现的次数: "+ str(test_count.count("name")))
#使用s.splitlines()将一个有多行文本的字符串分隔成多行字符串并入一个列表中
one_large_str = "chu he ri dang wu, \n han di he xia tu"
list_lines = one_large_str.splitlines() #['chu he ri dang wu, ', ' han di he xia tu']
print (list_lines)
#使用'\n'.join()重新生成一个庞大的单字符串
one_large_str2 = '\n'.join(list_lines)
print (one_large_str2)
三,购物车范例
salary = input("your money:")
if salary.isdigit():
salary = int(salary)
else:
exit("you input error,please input money")
welcome_msg = "welcome to my shopping mall".center(60,'-')
print(welcome_msg)
exit_flag = False
product_list = [
('A',1000),
('B',2000),
('C',3000),
('D',4000),
('E',5000),
('F',6000),
]
shop_car = []
while exit_flag is not True:
print ("product list".center(60,'-'))
for item in enumerate(product_list):
index = item[0]
p_name = item[1][0]
p_price = item[1][1]
print(index,'.',p_name,p_price)
user_choice = input("[q=quit,c=check]What do you want to buy?:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list):
p_item = product_list[user_choice]
print(p_item)
if p_item[1] <= salary:
shop_car.append(p_item)
salary -= p_item[1]
print ("added [%s] into shop car,you current balance is [%s]" %
(p_item,salary))
else:
print ("Your banlance is [%s],cannot offer this...." % salary)
else:
if user_choice == 'q' or user_choice == 'quit':
print("purchased products as below".center(60,'*'))
for item in shop_car:
print(item)
print("END".center(60,'*'))
print("Your balance is [%s]" % salary)
print("Bye")
exit_flag = True
elif user_choice == 'c' or user_choice == 'check':
print ("purchased products as below".center(60,'*'))
for item in shop_car:
print(item)
print("END".center(60,'*'))
print("Your balance is [%s]" % salary)
转载于:https://blog.51cto.com/7069044/1775645