Completed: / exercises

Control structures in Python

This material reviews Python's conditional and loop structures.

Conditional structures

Conditional structures allow the execution of a program to vary depending on the information given to it. In other words, conditional structures test the truth value of a given condition and the program acts accordingly.
if number < 10:
    print("The number is smaller than 10")
elif number < 100:
    print("The number is smaller than 100")
elif number < 1000:
    print("The number is smaller than 1000")
else:
    print("The number is equal to or greater than 1000")
Above, if the condition number < 10 is true, the text The number is smaller than 10 is printed on the screen and the conditional structure is exited.
Remember that in Python indentation is part of the syntax: it determines which lines belong to the same code block. Lines at the same indentation level belong to the same block. In this example, when the condition of the if statement is true, the indented code block associated with it is executed, and the same applies to the other branches.
The differences between programming languages are mostly syntactical. Conditional structures are based on an if branch and can also contain additional conditional branches and an optional final else branch. In Python, the additional conditional branches are written using elif. The branches are examined in the order in which they are written, and the first branch whose condition is true is selected.
Okay, so how is the truth value of a conditional expression determined?
Example. Here, the user is asked to choose between different calculations, and the selected operation is then performed. The else statement is used to indicate an error situation when none of the conditions are true.
choice = input("Make your choice: ").strip().lower()
if choice == "l" or choice == "length":
    length()
elif choice == "m" or choice == "mass":
    mass()
elif choice == "v" or choice == "volume":
    volume()
elif choice == "t" or choice == "temperature":
    temperature()
else:
    print("The operation you selected does not exist")

Conditional repetition

The familiar
while statement
repeats the code block inside the loop for as long as the
condition
is true. In Introduction to Programming, one of the most common uses of the while loop was checking different kinds of input and asking for the input again, because a while loop is best suited to situations where the number of repetitions is not known in advance.
Example.
a = 1
s = 0
print("Enter a number to add")
print("Addition is stopped with the number 0")
while a != 0:
    print("sum:", s)
    a = int(input("number to add: "))
    s = s + a
print("Total sum =", s)

Counted repetition

Counted repetition is implemented using a
for loop
, which iterates through an
iterable
object (for example, a list) or through a numbered range using the range() function.
flowers = ['Rose', 'Violet', 'Sunflower']
for flower in flowers:
    print(flower)
And:
for i in range(0, 10):
    print(i)
Here, i is a
loop variable
. If you remember where i and similar variables usually appeared in Introduction to Programming, you may remember the range loop: for i in range(0, 10) (the unnecessary 0 has been added to make the comparison easier), which iterates through the given numerical range.
The basic use of a general for loop in many other programming languages actually resembles this range loop quite closely.

Finally

Now we can move on to see how control structures are implemented in C. There will also be some new material, including new control structures..
?