Python学习笔记二_if 条件语句、for循环语句、while 循环语句

条件语句

if 条件语句

定义

if语句用来做判断,如果if条件成立就会执行下一条相对应的语句
True:指任何非0和非空值
False:指0或者空值

  1. 单一条件判断

语法格式:

if 判断条件:
    执行语句……
else:
    执行语句……

举例说明:

age = 15
print('我是分界线')
#if语句以外的不受印象
if age >= 18:     #False
	print('成年了')
else:
	print('我还小')

输出结果:

我是分界线
我还小
[Finished in 0.1s]

age = 20
if age >= 18: # True
	print('成年了')
else:
	print('我还是个孩纸')

输出结果:

成年了
[Finished in 0.0s]

  1. 多条件判断 if - elif -elif -else

语法格式:

if 判断条件1:
    执行语句1……
elif 判断条件2:
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
    执行语句4……

举例说明:

a = 3
if a == 1 :
	print('a = 1')
elif a == 2:
	print('a = 2')
elif a==3:
	print('a = 3')
else:
	print('X')

输出结果:

a = 3
[Finished in 0.0s]

循环语句

循环就是条件成立的时候按照循环次数循环执行,反之则结束循环
循环重要三要素:顺序执行,选择执行,循环次数

循环类型:

循环 描述
for 循环 重复执行语句
while 循环 判断条件为 true 时执行循环,否则退出循环
嵌套循环 在一个循环中嵌套另一个循环

for 循环语句

for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
语法格式

for iterating_var in sequence:
	statements(s)
  1. 举例说明(遍历字符串):
for letter in 'Python3.7':
   print ('当前字母 :', letter)

输出结果:

当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
当前字母 : 3
当前字母 : .
当前字母 : 7
[Finished in 0.0s]

  1. 举例说明(遍历列表)
fruits = ['banana','apple','mango']
for fruit in fruits:        
	print ('当前水果 :', fruit)

输出结果:

当前水果 : banana
当前水果 : apple
当前水果 : mango
[Finished in 0.0s]

  1. 举例说明(序列索引)
for x in range(1,6):  
	print(x)
print('循环结束')

输出结果:

1
2
3
4
5
循环结束
[Finished in 0.0s]

range() 返回一个可迭代的对象 range对象
range(起始值,结束数据,步长) 步长是一个有默认值的 1
range包含起始值,不包含结束数据 [起始值,结束值 )

  1. 举例说明(通过索引遍历列表)
fruits = ['banana', 'apple',  'mango','orange']
for fruit in range(len(fruits)):  #fruit,0,1,2,3
   print ('当前水果 :', fruits[fruit])

输出结果:

当前水果 : banana
当前水果 : apple
当前水果 : mango
当前水果 : orange
[Finished in 0.0s]

len() 返回列表的长度,即元素的个数

while 循环语句

while 语句用于循环执行程序,在某条件下,循环执行某段程序,

语法格式

while 判断条件:
	执行语句……
  1. 举例说明(死循环):
    如果条件判断语句永远为 true,循环将会无限的执行下去
#死循环
num = 1  
while num <= 5:
	print(num)

注意:以上的无限循环,请中断

2.举例说明(增加结束循环的条件)

num = 1  
while num <= 5:
	print(num)
	num += 1

输出结果:

1
2
3
4
5
[Finished in 0.0s]

嵌套循环

for 循环语法格式:

for iterating_var in sequence:
	for iterating_var in sequence:
		statements(s)
	statements(s)

while 循环语法格式:

while expression:
	while expression:
		statement(s)
	statement(s)

可以在循环体内嵌入其他的循环体,如在while循环中可以嵌入for循环, 反之,你可以在for循环中嵌入while循环

  1. 举例说明(九九乘法表 for 循环):
for x  in range(1,10):
   for y in range(1,10):
      if x < y:
         continue
      print('%d * %d = %-2d'%(x,y,x * y),end=" | ")

   print('\n')
  1. 举例说明(九九乘法表 while 循环):
x = 1
while x <= 9:
   y = 1
   while y <= x:
      print('%d * %d = %-2d' % (x, y, x * y), end=" | ")
      y += 1
   x += 1

   print('\n')

结果输出:

1 * 1 = 1  | 

2 * 1 = 2  | 2 * 2 = 4  | 

3 * 1 = 3  | 3 * 2 = 6  | 3 * 3 = 9  | 

4 * 1 = 4  | 4 * 2 = 8  | 4 * 3 = 12 | 4 * 4 = 16 | 

5 * 1 = 5  | 5 * 2 = 10 | 5 * 3 = 15 | 5 * 4 = 20 | 5 * 5 = 25 | 

6 * 1 = 6  | 6 * 2 = 12 | 6 * 3 = 18 | 6 * 4 = 24 | 6 * 5 = 30 | 6 * 6 = 36 | 

7 * 1 = 7  | 7 * 2 = 14 | 7 * 3 = 21 | 7 * 4 = 28 | 7 * 5 = 35 | 7 * 6 = 42 | 7 * 7 = 49 | 

8 * 1 = 8  | 8 * 2 = 16 | 8 * 3 = 24 | 8 * 4 = 32 | 8 * 5 = 40 | 8 * 6 = 48 | 8 * 7 = 56 | 8 * 8 = 64 | 

9 * 1 = 9  | 9 * 2 = 18 | 9 * 3 = 27 | 9 * 4 = 36 | 9 * 5 = 45 | 9 * 6 = 54 | 9 * 7 = 63 | 9 * 8 = 72 | 9 * 9 = 81 | 


Process finished with exit code 0

循环控制语句(配合着循环使用,不能单独使用)

循环 描述
break 在语句块执行过程中终止循环,并且跳出整个循环
continue 在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环。
pass pass是空语句,是为了保持程序结构的完整性

1.举例说明(break)

for letter in 'Python3.7':
   if letter == '3':
      break
   print ('当前字母 :', letter)
print('结束')

输出结果:循环终止,3.7没有输出

当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
结束
Process finished with exit code 0

2.举例说明(continue)

for letter in 'Python3.7':
   if letter == '3':
      continue
   print ('当前字母 :', letter)
print('结束')

输出结果:结束当前循环,没有3

当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
当前字母 : .
当前字母 : 7
结束
Process finished with exit code 0

3.举例说明(pass)

for letter in 'Python3.7':
   if letter == '3':
      pass
   print ('当前字母 :', letter)
print('结束')

输出结果:没有什么作用,为了结构的完整性

当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
当前字母 : 3
当前字母 : .
当前字母 : 7
结束
Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/weixin_43817580/article/details/86623231