Python之路,第五篇:Python入门与基础5
python 循环语句
作用: 根据一定的条件,重复的执行一个或多个语句
两种循环语句:
while 语句
for 语句
while 语句:
语法:
while 真值表达式:
语句1
。。。
else:
语句2
。。。
语法说明: else子句可以省略
执行顺序: (1) 先判断真值表达式是否为True
(2)如果第1步为True,则执行语句1后跳到第1步,否则跳到第3步;
(3)执行else 子句
(4)结束 while 语句的执行
>>> n = 1 >>> while n <= 10 : print(n) n += 1 1 2 3 4 5 6 7 8 9 10 >>>
练习1 :用字符串 * 运算符打印三角形;(要求输入一个整数,此整数代表三角形离左侧的字符串* 的距离)
n = int(input("请输入一个整数: ")) left = ' ' * n print(left + ' *') print(left + ' ***') print(left + ' *****') print(left + '*******') >>> ================================ RESTART ================================ >>> 请输入一个整数: 3 * *** ***** ******* >>> ================================ RESTART ================================ >>> 请输入一个整数: 0 * *** ***** ******* >>>
练习2 输入三行文字,让三行文字在一个方框内居中显示(输入不要输入中文);
1 #!/usr/bin/python 2 3 #输入3行文字 4 line1 = input("请输入第一行字符:") 5 line2 = input("请输入第二行字符:") 6 line3 = input("请输入第三行字符:") 7 8 #比较出3行中最大的一行字符长度 9 m = max(len(line1), len(line2), len(line3)) 10 11 #使3行输出的文字居中 12 print('+' + '-' * ( m + 2 ) + '+') 13 print('| ' + line1.center(m) + ' |') 14 print('| ' + line2.center(m) + ' |') 15 print('| ' + line3.center(m) + ' |') 16 print('+' + '-' * ( m + 2 ) + '+') 17 >>> ================================ RESTART ================================ 18 >>> 19 请输入第一行字符:aaaaaaaaaaaaa 20 请输入第二行字符:aaaaaaaa 21 请输入第三行字符:aaaaaaaaaa 22 +---------------+ 23 | aaaaaaaaaaaaa | 24 | aaaaaaaa | 25 | aaaaaaaaaa | 26 +---------------+ 27 >>>
while 语句嵌套
语法:
while a > b:
while b > c:
......
else:
.......
1 >>> i = 1 2 >>> while i < 10: 3 j = 1 4 while j < 10: 5 print("i =", i, "j =", j) 6 j += 1 7 i += 1 8 9 10 i = 1 j = 1 11 i = 1 j = 2 12 i = 1 j = 3 13 i = 1 j = 4 14 i = 1 j = 5 15 i = 1 j = 6 16 i = 1 j = 7 17 i = 1 j = 8 18 i = 1 j = 9 19 i = 2 j = 1 20 i = 2 j = 2 21 i = 2 j = 3 22 i = 2 j = 4 23 i = 2 j = 5 24 i = 2 j = 6 25 i = 2 j = 7 26 i = 2 j = 8 27 i = 2 j = 9 28 i = 3 j = 1 29 i = 3 j = 2 30 i = 3 j = 3 31 i = 3 j = 4 32 i = 3 j = 5 33 i = 3 j = 6 34 i = 3 j = 7 35 i = 3 j = 8 36 i = 3 j = 9 37 i = 4 j = 1 38 i = 4 j = 2 39 i = 4 j = 3 40 i = 4 j = 4 41 i = 4 j = 5 42 i = 4 j = 6 43 i = 4 j = 7 44 i = 4 j = 8 45 i = 4 j = 9 46 i = 5 j = 1 47 i = 5 j = 2 48 i = 5 j = 3 49 i = 5 j = 4 50 i = 5 j = 5 51 i = 5 j = 6 52 i = 5 j = 7 53 i = 5 j = 8 54 i = 5 j = 9 55 i = 6 j = 1 56 i = 6 j = 2 57 i = 6 j = 3 58 i = 6 j = 4 59 i = 6 j = 5 60 i = 6 j = 6 61 i = 6 j = 7 62 i = 6 j = 8 63 i = 6 j = 9 64 i = 7 j = 1 65 i = 7 j = 2 66 i = 7 j = 3 67 i = 7 j = 4 68 i = 7 j = 5 69 i = 7 j = 6 70 i = 7 j = 7 71 i = 7 j = 8 72 i = 7 j = 9 73 i = 8 j = 1 74 i = 8 j = 2 75 i = 8 j = 3 76 i = 8 j = 4 77 i = 8 j = 5 78 i = 8 j = 6 79 i = 8 j = 7 80 i = 8 j = 8 81 i = 8 j = 9 82 i = 9 j = 1 83 i = 9 j = 2 84 i = 9 j = 3 85 i = 9 j = 4 86 i = 9 j = 5 87 i = 9 j = 6 88 i = 9 j = 7 89 i = 9 j = 8 90 i = 9 j = 9 91 >>>
for 循环语句
for 语句可以用来遍历或者可迭代对象的每一个元素;
可迭代对象包括:
字符串str 、 列表list 、 元组tuple 、 字典dict 、 集合set 、 固定集合 frozenset 、 迭代器
for 语句的语法:
for 变量列表 in 可迭代对象
语句1
。。。。
else:
语句2
。。。。
说明: else子句部分可以省略 ; 语句1的执行次数与可迭代对象的元素个数有关;
#!/usr/bin/python s = "Hello" for i in s: print("i->>", i) else: print("for 语句结束") print("程序结束") >>> ================================ RESTART ================================ >>> i->> H i->> e i->> l i->> l i->> o for 语句结束 程序结束 >>>
练习: 1任意输入一个字符串,计算输入的字符‘a’的个数,并打印
1 #!/usr/bin/python 2 3 s = input("请输入字符:") 4 print(s.count('a')) 5 >>> ================================ RESTART ================================ 6 >>> 7 请输入字符:abcdefaa 8 3 9 >>>
1 #!/usr/bin/python 2 3 s = input("请输入字符:") 4 count = 0 5 for i in s: 6 if i == 'a': 7 count += 1 8 print("a的个数为:", count) 9 >>> ================================ RESTART ================================ 10 >>> 11 请输入字符:abceaassevfaaazzz 12 a的个数为: 6 13 >>>
2 输入字符,打印出来成为一行每个字符有空格间隔;
1 #!/usr/bin/python 2 3 s = input("请输入字符:") 4 for i in s: 5 if i != 'a': 6 pass 7 else: 8 print(i, end=' ') 9 >>> ================================ RESTART ================================ 10 >>> 11 请输入字符:abcdeaa 12 a a a 13 >>>
range 函数
格式:range(stop) -> range object ; range(start, stop[, step]) -> range object (step步长可以为负数,且可以省略)
range(stop): range函数从零开始,每次生成一个整数,后加1操作,直到stop为止(不包含stop) ,返回一个可迭代对象。
例子: range(3) # 0 1 2
range(5) # 0 1 2 3 4
for x in range(10):
print(x)
range(1, 8 ,2) #1 3 5 7
range(5, 0, -1) #5 4 3 2 1
range(5, 0, -2) #5 3 1
range(5, 0) #空, 什么也没有
练习:打印1-10的奇数和偶数
#!/usr/bin/python for i in range(1,11): if i % 2 == 0: print("i的偶数:",i) else: print("i的奇数:",i) >>> ================================ RESTART ================================ >>> i的奇数: 1 i的偶数: 2 i的奇数: 3 i的偶数: 4 i的奇数: 5 i的偶数: 6 i的奇数: 7 i的偶数: 8 i的奇数: 9 i的偶数: 10 >>>
1 #!/usr/bin/python 2 3 for i in range(1,11,2): 4 print(i, end=' ') 5 print() 6 for i in range(2,11,2): 7 print(i, end=' ') 8 print() 9 >>> ================================ RESTART ================================ 10 >>> 11 1 3 5 7 9 12 2 4 6 8 10 13 >>>
练习: 计算出100-1000之间的水仙花数(Narcissistic number),(水仙花数是指 百位的立方+ 十位的立方 + 个位的立方)
例如: 153 = 1**3 + 5 ** 3 + 3 **3
1 #!/usr/bin/python 2 3 for i in range(100,1000): 4 a = i // 100 #百位数 5 b = (i % 100) // 10 #十位数 6 c = i % 100 % 10 7 if i == a ** 3 + b ** 3 + c ** 3: 8 print(i, end=' ') 9 >>> ================================ RESTART ================================ 10 >>> 11 153 370 371 407 12 >>>
1 #!/usr/bin/python 2 3 for i in range(100,1000): 4 s = str(i) 5 a = int(s[0]) #百位数 6 b = int(s[1]) #十位数 7 c = int(s[2]) 8 if i == a ** 3 + b ** 3 + c ** 3: 9 print(i, end=' ') 10 print() 11 >>> ================================ RESTART ================================ 12 >>> 13 153 370 371 407 14 >>>
1 #!/usr/bin/python 2 3 for a in range(1,10): 4 for b in range(0,10): 5 for c in range(0,10): 6 #print(a,b,c,end=' ') 7 n = a * 100 + b * 10 + c 8 if n == a ** 3 + b ** 3 + c ** 3: 9 print(n, end=' ') 10 >>> ================================ RESTART ================================ 11 >>> 12 153 370 371 407 13 >>>
for 语句嵌套
例子: for x in “abc”:
for y in "123":
print(x+y)
1 >>> for x in "abc": 2 for y in "123": 3 print(x+y) 4 5 6 a1 7 a2 8 a3 9 b1 10 b2 11 b3 12 c1 13 c2 14 c3 15 >>>
break 语句
作用:用于循环语句(while , for)中, 用来阻止当前循环语句的执行。
说明: break语句通常和if语句组合使用; 当break语句执行后,此循环语句break之后的语句将不再执行;
break语句终止循环时,循环语句的else子句 将不再执行; break语句只能终止当前循环语句的执行,如有循环嵌套时,不会跳出外重循环;
1 i = 0 2 while i < 5: 3 print(i) 4 i += 1 5 else: 6 print("while程序结束") 7 >>> ================================ RESTART ================================ 8 >>> 9 0 10 1 11 2 12 3 13 4 14 while程序结束 15 >>>
#!/usr/bin/python i = 0 while i < 5: break print(i) i += 1 else: print("while程序结束") print("程序结束") >>> ================================ RESTART ================================ >>> 程序结束 >>> #当while循环遇到break语句时,else语句将不执行
1 #!/usr/bin/python 2 3 i = 0 4 while i < 5: 5 print(i) 6 if i >= 3: 7 break 8 i += 1 9 else: 10 print("while程序结束") 11 print("程序结束") 12 >>> ================================ RESTART ================================ 13 >>> 14 0 15 1 16 2 17 3 18 程序结束 19 #当while循环中i =3时,break 语句执行,整个while语句停止
嵌套循环,break语句
1 i = 0 2 while i < 2: 3 j= 5 4 while (j < 50): 5 print(i,j) 6 if j >= 10: 7 break 8 j += 1 9 i += 1 10 else: 11 print("while程序结束") 12 print("程序结束") 13 >>> ================================ RESTART ================================ 14 >>> 15 0 5 16 0 6 17 0 7 18 0 8 19 0 9 20 0 10 21 1 5 22 1 6 23 1 7 24 1 8 25 1 9 26 1 10 27 while程序结束 28 程序结束 29 >>>
for循环,break语句
#!/usr/bin/python for x in range(100): if x >= 3: break print(x, end=' ') else: print("for程序结束") print("程序结束") >>> ================================ RESTART ================================ >>> 0 1 2 程序结束 >>>
for循环,break语句2
1 #!/usr/bin/python 2 3 for x in range(100): 4 if x >= 3: 5 break 6 print(x, end=' ') 7 if x >= 5: 8 break 9 print(x, end=' ') 10 else: 11 print("for程序结束") 12 print("程序结束") 13 >>> ================================ RESTART ================================ 14 >>> 15 0 0 1 1 2 2 程序结束 16 >>>
continue 语句
作用:用于(while,for)循环语句中,不再执行本次循环内continue 之后的语句,重新开始一次新的循环;
例子:
1 #!/usr/bin/python 2 3 for x in range(10): 4 #如果是奇数则跳过打印 5 if x % 2 == 1: 6 continue 7 print(x, end=' ') #x 一定是偶数 8 else: 9 print("打印结束") 10 print("程序结束") 11 >>> ================================ RESTART ================================ 12 >>> 13 0 2 4 6 8 打印结束 14 程序结束 15 >>>
1 x = 0 2 while x < 10: 3 if x % 2 == 1:#奇数 4 x += 1 5 continue 6 print(x, end=' ') 7 x += 1 8 9 print("程序结束") 10 >>> ================================ RESTART ================================ 11 >>> 12 0 2 4 6 8 程序结束 13 >>>
说明: 在while语句中,执行continue 语句,会直接跳转的while语句真值表达式处重新判断循环条件;
在for语句中,执行continue 语句,将会从可迭代对象中移向下一个元素再次进行循环;
死循环
死循环是指循环条件一直成立的循环;死循环通常用break语句来终止;死循环的else子句永远不会执行;(死循环存在于while循环语句中,for循环不存在)
1 #!/usr/bin/python 2 3 while True: 4 pass 5 6 print("程序结束")
1 #!/usr/bin/python 2 i = 0 3 while True: 4 if i % 2 == 1: 5 i += 1 6 continue #死循环 7 if i > 10: 8 break #满足条件后,break结束循环 9 print(i) 10 i += 1 11 12 13 print("程序结束") 14 >>> ================================ RESTART ================================ 15 >>> 16 0 17 2 18 4 19 6 20 8 21 10 22 程序结束 23 >>>
Python的基本核心数据类型是数字类型,字符串类型,布尔类型,None
True为布尔类型,其值不是0或1,同时也是个关键词
练习:
1 #!/usr/bin/python 2 3 for i in range(1,21): 4 print(i,end=' ') 5 >>> ================================ RESTART ================================ 6 >>> 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 8 >>>
1 #!/usr/bin/python 2 3 4 for i in range(1,6): 5 print(i, end=' ') 6 print() 7 for i in range(6,11): 8 print(i, end=' ') 9 print() 10 for i in range(11,16): 11 print(i, end=' ') 12 print() 13 for i in range(16,21): 14 print(i, end=' ') 15 print() 16 >>> ================================ RESTART ================================ 17 >>> 18 1 2 3 4 5 19 6 7 8 9 10 20 11 12 13 14 15 21 16 17 18 19 20 22 >>>