day2

1.python的模块有:1)标准库--可直接导入

         2)第三方库--必须安装

 通过import来调用,eg:

import sys
print(sys.path)     
'''打印环境变量,其中以site-packages结尾的目录是python的第三方库位置,上一级是标准库的位置'''
print(sys.argv) #打印相对路径
import os
os.system("dir")    #输出乱码是因为windows的字符编码于utf-8不一致,0代表执行成功
cmd_res = os.system("dir") #执行命令,但不会保存结果
cmd_res = os.popen("dir")    #打印的是一个内存的对象地址,不是结果
cmd_res = os.popen("dir").read() 
 #执行完后,这结果是存到以一个相当于内存的临时地方,这地方必须通过read来取得
os.mkdir("new_dir")     #当前目录下创文件夹

  

当然,也可自己写模块,然后放到当前目录或python全局环境变量中,python会优先查找当前目录,后再查看全局

2.*.pyc文件后的c是compiled。python是一门基于虚拟机的语言,先翻译后解释的语言。

3.数据类型有:1)数字,分为整形int 和 浮点数float(python3将整形int和长整形long合并)

       2)布尔值;真或假;0或1

       3)字符串

 格式化输出中,%s为字符串;%d为整数;%f为浮点数

       4)列表  5)元组--不可变列表  6)字典--无序

4.python2--字节bytes和字符串一个概念,而3中是分开的

 文本总是unicode,由str表示;而二进制数据由bytes类型表示

  视屏文件--二进制;文本文件--一般字符串,也可以用二进制表示

  string  
decode解码↑↓encode变吗
  bytes  

  python3的数据传输都必须以二进制传输

5.列表:

names = ["1ZhangYang","#Gunyun",["Francis","abc"],"xXiangpen","XuLiangchen"]     #标志性[]

  

1)查
print(names[2])
print(names[0:2])  #切片
print(names[:2])
print(names[-2:])

print(names.index("XieDi")) #索引

 2)增

names.append("LeiHaidong")  #后面追加
names.insert(1,"ChengRonghua")

 3)改

names[2] = "XieDi"

 4)删

remove name("XieDi")
del names[1]
names.pop()  #不加下标时,默认在淡出最后一个  
names.pop(1)

 

浅copy包含有:1)p1 = copy.copy(person)  2) p2 = person[:]   3) p3 = list(person)

浅copy只copy第一层,深copy:copy.deepcopy(names)

 

6.目前,第一个最长的程序

程序:购物车程序

需求:

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
  4. 可随时退出,退出时,打印已购买商品和余额
product_list = [("IPhone",5800),
                ("Mac Pro",12000),
                ("Starbuck Latte",31),
                ("Bike",800)]
shopping_list = []
salary = input("Input your salary:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(product_list):
            print(index,item)
        user_choice = input("请输入您的选择:")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(product_list) and user_choice >= 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 your shopping cart,your current balance is \033[31;1m%s\033[0m"%(p_item,salary))
                else:
                    print("your current balance is not enough")
            else:
                print("product code %s is not exist"%user_choice)
        elif user_choice == "q":
            print("--------shopping list--------")
            for i in shopping_list:
                print(i)
            print("your current balance:",salary)
            exit()
        else:
            print("Invild option")

  

posted @ 2018-11-20 23:18  Chubo_Lin  阅读(186)  评论(0)    收藏  举报