好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

2-python自动化自学

基础知识:https://www.cnblogs.com/alex3714/articles/5717620.html 一:列表:     格式:variable=["value1","value2","value3",.....]     查看:         variable[#]    ---取单个         variable[#:#]  ---取出#到#-1的value         variable[#:-1] ---取出#到最后一个         variable[-#:-#]---倒数显示[-2,-1],[-3,-2]...         variable[:#]  ---0和-1是可省略的     操作:         variable.append("value") ---追加         variable.insert(#,"value")---插入         variable[#] ---改         variable.remove("value") || del variable[#] variable.pop(#)----删除         variable.index("value") ---获取下标         variable.count("value")---统计value         variable.clear()---清空variable         variable.reverse()----反转列表         variable.sort()---排序,特殊符号>数字>大写>小写         variable.extend(variable1) ---将v1里面的值赋值到v                  variable=variable1.copy() ---复制V1到V  ***这是浅copy         variable=variable1  ---复制,属于共享一个内存地址             ***补充:                 列表里面在包含一个列表,在使用copy时候会出现每有办法复制内容,这与内存的地址存储方式有关                     格式:varaible[#][#]                 使用深度COPY:                     import copy                     variable=copy.deepcopy(variable1)     列表打印:         for i in variable: ---全打印             print(i)         for i in variable[0:-1:2]: ---步长打印 二:元组:就是列表,只是创建之后value不可以更改     格式:vraiable=("value","value1","value2") 三:字符串操作:     variable.capitalize()------首字母大写     variable.casefold()--------大写全部变小写     variable.center(10,"-")----输出 ‘-------value-------‘     variable.count(‘value‘)----统计value出现次数     variable.encode()----------将字符串编码成bytes格式     variable.endswith("Li")----判断字符串是否以 Li结尾     variable.expandtabs(#) ----将\t转换成#个空格     variable.find(‘value‘)-----查找value,找到返回其索引,找不到返回-1,例如print([name.find("name"):])---展现了字符串可以切片     variable.format(V1="value",V2="value2",.....) ----用于格式化:msg = "my name is {name}, and age is {age}"     variable.format_map(‘V1’:’value‘,’V2‘:‘value2‘,.....) ----用于格式化(但是用的是字典方式):msg = "my name is {name}, and age is {age}"     variable.isalnum() --------检测字符串是否由字母和数字组成,是返回True     variable.isalpha() --------检测纯英文字符,返回false     variable.isdecimal() ------检查字符串是否只包含十进制字符,***注意:定义一个十进制字符串,只需要在字符串前添加 ‘u‘ 前缀即可。     variable.isdigit() ---是否是一个整数     variable.isidentifier() ---判断是不是一个合法的标识符(合法变量名)     variable.islower ----------是不是一个小写     variable.upper() ----是不是大写     variable.isnumeric() -----是不是一个只有数字     variable.isspace() ----是不是空格     variable.istitle() ----检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写     variable.title() ------每一个字符串开头大写     variable.isprintable()  ----是不是可以打印,tty,drive格式不能打印          variable.join(‘‘,join[‘1‘,‘2‘,‘3‘]) ---***将参数     variable.ljust(#,"*") ---不够#个则右补齐*     variable.rjust(#,‘-‘) ---不够#个则左补齐-     variable.lower()  -------将大写变为小写     variable.upper() --------将小写变大写     variable.lstrip() -------将字符最前的\n和空格去掉     variable.strip() --------将字符两边的\n和空格去掉     variable.rstrip() -------将字符最后的\n和空格去掉          variable.maketrans("V1","V2") ----将V1映射为V2,对称加密,例如 p=str.maketrans("abc","123")         variable.translate(str.maketrans)  ----加密,print("acdbf".translate(p))  结果: 1cd2f          variable.replace("old","new",#) ---替换old为new,个数为#个     variable.rfind(‘value‘) ---返回字符串最后一次出现的位置(从左向右查询)      variable.split("Separator",#) -----通过指定分隔符对字符串进行切片,Separator是分隔符,#是分隔次数,类似于cut命令;     variable.startswith("value",#,#)------用于检查字符串是否是以指定子字符串开头,#,#表示开头和结尾     variable.swapcase() ---------------小写变大写,大写变小写     variable.zfill(#) ---补0, 四:字典     特性:dict是无序的,key必须是唯一的,     格式:variable={"key":"value"}     存在更改,不存在添加:variable["key"]="value"     删除:         del variable  || del variable["key"]         variable.pop("key")         variable.popitem() ---随机删除     查看:         variable["key"]  ----不安全的获取方法         print(variable.get("key")) ---安全的获取方法     判断:         print("key" in variable)     打印:         variable.values()  ----显示所有的value         variable.key() -----显示所有的key         variable.setdefault("key1",{"key2":[value]}) ----先去字典里面寻找,如果取到就返回,如果取不到就创建一个新值         variable.update() ---分并两个字典,有key就更新,没有key就创建         variable.items() ---将一个字典转变为字符串         variable.fromkeys("[key1,key2,...]","value") ---初始化一个新字典                 可多级嵌套:[key1,key2,...],[ {"[key1-1,key1-2,...]","value"},"value" ]                 ***注意:数据创建多层嵌套是使用同一个内存,及一份数据     多级嵌套key:         variable={ "key1-1":                     { "key2-1":["value"] },                     { "key2-2":["value"] }             },             {                  "key1-2":                     { "key2-3":["value"] }                     }         更改:             variable["key1"]["key2"][#] = "value"     字典循环:         for i in variable:        ---高效             print(i,varaible[i])                  for k,v in variable.item() --低效             print(k,v)

 

 

项目1:    product_list=[        (‘phone‘,5000),        (‘book‘,4000),        (‘bike‘,3000),        (‘watch‘,2000),        (‘coffee‘,1000)    ]    shopping_list = []    salary=input("please input a number(quit):")     #judge string or number    if salary.isdigit():         salary=int(salary) #change string to number         while True:            for i in product_list:                    print(product_list.index(i),i)    #enumerate(variable) == variable.index()            user_choice = input("choose produce(‘quit‘)")            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 shopping,your current balance is %s" %(p_item,salary))                    else:                        print("money is no enough")                else:                    print("goods is not exist")            elif user_choice == "quit":                print("----shopping list------")                for i in shopping_list:                    print(i)                print("you have money is: ",salary)                exit()            else:                print("wrong")项目2:    date1={        "key1":{            "key2":{                "key3":{                    "1":["value1-0","value2-0"],                    "2":["value1-1","value2-1"],                    "3":["value1-2","value2-2"]                    }            }        }    }    while True:        for i in date1:        print(i)        choose1=input("go to:")        if choose1 in date1:        while True:            for i in date1[choose1]:                print("\t",i)            choose2=input("go to:")            if i in date1[choose1]:                while True:                    for i in date1[choose1][choose2]:                        print("\t\t",i)                    choose3=input("go to:")                    if choose3 in date1[choose1][choose2]:                        while True:                            for i in date1[choose1][choose2][choose3]:                                print(date1[choose1][choose2][choose3][i])                            choose5=input("input:")                            if choose5 == "end":                                print("exit")                                break                    break            break        break附加:    可以用声明来实现:        提前声明一个变量:end_method = True        在退出的时候改变:end_method = False        放在循环开始处

查看更多关于2-python自动化自学的详细内容...

  阅读:24次