一、for 循环
uname = ['rocco', 'dennis', 'min', 'qiangdong'] # 普通取值将,重复写代码 print(uname[0]) # 'rocco' print(uname[1]) # 'dennis' print(uname[2]) # 'min' print(uname[3]) # 'qiangdong' print(len(uname)) # 4 # # 使用while循环将所有值便利出来 count = 0 while count < len(uname): print(uname[count]) count += 1 # for循环在循环取值方面相对while循环要方便的多 # 便利列表 uname = ['rocco', 'dennis', 'min', 'qiangdong'] for i in uname: print(i) # # 便利字典 dic = {'name': 'liu', 'age': 11, 'level': 99} for x in dic: print(x, dic[x]) # # 控制次数 count = 0 while count < 3: print('hello') count += 1 for x in 'hel': print("word") # for + range for i in range(1, 6): print(i) for i in range(1, 6, 3): print(i) for y in range(10): print('hello,word') # 解压赋值 info = [['name', 'rocco'], ['age', 20], ['gender', 'male']] for x, y in info: print(x, y) # for + break for i in range(6): if i == 4: break print(i) # for + continue for i in range(6): if i == 4: continue print(i) # for循环嵌套 for x in range(3): print("===》》") for y in range(4): print("\t||==>> ")
二、可变与不可变
# 值变了内存地址不变,就说明原值是可变类型 # 列表可变指的是索引对应内存地址 l = [11, 22, 33] print(id(l)) # 140326514185792 l[0] = 66 print(id(l)) # 140326514185792 # 值变了内存地址也变了,就说明原值是不可变类型 age = 10 print(id(age)) # 4477979584 age = 11 print(id(age)) # 4477979616 # 可变 lx = ['rocco', 000, 99] print(id(lx)) # 140540054328896 lx[0] = 777 print(id(lx)) # 140540054328896 # 不可变 age = 100 # 4330780416 print(id(age)) age = 20000 print(id(age)) # 140348391945520
三、数字类型 int float
# 整型 int # int只能转换整数 age = 10 # age = int(10) age = int(("100")) print(type(age)) # 只能存一个值的不可改变 # 长整型python2才有的 >>> l = 123123L >>> print(type(l)) <type 'long'> # 浮点型 float # 只能转换 浮点型的数字 # 进制抓换 print(bin(11)) # 十进制转换成二进制 print(oct(11)) # 十进制转换成八进制 print(hex(11)) # 十进制转化成十六进制 res = int("1000") print(type(res)) # <class 'int'> sfloat = 5.5 print(type(sfloat)) # <class 'float'> res = float("99.9") print(res + 3) # 102.9
四、元组
元组的内存地址没改变,就说明了元组的值没有改变。
# 1、按照索引取值(正反都可取)也只能取 b = (33, 44, 66) print(b[::-1]) # (66,44,33) print(b) # (33,44,66) print(len(b)) # 3 print(33 in b) # True print(33 not in b) # False # 1、用途:按照位置存放多个任意类型的元素 # 2、定义方式:()内用逗号分隔开多个任意类型的值 t = (11, 22, 33) # t = tuple((11, 22, 33)) print(type(t)) # 注意: 如果元组只有一个元素.那么必须加一个逗号 x = (10,) # print(type(x)) # 数据类型转换 res = tuple([666, 777]) print(res, type(res)) # 3、常用操作+内置的方法 # 优先掌握的操作: # 1、按索引取值(正向取+反向取):只能取 t = (11, 22, 33) # 2、切片(顾头不顾尾,步长) print(t[1:1]) # () # 3、长度 print(len(t)) # 3 # 4、成员运算in和not in print(11 in t) # True # 5、循环 t = (11, 22, 33)
五、字符串类型 str
name = "rocco" # name = str("rocco") 引号可以是''、""、""" """、''' ''' 注意1:引号的嵌套 注意2: file_path=r'C:\a\b\new.txt' # 类型转换:str可以把任意类型都转成字符串类型 res=str([1,2,3]) # "[1,2,3]" print(type(res)) # 3、常用操作+内置的方法 # 3.1 优先掌握的操作: # 1、按索引取值(正向取+反向取) :只能取 msg = 'hello world' print(msg[0]) print(msg[-1]) msg[0] = "H" # 2、切片(顾头不顾尾,步长)===>复制 msg = 'hello world' res = msg[1:5:1] # 1 2 3 4 print(res) print(msg) res=msg[1:5:2] print(res) ------------------------- res=msg[1:5] print(res) ------------------------- res=msg[1:] res=msg[:5] res=msg[::1] ------------------------- print(id(msg)) res=msg[:] print(id(res)) print(res) ------------------------- msg = 'hello world' print(msg[-1:-3:-1]) # -1 -2 print(msg[-1::-1]) # -1 -2 print(msg[::-1]) # -1 -2 ------------------------- # 3、长度len msg = 'hello\n' print(len(msg)) ------------------------- # 4、成员运算in和not in msg='lxx is sb' print('sb' in msg) print('sb' not in msg) # 推荐 print(not 'sb' in msg) ------------------------- # 5、移除空白strip msg=' \n lxx is sb ' print(msg) res = msg.strip() print(res msg = "()=h-,^&*ello*&-=," res = msg.strip("()=-,^&*") print(res new_res=res.replace('*-,^&','') print(new_res msg = "lxx is sb sb is lqz" res = msg.replace('sb','SB',1) print(res 不是所有的功能运行完毕都有返回值 res = print("hello") print(res name = input("your name: ").strip() # name = "rocco " pwd = input("your password: ").strip() if name == "rocco" and pwd == "123": print('认证成功') else: print('输入的账号或密码错误' ------------------------- # 6、切分split info="rocco:123:10" res = info.split(":",1) print(res) res = ['rocco', '123','10'] msg = ":".join(res) # res是由纯字符串组成的列表 print(msg) ------------------------- # 7、循环 for x in "hello": print(x) ####### 需要掌握的操作 # 1、strip 方法(去除左右两边的所有符号+-*/%以及空白符号) mm = " ls is nn" mm = " ls is nn " mm = " \n ls is nn " print(mm.strip()) # 2、strip, lstrip, rstrip print("***hello*".strip('*')) # 左右 print("***hello*".lstrip('*')) # 左 print("***hello*".rstrip('*')) # 右 print("000hello,rocco00000".strip('0')) # hello,rocco print("000hello,rocco00000".lstrip('0')) # hello,rocco00000 print("000hello,rocco00000".rstrip('0')) # 000hello,rocco # 3、lower, upper print("AbcDDDDEEE".lower()) # 全消息 print("AbcDDDDEEE".upper()) # 全大写 # 4、startswith, endswith print("hello".startswith("he")) # 是否以he开头 print("hello".endswith("lo")) # 是否以lo结尾 # 5、格式化字符 x = "rocco" y = "123" m = f"my name is {x} ma age {y}" print(m) -------------------------方式一 x = "rocco" y = "18" msg = "my name is %s my age is %s" %(x,y) print(msg) -------------------------方式二 x = "rocco" y = "18" msg = "my name is {} my age is {}".format(x,y) msg = "my name is {0} my age is {1}{1}{1}{0}".format(x,y) msg = "my name is {name} my age is {age}".format(age=18,name="rocco") print(msg) -------------------------方式三 # 6、字符串转换为列表 split, join info = "rocco:111:666" res = info.split(":", 1) print(res) # ['rocco', '111:666'] res = ['rocco', '341', '00'] msg = ':'.join(res) print(msg) # rocco:341:00 info = "rocco:999:222" # ----- split, rsplit print(info.split(':')) # ['rocco', '999', '222'] print(info.split(':', 1)) # ['rocco', '999:222'] print(info.rsplit(':', 1)) # ['rocco:999', '222'] # 7、 判断字符串是否是纯数字组成 isdigit print('120'.isdigit()) # True print('123.4'.isdigit()) # False # 小例子: age = input("age:").strip() if age.isdigit(): age = int(age) if age > 20: print('hao lao a') elif age < 20: print('good') else: print('error') else: print("isdigit") # 8、找出并显示字符串索引位置 ---- 了解操作 find, rfind, index, count msg = "good morning rocco rocco" ls = msg.find('rocco') ls = msg.find('sb', 0, 1) print(ls) # 13 ls = msg.rfind('rocco') print(ls) # 19 ls = msg.index('rocco0') print(ls) ls = msg.count('rocco', 0, 24) print(ls) # 总共24个字节中有两个rocco # 9、填充字符串 ---- center, ljust, rjust, zfill name = 'Rocco' print(name.center(10, '-')) # --Rocco--- print(name.ljust(10, '-')) # Rocco----- print(name.rjust(10, '-')) # Rocco print(name.rjust(10)) # 默认空格补齐 print(name.zfill(10)) # 默认0补齐 # 10、 移除空白 / 转译特殊符号 ---- expandtabs print("Rocco\tname") # Rocco name print("Rocco\tname".expandtabs(1)) # Rocco name # 11、大小写转化 ----- capitalize, swapcase, title mms = "hello morning rocco" print(mms.capitalize()) # Hello morning rocco ms = "AAABBBCCC" print(ms.swapcase()) # aaabbbccc m = "hello morning rocco" print(m.title()) # Hello Morning Rocco # 12、特殊数字 ------ isdigit, isnumeric, isdecimal num1 = b'4' # bytes num2 = '4' # unicode,python3中无需加u就是unicode num3 = '四' # 中文数字 num4 = 'Ⅳ' # 罗马数字 # isdigit:bytes中包阿拉伯数字,str内包含阿拉伯数字 print(num1.isdigit()) # True print(num2.isdigit()) # True print(num3.isdigit()) # False print(num4.isdigit()) # False # str内包含阿拉伯数字、str内包含中文数字、str内包含罗马数字 print(num2.isnumeric()) # True print(num3.isnumeric()) # True print(num4.isnumeric()) # True # str内包含阿拉伯数字 print(num2.isdecimal()) # True print(num3.isdecimal()) # False print(num4.isdecimal()) # False # 13、其他 n = 'roccO' print(n.isalnum()) # True 字符串由字母或数字组成 print(n.isalpha()) # True 字符串只由字母组成 n = "andibbble." print(n.isidentifier()) # False 字符串中是否存在符号 n = "ROCCO00;" print(n.islower()) # False print(n.isupper()) # True
六、列表类型
# # 定义方式 s = ['aa', 'bb', 'cc'] print(type(s))#<class 'list'> # # 数据类型转换 ss = list('Rocco')#['R', 'o', 'c', 'c', 'o'] print(ss) # 内置方法/常用操作 # 1、按索引存取值(正向存取+反向存取):即可存也可以取 slist = [10, 20, 30] print(slist[0]) print(slist[::-1]) slist[-1] = 300 print(slist) # 2、切片(顾头不顾尾,步长) slist = [10, 20, 30] print(slist[0:2:1]) print(slist[:]) print(slist[-1]) slist[-1] = 300 print(slist) # 3、Copy操作 ------ copy, deepcopy src_l = [100, 200, 400, [600, 90]] cp_src_l = src_l[:] cp_src_l = src_l.copy() # 浅Copy cp_src_l[3][1] = 900 # from copy import deepcopy cp_src_l = deepcopy(src_l) # 深Copy cp_src_l[0] = 1000 print(src_l) [100, 200, 400, [600, 900]] print(id(src_l[0]), id(src_l[1]), id(src_l[2]), id(src_l[3])) # 4404586240 4404589440 140646221739280 140646222215040 print(cp_src_l) # [1000, 200, 400, [600, 900]] print(id(cp_src_l[0]), id(cp_src_l[1]), id(cp_src_l[2]), id(cp_src_l[3])) # 140646221739376 4404589440 140646221739280 140646222215872 # 4、长度 ------ len() slist = [10, 20, 30] print(len(slist)) # 5、成员运算in和not in ssr = ['aa', 11, 'bb', [22, 'cc']] print([22, 'cc'] in ssr) # True print(['cc', 22] in ssr) # False # 6、追加 ---- append,insert # adppend 只能加在最后面 rss = ['chen', 'dan', 'liu', 'xin'] res = rss.append('shuang') # print(rss) # ['chen', 'dan', 'liu', 'xin', 'shuang'] # insert 按照索引位置追加值 res = rss.insert(0, 'yongyuan') print(rss) # ['yongyuan', 'chen', 'dan', 'liu', 'xin'] pw = [5,2,0] username = ['liu', 'xin', 'chendan'] sss = ['rocco'] for name in username: sss.append(name) sss.extend(username) sss.extend("tudo") print(sss) # ['rocco', 'liu', 'xin', 'chendan', 't', 'u', 'd', 'o'] # 7、删除 ---- del, remove, pop username = ['liu', 'xin', 'chendan'] del username[1] # 单纯的删,没有返回值 print(username) # ['liu', 'chendan'] username = ['liu', 'xin', 'chendan'] u = username.remove('xin') print(username) username = ['liu', 'xin', 'chendan'] u = username.pop(1) print(username) # 8 、循环列表 us = ['he','chen','dan','liu','xin'] for use in us: print(use) # ------------- 小案例 # 队列:模拟案例先进先出 a = [] # 入队 a.append('11111') a.append('22222') a.append('33333') # 出队 print(a.pop(0)) # 11111 print(a.pop(0)) # 22222 print(a.pop(0)) # 33333 # 堆栈:先进后出 b = [] # 入栈 b.append('11111') b.append('22222') b.append('33333') # 出栈 print(b.pop()) # 33333 print(b.pop()) # 22222 print(b.pop(-1)) # 11111
七、字典类型 dict
# 用途:按照属性存放多个值 # 数据类型转换 # 列表转字典 a = [('name', 'chendan'), ('age', 19)] b = dict(a) print(b) # {'name': 'chendan', 'age': 19} by = dict(k=11, w=22, z=33) print(by) # {'k': 11, 'w': 22, 'z': 33} # 1、定义好key值的位置默认补全 ------- formkeys ll = ['name', 'age', 'gender'] o = {}.fromkeys(ll, None) print(o) # {'name': None, 'age': None, 'gender': None} o['name'] = 'chen' print(o) # {'name': 'chen', 'age': None, 'gender': None} # 常用方法内制操作 # 1、按key存取值:可存可取 y = {'name': 'roccoliu', 'name2': 'chendann'} print(y['name']) # get 使用get取值不存在不会报错,会返回None print(y.get('name')) # 2、长度 ----- len() ou = {'a1': 18, 'b1': 19, 'c1': 20} print(len(ou)) # 3、成员运算in和not in # 只能运算key y = {'name': 'roccoliu', 'name2': 'chendann'} print('name2' in y) # True print('roccoliu' in y) # False # 4、删除 ---- pop, del y = {'name': 'roccoliu', 'name2': 'chendann'} del y['name'] print(y) # {'name2': 'chendann'} # pop 指定key删除,返回删除的value L = y.pop('name2') print(L) # chendann print(y) # {'name': 'roccoliu'} # 5、键keys(),值values(),键值对items() dan = {'name': 'rocco', 'age': 18} print(dan.keys()) # dict_keys(['name', 'age']) print(dan.values()) # dict_values(['rocco', 18]) print(dan.items()) # dict_items([('name', 'rocco'), ('age', 18)]) # 6、循环 d = {'name': 'rocco', 'age': 11} for i in d.keys(): print(i) # name age for n in d.values(): print(n) # rocco 11 for i in d.items(): print(i) # ('name', 'rocco'); ('age', 11) for i, n in d.items(): print(i, n) # name rocco; age 11 ----------------------------------- 重点 ========================= # 更新值 如果值不存在就添加 ------ setdefault, update dic={'k1':111,'k2':2222} dic.update({"k2":4444,'k3':5555}) print(dic) dic = {'age': 18} if 'name' not in dic: dic['name']="EGON" res = dic.setdefault('name',"EGON") print(dic) print(res) us = {'name': "chendan", 'name1': 'liuxin'} print(us) # {'name': 'chendan', 'name1': 'liuxin'} us.update({"name": 'cd', "name2": 'dandan'}) print(us) # {'name': 'cd', 'name1': 'liuxin', 'name2': 'dandan'} dc = {'age': 22} if 'name' not in dc: dc['name'] = "rocco" rs = dc.setdefault('name', 'ROCCO') print(dc) # {'age': 22, 'name': 'rocco'} print(rs) # rocco # 返回值 ----- count, index, clear nams = [99, 88, 44, 'rocco', 'rocco', 'age', 'get', 'rocco', 'toto'] print(nams.count("rocco")) # 返回值出现的次数 print(nams.index("rocco")) # 返回第一个值的索引 # print(nams.index("rocco199")) # 不存在则报 ValueError: 'rocco199' is not in list nams.clear() # 从列表中删除所有项目 print(nams) # 倒序值 ----- reverse nams.reverse() # nams[::-1] print(nams) # ['toto', 'rocco', 'get', 'age', 'rocco', 'rocco', 44, 88, 99] # 顺序排列 ----- sort nams = [1, 2, 3, -1, -3, -4] nams.sort() print(nams) # [-4, -3, -1, 1, 2, 3] nams = [1, 2, 3, -1, -3, -4] nams.sort(reverse=True) print(nams) # [3, 2, 1, -1, -3, -4]
八、集合类型 set
# 1、用途 # (1)去重 # (2) 关系运算 # 2、定义方式:在{}内用逗号分割开多个元素,其中元素的特点为 # (1)所有的元素必须是不可变类型 # (2)集合内元素不能重复 # (3)集合内元素无序 s = {123, 123, 123, 123, 333, 4444, 555} # s = set(...) print(s) # {555, 123, 4444, 333} # 数据类型转换: print(set('hello')) # {'e', 'o', 'h', 'l'} # print(set([11,11,11,22,[33,44]])) # 报错:无法把可变类型放入集合 # 3、常用操作+内置的方法 # 3.1 去重 names = ['roccoliu', 'roccoliu', 'roccoliu', 18, 18, 18, 10] names = list(set(names)) print(names) # [18, 10, 'roccoliu'] # 示范 infos = [ {'name': 'roccoliu', 'age': 18, 'sex': 'male'}, {'name': 'jack', 'age': 73, 'sex': 'male'}, {'name': 'tom', 'age': 20, 'sex': 'female'}, {'name': 'roccoliu', 'age': 18, 'sex': 'male'}, {'name': 'roccoliu', 'age': 18, 'sex': 'male'}, ] l = [] for info in infos: if info not in l: l.append(info) print(l) # 3.2 关系运算 # 3.2.1 自己写代码做关系运算 python_stus=["张三",'roccoliu','jack','lili','李四'] linux_stus=['李大炮','李二炮','jack','lili','王三炮'] l=[] for stu in python_stus: if stu in linux_stus: l.append(stu) print(l) # 3.2.2 集合的关系运算 python_stus = {"张三", 'roccoliu', 'jack', 'lili', '李四'} linux_stus = {'李大炮', '李二炮', 'jack', 'lili', '王三炮'} # (1)交集:既报名python又报名linux的学员 ---- &,intersection res = python_stus & linux_stus print(res) res = python_stus.intersection(linux_stus) print(res) # (2)并集:上海校区所有的学员姓名 ------ |, union res = python_stus | linux_stus print(res) res = python_stus.union(linux_stus) print(res) # (3)差集:只报名python的学员 ----- -, difference res = python_stus - linux_stus print(res) res = python_stus.difference(linux_stus) print(res) res = linux_stus - python_stus print(res) # (4) 对称差集: 只报名python的学员和只报名了linux的学员 ------- |, ^, difference res = (python_stus - linux_stus) | (linux_stus - python_stus) res = linux_stus ^ python_stus print(res) res = linux_stus.symmetric_difference(python_stus) print(res) # (5) 父子集:包含与被包含的关系 # 包含才能比大小 s1 = {1, 2, 3} s2 = {1, 2} print(s1.issuperset(s2)) print(s2.issubset(s1)) print(s1 >= s2) # 如果s1包含s2则成立,称之为s1是s2他爹 print(s2 <= s1) # 如果s1包含s2则成立,称之为s1是s2他爹 # 3.3 其他了解操作 s1 = {1, 2, 3} s2 = {1111, 2, 3, 333333} s1.intersection_update(s2) # s1=s1.intersection(s2) 用本身和另一个交集更新一个集合 print(s1) s1 = {1, 2, 3} s1.update({3, 4, 5}) # update 用自身和其他元素的并集更新一个集合 print(s1) s1 = {1, 2, 3} res = s1.pop() # pop 删除并返回任意集合元素 print(res, s1) s1 = {1, 2, 3} res = s1.remove(3) # remove 从集合中删除一个必须存在的元素 print(res) print(s1) s1 = {1, 2, 3} s1.add("hello") # 将元素添加到集合中如果存在则不生效 print(s1) # {1, 2, 3, 'hello'} s1.update("hello") print(s1) # {'h', 1, 2, 3, 'hello', 'o', 'e', 'l'} s1 = {1, 2, 3} s2 = {4, 4, 6} # print(s1.isdisjoint(s2)) # 如果两个集合的交点为空则返回True s1 = {1111, 2222, 333} s1.discard(444) # 删除的元素存在则删除,不存在也不会报错 print(s1)
最新评论
# 这只是一个创建远程登录并授权的语句、仅作为记录 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Fit2cloud!' WITH GRANT OPTION;
当MGR集群初始化结束后,需要开启MGR集群自启动(需要有一台节点是自动开启引导) loose-group_replication_start_on_boot = ON #设置节点是否在启动时自动启动 MGR 集群 loose-group_replication_bootstrap_group = ON #设置节点是否作为初始引导节点启动集群
密码:blog.sirliu.com
本内容密码:blog.sirliu.com 最新整理的文章在这里喔:https://blog.sirliu.com/2018/11/shell_lian_xi_ti.html