Web Analytics

Control Flow

Beginner ~12 min read

If...Else Statements

Python supports the usual logical conditions from mathematics. These can be used in if statements.

Note: Python uses indentation (whitespace) to define scope, instead of curly brackets.

a = 33
b = 200
if b > a:
    print("b is greater than a")
elif a == b:
    print("a and b are equal")
else:
    print("a is greater than b")

Loops

While Loop

With the while loop we can execute a set of statements as long as a condition is true.

For Loop

A for loop is used for iterating over a sequence (like a list, tuple, dictionary, set, or string).

Output
Click Run to execute your code

Range Function

To loop through a set of code a specified number of times, we can use the range() function.

  • range(6) generates 0 to 5.
  • range(2, 6) generates 2 to 5.
  • range(2, 30, 3) generates 2, 5, 8, ... (step 3).