[ad_1]
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:
color1 = “blue” color2 = “green” color3 = “red” color4 = “yellow”
color1 = “blue” color2 = “green” color3 = “Red” color4 = “Yellow” |
To print these colors, we could do something like this:
printing (color1, color2, color3, color4)
to print (color1,color2,color3,color4) |
Our script would look like:
# Define our variables as colors color1 = “blue” color2 = “green” color3 = “red” color4 = “yellow” #Print a list of colors print (color1, color2, color3, color4)
# Define our variables as colors color1 = “blue” color2 = “green” color3 = “Red” color4 = “Yellow” #Print a list of colors to print (color1,color2,color3,color4) |
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:
color = [‘blue’,’green’,’red’,’yellow’]
Color = [‘blue’,‘green’,‘red’,‘yellow’] |
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:
# Define our variable as a list color = [‘blue’,’green’,’red’,’yellow’]#Print a color print (color[1])
# Define our variable as a list Color = [‘blue’,‘green’,‘red’,‘yellow’] #Print a color to print (Color[1]) |
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:
fruit = [‘blueberry’,’apple’,’cherry’,’banana]
fruit = [‘blueberry’,‘apple’,‘cherry’,‘banana] |
You see where it’s going. To be printed green apple, you would use the following statement:
print (color[1],fruit[1])
to print (Color[1],fruit[1]) |
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:
print (color[0],fruit[0], Color[1]fruit[1], Color[2]fruit[2], Color[3]fruit[3])
to print (Color[0],fruit[0], Color[1], fruit[1], Color[2], fruit[2], Color[3], fruit[3]) |
This script would look like:
# Define our color variable as a list color = [‘blue’,’green’,’red’,’yellow’]# Define our fruit variable as a list fruit = [‘blueberry’,’apple’,’cherry’,’banana]#Print the two variables print (color[0],fruit[0], Color[1]fruit[1], Color[2]fruit[2], Color[3]fruit[3])
# Define our color variable as a list Color = [‘blue’,‘green’,‘red’,‘yellow’] # Define our fruit variable as a list fruit = [‘blueberry’,‘apple’,‘cherry’,‘banana] #Print both variables to print (Color[0],fruit[0], Color[1], fruit[1], Color[2], fruit[2], Color[3], fruit[3]) |
If we run this program (using a command like lists from python.py), the output would look like this:
blue blueberry green apple cherry red yellow banana
blue blueberry green Apple Red Cherry Yellow banana |
Let’s be a little tricky here. Instead of a single line of output, what if we wanted our output to look like this:
0 colored fruit 1 blue blueberry 2 green apple 3 red cherry 3 yellow banana
0 Color fruit 1 blue blueberry 2 green Apple 3 Red Cherry 3 Yellow banana |
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 the parts of our script that you now understand look like this: #Import the Pandas framework, defined as pd import the pandas as pd # Define our color variable as a list color = [‘blue’,’green’,’red’,’yellow’]# Define our fruit variable as a list fruit = [‘blueberry’,’apple’,’cherry’,’banana’]
So far, the rooms of our scenario you now to understand see Like this: #Import the Pandas framework, defined as pd import pandas like pd # Define our color variable as a list Color = [‘blue’,‘green’,‘red’,‘yellow’] # Define our fruit variable as a list fruit = [‘blueberry’,‘apple’,‘cherry’,‘banana’] |
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:
df = pd.DataFrame (columns =[‘color’, ‘fruit’])
df=pd.Data frame(Columns=[‘color’, ‘fruit’]) |
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:
df[‘COLOR’], df[‘FRUIT’]= color, fruit
df[‘COLOR’],df[‘FRUIT’]=Color,fruit |
Finally, we print everything with:
Our whole program looks like this:
#Import the Pandas framework, defined as pd import pandas as pd # Define our color variable as list color = [‘blue’,’green’,’red’,’yellow’]# Define our fruit variable as a list fruit = [‘blueberry’,’apple’,’cherry’,’banana’]# Set variable df as columns formatted for color and fruit df = pd.DataFrame (columns =[‘color’, ‘fruit’]) # Label our columns as color and fruit df[‘color’], df[‘fruit’]= color, fruit #Print print all (df)
#Import the Pandas framework, defined as pd import pandas like pd # Define our color variable as a list Color = [‘blue’,‘green’,‘red’,‘yellow’] # Define our fruit variable as a list fruit = [‘blueberry’,‘apple’,‘cherry’,‘banana’] # Define variable df as columns formatted for color and fruit df=pd.Data frame(Columns=[‘color’, ‘fruit’]) # Label our columns as color and fruit df[‘color’],df[‘fruit’]=Color,fruit #Print all to print(df) |
Now when we run the app we will see the output as expected:
0 colored fruit 1 blue blueberry 2 green apple 3 red cherry 3 yellow banana
0 Color fruit 1 blue blueberry 2 green Apple 3 Red Cherry 3 Yellow banana |
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.
[ad_2]