[016] Python Basics: Loops, Iterables

Koay Yong Cett
5 min readOct 6, 2020
Photo by Jeremy Perkins on Unsplash

When it comes to our machines the concept of loops and looping is really powerful. With logical operators and conditional logic, we are able to skip lines in our program. Then, we don’t need to go one by one through the lines.

However, loops do a interesting things which allows us to run lines of code over and over. It is really powerful because that mean we can run codes thousand or millions of times. This is where machine excel at performing small tasks over and over with high speed that is way better than human.

👉 Therefore, loops are one of the most powerful features of programming languages and how do we use loops? Well, it is as simple as using the keywords for which we called as for loops.

🌟This is what we called for loops and they are kind of like conditional operators which we have the if.

🌟The item in the figure is a variable that we create and named it what ever we want. A variable is created here for each item after the ‘in’.

🌟It is something like for every item in basket do something. We called the basket as iterable which is something that are able to get looped over.

🌟For instance, we print item here and it prints each item in the iterable (item). Then, every letter goes into every book shelf in our machine’s memory and prints each item one at a time.

🌟This also works with lists. As you can see the list([1,2,3,4,5]) is an iterable that we are able to iterate over it and grabs each items in a lists.

🌟Well, this works with set as well.

🌟It is still working when the tuple is used.

👉For loops allow us to iterate over anything that has a collection of items. in this case, we are looping 5 times.

🌟I can print item as many time as possible as long as I have identation in front of the print(item) or line of code.

🌟As soon as I open up a new line and print something else. Well, I only get that once because it’s not in the loop. Here, it will finish with the loop first and then only print the single line we added.

🌟Then, we print out the item outside of the loop. There is four 5 printed whereas other number printed three times. This is because the last print at the very end is 5 as the at loop ends the value of item is 5.

🌟We can nest things in Python such as in the loops and if staement.

🌟Here, you can see that we are printing 1 a, 1 b, 1 c and so on. This is because we run line 1 item is currentlty set as 1 and then we are going to line 2 which print out the a, b and c. Only the line 2 is done , then the item will be set to 2.

👉Thus, we can have nested loops over and over as well. Maybe not completely obivious now that loops are useful. However, you will noticed it after you apply it.

Iterables:

🌟Iterables simply mean it is an object or a collection that can be iterated over. An iterable can be a list, dictionary, tuple and set which is a collection of item. We also know that string could be iterable.

🌟Why are they is iterable because they can be iterated which means that we can go through one by one to check them in the collection.

👉So iterable is the noun and iterated is the action of iterating over the iterable. This is quite confusing.

🌟We saw how lists, topples, sets and strings can be iterated but we haven’t look into dictionary yet.

🌟Let’s look into dictionary and say we have an object here and this object will be user and user will have few things inside.

🌟Here, we printed the keys of the dictionary. Hmm Now we have the keys that is great but what if I wanted the values of the keys as well.

🌟Well, we have few methods of printing out the values. The first method is items and we will be able to get the values. This is a very common pattern that you all will see alot of.

🌟Another method is values and it gives us the values of dictionary.

🌟The last method will be the keys which print out the keys.

👉This three methods are very common and be used alot in programming. This allows us to iterate over dictionary.

🌟Using the key and value we are able to print out the key and value in dictionary seperately. This is a very common pattern and these can be any variables that you want and name them whatever that helps your code be more readable.

🌟Here, we have an error and it make sense because integer is not iterable over and not a collection of item.

Thanks for reading 😃

Note: This is based on my understanding and please refer to the documentation online for the detail information needed. Thanks

--

--

Koay Yong Cett

A Bachelor CS student with major in Network Security (UniSZa). Every stories I shared is based on my personal opinion. Thanks you. Having my Internship now.