码迷,mamicode.com
首页 > 编程语言 > 详细

python第二日随堂练习题

时间:2017-09-16 11:47:43      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:git   序列   购物   pen   start   oldboy   input   习题   upper   

 

练习1:

# 写代码,有如下变量,请按照要求实现每个功能 (共6分,每小题各0.5分)
name = " aleX "
# 1) 移除 name 变量对应的值两边的空格,并输出处理结果
print(‘------------1-----------‘)
print(name.strip( ))
# 2) 判断 name 变量对应的值是否以 "al" 开头,并输出结果?
print(‘------------2-----------‘)
print(name.startswith(‘al‘))
# 3) 判断 name 变量对应的值是否以 "X" 结尾,并输出结果?
print(‘-----------3------------‘)
print(name.endswith(‘X‘))
# 4) 将 name 变量对应的值中的 “l” 替换为 “p”,并输出结果
print(‘-----------4------------‘)
print(name.replace(‘l‘,‘p‘))
# 5) 将 name 变量对应的值根据 “l” 分割,并输出结果。
print(‘------------5-----------‘)
print(name.split(‘l‘))
# 6) 将 name 变量对应的值变大写,并输出结果?
print(‘-----------6------------‘)
print(name.upper())
# 7) 将 name 变量对应的值变小写,并输出结果?
print(‘------------7-----------‘)
print(name.lower())
# 8) 请输出 name 变量对应的值的第 2 个字符?
print(‘------------8-----------‘)
print(name[1])
# 9) 请输出 name 变量对应的值的前 3 个字符?
print(‘-----------9------------‘)
print(name[:3])
# 10) 请输出 name 变量对应的值的后 2 个字符??
print(‘-----------10------------‘)
print(name[-2:])
# 11) 请输出 name 变量对应的值中 “e” 所在索引位置??
print(‘------------11-----------‘)
print(name.find(‘e‘))
# 12) 获取子序列,去掉最后一个字符。如: oldboy 则获取 oldbo。
print(‘-----------12------------‘)
print(name[0:5])

练习2:

 

#有列表data=[‘alex‘,49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量

data=[‘xiaojing‘,24,[1993,5,6]]
name=data[0]
age=data[1]
date=data[2]
print(name,age,date)

#用列表模拟队列,队列:先进先出
#append pop
s1=[]
s1.append(‘he1‘)
s1.append(‘he2‘)
s1.append(‘he3‘)
print(s1.pop(0))
print(s1.pop(0))
print(s1.pop(0))
#用列表模拟堆栈,队列:先进后出
#insert pop

h1=[]
h1.insert(0,‘y1‘)
h1.insert(0,‘y2‘)
h1.insert(0,‘y3‘)
print(h1.pop(0))
print(h1.pop(0))
print(h1.pop(0))

练习3:

‘‘‘
#简单购物车,要求如下:
实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,如果输入为空或其他非法输入则要求用户重新输入  

msg_dic={
‘apple‘:10,
‘tesla‘:100000,
‘mac‘:3000,
‘lenovo‘:30000,
‘chicken‘:10,
}

‘‘‘


msg_dic={
‘apple‘:10,
‘tesla‘:100000,
‘mac‘:3000,
‘lenovo‘:30000,
‘chicken‘:10,
}
list=[]
while True:
for items in msg_dic:
print(items,msg_dic[items])
pin=input(‘商品:‘).strip()
if not pin or pin not in msg_dic:
continue
num=input(‘数量:‘).strip()
if not num.isdigit():
continue
list.append((pin, msg_dic[pin], num)) #append一个元组
print(list)

练习4:

 

python第二日随堂练习题

标签:git   序列   购物   pen   start   oldboy   input   习题   upper   

原文地址:http://www.cnblogs.com/xiaojing3711/p/7529359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!