day2
第二周 第一到四章节 初识模块
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: sys_mode.py sys模块
@time: 2020/05/27
"""
import sys ---->sys模块
import os ---->os模块
#print(sys.path)
print(sys.argv) ---->捕捉参数
cmd_res = os.popen("dir").read()
print("--->",cmd_res)
创建一个名称为"new_dir"的文件夹
os.mkdir("new_dir")
第二周 第五章节 python的数据类型
https://www.cnblogs.com/alex3714/articles/5465198.html
int(整型)
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
跟C语言不同,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。
注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。
float(浮点型)
浮点数用来处理实数,即带有小数的数字。类似于C语言中的double类型,占8个字节(64位),其中52位表示底,11位表示指数,剩下的一位表示符号。
complex(复数)
复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。
第二周 第七章节 列表的使用
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: list.py
@time: 2020/05/27
"""
###列表
###定义列表
names = ["xiaoliangliang","xiaomingming","xiaoyangyang","xiaoqiangqiang"]
###打印列表
print(names)
###通过列表下标取值
print(names[2])
###下标连环取
print(names[0:2])
###取最后一个值
print(names[-1])
print(names[-3:-1])
print(names[-2:])
###列表的追加
names.append("zhangxiaojie")
print(names)
###数据的插入,第一个是插入的位置,第二个是插入的列表数值
names.insert(2,"chenhua")
print(names)
###数据的替换
names[2]="xiexuhui"
print(names)
###删除列表第一种方法
names.remove("xiexuhui")
print(names)
###删除列表第二种方法
del names[0]
print(names)
###删除列表第三种方法
names.pop(1)
print(names)
###通过名字找位置
print(names.index("zhangxiaojie"))
####通过位置打印
print(names[names.index("zhangxiaojie")])
names.append("zhangxiaojie")
print(names)
print(names.count("zhangxiaojie"))
names.clear()
print(names)
###reverse反转
names = ['xiaoliangliang', 'xiaomingming', 'chenhua', 'xiaoyangyang', 'xiaoqiangqiang', 'zhangxiaojie']
names.reverse()
print(names)
###sort排序
names.sort()
print(names)
###extend
names2 = ["abc","def","ghi"]
names.extend(names2)
print(names)
D:\Python3.5.2\python.exe "D:/lesson 14/day2/list.py"
['xiaoliangliang', 'xiaomingming', 'xiaoyangyang', 'xiaoqiangqiang']
xiaoyangyang
['xiaoliangliang', 'xiaomingming']
xiaoqiangqiang
['xiaomingming', 'xiaoyangyang']
['xiaoyangyang', 'xiaoqiangqiang']
['xiaoliangliang', 'xiaomingming', 'xiaoyangyang', 'xiaoqiangqiang', 'zhangxiaojie']
['xiaoliangliang', 'xiaomingming', 'chenhua', 'xiaoyangyang', 'xiaoqiangqiang', 'zhangxiaojie']
['xiaoliangliang', 'xiaomingming', 'xiexuhui', 'xiaoyangyang', 'xiaoqiangqiang', 'zhangxiaojie']
['xiaoliangliang', 'xiaomingming', 'xiaoyangyang', 'xiaoqiangqiang', 'zhangxiaojie']
['xiaomingming', 'xiaoyangyang', 'xiaoqiangqiang', 'zhangxiaojie']
['xiaomingming', 'xiaoqiangqiang', 'zhangxiaojie']
2
zhangxiaojie
['xiaomingming', 'xiaoqiangqiang', 'zhangxiaojie', 'zhangxiaojie']
2
[]
['zhangxiaojie', 'xiaoqiangqiang', 'xiaoyangyang', 'chenhua', 'xiaomingming', 'xiaoliangliang']
['chenhua', 'xiaoliangliang', 'xiaomingming', 'xiaoqiangqiang', 'xiaoyangyang', 'zhangxiaojie']
['chenhua', 'xiaoliangliang', 'xiaomingming', 'xiaoqiangqiang', 'xiaoyangyang', 'zhangxiaojie', 'abc', 'def', 'ghi']
Process finished with exit code 0
第二周 第八章节 列表的使用2
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: copy.py 夫妻联合账号
@time: 2020/05/27
"""
import copy
###浅copy
###列表
###定义列表
person = ["name",["saving",100]]
#print(person)
'''
p1=copy.copy(person)
p2=person[:]
p3=list(person)
'''
p1=person[:]
p2=person[:]
p1[0]="alex"
p2[0]="fengjie"
p1[1][1]=50
print(p1)
print(p2)
D:\Python3.5.2\python.exe "D:/lesson 14/day2/copy.py"
['alex', ['saving', 50]]
['fengjie', ['saving', 50]]
['alex', ['saving', 50]]
['fengjie', ['saving', 50]]
Process finished with exit code 0
第二周 第九章节 元祖与购物车程序练习
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: shopping.py
@time: 2020/05/27
解题思路:
"""
product_list = [
('iphone',5800),
("mac pro",12000),
("bike",780),
("watch",9000),
("coffee",50),
("python",120)
]
shopping_list=[]
salary = input("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): ###循环取产品列表下标和产品列表
print(index,item) ###打印下标和列表
user_choice = input("选择要买的商品,请输入序号:>>>") ###输入选择的商品序号
if user_choice.isdigit(): ###判断选择的是不是数字
user_choice = int(user_choice) ###int用户的选择
if user_choice < len(product_list) and user_choice >= 0: ###如果用户的选择小于商品列表的长度并且大于等于0
p_item = product_list[user_choice] ###取出商品列表
if p_item[1] <= salary: ###买得起 ###判断工资是否买得起
shopping_list.append(p_item) ###如果买得起则添加到商品列表
salary -= p_item[1] ###扣除购买商品的钱
print("Added %s into shopping cart,your current balace is \033[31;1m%s\033[0m" %(p_item,salary)) ###打印余额
else:
print("\033[41;1m你的余额只剩[%s]啦,还买个锤子\033[0m" %(salary)) ###如果买不起,则打印如下的话
else:
print("product code [%s] is not exist!"% user_choice) ###打印选择的商品不存在
elif user_choice == "q": ###如果用户选择退出
print("------------------shopping list------------------") ###打印商品列表
for p in shopping_list:
print(p)
print("Your current balance:",salary) ###打印余额
exit()
else:
print("invalid options") ###否则打印无效选项