Explanation of Python condition and loop statement

  • 2021-12-04 10:47:43
  • OfStack

Directory 1, Python Conditional Statement 1.1 pass Statement 2, Python for Loop Statement 2.1 for Nested Loop 3, Python while Loop Statement 3.1 while Loop Nested 4, break Statement 5, continue Statement Summary

1. Python conditional statement

An Python conditional statement is a block of code that determines execution by the execution results of one or more statements. In Python programming, if statement is used to control the execution of program. Python does not support the switch statement, so when there are multiple conditional decisions, you can only use elif for programming. The basic form of the if statement is:


if ( Conditional expression ):
	 Conditional statement 
elif ( Additional conditions ):
	 Conditional statement 
else:
	 Conditional statement 

Example:


a = 1
if type(a) == int:		#  Judge  a  Is it plastic 
	print(' It's plastic surgery ')		#  If  a  Is shaping, executing the conditional statement 
elif type == float:		#  Judge  a  Whether it is floating point type 
	print(' Is a floating-point type ')	#  If  a  Is a floating-point type, execute the conditional statement 
else:					#  Any type except shaped floating point type 
	print(' Ha ha ')		#  Execute the conditional statement 
#  Output result: for shaping 

1.1 pass Statement

The if statement cannot be empty, and you don't want to execute anything in the statement block after the colon. You can use the pass statement to avoid errors. Example:


a = 0
if a == 0:
	pass
else:
	print('hello')
print('end')
#  Output result   : end

2. Python for Loop Statement

The for loop is used to traverse any sequence of items, such as strings or lists. The for loop judges one condition at a time. Example of dictionary loop traversal:


person = {"name":"mj","age":31,"hobby":"dance"}
#  Get all of the dictionary  key value  Value 
for i,v in person.items():
    print(i)
    print(v)

# Output:
name
mj
age
31
hobby
dance

2.1 for nested loop

A nested loop is a loop within a loop, and the inner loop is executed once every time the outer loop iterates. Example:


#  Use for  Cyclic printing 99 Multiplication table 
for i in range(1,10):
    for j in range(1,i+1):
        print("{}*{}={}".format(j,i,i*j),end=' ')
    print()
"""

# Output:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
"""

3. Python while Loop Statement

The while loop execution statement can be a single statement or a block of statements. As long as the condition is true, we can execute 1 group of statements. If the conditional statement is always true, the loop will execute indefinitely, for example:


while (1):
	print(' Miss ')

Output:
Miss
Miss
Miss
...
"""

3.1 while Loop Nesting

Print a 99 multiplication table instance using the while statement:


i =1
while (i<=9):
    j=1
    while (j<=i):
        print("{}*{}={}".format(j,i,i*j),end=' ')
        j+=1
    print()
    i+=1

"""
# Output:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
"""

4. break statement

The break statement is used in the for and while loop statements to terminate the loop. Example:


# for  Loop statement :
list = ['for','while','else','break','continue']
for x in list:
    if x == 'while':
        break
    print(x)
#  Output: for

# while  Loop statement: 
i = 0
while (i<10):
    print('happy')
    i += 1
    if i == 2:
        break

# Output:
happy
happy

5. continue statement

The continue statement jumps out of this loop, and break jumps out of the entire loop. That is, continue is the remaining statement that skips the current loop and proceeds to the next loop. Example:


#  Do not print continue
list = ['for','while','else','break','continue']
for b in list:
    if b == 'while':
        continue
    print(b)

# Output:
for
else
break
continue

You can use the continue statement to skip some loops, such as I want to print odd numbers between 0 and 10:


n = 0
while (n<10):
    n += 1
    if n%2==0:
        continue
    print(n)

# Output:
1
3
5
7
9

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: