Basic introduction to python (10) loop statement

catalogue

1, If... Else statement

I if statement

II indent

III elif statement

IV else statement

4.1 basic else

4.2) and statement

4.3) or statement

4.4) nested if statements

4.4) pass statement

2, while loop statement

I Basic understanding

II Interrupt statement

III continue statement

IV else statement

3, for loop statement

I Basic traversal

II Traversal string

III Interrupt statement

IV continue statement

V range() function

1, If... Else statement

Python supports common logical conditions in Mathematics:

  • Equal to: a == b
  • Not equal to: a= b
  • Less than: a < B
  • Less than or equal to: a < = b
  • Greater than: a > b
  • Greater than or equal to: a > = b

I if statement

a = 33
b = 200
if b > a:
  print("b greater than a")

return:

Note: if should be followed by a colon
In this example, we use two variables A and b as part of the if statement to test whether b is greater than a. Since a is 33 and b is 200, we know that 200 is greater than 33, so we print "b is greater than a" on the screen.

II indent

Python relies on indentation (space at the beginning of a line) to define the scope in code. For this reason, other programming languages usually use curly braces.
If statement, no indentation (will cause an error):

III elif statement

If the previous condition is incorrect, try this condition.
For example:

a = 33
b = 33
if b > a:
  print("b greater than a")
elif a == b:
  print("a be equal to b")

return:

In this example, a is equal to b, so the first condition is not true, but elif condition is true, so we print to the screen "a and b are equal"

IV else statement

4.1 basic else

Capture any other keywords that do not pass the previous conditions.

a = 200
b = 33
if b > a:
  print("b greater than a")
elif a == b:
  print("a be equal to b")
else:
  print("a less than b")

return:

In this example, a is greater than b, so the first condition is not true and elif condition is not true, so we go to else condition and print to the screen "a is less than b".

You can also have an else without an elif:

4.2) and statement

Test if a is greater than b and if c is greater than a:

a = 200
b = 33
c = 500
if a > b and c > a:
  print("Both conditions are met")

Output:

 

4.3) or statement

Test if a is greater than b, or if a is greater than c:

4.4) nested if statements

x = 41
if x > 10:
  print("Greater than 10")
  if x > 20:
    print("Also greater than 20")
  else:
    print("Not more than 20")

return:

 

4.4) pass statement

The if statement cannot be empty, but if you have an IF statement without content for some reason, please put it in the pass statement to avoid errors.

a = 33
b = 200

if b > a:
  pass

Result no output

2, while loop statement

I Basic understanding

Using the while loop, we can execute a set of statements as long as the condition is true.
For example, print i as long as i is less than 6

i = 1
while i < 6:
  print(i)
  i += 1

Return to:

Note: remember to limit i, otherwise the cycle will last forever.

II Interrupt statement

Using the break statement, we can stop the loop even if the while condition is true:
For example, when i is 3, exit the loop:

i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1

return:

III continue statement

Using the continue statement, we can stop the current iteration and continue to the next:

i = 0
while i < 6:
  i += 1
  if i == 3:
    continue
  print(i)

Return: can not print 3

IV else statement

Using else statement, when the condition is no longer true, we can run the code block once:
Once the condition is false, print a message:

i = 1
while i < 6:
  print(i)
  i += 1
else:
  print("i Greater than or equal to 6")

return:

  

3, for loop statement

I Basic traversal

Using the for loop, we can execute a set of statements once for each item in a list, tuple, collection, etc.
For example, print each fruit in the fruit list

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

return:

 

II Traversal string

For example, loop through the letters in the word "banana":

for x in "banana":
  print(x)

III Interrupt statement

Using the break statement, we can stop the loop before it traverses all items
For example, when x is "banana", exit the loop:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
  if x == "banana":
    break

return:

Exit the loop when x is "banana", but this interrupt occurs before printing:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    break
  print(x)

Only applr:

IV continue statement

Using the continue statement, we can stop the current iteration of the loop and continue to the next:
Do not print banana:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    continue
  print(x)

return:

 

V range() function

To cycle a set of codes for the specified number of times, we can use the range() function, and the range() function returns a number of 1, starting from 0 by default and increasing in order (default) and ending at the specified number of times.
For example:

for x in range(6):
  print(x)

return:

Note that range(6) is not a value from 0 to 6, but a value from 0 to 5.
The range function is 1 to the increment sequence by default, but it is possible to specify the increment value by adding the third parameter: range (2, 30, 3)

Vi Nested loop

A nested loop is a loop within a loop. Inner loop executes once in each iteration of outer loop.
Print each adjective for each fruit:

adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]

for x in adj:
  for y in fruits:
    print(x, y)

Return:

VII pass statement

The for loop cannot be empty, but if for some reason there is a loop with no content for, please put in the pass statement to avoid errors.

for x in [0, 1, 2]:
  pass

No output result

Keywords: Python Back-end

Added by TashaAT on Wed, 29 Dec 2021 23:39:05 +0200