Day_2_Python_str_list_dict的使用

模块的命名
1 假设命名的是sys.py 在sys.py中引入sys 这样是有问题的,不建议引入库与文件重名
2 base/lib/site-packages  #第三方库会放在这里
3 base/lib                #一般存放的是标准库
sys模块
case_1
import sys
print (sys.path)
#在pycharm中会显示绝对路径,在cmd中调用的话会使用相对路径
 
case_2
import sys
print (sys.argv[2])
#可以打印传入参数的第3个值
 
os模块
case_1
import os
os.system("dir")
 
case_2 
import os 
cmd_res = os.system("dir")     #执行命令不保存命令
cmd_res = os.popen("dir").read #先存到内存,再通过read读取
cmd_res = os.mkdir("new_dir")
print (cmd_res)
#返回 0 代表执行成功
#返回 1 代表执行失败
system和popen的区别
os.system #调用系统命令,完成后退出,返回结果是命令执行状态,一般是0
os.popen  #可以实现一个“管道”,从这个命令获取的值可以在python 中继续被使用
两者的区别是:
os.system(cmd)#的返回值只会有0(成功),1,2
os.popen(cmd) #会吧执行的cmd的输出作为值返回。
Python中的Pyc文件的由来
在执行python的时候会先去找有没有pyc文件
python在执行过程中第一步也是先编译,编译成一个pyc
布尔值
真   或   假
1    或   0 
True 或 False
运算法则
python3会精确计算例如
/ 除法 10/3 = 10.3333335
% 取模 10%3 = 1
** 取幂 10**2 = 100
 
位运算
 
三元运算
 
python3中有个新类型叫 bytes
 

list
names = ["a","b","c","d"]
print(names)
print (names[0],names[2]) #取 下标
print (names[1:3])        #顾 头不顾尾
print (names[-1])         #取 -1
print (names[-2::]) 
print (name[0:3])         #前边的0可以省略
 
向后追加
names = ["a","b","c","d","e","f"]
names.append("e")
向指定位置插入 1是下标,插入后 1的位置就是e
names = ["a","b","c","d","e","f"]
names.insert(1,"e")
修改list
names = ["a","b","c","d","e","f"]
names[2] = "gai"
#直接对下标元素进行赋值
删除一
names = ["a","b","c","d","e","f"]
names.remove["a"]
#移除指定元素
删除二
names = ["a","b","c","d","e","f"]
del names[0]
#删除最后一个,加下标可以删除指定下标的
names = ["a","b","c","d","e","f"]
names.pop()
 
#查询下标位置,但只能查询到第一次匹配的位置
names = ["a","b","c","d","e","f"]
names.index("a") 
清除
names.clear() #清除names这个list的内容
反转
names = ["a","b","c","d","e","f"]
names.reverse()
print (name)
#print(names.reverse))则不会有输出
合并list
names1 = ["1","2"]
names = ["a","b","c","d","e","f"]
names.extend(names1)
#将 两个list进行合并
copy浅拷贝
names1 = ["a","b","c",[1,2]]
names2 = names1.copy()
names1[-1][0] = 8
names1[0] = 8
 
print(names1,names2)
[8, 'b', 'c', [8, 2]] ['a', 'b', 'c', [8, 2]]
 
copy深拷贝
#两份独立的内存空间,各自修改不影响
import copy
names1 = ["a","b",[1,2],"d"]
names2 = copy.deepcopy(names1)
names1[0] = 8
names1[2][0] = 8
print (names1,names2)
举例
person = ["name",["saving",100]]
p1 = person[:]
p2 = person[:]
 
p1[0] = "alex"
p2[0] = "fengjie"
 
p1[1][1] = 50
 
print (p1,p2)
 
tuple
 
tuple只有两个属性
1、count
2、index
小练习
#判断是否是数字
if salary_input.isdigit():
      salary = int(salary)
#小练习中的亮点:
#一、判断是否为数字 .isdigit()函数
#二、减价钱 input_usermoney -= 100
#三、彩色字体显示
#四、enumerate取下标和内容
#         for index,item in enumerate(shop):
#             print ("index %s,item %s: "%(index,item))
 
cart_list = []
shop = [["phone",4999],["mac air",6000],["coffe",30]]
user_money = int(input("输入你的现金 : "))
while True:
    print ("可购买的商品列表 
0 :{}
1 :{}
2 :{}
q 退出 :".format(shop[0],shop[1],shop[2]))
    salary_input = input("Plz your want buy index : ")
    if salary_input.isdigit():
        salary_input =int(salary_input)
        if salary_input <= len(shop):
            if user_money >= shop[salary_input][1]:
                user_money -= shop[salary_input][1]
                cart_list.append(shop[salary_input][0])
                print ("已购买一台 {},花了{}大洋:".format(shop[salary_input][0],shop[int(salary_input)][1]))
                print ("剩余{}钱
".format(user_money))
            elif user_money < shop[salary_input][1]:
                print ("你没这么多钱
")
        else:
            print("没有这个商品
")
    else:
        if salary_input == "q":
            cart_list = list(set(cart_list))
            print ("你买了33[41;m{},还有{}钱,退出..".format(cart_list,user_money))
            exit()
 字符
name = "zouyi"
name.capitalize()                                          #首字母大写
name.count("a")                                            #判断a的数量
name.center(50,-)               `                          #name放中间,其余用-补齐一共50个字符
name.endswith("yi")                                        #判断是否以ex结尾
name.expandtabs(tabsize=30)                             #将tab转换成30个空格
name[name.find(yi):]                                       #字符串可以用来切片 
name.format(name="alex",year=23)                           #格式化输出
name.format_map({"name":"zouyi","year":12})                #格式化输出
name.isalpha()                                             #判断是不是纯英文字符
name.isnumeric()                                           #判断是不是只有only数字
name.istitle()                                             #判断是否 每个单词是大写瘦子母
("+".join(["1","2","3","4"]))  ===>1+2+3+4
name.lstrip()                                              #去掉左边的空格
name.rstrip()                                              #去掉右边的空格
p = str.maketrans("abcdef","123456")                       #将abcdef转换成123456在translate中使用
print ("zouyi",translate(p))
print ("zouyi",replace("z",Z,1))                           #替换大小写,可以选择全部替换或者替换部分
print ("zouyi".rfind("e"))
print ("1+2+3+4",split("+"))
 
字典
 
#字典是无序的,因为字典没有下标,因为字典有key
 
info = {
"stu1101" : "a"
"stu1102" : "b"
"stu1103" : "c"
}
print (info)
 
#字典修改value
info["stu1101"] = "武藤兰"
 
#字典添加
info["stu1104"] = "d"
字典删除
1、del info["stu1101"]    #del是个通用方法
2、info.pop("stu1101")
3、info.popitem()         #随机删除一个
 
字典查找(推荐方案)
print(info.get("stuNone"))   #即便没有也不会报错

#字典确认可以是否存在
print("stu1103" in info)
 字典默认值
setdefault     #创建(但是若存在这个key则不创建)
info.setdefault("stu1104",{"www.baidu.com":"1234"})
 
#更新合并updata
b = {
"m1":1
"stu1101":"zzzzz"
}
info.update(b)             #将b合并更新到info这个字典
 
初始化一个新的字典,若修改一个values则会都变,相当于浅copy
 
字典的循环,推荐用第一种,第二种会耗费性能
for i in info:
     print(i,info[i])
 
for k,v in info.items():
     print(k,v)
原文地址:https://www.cnblogs.com/zoee/p/5733568.html