python老师布置的作业自己实践的结果
一、回顾内容
1、顶部
解释器
编码(2.7默认ascii,3.6默认utf-8)
2、print("hello")2.7
print "hello" 3.6
3、编码之间的关系
ascii 万国码 (最少两个字节unicode) gbk utf-8
1个字节 最少两个字节 2个 中文3个字节
4、命名
首字母不是数字
变量名不能是关键字
数字字母下划线
变量存在的意义-->方便调用
5、条件
if条件:
pass
elif 条件:
pass
else:
pass
注意缩进,冒号
6、while
while 条件:
pass 每次条件被判断成立无限执行
continue 中止此次循环,从新开始循环
break 终止所有循环
7、运算符
*=
+=
-=
/=
%= 取余
一个等号是赋值,两个等号才是等于
num = 13
zq = num % 2 #余数
if zq == 0:
# 偶数
else:
# 奇数
in 判断元素在不在列表里面
num = “zq”
li = ["zq","zw"]
if num in li:
print('zai')
else:
print('buzai')
再加一个判断是否以z开头的
if num in li and num.startwith('z')
# and 可以在加一个条件
8、基本的数据类型
int,整形
n = 123
n = int(123) # int类的 ——init——
s = "123"
m = int(s) #字符串类型转换整形
整形有个长度限制,32位电脑正负2的31次方....(python2.7)
python3.6里面没有long了 不管多长的数字都是int。2.7里面超过范围会转换成长整形
s = "132sdfg" #字符串里面只包含数字时才能转换
m = int(s)
str,字符串
s = "zq"
s = str("zq")
a = 123
m = str(a) 把一个数字转换成字符串
=======
bytes =》 字节类型
str =》 字符串
目的:字节转换成字符串
b = 字节类型的对象
# m = bytes(b)
m = str(b,encoding=“utf-8”)
首字母变大写 去空格 变大小写 替换 是否为数字、字母 开头结尾 查找
个数 格式化 编码解码 居中左飘右飘 连接
li = ["zq","ss"]
l1 = "_".join(li)
l1对应的值 zq_ss
list,列表
i = [11,22,33]
i = list(11,22,33) 这是错误的
i = list([11,22,33]) list 是个类名 后面小括号里面的元素只能有一个但是里面必须用中括号加序列
=====
t = (11,22,33)
i = list([t]) 这是错误的 出来的结果是这样的[(11,22,33)]
i = list(t) 这是正确的
t = [11,22,33]
t = [11,22,33,]这两种是样的
列表的公共功能:
索引
切片
for
长度
enumerate
删除 del li[0]
del li[0:2]
特有的功能:
翻转
排序
追加
插入
索引位置
删除
pop
扩展
清楚
tuple:
t = (11,22,33,44)
t = tuple(可迭代的对象)
li = (11,22,33,44)
l1 = tuple(li)
公共功能:
索引
切片
for
长度
enumerate
in
特有的功能:
个数
索引位置
特性:不能修改....
dict:
d = {"k":123,"k2":65}
d2 = {
"k":123,
"k2":156
}
li = [1,2,3,4]
字典: key:10 递加
value: 列表的元素
dic = []
for i,j in enumerate(li,10):
new_dict = dict(enumerate(li,10))
公共功能:
索引
增加 dic[key]=vlue
删除
for
长度
in
特有的功能:
项 键 值 get haskey--》is
update
fromkey
字符串,字节
str
bytes
a = "张强" gbk编码的字节
b = bytes(a,encoding="gbk") type(b)=> bytes
c = str(b,encoding="gbk")
int优化机制
a = 123
b = 123
id(a)
相同,因为有优化,-5~257
a = 123
b = a
二、作业
1、元素分类
有如下值集合【11,22,33,44,55,66,77,88,99,90】,将所有大于66的值保存至字典的第一个key中,将小于66的值保存至第二个key的值中。
既:{k1:大于66的所有值,k2:小于66的所有值}
第一种方法
# li = [11,22,33,44,55,66,77,88,99,90]
#字典
# {
# "k1":[11,22,33,44,55],
# "k2":[66,77,88,99]
# }
#code
li = [11,22,33,44,55,66,77,88,99,90]
l1 = []
l2 = []
for i in li:
if i <= 66:
l1.append(i)
else:
l2.append(i)
temp = {"k1":l1, "k2":l2}
print(temp)
第二种方法
dic = {
"k1":[],
"k2":[]
}
li = [11,22,33,44,55,66,77,88,99,90]
for i in li:
if i <= 66:
dic['k1'].append(i)
else:
dic['k2'].append(i)
print(dic)
2、查找
查找列表中元素,移动空格,并查找以a或A开头并且以c结尾的所有元素。
h=["alec","aric","Alex","Tony","rain"]
tu=["alec","aric","Alex","Tony","rain"]
dic=['k1':"alec",'k2':"aric",'k3':"Alex",'k4':"Tony",'k5':"rain"]
li = ["aleb","aric"," Alex","Tony","rain"]
for i in li:
new_i = i.strip()
if new_i.startswith('a') or new_i.startswith('A') and new_i.endswith('c'):
print(i)
换成元组序列也一样
li = ("aleb","aric"," Alex","Tony","rain")
for i in li:
new_i = i.strip()
if new_i.startswith('a') or new_i.startswith('A') and new_i.endswith('c'):
print(i)
dic={'k1':"alec",'k2':"aric",'k3':"Alex",'k4':"Tony",'k5':"rain"}
for i in dic.values():
new_i = i.strip()
if new_i.startswith('a') or new_i.startswith('A') and new_i.endswith('c'):
print(i)
3、输出商品列表,用户输入序号,显示用户选中的商品
商品li=["手机","电脑","鼠标","游艇"]
li = ["手机","电脑","鼠标","游艇"]
# for i,j in enumerate(li):
# print(i,j)
for i,j in enumerate(li):
print(i+1,j)
num = input('num:')
num = int(num)
len_i = len(li)
# if num >0 and num < 5:
if num > 0 and num <= len_i:
good = li[num-1]
print(good)
else:
print("商品不存在")
5、用户交互,显示省市县三级联动的选择
dic = {
"河北":{
"石家庄": ["上单","打野","下路"],
"邯郸" : ["地方","对方","好人"]
},
"河南":{
"郑州": ["上上","下下","左左"],
"开封" : ["悠悠","多少","东方"]
},
"山西":{
"太原": ["一一","二二","三三"],
"长治" : ["四四","呜呜","溜溜"]
}
}
#循环输出所有的省
for x in dic:
print(x)
i1 = input("请输入省份:")
a = dic[i1]
#循环输出所有的市
for j in a:
print(j)
i2 = input("请输入省份:")
b = dic[i1][i2]
#print(b)#列表类型
for z in b:
print(z)
4、购物车
功能要求:
a.要求用户输入总资产,例如:2000
b.显示商品列表,让用户根据序号选择商品,加入
购物车
c.购买,如果商品总额大于总资产,提示账户余额
不足,否则,购买成功。
d.附加:可充值、某商品移除购物车
goods = [
{"name":"电脑","price":1999},
{"name":"鼠标","price":10},
{"name":"游艇","price":20},
{"name":"美女","price":998}
]
allset = 0
car_list = []
# {
# "电脑":{'price':"单个商品价格","num":"购买多少"}
# }
i1 = input("请输入总资产:")
allset_all = int(i1)
for i in goods:
#i, 每一个列表的元素,字典
print(i['name'],i['price'])
while True:
i2 = input('请选择商品(Y/y结算):')
if i2.lower() == "y":
break
for j in goods:
if j['name'] == i2:
# print(j)
car_list.append(j)
print(car_list)
# 结算
all_price = 0
for item in car_list:
p = item['price']
all_price = all_price + p
print(allset_all,all_price)
if all_price > allset_all:
print('不行')
else:
print('行')
#总资产
asset_all = 0
i1 = input("请输入总资产:")
asset_all = int(i1)
goods = [
{"name":"电脑","price":1999},
{"name":"鼠标","price":10},
{"name":"游艇","price":20},
{"name":"美女","price":998}
]
for i in goods:
# {"name":"电脑","price":1999}
print(i['name'],i['price'])
car_dict = {}
# car_dict = {
# "电脑":{"price":"单价", "num":123}
# }
while True:
i2 = input("请选择商品(Y/y结算):") # 电脑
if i2.lower() == "y":
break
# 循环所有的商品,查找需要的商品
for item in goods:
if item['name'] == i2:
name = item['name']
# 判断购物车是否已经有该商品,有,num+1
if name in car_dict.keys():
# pass
car_dict[name]['num'] = car_dict[name]['num'] + 1
else:
car_dict[name] = {"num":1,"single_price": item['price']}
print(car_dict)
# {
# '电脑': {'num': 6, 'single_price': 1999} 6*1999
# '鼠标': {'num': 3, 'single_price': 10}} 3*10
# }
all_price = 0
for k,v in car_dict.items():
n = v['single_price']
m = v['num']
all_sum = m*n
all_price = all_price + all_sum
if all_price > asset_all:
print('买不起')
else:
从第一个print往后一格效果完全不一样
#总资产
asset_all = 0
i1 = input("请输入总资产:")
asset_all = int(i1)
goods = [
{"name":"电脑","price":1999},
{"name":"鼠标","price":10},
{"name":"游艇","price":20},
{"name":"美女","price":998}
]
for i in goods:
# {"name":"电脑","price":1999}
print(i['name'],i['price'])
car_dict = {}
# car_dict = {
# "电脑":{"price":"单价", "num":123}
# }
while True:
i2 = input("请选择商品(Y/y结算):") # 电脑
if i2.lower() == "y":
break
# 循环所有的商品,查找需要的商品
for item in goods:
if item['name'] == i2:
name = item['name']
# 判断购物车是否已经有该商品,有,num+1
if name in car_dict.keys():
# pass
car_dict[name]['num'] = car_dict[name]['num'] + 1
else:
car_dict[name] = {"num":1,"single_price": item['price']}
print(car_dict)
# {
# '电脑': {'num': 6, 'single_price': 1999} 6*1999
# '鼠标': {'num': 3, 'single_price': 10}} 3*10
# }
all_price = 0
for k,v in car_dict.items():
n = v['single_price']
m = v['num']
all_sum = m*n
all_price = all_price + all_sum
if all_price > asset_all:
print('买不起')
else:
print('可以')
名称栏目:python老师布置的作业自己实践的结果
文章分享:http://pwwzsj.com/article/igjdph.html
1、顶部
解释器
编码(2.7默认ascii,3.6默认utf-8)
2、print("hello")2.7
print "hello" 3.6
3、编码之间的关系
ascii 万国码 (最少两个字节unicode) gbk utf-8
1个字节 最少两个字节 2个 中文3个字节
4、命名
首字母不是数字
变量名不能是关键字
数字字母下划线
变量存在的意义-->方便调用
5、条件
if条件:
pass
elif 条件:
pass
else:
pass
注意缩进,冒号
6、while
while 条件:
pass 每次条件被判断成立无限执行
continue 中止此次循环,从新开始循环
break 终止所有循环
7、运算符
*=
+=
-=
/=
%= 取余
一个等号是赋值,两个等号才是等于
num = 13
zq = num % 2 #余数
if zq == 0:
# 偶数
else:
# 奇数
in 判断元素在不在列表里面
num = “zq”
li = ["zq","zw"]
if num in li:
print('zai')
else:
print('buzai')
再加一个判断是否以z开头的
if num in li and num.startwith('z')
# and 可以在加一个条件
8、基本的数据类型
int,整形
n = 123
n = int(123) # int类的 ——init——
s = "123"
m = int(s) #字符串类型转换整形
整形有个长度限制,32位电脑正负2的31次方....(python2.7)
python3.6里面没有long了 不管多长的数字都是int。2.7里面超过范围会转换成长整形
s = "132sdfg" #字符串里面只包含数字时才能转换
m = int(s)
str,字符串
s = "zq"
s = str("zq")
a = 123
m = str(a) 把一个数字转换成字符串
=======
bytes =》 字节类型
str =》 字符串
目的:字节转换成字符串
b = 字节类型的对象
# m = bytes(b)
m = str(b,encoding=“utf-8”)
首字母变大写 去空格 变大小写 替换 是否为数字、字母 开头结尾 查找
个数 格式化 编码解码 居中左飘右飘 连接
li = ["zq","ss"]
l1 = "_".join(li)
l1对应的值 zq_ss
list,列表
i = [11,22,33]
i = list(11,22,33) 这是错误的
i = list([11,22,33]) list 是个类名 后面小括号里面的元素只能有一个但是里面必须用中括号加序列
=====
t = (11,22,33)
i = list([t]) 这是错误的 出来的结果是这样的[(11,22,33)]
i = list(t) 这是正确的
t = [11,22,33]
t = [11,22,33,]这两种是样的
列表的公共功能:
索引
切片
for
长度
enumerate
删除 del li[0]
del li[0:2]
特有的功能:
翻转
排序
追加
插入
索引位置
删除
pop
扩展
清楚
tuple:
t = (11,22,33,44)
t = tuple(可迭代的对象)
li = (11,22,33,44)
l1 = tuple(li)
公共功能:
索引
切片
for
长度
enumerate
in
特有的功能:
个数
索引位置
特性:不能修改....
dict:
d = {"k":123,"k2":65}
d2 = {
"k":123,
"k2":156
}
li = [1,2,3,4]
字典: key:10 递加
value: 列表的元素
dic = []
for i,j in enumerate(li,10):
new_dict = dict(enumerate(li,10))
公共功能:
索引
增加 dic[key]=vlue
删除
for
长度
in
特有的功能:
项 键 值 get haskey--》is
update
fromkey
字符串,字节
str
bytes
a = "张强" gbk编码的字节
b = bytes(a,encoding="gbk") type(b)=> bytes
c = str(b,encoding="gbk")
int优化机制
a = 123
b = 123
id(a)
相同,因为有优化,-5~257
a = 123
b = a
二、作业
1、元素分类
有如下值集合【11,22,33,44,55,66,77,88,99,90】,将所有大于66的值保存至字典的第一个key中,将小于66的值保存至第二个key的值中。
既:{k1:大于66的所有值,k2:小于66的所有值}
第一种方法
# li = [11,22,33,44,55,66,77,88,99,90]
#字典
# {
# "k1":[11,22,33,44,55],
# "k2":[66,77,88,99]
# }
#code
li = [11,22,33,44,55,66,77,88,99,90]
l1 = []
l2 = []
for i in li:
if i <= 66:
l1.append(i)
else:
l2.append(i)
temp = {"k1":l1, "k2":l2}
print(temp)
第二种方法
dic = {
"k1":[],
"k2":[]
}
li = [11,22,33,44,55,66,77,88,99,90]
for i in li:
if i <= 66:
dic['k1'].append(i)
else:
dic['k2'].append(i)
print(dic)
2、查找
查找列表中元素,移动空格,并查找以a或A开头并且以c结尾的所有元素。
h=["alec","aric","Alex","Tony","rain"]
tu=["alec","aric","Alex","Tony","rain"]
dic=['k1':"alec",'k2':"aric",'k3':"Alex",'k4':"Tony",'k5':"rain"]
li = ["aleb","aric"," Alex","Tony","rain"]
for i in li:
new_i = i.strip()
if new_i.startswith('a') or new_i.startswith('A') and new_i.endswith('c'):
print(i)
换成元组序列也一样
li = ("aleb","aric"," Alex","Tony","rain")
for i in li:
new_i = i.strip()
if new_i.startswith('a') or new_i.startswith('A') and new_i.endswith('c'):
print(i)
dic={'k1':"alec",'k2':"aric",'k3':"Alex",'k4':"Tony",'k5':"rain"}
for i in dic.values():
new_i = i.strip()
if new_i.startswith('a') or new_i.startswith('A') and new_i.endswith('c'):
print(i)
3、输出商品列表,用户输入序号,显示用户选中的商品
商品li=["手机","电脑","鼠标","游艇"]
li = ["手机","电脑","鼠标","游艇"]
# for i,j in enumerate(li):
# print(i,j)
for i,j in enumerate(li):
print(i+1,j)
num = input('num:')
num = int(num)
len_i = len(li)
# if num >0 and num < 5:
if num > 0 and num <= len_i:
good = li[num-1]
print(good)
else:
print("商品不存在")
5、用户交互,显示省市县三级联动的选择
dic = {
"河北":{
"石家庄": ["上单","打野","下路"],
"邯郸" : ["地方","对方","好人"]
},
"河南":{
"郑州": ["上上","下下","左左"],
"开封" : ["悠悠","多少","东方"]
},
"山西":{
"太原": ["一一","二二","三三"],
"长治" : ["四四","呜呜","溜溜"]
}
}
#循环输出所有的省
for x in dic:
print(x)
i1 = input("请输入省份:")
a = dic[i1]
#循环输出所有的市
for j in a:
print(j)
i2 = input("请输入省份:")
b = dic[i1][i2]
#print(b)#列表类型
for z in b:
print(z)
4、购物车
功能要求:
a.要求用户输入总资产,例如:2000
b.显示商品列表,让用户根据序号选择商品,加入
购物车
c.购买,如果商品总额大于总资产,提示账户余额
不足,否则,购买成功。
d.附加:可充值、某商品移除购物车
goods = [
{"name":"电脑","price":1999},
{"name":"鼠标","price":10},
{"name":"游艇","price":20},
{"name":"美女","price":998}
]
allset = 0
car_list = []
# {
# "电脑":{'price':"单个商品价格","num":"购买多少"}
# }
i1 = input("请输入总资产:")
allset_all = int(i1)
for i in goods:
#i, 每一个列表的元素,字典
print(i['name'],i['price'])
while True:
i2 = input('请选择商品(Y/y结算):')
if i2.lower() == "y":
break
for j in goods:
if j['name'] == i2:
# print(j)
car_list.append(j)
print(car_list)
# 结算
all_price = 0
for item in car_list:
p = item['price']
all_price = all_price + p
print(allset_all,all_price)
if all_price > allset_all:
print('不行')
else:
print('行')
#总资产
asset_all = 0
i1 = input("请输入总资产:")
asset_all = int(i1)
goods = [
{"name":"电脑","price":1999},
{"name":"鼠标","price":10},
{"name":"游艇","price":20},
{"name":"美女","price":998}
]
for i in goods:
# {"name":"电脑","price":1999}
print(i['name'],i['price'])
car_dict = {}
# car_dict = {
# "电脑":{"price":"单价", "num":123}
# }
while True:
i2 = input("请选择商品(Y/y结算):") # 电脑
if i2.lower() == "y":
break
# 循环所有的商品,查找需要的商品
for item in goods:
if item['name'] == i2:
name = item['name']
# 判断购物车是否已经有该商品,有,num+1
if name in car_dict.keys():
# pass
car_dict[name]['num'] = car_dict[name]['num'] + 1
else:
car_dict[name] = {"num":1,"single_price": item['price']}
print(car_dict)
# {
# '电脑': {'num': 6, 'single_price': 1999} 6*1999
# '鼠标': {'num': 3, 'single_price': 10}} 3*10
# }
all_price = 0
for k,v in car_dict.items():
n = v['single_price']
m = v['num']
all_sum = m*n
all_price = all_price + all_sum
if all_price > asset_all:
print('买不起')
else:
从第一个print往后一格效果完全不一样
#总资产
asset_all = 0
i1 = input("请输入总资产:")
asset_all = int(i1)
goods = [
{"name":"电脑","price":1999},
{"name":"鼠标","price":10},
{"name":"游艇","price":20},
{"name":"美女","price":998}
]
for i in goods:
# {"name":"电脑","price":1999}
print(i['name'],i['price'])
car_dict = {}
# car_dict = {
# "电脑":{"price":"单价", "num":123}
# }
while True:
i2 = input("请选择商品(Y/y结算):") # 电脑
if i2.lower() == "y":
break
# 循环所有的商品,查找需要的商品
for item in goods:
if item['name'] == i2:
name = item['name']
# 判断购物车是否已经有该商品,有,num+1
if name in car_dict.keys():
# pass
car_dict[name]['num'] = car_dict[name]['num'] + 1
else:
car_dict[name] = {"num":1,"single_price": item['price']}
print(car_dict)
# {
# '电脑': {'num': 6, 'single_price': 1999} 6*1999
# '鼠标': {'num': 3, 'single_price': 10}} 3*10
# }
all_price = 0
for k,v in car_dict.items():
n = v['single_price']
m = v['num']
all_sum = m*n
all_price = all_price + all_sum
if all_price > asset_all:
print('买不起')
else:
print('可以')
名称栏目:python老师布置的作业自己实践的结果
文章分享:http://pwwzsj.com/article/igjdph.html