Conditionals and Looping Statements (Python)

Imagine you are assigned to do a task of painting a toy in the long process of producing a toy. All you have to do is paint it in a single color. It would be really tiring and boring for you to do this task repeatedly every single day, correct? What if you could have a robot do it for you? All you have to do is program the robot by telling it what needs to be done, and how many times?

Similarly, in Python, if you are having to do a single task a number of times, you can simply use looping statements where you just have to tell the computer what to do, and how many times. There are two types of looping statements: A for loop and a while loop. Let’s look at a for loop first:

What is a for loop?

A for loop is a type of loop that is used when you want to repeat a certain code for a certain number of times. Here’s an example:

In pseudocode, the example above basically starts off with defining str. The next line of code says for every character, or index, in the defined variable, change the case from lower case to upper case. After every character has been changed, print every character. This code runs until the interpreter has gone through every single character of the str variable. Let’s look at another example!

In the example above, the overall goal was to add all the elements in the list. We used the looping statement to tell the computer that every number it runs through has to be added to the counter, 0. The “sum = sum + x” is where the sum counter is updated with a new value every time a number is added. Now, what if we want the computer to run in a loop until it reaches a certain boundary?

This is where the while loop comes into play.

A while loop is a type of loop that keeps running until it reaches a given restriction.

Example 1:

Just for a basic understanding of the while loop, this is a very simple example. First, I defined a variable. While the number in the variable is less than 10, print the number. In the last line, I told the computer to add 1 to the counter every time it is less than 10.

Example 2:

In this example, the goal was for the interpreter to keep asking the user to enter a number until the user types stop.

Conditionals in Python:

Conditionals are used when you want a certain output to occur only when a certain condition is met. If the condition is not met, then you will not get an output. A conditional in Python is usually in an if-else format. If a certain condition is met, then you do this. Else, if the condition is not true, then do this.

The example above is basically saying that if the two numbers that were previously defined are not equal, print that they are not equal. Else, print that they are equal. In this situation, they could either be “this” or “that”. In some cases, you could have more than two options. That is when you use the elif conditional.

In this example, the number can either be a positive or negative, but also it could be 0. So, the elif comes in handy!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store