If conditions

Important for the if conditions and for the for loop is to have a colon after the statement and then have everything in this block indented. So do no introduce additional indentations, this will cause an error.

# Defining an if condition, this could also be used to compare two variables

a = 2
if a > 10 and a <= 50:
    print("a is large")
elif a > 50:
    print("a is very large")
else:
    print("a is small")

Here is a table of boolean operation that Python supports: (All of them support short circuit)

And, Or

a

b

a and b

a or b

False

False

False

False

False

True

False

True

True

False

False

True

True

True

True

True

Any, All

l

any(l)

all(l)

[]

False

False

[False]

False

False

[True]

True

True

[False, True]

True

False

[True, False]

True

False

[True, True]

True

True

[False, False, True]

True

False

[True, True, True]

True

True

[…, True, …]

True

False

[…, False, …]

True

False

For loops and if conditions

Now let’s combine a for loop and if condition to solve the following exercise.

Attention

Exercise 3.1:

  • Create a vector including numbers from 1 to 9.

  • Using a for loop to iterate over each of these numbers.

  • If the number is odd (which you can check using the % (modulus) operator) then add a 1 to the number and print the number.

  • If the number is even then simply print this number.