Make a Python List
Today, we're going to teach you how to make a list in Python!
What's a Python list? Basically, it helps you store a collection of items. This can be used for anything from a list of numbers to shopping list items.
Python is an easy way to learn a high-level programming language. With some simple steps, you'll be able to build your skills in Python programming efficiently & effectively.
Ready for a learning adventure? Let's go!
Why Make a Python List?
Python is a powerful coding language. By learning how to make lists, a young coder can boost their skill set. This helps to prepare them for more advanced programming.
As we already mentioned, a list is made up of items (known as list elements) grouped together (in what's called a variable). This is done in an ordered way. Lists are one of the four data structures built into Python. The others are dictionaries, sets, and tuples.
A Python list is considered one of the most useful data types.
By building a solid foundation in Python, a kid can get a major head start. With more practice and instruction, they'll be ready to create many types of coding projects—fun video games, web design, mobile app development, and even artificial intelligence!
What You Need to Start
Python is a free, open-source programming language available for anybody to use!
Looking for a copy of Python? You can download Python from their website (for Windows, Linux/UNIX, MacOS, and other platforms). During setup, you'll be provided with links to an online Python tutorial and Python documentation.
New to Python or coding in general? Here are beginner resources:
Python Coding 101 for Kids (age 11+)
What Python Is Used For (major industries)
Python Virtual Zoom Background (beginner)
For more advanced Python tutorials, check out the following:
Python Rock Paper Scissors Game (intermediate)
Python Pet Simulator (intermediate)
How to Graph in Python (intermediate to advanced)
Coding can seem scary at first. Don’t be nervous! Once you practice the basics, it’s simple & fun to program in Python.
Python Terms and Concepts
Before we jump into making your first list, let's get familiar with Python terms!
Append and Extend Functions
With the append function, you can add an item to an existing list. The item is placed at the end of the list. This will then increase the size of the list by one. The append() function makes it easy to modify an original list.
As for the extend function, it's used to add all the elements from an iterable. This is an object (like lists, tuples, dictionaries, and sets) over which you can iterate or use repeatedly.
List comprehension is an easy way to create a new list from the values of an existing list. Using simple syntax—this is the set of rules that defines how a program is written—it's a lot quicker than using the for loop function.
More Python Terms
- Boolean: Represents the truth value of an expression.
- For loop: Used for iterating over a sequence.
- Data types: The classification of data items.
- Lambda: A single-line function that reduces the number of lines of code.
- List methods: Used to add or remove elements in a list.
- Iteration: The process of repeatedly executing a set of statements.
- Iterator: An object that holds a countable number of values.
- NumPy: A popular Python package used for scientific computing.
- Pandas: Wildly used package for data science and machine learning.
- TypeError: Occurs when an operation is performed on incorrect object type.
These are just a few terms. For more definitions, refer to the Python glossary.
Python List Tutorial
Okay, let's get hands-on!
In today's tutorial, we'll show you examples on how to make and edit your own lists.
To create a list in Python, place elements inside square brackets. Each item is separated by commas. An empty list is simply a pair of empty brackets.
Computer languages deal with numbers. When you want to use words, you'll need to create a list of strings. Strings are a collection of characters. To make a list composed of strings, you place each of them inside quotation marks.
In Python, a string of characters is encoded. This conversion process happens with the use of Python Unicode, which covers every character found in languages around the world!
A list can contain any object or even another list (called a sublist). Lists that contain sublists are known as nested lists. Elements in a list are numbered 0 (first element) to -1 (last item).
How to Make Python List of Numbers
For our first example, we'll make a simple list of the first 5 prime numbers. When dealing with these numbers, we don't need quotation marks (as we do for a sequence of characters).
First, enter the variable name of 'list' in the command line. Type in an equal sign and then a pair of square brackets. After that, add the 5 prime numbers (2, 3, 5, 7, and 11). Make sure to separate them with commas.
Next, type in 'print(list)' in the next line. The 'list' part represents the variable we want to print. Be sure to place the variable in parentheses.
Finally, run the code. You will then see your list of the first 5 prime numbers displayed like the above image. You've just made your first list!
How to Make Python Shopping List
In this example, we'll make a grocery shopping list.
First, we'll create a variable name. Let's type in 'groceryshoppinglist' (using underscores to separate each word). Then enter the equal sign and add square brackets.
We'll now enter the grocery shopping items. Each item needs to be placed in quotation marks and separated with commas.
Here's the list of items:
Bananas Bread Broccoli
'print(grocery_shopping_list)' in the next line. As with the list of prime numbers, run the code. Tada, here's your grocery list!
Find Out Size of List
To know how many items are in a list, use the code below.
For this example, we're using the prime number list. Run the code to see size.
Append a List (Add to the List)
Appending adds items to the end of a list. This uses the append () function.
In the above example, we're adding the next 3 prime numbers to our original list with the use of list.append() functions. When you enter the code and run it, you'll see the output.
Remove from List
Here, we show how to remove items using the remove() function.
Let's remove '2' and '11' from our list. As you can see, these two elements are removed via list.remove() functions. Again, once you run the code, it will display the above output.
Congratulations! You can now officially call yourself a Python programmer.
Once you've mastered these techniques, you can level up to more advanced skills. With continued learning, you'll be on your way to making your own games and apps!
You’ve Made It! Now Keep Learning
You know how to make your own list in Python. Continue to discover new Python skills. There are so many fun things to do, from web development to game programming!
At Juni Learning, we teach Python and other popular coding languages (including Scratch, Java, and C++) to aspiring kid coders age 8+. With the help of our friendly instructors, students can explore how to build their own coding projects.