Python 条件 循环 及其他语句
1. 再次认识 input import
print('European Union', 2018) # print()多字符串输出 print("*****输出多种方式******\n") str1 = "unified" str2 = 'China' str3 = 'American' print(str2,str3,str1) # print()多字符串输出,但结果有空格,可用+加号改善 print(str2+str3+str1) # print()多字符串输出,但结果有空格,可用+加号改善 print(str2,str3,str1,sep = "_") # print()多字符串输出,可以自定义sep = "指定字符"
效果如下:
European Union 2018
*****完美分割线1******
China American unified
ChinaAmericanunified
China_American_unified
print("*****导入时重命名模块,常规来说命名调用不宜太长*****\n") import math from math import sqrt as foobar # 从math 中导入 sqrt ,并指定sqrt别名 foobar print(foobar(4)) import math as foobar # 从math 中导入 sqrt ,并指定sqrt别名 foobar print(foobar.sqrt(4))
效果如下:
*****导入时重命名模块,常规来说命名调用不宜太长***** 2.0 2.0
2. 赋值魔法 序列解包 链式赋值 增强赋值
a, b, c, d, e, f = 'Japan', 'Russia', 'China', 'European Union', 'American', 'European' print(a, b, c, d, e, f, sep="*") a = b print(a, b, c, d, e, f, sep="*") values = '2018', '2018', '2018', '2018', '2018', '2018' print(type(values)) a, b, c, d, e, f = values print(a, b, c, d, e, f, sep="*") print(type(a)) print("*****通过赋值语句,接受返回的元组解包,但两侧的元素个数与变量数量 需一致 或者用带星号 *收集*****\n") dict1 = {'European Union': '2018', 'American': '2018', 'Japan': '2018', 'China': '2018', 'Russia': '2018'} it2 = dict1.popitem() # 随机删除,并返回删除的值 print(it2) h,j = it2 # 通过赋值语句,接受返回的元组解包,但两侧的元素个数与变量数量 需一致 或者用带星号 *收集 print(h) print(j) print("*****以元组为例:用带星号 *收集,最终带星号的变量 返回的是列表*****\n") k,l,*m = 1,2,3,4 # 通过赋值语句,接受返回的元组解包,但两侧的元素个数与变量数量 需一致 或者用带星号 *收集 print(m) print(k,l,m) k,l,*m = (1,2,3,4 ) print(m) k,*l,m = (1,2,3,4 ) print(m) print(type(m)) print(type(l)) print(k,l,m) print("*****以字符串为例,用带星号 *收集,最终带星号的变量 返回的是列表*****\n") k,l,*m = "American" # 通过赋值语句,接受返回的元组解包,但两侧的元素个数与变量数量 需一致 或者用带星号 *收集 print(m) print(k,l,m) k,*l,m = "American" print(m) print(l) print(type(m)) print(type(l)) print(k,l,m) print("****链式赋值,使多个变量关联到同一个值(这个值是返回值或函数本身,类似于并行赋值*****\n") dict6 = {'European Union': '2018', 'American': '2018', 'Japan': '2018', 'China': '2018', 'Russia': '2018'} n=o= dict6.popitem() # 通过赋值语句,接受返回的元组解包,但两侧的元素个数与变量数量 需一致 或者用带星号 *收集 print(n,o) print(n == o) print(id(n),id(o)) n=dict6.popitem() # 上一行,与这两行分开赋值,不同 o= dict6.popitem() print(n,o) print(n == o) print(id(n),id(o)) print("***增强赋值,+= -= *= /= *****\n")
效果如下:
Japan*Russia*China*European Union*American*European Russia*Russia*China*European Union*American*European <class 'tuple'> 2018*2018*2018*2018*2018*2018 <class 'str'> *****通过赋值语句,接受返回的元组解包,但两侧的元素个数与变量数量 需一致 或者用带星号 *收集***** ('American', '2018') American 2018 *****以元组为例:用带星号 *收集,最终带星号的变量 返回的是列表***** [3, 4] 1 2 [3, 4] [3, 4] 4 <class 'int'> <class 'list'> 1 [2, 3] 4 *****以字符串为例,用带星号 *收集,最终带星号的变量 返回的是列表***** ['e', 'r', 'i', 'c', 'a', 'n'] A m ['e', 'r', 'i', 'c', 'a', 'n'] n ['m', 'e', 'r', 'i', 'c', 'a'] <class 'str'> <class 'list'> A ['m', 'e', 'r', 'i', 'c', 'a'] n ****链式赋值,使多个变量关联到同一个值(这个值是返回值或函数本身,类似于并行赋值***** ('American', '2018') ('American', '2018') True 2384612006920 2384612006920 ('China', '2018') ('European Union', '2018') False 2384612006792 2384612005448
3.条件语句
x == y
x < y
x> y
x <= y
x != y
x is y
x is not y
x in y
x not in y
4.循环语句
while 循环
for 循环
print("***打印 1-99 *****\n") num1 = 1 while num1 < 100: print(num1) num1 += 1 print("***打印 1-99 ,不打印88*****\n") num1 = 1 while num1 < 100: if num1 == 88: num1 += 1 print(num1) num1 += 1 print("***列表中的值 for循环,迭代列表*****\n") lst1 = ['Japan', 'Russia', 'China', 'European Union', 'American', 'European'] for str1 in lst1: # for 遍历,基本上可迭代对象, print(str1) print("***列表中的值 while循环,迭代列表*****\n") lst1 = ['Japan', 'Russia', 'China', 'European Union', 'American', 'European'] count1 = 0 while count1 < len(lst1): # while 采用索引 print(lst1[count1]) count1 += 1 print("***列表中的值 Python 内置range 函数,类似于切片,迭代列表*****\n") print(list(range(100))) for num2 in range(0, 100): print(num2) print("***字典里面的值 for 循环,获取键或值,迭代字典*****\n") dict7 = {'European Union': 2018, 'American': 2018, 'Japan': 2018, 'China': 2018, 'Russia': 2018} for key1 in dict7: print(key1) print(type(key1)) print("----------------------") for key1 in dict7: print(dict7[key1]) print(type(dict7[key1])) print("----------------------") for key1,values1 in dict7.items(): print(key1,values1) print(type(key1),type(values1),) print("***其他迭代内置函数,如 并行迭代 / 获取索引/ 反向迭代/排序后迭代*****\n") # 有时候 你想并行迭代两个序列,如下: lst11= ['Japan', 'Russia', 'China', 'European Union', 'American', 'European'] lst12 =[ '2018', '2018', '2018', '2018', '2018', '2018'] lst13 = list(zip(lst11,lst12)) # zip 是很有用的并行迭代工具,可缝合两个序列,如果两个序列长短不一,将缝合完最短的,结束. dict8 = dict(lst13) # 转换为 字典 print(lst13) print(dict8) for i in range(len(lst11)): # i 是 用作循环索引的标准变量的名称 print(lst1[i],lst12[i]) # 打印对应字典键和值 # 有时候 你想获取索引和迭代一起 lst14 = ['E', 'u', 'r', 'o', 'p', 'e', 'a', 'n'] for i in lst14 : if "n" in i: seat1 = lst14.index(i) lst14[seat1] = "A" print(lst14) print("-----------------") lst14 = ['E', 'u', 'r', 'o', 'p', 'e', 'a', 'n'] seat2 = 0 for i in lst14 : if "n" in i: lst14[seat2] = "A" seat2 +=1 print(lst14) print("-----------------") lst15 = ['E', 'u', 'r', 'o', 'p', 'e', 'a', 'n'] for index1 , string1 in enumerate(lst15) : if "n" in string1 : lst15[index1] = "A"
效果 如下:
***打印 1-99 ***** 1-99 ***打印 1-99 ,不打印88***** 1-99 ***列表中的值 for循环,迭代列表***** Japan Russia China European Union American European ***列表中的值 while循环,迭代列表***** Japan Russia China European Union American European ***列表中的值 Python 内置range 函数,类似于切片,迭代列表***** [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] 0-99 ***字典里面的值 for 循环,获取键或值,迭代字典***** China <class 'str'> American <class 'str'> Russia <class 'str'> Japan <class 'str'> European Union <class 'str'> ---------------------- 2018 <class 'int'> 2018 <class 'int'> 2018 <class 'int'> 2018 <class 'int'> 2018 <class 'int'> ---------------------- China 2018 <class 'str'> <class 'int'> American 2018 <class 'str'> <class 'int'> Russia 2018 <class 'str'> <class 'int'> Japan 2018 <class 'str'> <class 'int'> European Union 2018 <class 'str'> <class 'int'> ***其他迭代内置函数,如 并行迭代 / 获取索引/ 反向迭代/排序后迭代***** [('Japan', '2018'), ('Russia', '2018'), ('China', '2018'), ('European Union', '2018'), ('American', '2018'), ('European', '2018')] {'China': '2018', 'American': '2018', 'Japan': '2018', 'European': '2018', 'Russia': '2018', 'European Union': '2018'} Japan 2018 Russia 2018 China 2018 European Union 2018 American 2018 European 2018 ['E', 'u', 'r', 'o', 'p', 'e', 'a', 'A'] ----------------- ['E', 'u', 'r', 'o', 'p', 'e', 'a', 'A'] -----------------
反向迭代 和 排序后再迭代
sorte reversed
lst16 = ['E', 'u', 'r', 'o', 'p', 'e', 'a', 'n'] print(sorted(lst16)) # 返回排序后的列表 print(lst16) # sorted 不改原表 lst17 = reversed(lst16) print(lst17) # reversed是个更神秘的结果 print(list(lst17)) # reversed是个更神秘的结果,需要用list转换 print(str(lst17))
效果如下:
['E', 'a', 'e', 'n', 'o', 'p', 'r', 'u'] ['E', 'u', 'r', 'o', 'p', 'e', 'a', 'n'] <list_reverseiterator object at 0x0000020D8B1FF358> ['n', 'a', 'e', 'p', 'o', 'r', 'u', 'E'] <list_reverseiterator object at 0x0000020D8B1FF358>
4. 语句与 跳出循环 break continue else
5. 列表推导 ,通过一个从其他列表创建列表的方法
print([x *x for x in range(10)]) print([x *x for x in range(10) if x % 3 == 0 ]) print([(x,y) for x in range(4) for y in range(3)]) print("---------") lst18 = [] for x in range(4): for y in range(3): lst18.append((x,y)) # append((x,y)) 就地追加,无返回值 print(lst18) dict1 = {i:" {} squared is {} ".format(i , i**2) for i in range(10)} # 字典推导,for 前面分别有两个冒号表达式,分别表示健和值 print(dict1) print(dict1[8])
效果如下:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] [0, 9, 36, 81] [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2), (3, 0), (3, 1), (3, 2)] --------- [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2), (3, 0), (3, 1), (3, 2)] {0: ' 0 squared is 0 ', 1: ' 1 squared is 1 ', 2: ' 2 squared is 4 ', 3: ' 3 squared is 9 ', 4: ' 4 squared is 16 ', 5: ' 5 squared is 25 ', 6: ' 6 squared is 36 ', 7: ' 7 squared is 49 ', 8: ' 8 squared is 64 ', 9: ' 9 squared is 81 '} 8 squared is 64