đź› ď¸Ź Scheduled Maintenance | Weâ€™ll be undergoing scheduled maintenance and upgrades between 00:00 PST Jan 26th until 00:00 PST Jan 28th. There may be brief interruption of services in that period. We apologize for the inconvenience.

×

03:05

# Conditionals and Loops in Python: Learn how to use them together

03:05
/ Conditionals and Loops in Python: Learn how to use them together

## Conditional

Letâ€™s see how to combine conditionals and loops in Python.

To print all the values from 2 to the power of 0, 2 to the power of 1, and so on, until 2 to the power of 9, we can use the following code: for â€śnâ€ť in range of 10, print â€ś2 double star nâ€ť. I will also have to insert a comma, because I would like to see the output on a single line.

I guess you can agree it was not necessary to specify the name of a list that exists in our code â€“ using a list created through the â€śrangeâ€ť function is going to work too!

Now, letâ€™s be brave and create an iteration that includes a conditional in the loop body. We can tell the computer to print all the even values between 0 and 19 and state â€śOddâ€ť in the places where we have odd numbers.

Letâ€™s translate this into computational steps and learn how to combine conditionals and loops in Python.

If x leaves a remainder of 0 when divided by 2, which is the same as to say â€śif x is evenâ€ť, then print x on the same line. â€śElseâ€ť, which means unless x is even, or if x is odd, print â€śOddâ€ť.

Lovely! This is an example of a combination of an iteration and a conditional in Python.

There are two main ways to program a loop, and until this moment, we paid attention only to the first one.

We have a list x that contains the numbers 0, 1, and 2. We saw we can print out each of its elements by typing â€śfor each item in the x list, print out that itemâ€ť.

The second way finds its practical application in more sophisticated codes. Its structure takes advantage of the range and len functions in the following way: â€śfor each item in a range that goes through the elements from the list x, that is â€ślen with an argument xâ€ť, print out each itemâ€ť.

If we do this, the variable item will loop through a new list created by range, and that has as many elements as the x list itself.

Please note that, in this situation, the second line of our code needs indexing to extract each item from the â€śxâ€ť list.

In practice, we will print out the element at position 0 from the list x, then the element at position 1, and finally the element at position 2.

To conclude, both approaches can lead to the same outcome. Although the second one looks unnecessarily complicated, in advanced coding, it might turn out to be a lot more useful. So, it is important you know both.

Hope that you liked our lesson on how to combine conditionals and loops in Python.

Eager to learn more? Check out our 365 Data Science Training. It covers everything you need from Mathematics and Statistics, through R, Python, and SQL, to Machine learning and Deep Learning (with TensorFlow).

Next Video: An example of skewness

## Earn your Data Science Degree

Expert instructions, unmatched support and a verified certificate upon completion!