Define Numeric Lists
In this step, you will learn how to define numeric lists in Python. A list is a collection of items, and in this case, we'll focus on lists containing numbers. Lists are a fundamental data structure in Python and are used to store and manipulate ordered sequences of data.
First, let's create a new Python file named numeric_lists.py
in your ~/project
directory using the VS Code editor.
~/project/numeric_lists.py
Now, open numeric_lists.py
in the editor and add the following code to define a list of integers:
## Define a list of integers
integer_list = [1, 2, 3, 4, 5]
## Print the list to the console
print(integer_list)
Save the file. Next, open a terminal in VS Code and navigate to the ~/project
directory. You should already be in this directory by default. Now, execute the Python script using the following command:
python numeric_lists.py
You should see the following output:
[1, 2, 3, 4, 5]
Now, let's define a list of floating-point numbers (decimal numbers):
Modify the numeric_lists.py
file to include the following:
## Define a list of floating-point numbers
float_list = [1.0, 2.5, 3.7, 4.2, 5.9]
## Print the list to the console
print(float_list)
Save the file and run the script again:
python numeric_lists.py
You should see the following output:
[1.0, 2.5, 3.7, 4.2, 5.9]
You can also create a list containing a mix of integers and floating-point numbers:
Modify the numeric_lists.py
file to include the following:
## Define a list of mixed numbers (integers and floats)
mixed_list = [1, 2.0, 3, 4.5, 5]
## Print the list to the console
print(mixed_list)
Save the file and run the script again:
python numeric_lists.py
You should see the following output:
[1, 2.0, 3, 4.5, 5]
Congratulations! You have successfully defined and printed numeric lists in Python. This is a fundamental step towards working with numerical data in Python.