Python2
安装pycharm专业版,不要汉化
要想写的代码支持linux和2.0版本需要在开头加上注释
#/usr/bin/env python
#-*- coding:utf-8 -*-
运算符
结果是值
算数运算
a = 10*10
赋值运算
a = a+1 即a+=1
结果是布尔值(返还真或者假)
比较运算
a = 1>5
逻辑运算
a = 1>6 or 1==1
成员运算
a = "tom" in "l"
基本数据类型
str用法:
首字母变大写
test = "tom" v = test.capitalize() print(v) # 结果:Tom
所有的变小写,有两种方式,casefold和lower
test = "TOM" v = test.casefold() v = test.lower() print(v) #结果:tom
设置宽度,并将内容居中,*代表吧剩下的20个填充
test = "tom" v = test.center(20,"*") print(v) # 结果:********tom*********
去字符串中寻找,寻找子序列出现的次数
test = "tomtomtomtom" v = test.count("to") # print(v)
以什么什么开头什么什么结尾
test = "tomjerry" v = test.endswith("to") #不是以to结尾,返回值为false v = test.startswith("to")#是以to开头,返回值为True print(v)
从开始往后找,找到第一个后,获取其未知
test = "tomjerry" v = test.find("p") print(v) 结果为3 从第三个开始出现J如果找不到会返回值-1 test = "tomjerry" print(test.index("j"))#如果找不到j会报错
格式化,将一个字符串中的占位符替换为指定的值
test = "name{n},age{a}" v = test.format(n="tom",a=106) print(v) 结果:nametom,age106
字符串中是否只包含字母和数字
test = "tom19" v = test.isalnum() 结果为true print(v)
断句
test = "1234567\t9" v = test.expandtabs(6) print(v,len(v)) 结果:1234567 9 13
字符串中是否包含字母汉字
test = "汤姆jerry" v = test.isalpha() print(v) 结果ture
是否包含数字
test = "二" v1 = test.isdecimal() #阿拉伯 v2 = test.isdigit() #阿拉伯 ② v3 = test.isnumeric() #阿拉伯 ② 二 print(v1,v2,v3)
是否存在不可显示的字符
#\t 制表符 #\n 换行 test = "tom\njerry\ttom\njerry\t" v= test.isprintable() print(v) # 结果false 因为存在\n \t
判断是否全是空格
test = " " v = test.isspace() print(v) # 结果true
判断是否是标题
test = "Tom and Jerry" v1 = test.istitle() # 结果: False print(v1) # v2 = test.title() # print(v2) #结果:Tom And Jerry 变成标题格式 v3 = v2.istitle() print(v3) #结果:ture
将字符串中的每一个元素按照指定分隔符进行拼接
test = "tomjerry" v= "_".join(test) print(v) # 结果:t_o_m_j_e_r_r_y
判断是否全部是大小写 和 转换为大小写
test = "tOM" v1 = test.islower() v2 = test.lower() print(v1, v2) v1 = test.isupper() v2 = test.upper() print(v1,v2)
移除指定字符串
test = "tom" #v = test.lstrip("t") 去掉t v= test.strip("9") print(v)
对应关系替换
test = "tomjerry" test1 = "12345678" v ="jason,enbo" m = str.maketrans("tomjerry","12345678") new_v = v.translate(m) print(new_v)
分割为三部分
test = "tomjerry" v = test.partition("e")#('t', 'o', 'mjerry') v1 = test.rpartition("e") print(v)
分割为指定个数
test = "tomjerry" v = test.split("m",2) #'to', 'jerry's=吧m去掉剩下的分为两部分 print(v)
只能根据true false是否保留换行
test = "tom\njerry\nnatasha" v = test.splitlines(False) #['tom', 'jerry', 'natasha'] print(v)
# test = "backend 1.1.1.1" # v = test.startswith("b") 是否以b开头 # v = test.endswith("1")是否以1结尾 返回true和false值 # print(v)
大小写转换
# test = "Tom" # v = test.swapcase() #tOM 大小写转换 # print(v)
字母,数字,下划线
# a = "tom_231" # v= a.isidentifier() #判断字母数字下划线 true或者false # print(v)
将指定字符串替换为新的字符串
# test = "tom" # #v = test.replace("tom","jerry") #将tom替换为jerry # print(v)
七个常用的:
join split find strip upper lower reolace
for循环:
# name = "tom" #一个一个字符输出 t # for test in name: o # print(test) m # text = [5] # index = 0 # while index <len(text): # v = text[index] # print(v) # index+=1 # print("========")
索引下标就是 tom 012 中的012就是索引 0对应t 1对应o 2对应m
# text = "tom" # v = text[2] # print(v)
切片
# text = "tom" 去除1和2 的值 # v = text[1:3] # print(v)
获取长度 len 获取当前字符串有几个字符组成
# text = "tom" # v = len(text) #3个 # print(v)
range获取连续或者不连续的数字 只有for循环的时候才显示
# v = range(0,100,5) #0 5 10 15 20 。。。 # for item in v: # print(item)
字符串一旦创建不可修改,一旦修改或者拼接,都会重新生成字符串
# text = "tom" # age = "20" # info = text + age # print(info) # #结果tom20
list列表
# li = [12,34,56,78,"tom"] # #li.append(5) #追加 # #li.append("jerry") # li.append([123,456]) #里面再追加列表 # print(li) #li = ["tom",123,"jerry",123] # li.clear() #清除整个列表 #v = li.copy()#结果['tom', 123] copy拷贝命令 #v = li.count(123) #计算123出现的次数 2次 #li.extend(["natasha",123]) #扩展 #li = [0,456,789,123] #v = li.index(123) #获取123的位置,从左到右开始寻找 #li.insert(0,123) #在第一个插入123 0代表第一个 #li.remove(123) #删除123 #li.reverse()#翻转 #li.sort() [列表只能有数字才能排序] #print(li)
#列表中可以嵌套任何类型:[123,"tom",[0,[tom]]]
#列表中的元素可以是数字,字符串,列表,布尔值。。。。所有的元素
#li = [123,"tom",True,456] #print(li[3]) #索引取值第四个值 #print(li[0:2]) #切片取值 # for item in li: #也可以循环 # print(item) # li = [123,"tom",True,456] # #li[0] = 789 #修改,把123修改成789 0代表第一个 # #del li[0:2] #删除前两个元素,也可以删除第一个 # print(li
字符串转换成列表
# test = "name" # li = list(test) #结果['n', 'a', 'm', 'e']把字符串指定成列表 # print(li)
列表换成字符串:既有数字又有字符串需要些for循环
# li = [123,"dsdas"] # r = str(li) # #print(li) # s = "" # for i in li: # s = s+str(i) # print(s) #只有字符串的话用join # li = ["ddd","ddd","ddd"] # v= "".join(li) # print(v)
元祖,元素不可修改,不能增加或者删除
# tu = (11,22,33,44)# tu.count(22),获取指定元素在元组中出现的次数 # v = tu.index(22) # print(v)
索引
# v = tu[0] # print(v)
切片
# v = tu[0:2] # print(v)
可以被for循环,可迭代对象
# for item in tu: # print(item)
转换
# s = "asdfasdf0" # li = ["asdf","asdfasdf"] # tu = ("asdf","asdf") # # v = tuple(s) # print(v) # v = tuple(li) # print(v) # v = list(tu) # print(v) # v = "_".join(tu) # print(v) # li = ["asdf","asdfasdf"] # li.extend((11,22,33,)) # print(li)
元组的一级元素不可修改/删除/增加
# tu = (111,"alex",(11,22),[(33,44)],True,33,44,) # # 元组,有序。 # # v = tu[3][0][0] # # print(v) # # v=tu[3] # # print(v) # tu[3][0] = 567 # print(tu)
字典
根据序列,创建字典,并指定统一的值
# v = dict.fromkeys(["k1",123,"999"],123) # print(v)
删除并获取值
dic = {'k1': 123, 123: 123, '999': 123}
v = dic.pop("k1")
print(v) #获取你删除的值
print(dic)# 删除后的字典
设置值,
已存在,不设置,获取当前key对应的值
不存在,设置,获取当前key对应的值
dic = {'k1': 123, 123: 123, '999': 123}
v = dic.setdefault("k1","123")
print(v,dic)
更新
# dic = {'k1': 123, 123: 123, '999': 123}
# dic.update({"k3":"v3"})
# print(dic)
字典的value可以是任何值
# info = { # "k1": 18, # "k2": True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # 'kk1': 'vv1', # 'kk2': 'vv2', # 'kk3': (11,22), # } # ], # "k4": (11,22,33,44) # } # print(info)
布尔值(1,0)、列表、字典不能作为字典的key
# info ={ # 1: 'asdf', # "k1": 'asdf', # True: "123", # # [11,22]: 123 # (11,22): 123, # # {'k1':'v1'}: 123 # # } # print(info)
字典无序
# info = { # "k1": 18, # "k2": True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # 'kk1': 'vv1', # 'kk2': 'vv2', # 'kk3': (11,22), # } # ], # "k4": (11,22,33,44) # } # print(info)
索引方式找到指定元素
#info = { # "k1": 18, # 2: True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # 'kk1': 'vv1', # 'kk2': 'vv2', # 'kk3': (11,22), # } # ], # "k4": (11,22,33,44) # } # # v = info['k1'] # # print(v) # # v = info[2] # # print(v) # v = info['k3'][5]['kk3'][0] # print(v)
字典支持 del 删除
# info = { # "k1": 18, # 2: True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # 'kk1': 'vv1', # 'kk2': 'vv2', # 'kk3': (11,22), # } # ], # "k4": (11,22,33,44) # } # del info['k1'] # # del info['k3'][5]['kk1'] # print(info
for循环
# dict # info = { # "k1": 18, # 2: True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # 'kk1': 'vv1', # 'kk2': 'vv2', # 'kk3': (11,22), # } # ], # "k4": (11,22,33,44) # } # for item in info: # print(item) # # for item in info.keys(): # print(item) # for item in info.values(): # print(item) # for item in info.keys(): # print(item,info[item]) # for k,v in info.items(): # print(k,v) # True 1 False 0 # info ={ # "k1": 'asdf', # True: "123", # # [11,22]: 123 # (11,22): 123, # # {'k1':' v1'}: 123 # # } # print(info)
整理
# 一、数字 # int(..) # 二、字符串 # replace/find/join/strip/startswith/split/upper/lower/format # tempalte = "i am {name}, age : {age}" # # v = tempalte.format(name='alex',age=19) # v = tempalte.format(**{"name": 'alex','age': 19}) # print(v) # 三、列表 # append、extend、insert # 索引、切片、循环 # 四、元组 # 忽略 # 索引、切片、循环 以及元素不能被修改 # 五、字典 # get/update/keys/values/items # for,索引 # dic = { # "k1": 'v1' # } # v = "k1" in dic # print(v) # v = "v1" in dic.values() # print(v) # 六、布尔值 # 0 1 # bool(...) # None "" () [] {} 0 ==> Fals