Conditional statements and iteration

Python supports several different “programming paradigms“. A paradigm simply dictates how the code is written. The one you’re likely most familiar with is the “imperative programming paradigm“. Lets’ start there.

In imperative programming, our program maintains something called a state. A state is essentially a snapshot of our program at some arbitrary moment in time, and simply describes the values stored in each of our variables.  In imperative programming, we make use of conditional statements and iteration (loops) to solve problems. If it sounds confusing right now, not to worry, it’ll become more clear with examples.

Conditional Statements

Conditional statements are something you’re already familiar with if you’ve previously programmed in another programming language. As the name suggests, you are checking the verity of a condition, and based on it being true or false performing a different task.

The syntax for this in Python is the following.

if <condition == True>:
    <expression 1>
else:
    <expression 2>

Putting it into practice, we can check whether an input is greater than zero, and print a corresponding message.

x = int(input("Enter a number: "))

if x > 0:
    print("{} is larger than zero!".format(x))
else:
    print("{} is less than zero!".format(x))

This can be applied to any scenario. Try the following example.

1) check if an entered number is even, if it is, print it out. Otherwise, print out, “not even”.
Solution below.

x = int(input("Enter a number: "))

if x % 2 == 0:
 print(x)
else:
 print("Not even")

Iteration

A second construct we frequently employ in imperative programming is known as iteration. Any time we wish to repeat the same operation multiple times, we perform an iteration. For example, say we want to sum all the digit from one to ten.

The first one we’ll look at is the while loop.

while(<condition> == True):
    #do something

The code we were to replace the comment “do something” is called our loop body. This code is executed while our condition remains true. We check the condition at each new iteration, prior to entering the loop body.
To complete the above example (summing up digits 1 to 10) we can do the following.

i = 0
sum = 0
while(i <= 10):
    sum = sum + i
    i = i + 1

Next, let’s look at the for loop.
Before we look at that in more detail, it’s worthwhile to look at a Python function called range().
We call this function like so.

range(a,b,c)

This produces a range of numbers from a to b (not including b) with an increment of c.
If we omit the a and c, simply writing range(b) — as we often will — it defaults them to zero and just produces a range of numbers between 0 and b, not including b.

Alright, back to the for loop.
The best way to show this is by example. Let’s take this while loop we saw above, and rewrite it using a for loop.

i = 0
sum = 0
while(i <= 10):
 sum = sum + i
 i = i + 1

Now as a for loop. Watch how we use our newly learned range function.

Unlike with the while loop, the loop counter — i — is never explicitly declared and initialized. It’s only created as part of our iteration to cycle through the range zero to ten; outside the loop, i doesn’t exist.

Here are a few examples.

1) Iterate through the digits zero to one-hundred, and print the square of every number that is even.

Solution:

 

for i in range(100):
 if i % 2 == 0:
     print(i*i)

2) Find the combined sum of all odd numbers between 10 and 100.

Solution.

sum = 0
for i in range(10,100):
   if i % 2 != 0:
   sum = sum + i

3) What is the output of the following?

for i in range(2,11,2):
	if i > 3:
		print(i)
	else:
		print(i+50)

Solution:
2
54
56
58
60