Configuration

Night mode

Review

Learn Python Essentials for Data Science Course

Engineers From These Top Companies and Universities Trust EXLskills

1M+ Professionals | 100+ Institutions

This is the EXLskills free and open-source Python Introduction Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Python, to a moderate level of essential coding proficiency.

Is this course FREE?

Yes, this a 100% free course that you can contribute to on GitHub here!

Have more questions?

Complex_Python

Review

For loops with logic

We have already discussed iterables and logic statements, and even had one example with both in it. Now are are going to take a closer look at this and see how we can put this together in a stronger fashion.

Recall:

We can put multiple pieces of data into an Iterable called a list and then create a for loop that will iterate through each value in the list

We can create if statements based the data that we are iterating through.

With this is mind, we can use these two functions together and create more complicated expressions. One example of this is that you have already seen is:

Lets consider a situation where we want to take a list for each number in the list, if it is divisible by three then we want to add it to the sum. Otherwise, we will want to move onto the next number.

lst = [3,10,33,14]
#Initialize the sum
s = 0
#Iterate through all the numbers in the list
for number in lst:
#if the number meets our condition, add it to the sum
if (number % 3) == 0:
s += number
# Print out the sum after the loop
print(s)

As we move on to more complex topics, you'll want to remember that it is all based on the basics you are learning right now.