Python之路,第五篇:Python入门与基础5

时间:2022-10-19 15:00:55

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  输入三行文字,让三行文字在一个方框内居中显示(输入不要输入中文);

 #!/usr/bin/python

 #输入3行文字
line1 = input("请输入第一行字符:")
line2 = input("请输入第二行字符:")
line3 = input("请输入第三行字符:") #比较出3行中最大的一行字符长度
m = max(len(line1), len(line2), len(line3)) #使3行输出的文字居中
print('+' + '-' * ( m + 2 ) + '+')
print('| ' + line1.center(m) + ' |')
print('| ' + line2.center(m) + ' |')
print('| ' + line3.center(m) + ' |')
print('+' + '-' * ( m + 2 ) + '+')
>>> ================================ RESTART ================================
>>>
请输入第一行字符:aaaaaaaaaaaaa
请输入第二行字符:aaaaaaaa
请输入第三行字符:aaaaaaaaaa
+---------------+
| aaaaaaaaaaaaa |
| aaaaaaaa |
| aaaaaaaaaa |
+---------------+
>>>

while 语句嵌套

语法:

while   a > b:

while  b > c:

......

else:

.......

 >>> i = 1
>>> while i < 10:
j = 1
while j < 10:
print("i =", i, "j =", j)
j += 1
i += 1 i = 1 j = 1
i = 1 j = 2
i = 1 j = 3
i = 1 j = 4
i = 1 j = 5
i = 1 j = 6
i = 1 j = 7
i = 1 j = 8
i = 1 j = 9
i = 2 j = 1
i = 2 j = 2
i = 2 j = 3
i = 2 j = 4
i = 2 j = 5
i = 2 j = 6
i = 2 j = 7
i = 2 j = 8
i = 2 j = 9
i = 3 j = 1
i = 3 j = 2
i = 3 j = 3
i = 3 j = 4
i = 3 j = 5
i = 3 j = 6
i = 3 j = 7
i = 3 j = 8
i = 3 j = 9
i = 4 j = 1
i = 4 j = 2
i = 4 j = 3
i = 4 j = 4
i = 4 j = 5
i = 4 j = 6
i = 4 j = 7
i = 4 j = 8
i = 4 j = 9
i = 5 j = 1
i = 5 j = 2
i = 5 j = 3
i = 5 j = 4
i = 5 j = 5
i = 5 j = 6
i = 5 j = 7
i = 5 j = 8
i = 5 j = 9
i = 6 j = 1
i = 6 j = 2
i = 6 j = 3
i = 6 j = 4
i = 6 j = 5
i = 6 j = 6
i = 6 j = 7
i = 6 j = 8
i = 6 j = 9
i = 7 j = 1
i = 7 j = 2
i = 7 j = 3
i = 7 j = 4
i = 7 j = 5
i = 7 j = 6
i = 7 j = 7
i = 7 j = 8
i = 7 j = 9
i = 8 j = 1
i = 8 j = 2
i = 8 j = 3
i = 8 j = 4
i = 8 j = 5
i = 8 j = 6
i = 8 j = 7
i = 8 j = 8
i = 8 j = 9
i = 9 j = 1
i = 9 j = 2
i = 9 j = 3
i = 9 j = 4
i = 9 j = 5
i = 9 j = 6
i = 9 j = 7
i = 9 j = 8
i = 9 j = 9
>>>

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’的个数,并打印

 #!/usr/bin/python

 s = input("请输入字符:")
print(s.count('a'))
>>> ================================ RESTART ================================
>>>
请输入字符:abcdefaa
3
>>>
 #!/usr/bin/python

 s = input("请输入字符:")
count = 0
for i in s:
if i == 'a':
count += 1
print("a的个数为:", count)
>>> ================================ RESTART ================================
>>>
请输入字符:abceaassevfaaazzz
a的个数为: 6
>>>

2   输入字符,打印出来成为一行每个字符有空格间隔;

 #!/usr/bin/python

 s = input("请输入字符:")
for i in s:
if i != 'a':
pass
else:
print(i, end=' ')
>>> ================================ RESTART ================================
>>>
请输入字符:abcdeaa
a a a
>>>

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
>>>
 #!/usr/bin/python

 for i in range(1,11,2):
print(i, end=' ')
print()
for i in range(2,11,2):
print(i, end=' ')
print()
>>> ================================ RESTART ================================
>>>
1 3 5 7 9
2 4 6 8 10
>>>

练习: 计算出100-1000之间的水仙花数(Narcissistic number),(水仙花数是指 百位的立方+ 十位的立方 + 个位的立方)

例如: 153 = 1**3  +  5 ** 3 + 3 **3

 #!/usr/bin/python

 for i in range(100,1000):
a = i // 100 #百位数
b = (i % 100) // 10 #十位数
c = i % 100 % 10
if i == a ** 3 + b ** 3 + c ** 3:
print(i, end=' ')
>>> ================================ RESTART ================================
>>>
153 370 371 407
>>>
 #!/usr/bin/python

 for i in range(100,1000):
s = str(i)
a = int(s[0]) #百位数
b = int(s[1]) #十位数
c = int(s[2])
if i == a ** 3 + b ** 3 + c ** 3:
print(i, end=' ')
print()
>>> ================================ RESTART ================================
>>>
153 370 371 407
>>>
 #!/usr/bin/python

 for a in range(1,10):
for b in range(0,10):
for c in range(0,10):
#print(a,b,c,end=' ')
n = a * 100 + b * 10 + c
if n == a ** 3 + b ** 3 + c ** 3:
print(n, end=' ')
>>> ================================ RESTART ================================
>>>
153 370 371 407
>>>

for 语句嵌套

例子: for  x in “abc”:

for  y in "123":

print(x+y)

 >>> for x in "abc":
for y in "":
print(x+y) a1
a2
a3
b1
b2
b3
c1
c2
c3
>>>

break 语句

作用:用于循环语句(while , for)中, 用来阻止当前循环语句的执行。

说明: break语句通常和if语句组合使用;   当break语句执行后,此循环语句break之后的语句将不再执行;

break语句终止循环时,循环语句的else子句 将不再执行;  break语句只能终止当前循环语句的执行,如有循环嵌套时,不会跳出外重循环;

 i = 0
while i < 5:
print(i)
i += 1
else:
print("while程序结束")
>>> ================================ RESTART ================================
>>>
0
1
2
3
4
while程序结束
>>>
#!/usr/bin/python

i = 0
while i < 5:
break
print(i)
i += 1
else:
print("while程序结束")
print("程序结束")
>>> ================================ RESTART ================================
>>>
程序结束
>>> #当while循环遇到break语句时,else语句将不执行
 #!/usr/bin/python

 i = 0
while i < 5:
print(i)
if i >= 3:
break
i += 1
else:
print("while程序结束")
print("程序结束")
>>> ================================ RESTART ================================
>>>
0
1
2
3
程序结束
#当while循环中i =3时,break 语句执行,整个while语句停止

    嵌套循环,break语句

 i = 0
while i < 2:
j= 5
while (j < 50):
print(i,j)
if j >= 10:
break
j += 1
i += 1
else:
print("while程序结束")
print("程序结束")
>>> ================================ RESTART ================================
>>>
0 5
0 6
0 7
0 8
0 9
0 10
1 5
1 6
1 7
1 8
1 9
1 10
while程序结束
程序结束
>>>

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

 #!/usr/bin/python

 for x in range(100):
if x >= 3:
break
print(x, end=' ')
if x >= 5:
break
print(x, end=' ')
else:
print("for程序结束")
print("程序结束")
>>> ================================ RESTART ================================
>>>
0 0 1 1 2 2 程序结束
>>>

continue 语句

作用:用于(while,for)循环语句中,不再执行本次循环内continue 之后的语句,重新开始一次新的循环;

例子:

 #!/usr/bin/python

 for x in range(10):
#如果是奇数则跳过打印
if x % 2 == 1:
continue
print(x, end=' ') #x 一定是偶数
else:
print("打印结束")
print("程序结束")
>>> ================================ RESTART ================================
>>>
0 2 4 6 8 打印结束
程序结束
>>>

for语句

 x = 0
while x < 10:
if x % 2 == 1:#奇数
x += 1
continue
print(x, end=' ')
x += 1 print("程序结束")
>>> ================================ RESTART ================================
>>>
0 2 4 6 8 程序结束
>>>

while语句

说明: 在while语句中,执行continue 语句,会直接跳转的while语句真值表达式处重新判断循环条件;

在for语句中,执行continue 语句,将会从可迭代对象中移向下一个元素再次进行循环;

死循环

死循环是指循环条件一直成立的循环;死循环通常用break语句来终止;死循环的else子句永远不会执行;(死循环存在于while循环语句中,for循环不存在)

 #!/usr/bin/python

 while True:
pass print("程序结束")

最简单是死循环

 #!/usr/bin/python
i = 0
while True:
if i % 2 == 1:
i += 1
continue #死循环
if i > 10:
break #满足条件后,break结束循环
print(i)
i += 1 print("程序结束")
>>> ================================ RESTART ================================
>>>
0
2
4
6
8
10
程序结束
>>>

break结束死循环

Python的基本核心数据类型是数字类型,字符串类型,布尔类型,None
True为布尔类型,其值不是0或1,同时也是个关键词

练习:

 #!/usr/bin/python

 for i in range(1,21):
print(i,end=' ')
>>> ================================ RESTART ================================
>>>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
>>>

练习1

 #!/usr/bin/python

 for i in range(1,6):
print(i, end=' ')
print()
for i in range(6,11):
print(i, end=' ')
print()
for i in range(11,16):
print(i, end=' ')
print()
for i in range(16,21):
print(i, end=' ')
print()
>>> ================================ RESTART ================================
>>>
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
>>>

练习2