Python Programming Tutorial 13

For Loops 

In this lesson, we will talk about for loops. For loops in Python, they work as sequence iterators. So let's create a list that we know is a sequence of elements. This list is going to be called blog_posts with some blog post titles:

blog_posts = ["The 10 coolest math functions in Python", "How to make HTTP requests in Python", "A tutorial about data types"]

Now, how could we print all the blog posts in that list using the same lines of code? It doesn't matter how many posts we have in a list. We can start a variable with a value of zero, then start a while loop, and use that variable as an index to print each blog post, but it is actually way easier to do this with a "for loop," and this is how it works:

we are going to write for, and then we are going to create a variable. Let's create one called post and then use the "in"" keyword and then the sequence we want to iterate over. So the first time we run this loop, "The 10 coolest math functions in Python,"  this value is going to be assigned to the post variable. The second time we are going to run this loop, this value "How to make HTTP requests in Python" is going to be assigned to the post variable. And the third time for the third one as well until the end of the list. And we don't have to control the number of iterations because the for loop is going to do that automatically for us. So in every iteration we are going to print the post, which is going to be each element of this list. Let's type the above information into a code and run it:

 





The list continues, however, my screen didn't display all so I gave the list earliar. Okay let us run it to see how it works:






We just printed the titles of our blog posts one by one. No matter how many blog posts we have, this is going to work. I hope you remember we learned about the break statement in the previous lesson. There is also the continue statement, which doesn't interrupt the loop but goes to the next iteration. So let's use it to avoid printing empty blog posts. Let's say for any reason we have some empty values at the beginning of the list and in the middle of the list. In this format: " ", " ",



Let me know if you have difficulty seeing any of the images. Alright let's run this and see what 

happens: 



 And now we have empty values printed as empty spaces. How can we avoid it?

We are going to check if our post is an empty string, and if it is, we are going to use the continue statement, which is going to jump out of the loop but is not going to interrupt the loop. It is just going to jump out of it and go to the next iteration. So when the post is empty it is not going to do anything. Afer that we will run and else statement to print the list. Let's visualise this and run to see if it is better.






Output: 





And it worked successfully. We didn't print the empty strings. Remember that you will get an error message if you use a single equal sign for the program. I explained the differences early in this course. Please refer to my previous tutorial if this feels new to you. 

Remember, a string is also a sequence, so we can also loop through all characters of a string. Let's create a string called myString and do a for loop to iterate over the string. So the variable name is going to be char, the sequence is going to be myString, and every iteration we are going to print a character.

 


 Okay so let's create a separator at the start of this code so this is organised:











let's run this and see:














We just iterated over a string. We can also define a specific number of iterations by using the range function:





















Which would be outputed as:























And by doing that we just printed 10 numbers. We can also use the for loop in dictionaries. So let's create a new dictionary called person.







We used the for loop and printed the key, the colon, and used the name of the dictionary and the key variable as the key. Let's print it to see how this is going to work:























This is the first part of the code and the continuation follows:























For this way we just listed all the elements of the dictionary. 

Just like conditionals loops can also be nested. We can use loops inside loops. We can also use loops inside conditionals, conditionals inside loops and so on.... So let's get the blog post variable we just posted. Remember to separate it....................




This is going to be a dictionary. We are going to change the brakets to curly brackets and add category-Python and assign to this key a list of blogposts. Then, let's add a new property to this dictionary, which is going to be let's say javascript, with a list of posts.


 continuation:



So this is how our dictionary is going to look like: 

print('-------------------------------------')

blog_posts = {"Python": ["The 10 coolest math functions in Python", "How to make HTTP requests in Python", "A tutorial about data types"], "Javascript": ["Namespaces in Javascript", "New functions available in ES6"]}

How can we print all the blog post by category?

we can do like: for category in blog_posts:

Everytime we use a for loop with a dictionary, every iterations the key: Python, is going to be assigned as the value of the variable category. So the first iteration of the loop Python is going to be assigned to the variable category. In this case we are going to print("Posts about", category). We are going to print post about the category which is python. Now we are going to start a new loop inside this loop.

for post in blog_posts[category] which is going to result in the first list- the 10 coolest..... by doing this we can just print(posts) and when it finishes it is going to start the first loop again. And then the next category is going to be Javascript . And then it is going to iterate over the list of Javascript. Let's see if this works:







This is how the code is going to look like. Let's run this code to see what happens:






























































And our code worked. Now we can see posts about Python. It lists all the posts. Posts about Javascript and the two posts about Javascript. Really cool, right? This might be a bit confusing at the beginning. So if you have any questions, just leave them in the comment section. This is all for this lesson, so I will see you in the next lesson.

 

No comments:

Post a Comment

Python Programming Tutorial 15

 Error Handling Anytime we work with user input, we should make sure not only the data is valid and within the range we want, like what we d...