[Python Basics] Working With Lists — Part 1

Photo by Lysander Yuen on Unsplash

We already know what the python list is and how to deal with it in the for loop. Do we?

For many of you, it is true. But there are still growing number of new generations who want to participate in this fascinating new world of machine learning, artificial intelligence, data science or whatever. We should provide them some materials to read, think and learn.

This post is an attempt to prepare teaching materials to my students whose major is not computer science, but who are willing to learn this new fascinating world.

Looping Over an Entire List

We often want to run over all entries in a list, performing the same task with each item. When we want to do this, we can use Python’s for loop.

my_string = "Hello, my Python!"  #1
my_list = list(my_string) #2
for item in my_list: #3
print(item.upper()) #4

At #1, we make a string. Then we make a list containing every letters, spaces and punctuation marks within the variable my_string at #2.

At #3 we define a for loop. This line tells Python to take an item in my_list and store it in the variable item. At #4, We tell Python to print the upper case letter in item. Python then repeats line #3 and #4, one for each item in the list.

A Closer Look at Looping

In some areas, computers perform a way better than human. The looping is one of them.

for item in my_list:

This code tells Python to retrieve the first element in the list my_list and store it in the variable named item. Note that the for statement always comes with in keyword in.

    print(item.upper())

Current value of variable item is ‘H’. This statement prints the upper case letter of the variable, which is the same as the original one. Now there are more elements in the list, Python goes back to the first line of the loop.

Doing More Work Within a for loop

Let’s build on the previous example by adding a new line. We can also include as many lines of code as we want in the for loop. Every indented line following the line for item in my_list is considered inside the loop, and each indented line is executed once for each value in the list. Therefore you can do as much work as you like with each value in the list.

my_string = "Hello, my Python!"  #1
my_list = list(my_string) #2
for item in my_list: #3
print(item.upper()) #4
print("processed") #5

Inside the for loop, #4 and #5 will be executed sequentially, and then it goes up the line #3.

Doing Something After a for loop

What happens once a for loop has finished?

my_string = "Hello, my Python!"  #1
my_list = list(my_string) #2
for item in my_list: #3
print(item.upper()) #4
print("Finally, I'm done.") #5

The line #5 will be executed after the for loop has finished.

Common Errors

Python used indentation to determine when one line of code is connected to the line above.

Forgetting the Colon

This is probably the most common error when we try to use the for loop for the first time.

my_string = "Hello, my Python!"  #
my_list = list(my_string) #
for item in my_list # Colon : is missing here.
print(item.upper()) #

Forgetting to Indent

Always indent the line after the for statement in a loop. It tells Python that we are going to start a new block.

my_string = "Hello, my Python!"  #
my_list = list(my_string) #
for item in my_list: #
print(item.upper()) # We need spaces.

Forgetting to Indent Additional lines or Indenting Unnecessarily After the Loop

The following example might be right or wrong depending on what you intend to do. Think about it as an exercise!!!

my_string = "Hello, my Python!"  #
my_list = list(my_string) #
for item in my_list: #
print(item.upper()) #
print('Done') # We need spaces here too or not.

--

--

--

Ydobon is nobody.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My First Real-World Experience Coding for a Non-Profit

READ/DOWNLOAD$= Using Samba: A File and Print Server for Linux, Unix & Mac OS X, 3rd Edition FULL…

5 ways to make your kid a coding prodigy

Rider Issues

HStreamDB 0.5 released — updates of this cloud-native distributed streaming database

Create a progress button using Flutter

#Long #Jump #Sand #Pit #Lincolnshire https://t.co/JdsHvWkGCo

Being a startup CTO in Stuttgart playing Tic Tac Toe at work

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
A Ydobon

A Ydobon

Ydobon is nobody.

More from Medium

Functions in Python

The Journey to Become a Regex Wizard

How to Repeat a List in Python

Series of Mastering Python: Everything About Type Data in Python You Should Know!