Lists – The New Stack

0


Our Python Beginner’s Tutorial continues, so let the ‘Huzzahs! To sound. We first introduced you to what makes Python so special, then we introduced variables, learned to accept user input, and found out how to save input to a file.

We will continue to build on these tutorials and do it in a way that everyone can follow. Our goal is to help those who are not inherently programmers learn a language and feel that they have understood it enough to make it work for them.

This time we are going to talk about lists. We use lists all the time. Shopping lists, to-do lists… you know the exercise. But a list in Python is a different big beast… sort of.

A list in Python is used to store multiple items for a single variable. You remember the variables:

In the example above, we have assigned the value “green” to the variable “color”. We can then use this variable in our script like this:

When we run our program, it displays:

Simple. But what if we wanted to define several colors? Of course, we could always do something like this:

To print these colors, we could do something like this:

Our script would look like:

You probably won’t be surprised to find that we could do this even easier with the help of a list. Let’s stick to our colors. Let’s define the color variable with a list of four colors. This variable declaration would look like this:

Now here is something you need to understand. You might think that the position of each item in the list would be blue = 1, green = 2, red = 3, and yellow = 4. It is important to understand that in software development, the count starts at 0. So , our color positions would be blue = 0, green = 1, red = 2, and yellow = 3. Why is this important? Let me show you.

Suppose you want to print the color green. To do this, you would add the line:

So our application would look like this:

When you run this program, it prints green color.

Add to that. Suppose you want to set a fruit variable to coincide with the colors. We could therefore define:

You see where it’s going. To be printed green apple, you would use the following statement:

We printed the color value at position 1 and the fruit value at position 1.

Pretty cool.

What if we wanted to map all the colors to all the fruits? It would look something like this:

This script would look like:

If we run this program (using a command like lists from python.py), the output would look like this:

Let’s be a little tricky here. Instead of a single line of output, what if we wanted our output to look like this:

To do this, we need to take a leap forward in our Python skills and introduce a framework, called Pandas. the pandas is a framework for data analysis and manipulation. To use Pandas, it must be installed. I am doing a demonstration on Ubuntu Linux 20.04 and the installation of Pandas is done with the command:

sudo apt-get install python3-pandas -y

Once the framework is installed, you have it at your disposal. To use Pandas, we need to import it into our program with the line:

import pandas as pd

This means that we will be calling the Pandas functions in our scripts using pd. The Pandas function we’ll be working on is DataFrame, which allows you to work with two-dimensional, size-editable, and potentially heterogeneous tabular data. In other words, it makes it easier for you to work with different types of data.

Still with me?

So far, so good. Now we come to the difficult part. The first thing we’re going to do is define a new variable using our color and fruit variables as columns with the DataFrame function. This new variable will be called df. Remember we are calling Pandas with pd and the function we are using is DataFrame. This line looks like this:

Our variable is therefore df, we call Pandas with pd, we use the DataFrame function and format the color and fruit variables as columns.

Good? Good.

Now we are going to define the labels of our columns using the variables that we have already created with the row:

Finally, we print everything with:

Our whole program looks like this:

Now when we run the app we will see the output as expected:

We took a leap in complexity there, you now know how to use external frameworks (such as Pandas), to make Python even more useful and flexible.

You should already be feeling smarter.


Share.

Comments are closed.