Lists and Loops in Python

Lists and Loops in Python

In this tutorial, we assume you know the very fundamentals of Python, including working with strings, integers, and floats

Lists are one of the most powerful data types in Python. In this Python List Tutorial, you’ll learn how to work with lists while analyzing data about mobile apps.

In this tutorial, we assume you know the very fundamentals of Python, including working with strings, integers, and floats.

We’ll be working with this table of data, taken from Mobile App Store data set (Ramanathan Perumal):

name price currency rating_count rating
Facebook 0.0 USD 2974676 3.5
Instagram 0.0 USD 2161558 4.5
Clash of Clans 0.0 USD 2130805 4.5
Temple Run 0.0 USD 1724546 4.5
Pandora – Music & Radio 0.0 USD 1126879 4.0

Each value in the table is a data point. For instance, the first row (after the column titles) has five data points:

  • Facebook
  • 0.0
  • USD
  • 2974676
  • 3.5

A collection of data points make up a dataset. We can understand our entire table above as a collection of data points, so we call the entire table a dataset. We can see that our data set has five rows and five columns.

Using our understanding of Python types, we might think we could store each data point in its own variable — for instance, this is how we might store the first row’s data points:

![data stored as individual variables](data:image/svg+xml,%3Csvg%20xmlns=%22

Above, we stored:

  • The text “Facebook” as a string
  • The price 0.0 as a float
  • The text “USD” as a string
  • The rating count 2,974,676 as an integer
  • The user rating 3.5 as a float

Creating a variable for each data point in our data set would be a cumbersome process. Fortunately, we can store data more efficiently using lists. This is how we can create a list of data points for the first row:

![a single rows data stored as a python list](data:image/svg+xml,%3Csvg%20xmlns=%22

To create the list above, we:

  • Typed out a sequence of data points and separated each with a comma: 'Facebook', 0.0, 'USD', 2974676, 3.5
  • Surrounded the sequence with brackets: ['Facebook', 0.0, 'USD', 2974676, 3.5]

After we created the list, we stored it in the computer’s memory by assigning it to a variable named row_1.

To create a list of data points, we only need to:

  • Separate the data points with a comma.
  • Surround the sequence of data points with brackets.

Now let’s create five lists, one for each row in our dataset:

Indexing Python Lists

A list can contain a variety of data types. A list like [4, 5, 6] has identical data types (only integers), while the list ['Facebook', 0.0, 'USD', 2974676, 3.5] has mixed data types:

  • Two strings ('Facebook', 'USD')
  • Two floats (0.0, 3.5)
  • One integer (2974676)

The ['Facebook', 0.0, 'USD', 2974676, 3.5] list has five data points. To find the length of a list, we can use the len() command:

![using len() to find the length of a list](data:image/svg+xml,%3Csvg%20xmlns=%22

For small lists, we can just count the data points on our screens to find the length, but the len() command will prove very useful whenever you work with lists containing many elements, or need to write code for data where you don’t know the length ahead of time.

Each element (data point) in a list has a specific number associated with it, called an index number. The indexing always starts at 0, so the first element will have the index number 0, the second element the index number 1, and so on.

![indexing a python list, 1](data:image/svg+xml,%3Csvg%20xmlns=%22

To quickly find the index of a list element, identify its position number in the list, and then subtract 1. For example, the string 'USD' is the third element of the list (position number 3), so its index number must be 2 since 3 – 1 = 2.

The index numbers help us retrieve individual elements from a list. Looking back at the list row_1 from the code example above, we can retrieve the first element (the string 'Facebook') with the index number 0 by running the code row_1[0].

![index a python list, 2](data:image/svg+xml,%3Csvg%20xmlns=%22

The syntax for retrieving individual list elements follows the model list_name[index_number]. For instance, the name of our list above is row_1 and the index number of the first element is 0 — following the list_name[index_number] model, we get row_1[0], where the index number 0 is in square brackets after the variable name row_1.

![syntax explanation for Python list indexing](data:image/svg+xml,%3Csvg%20xmlns=%22

This is how we can retrieve each element in row_1:

![extracting each element from a list](data:image/svg+xml,%3Csvg%20xmlns=%22

Retrieving list elements makes it easier to perform operations. For instance, we can select the ratings for Facebook and Instagram, and find the average or the difference between the two:

![using list indexing to extract values and perform a calculation](data:image/svg+xml,%3Csvg%20xmlns=%22

Let’s use list indexing to extract the number of ratings from the first three rows and then average them:

Using Negative Indexing with Lists

In Python, we have two indexing systems for lists:

  • Positive indexing: the _first) element has the index number 0, the second element has the index number 1, and so on.
  • Negative indexing: the last element has the index number -1, the second to last element has the index number -2, and so on.

![positive vs negative indexing](data:image/svg+xml,%3Csvg%20xmlns=%22

In practice, we almost always use positive indexing to retrieve list elements. Negative indexing is useful when we want to select the last element of a list — especially if the list is long, and we can’t tell the length by counting.

![extracting the last element from a list](data:image/svg+xml,%3Csvg%20xmlns=%22

Notice that if we use an index number that is outside the range of the two indexing systems, we’ll get an IndexError.

![Python indexerror examples](data:image/svg+xml,%3Csvg%20xmlns=%22

Let’s use negative indexing to extract the user rating (the last value) from each of the first three rows and then average them.

Slicing Python Lists

Instead of selecting list elements individually, we can use a syntax shortcut to select two or more consecutive elements:

![list slicing syntax shortcut](data:image/svg+xml,%3Csvg%20xmlns=%22

When we select the first n elements (n stands for a number) from a list named a_list, we can use the syntax shortcut a_list[0:n]. In the example above, we needed to select the first three elements from the list row_3, so we used row_3[0:3].

When we selected the first three elements, we sliced a part of the list. For this reason, the process of selecting a part of a list is called list slicing.

There are many ways that we might want to slice a list:

![Python list slicing examples](data:image/svg+xml,%3Csvg%20xmlns=%22

To retrieve any list slice we want:

  1. We first need to identify the first and the last element of the slice.
  2. We then need to identify the index numbers of the first and the last element of the slice.
  3. Finally we can retrieve the list slice we want by using the syntax a_list[m:n], where:
    • m represents the index number of the first element of the slice; and
    • n represents the index number of the last element of the slice plus one (if the last element has the index number 2, then we n will be 3, if the last element has the index number 4, then n will be 5, and so on).

![Python list slicing syntax explanation](data:image/svg+xml,%3Csvg%20xmlns=%22

When we need to select the first or last x elements (x stands for a number), we can use even simpler syntax shortcuts:

  • a_list[:x] when we want to select the first x elements.
  • a_list[-x:] when we want to select the last x elements.

![list slicing wildcard syntax example](data:image/svg+xml,%3Csvg%20xmlns=%22

Let’s look at how we extract the first four elements from the first row (with data about Facebook):

The last three elements from that same row:

And elements three and four from the fifth row (with data about Pandora):

Python List of Lists

Previously, we introduced lists as a better alternative to using one variable per data point. Instead of having a separate variable for each of the five data points 'Facebook', 0.0, 'USD', 2974676, 3.5, we can bundle the data points together into a list, and then store the list in a single variable.

So far, we’ve been working with a data set having five rows, and we’ve been storing each row as a list in a separate variable (the variables row_1, row_2, row_3, row_4, and row_5). If we had a data set with 5,000 rows, however, we’d end up with 5,000 variables, which will make our code messy and almost impossible to work with.

To solve this problem, we can store our five variables in a single list:

![creating a list of lists from individual lists](data:image/svg+xml,%3Csvg%20xmlns=%22

As we can see, data_set is a list that stores five other lists (row_1, row_2, row_3, row_4, and row_5). A list that contains other lists is called a list of lists.

The data_set variable is still a list, which means we can retrieve individual list elements and perform list slicing using the syntax we learned. Below, we:

  • Retrieve the first list element (row_1) using data_set[0].
  • Retrieve the last list element (row_5) using data_set[-1].
  • Retrieve the first two list elements (row_1 and row_2) by performing list slicing using data_set[:2].

![selecting ](data:image/svg+xml,%3Csvg%20xmlns=%22

We’ll often need to retrieve individual elements from a list that’s part of a list of lists — for instance, we may want to retrieve the value 3.5 from ['Facebook', 0.0, 'USD', 2974676, 3.5], which is part of the data_set list of lists. Below, we extract 3.5 from data_set using what we’ve learned:

  • We retrieve row_1 using data_set[0], and assign the result to a variable named fb_row.
  • We print fb_row, which outputs ['Facebook', 0.0, 'USD', 2974676, 3.5].
  • We retrieve the last element from fb_row using fb_row[-1] (since fb_row is a list), and assign the result to a variable named fb_rating.
  • Print fb_rating, which outputs 3.5

![selecting individual elements from a list of lists in two steps](data:image/svg+xml,%3Csvg%20xmlns=%22

Above, we retrieved 3.5 in two steps: we first retrieved data_set[0], and then we retrieved fb_row[-1]. However, there’s an easier way to retrieve the same value of 3.5 by chaining the two indices ([0] and [-1]) — the code data_set[0][-1] retrieves 3.5:

![selecting individual elements from a list of lists in one step](data:image/svg+xml,%3Csvg%20xmlns=%22

Above, we’ve seen two ways of retrieving the value 3.5. Both ways lead to the same output (3.5), but the second way involves less typing because it elegantly combines the steps we see in the first case. While you can choose either option, people generally choose the second one.

Let’s transform our five individual lists into a list of lists:

Repetitive List Processes

Previously in this mission, we were interested in computing the average rating of an app. This was a doable task when we were working with only three rows, but the more rows we add the harder it becomes. Using our strategy from earlier, we’ll:

  1. Retrieve each individual rating.
  2. Sum up the ratings.
  3. Divide by the number of ratings.

![Manually calculating app average](data:image/svg+xml,%3Csvg%20xmlns=%22

As you can see, with five ratings this becomes complex. If we were working with data containing 1,000s of rows, it would require an impractical amount of code! We need to find a simple way to retrieve many ratings.

Looking at the code example above, we see that a process keeps repeating: we select the last list element for each list within app_data_set. The app_data_set stores five lists, so we repeat the same process five times. What if we could tell Python directly that we want to repeat this process for each list in app_data_set?

Fortunately, we can do that — Python offers us an easy way to repeat a process, which helps us enormously when we need to repeat a process hundreds, thousands, or even millions of times.

Let’s say we have a list [3, 5, 1, 2] assigned to a variable ratings, and we want to repeat the following process: for each element in ratings, print that element. This is how we could translate that into Python syntax:

![first for loop example](data:image/svg+xml,%3Csvg%20xmlns=%22

In our first example above, the process we wanted to repeat was ”extract the last element for each list in app_data_set. This is how we can translate that process into Python syntax:

![second for loop example](data:image/svg+xml,%3Csvg%20xmlns=%22

Let’s try to get a better understanding of what happens above. Python isolates, one at a time, each list element from app_data_set, and assigns it to each_list (which basically becomes a variable that stores a list — we’ll discuss this more on the next screen):

![printing each item in a list using a loop](data:image/svg+xml,%3Csvg%20xmlns=%22

The code in the last diagram above is a much more simplified and abstracted version of the code below:

![manual version of printing each item in a list](data:image/svg+xml,%3Csvg%20xmlns=%22

Using the technique above requires us to write a line of code for every row in the data set. But using the for each_list in app_data_set technique requires us to write only two lines of code regardless of the number of rows in the data set — the data set can have five rows or one million.

Our intermediate goal is to use this new technique to compute the average rating for our five rows above, and our final goal is to compute the average rating for our data set with 7,197 rows. We’ll do exactly that over the next few screens of this mission, but for now, we’ll focus on practicing this technique to get a good grasp of it.

Before writing any code, we need to indent the code we want repeated four space characters to the right:

![example showing loop block indentation](data:image/svg+xml,%3Csvg%20xmlns=%22

Technically, we only need to indent the code at least one space character to the right, but the convention in the Python community is to use four space characters. This helps with readability — it will be easier for other people who follow this convention to read your code, and it will be easier for you to read theirs.

Let’s use this technique to print the name and rating of each app:

Lists and For Loops in Python

The technique we’ve just learned is called a loop. Loops are an incredibly useful tool that are used to perform repetitive processes with Python lists. Because we always start with for (like in for some_variable in some_list:), this technique is known as a for loop.

These are the structural parts of a for loop:

![Parts of a loop](data:image/svg+xml,%3Csvg%20xmlns=%22

The indented code in the body gets executed the same number of times as elements in the iterable variable. If the iterable variable is a list that has three elements, the indented code in the body gets executed three times. We call each code execution an iteration, so there’ll be three iterations for a list that has three elements. For each iteration, the iteration variable will take a different value, following this pattern:

  • For the first iteration, the value is the first element of the iterable (from the example above, 1).
  • For the second iteration, the value is the second element of the iterable (from the example above, 3).
  • For the third iteration, the value is the third element of the iterable (from the example above, 5).

![step-by-step iteration of a loop](data:image/svg+xml,%3Csvg%20xmlns=%22

The name of the interation variable can be whatever you like – if you replaced value in the code above with dog, the code will work exactly the same way. That said, it’s convention to use something that helps communicate what the data is.

The code outside the loop body can interact with the code inside the loop body. For instance, in the code below we:

  • Initialize a variable a_sum with a value of zero outside the loop body.
  • We loop (or iterate) over a_list. For every iteration of the loop, we:
    • Perform an addition (inside the loop body) between the current value of the iteration variable value and the current value stored in a_sum (a_sum was defined outside the loop body).
    • Assign the result of the addition back to a_sum (inside the loop body).
    • Print the value of the a_sum variable (inside the loop body). Notice that the value of a_sum changes after each addition. At the end of the loop, a_sum has the value 9, which is equivalent to the sum of the numbers in a_list (1 + 3 + 5).

![summing with a loop](data:image/svg+xml,%3Csvg%20xmlns=%22

Above, we created a way to sum up the numbers in a list. We can use this technique to sum up the ratings in our dataset. Once we have the sum, we only need to divide by the number of ratings to get the average value.

We’ve covered the fundamentals of for loops here, but if you’d like some more practice, we also have tutorials on for loop basics and advanced for loops that you can check out.

Alternative Way to Compute a List Average

Now we’ll learn an alternative way to compute the average rating value. Once we create a list, we can add (or append) values to it using the append() command.

![Using append to add values to a list](data:image/svg+xml,%3Csvg%20xmlns=%22

Unlike other commands we’ve learned, notice that append() has a special syntactical usage, following the pattern list_name.append() rather than being simply used as append().

Now that we know how to append values to a list, we can take the steps below to compute the average app rating:

  1. We initialize an empty list.
  2. We start looping over our data set and extract the ratings.
  3. We append the ratings to the empty list we created at step one.
  4. Once we have all the ratings, we:
    • use the sum() command to sum up all the ratings (to be able to use sum(), we’ll need to store the ratings as floats or integers); and then
    • we divide the sum by the number of ratings (which we can get using the len() command).

Below, we can see the steps above implemented for our data set with five rows:

![Using append to extract values and calculate an average](data:image/svg+xml,%3Csvg%20xmlns=%22

We can also use append() to add another row to our list of lists by appending the data as a list. Let’s look at how that works:

Now, let’s use the technique we learned above to calculate the average rating of all six apps:

Next Steps

In this tutorial we learned how to:

  • use Python lists to store and work with data
  • access values stored in lists using positive and negative indexing
  • use lists of lists to work with tabular data
  • use for loops to automate repetitive tasks
  • append values to lists

Thank for reading !

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.


Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more:

Learn Python Tutorial from Basic to Advance

Learn Python Tutorial from Basic to Advance

Basic programming concept in any language will help but not require to attend this tutorial

Become a Python Programmer and learn one of employer's most requested skills of 21st century!

This is the most comprehensive, yet straight-forward, course for the Python programming language on Simpliv! Whether you have never programmed before, already know basic syntax, or want to learn about the advanced features of Python, this course is for you! In this course we will teach you Python 3. (Note, we also provide older Python 2 notes in case you need them)

With over 40 lectures and more than 3 hours of video this comprehensive course leaves no stone unturned! This course includes tests, and homework assignments as well as 3 major projects to create a Python project portfolio!

This course will teach you Python in a practical manner, with every lecture comes a full coding screencast and a corresponding code notebook! Learn in whatever manner is best for you!

We will start by helping you get Python installed on your computer, regardless of your operating system, whether its Linux, MacOS, or Windows, we've got you covered!

We cover a wide variety of topics, including:

Command Line Basics
Installing Python
Running Python Code
Number Data Types
Print Formatting
Built-in Functions
Debugging and Error Handling
External Modules
Object Oriented Programming
File I/O
Web scrapping
Database Connection
Email sending
and much more!
Project that we will complete:

Guess the number
Guess the word using speech recognition
Love Calculator
google search in python
Image download from a link
Click and save image using openCV
Ludo game dice simulator
open wikipedia on command prompt
Password generator
QR code reader and generator
You will get lifetime access to over 40 lectures.

So what are you waiting for? Learn Python in a way that will advance your career and increase your knowledge, all in a fun and practical way!

Basic knowledge
Basic programming concept in any language will help but not require to attend this tutorial
What will you learn
Learn to use Python professionally, learning both Python 2 and Python 3!
Create games with Python, like Tic Tac Toe and Blackjack!
Learn advanced Python features, like the collections module and how to work with timestamps!
Learn to use Object Oriented Programming with classes!
Understand complex topics, like decorators.
Understand how to use both the pycharm and create .py files
Get an understanding of how to create GUIs in the pycharm!
Build a complete understanding of Python from the ground up!

Best Way to Learn Python Programming Language | Python Tutorial

Best Way to Learn Python Programming Language | Python Tutorial

Worried that you have no experience in handling Python? Don’t! Python programming language teaching from Simpliv puts you right there to be able to write Python programs with ease. Place object-oriented programing in a Python context and use Python to perform complicated text processing.

A Note on the Python versions 2 and 3: The code-alongs in this class all use Python 2.7. Source code (with copious amounts of comments) is attached as a resource with all the code-alongs. The source code has been provided for both Python 2 and Python 3 wherever possible.

What's Covered:

Introductory Python: Functional language constructs; Python syntax; Lists, dictionaries, functions and function objects; Lambda functions; iterators, exceptions and file-handling
Database operations: Just as much database knowledge as you need to do data manipulation in Python
Auto-generating spreadsheets: Kill the drudgery of reporting tasks with xlsxwriter; automated reports that combine database operations with spreadsheet auto-generation
Text processing and NLP: Python’s powerful tools for text processing - nltk and others.
Website scraping using Beautiful Soup: Scrapers for the New York Times and Washington Post
Machine Learning : Use sk-learn to apply machine learning techniques like KMeans clustering
Hundreds of lines of code with hundreds of lines of comments
Drill #1: Download a zip file from the National Stock Exchange of India; unzip and process to find the 3 most actively traded securities for the day
Drill #2: Store stock-exchange time-series data for 3 years in a database. On-demand, generate a report with a time-series for a given stock ticker
Drill #3: Scrape a news article URL and auto-summarize into 3 sentences
Drill #4: Scrape newspapers and a blog and apply several machine learning techniques - classification and clustering to these
Using discussion forums

Please use the discussion forums on this course to engage with other students and to help each other out. Unfortunately, much as we would like to, it is not possible for us at Loonycorn to respond to individual questions from students:-(

We're super small and self-funded with only 2 people developing technical video content. Our mission is to make high-quality courses available at super low prices.

The only way to keep our prices this low is to NOT offer additional technical support over email or in-person. The truth is, direct support is hugely expensive and just does not scale.

We understand that this is not ideal and that a lot of students might benefit from this additional support. Hiring resources for additional support would make our offering much more expensive, thus defeating our original purpose.

It is a hard trade-off.

Thank you for your patience and understanding!

Who is the target audience?

Yep! Folks with zero programming experience looking to learn a new skill
Machine Learning and Language Processing folks looking to apply concepts in a full-fledged programming language
Yep! Computer Science students or software engineers with no experience in Java, but experience in Python, C++ or even C#. You might need to skip over some bits, but in general the class will still have new learning to offer you :-)
Basic knowledge
No prior programming experience is needed :-)
The course will use a Python IDE (integrated development environment) called iPython from Anaconda. We will go through a step-by-step procedure on downloading and installing this IDE.
What will you learn
Pick up programming even if you have NO programming experience at all
Write Python programs of moderate complexity
Perform complicated text processing - splitting articles into sentences and words and doing things with them
Work with files, including creating Excel spreadsheets and working with zip files
Apply simple machine learning and natural language processing concepts such as classification, clustering and summarization
Understand Object-Oriented Programming in a Python context