Python Tutorials

Overview

Python lists are used to store multiple items in a single variable. List items are indexed, the first item has index [0], the second item has index [1], the third item has index [2], etc.

Note

Arrays count starting from zero NOT one. So item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on.

Creating a List

Lists are created using square brackets. List items are ordered as created, but that can be changed at a later point.

Duplicate values in the same list are allowed. Lists can be of any data type and can be a mixture of data types.

my_list1 = ["Ferrari", "Maserati", "Alfa Romeo", "Maserati"]
my_list2 = [2, 4, 6, 9]
my_list3 = [True, True, False]
my_list4 = ["Maserati", True, 2, 4, False, "Ferrari"]

Another way to create a list is to use the list() constructor.

my_list = list(("Ferrari", "Maserati", "Alfa Romeo"))

Note

If you add new items to an existing list, the new items will be placed at the end of the list.

Getting the Number of Items in a List

To determine how many items are in a list, the len() function is used.

my_list = ["Ferrari", "Maserati", "Alfa Romeo"]
print(len(my_list))
4

Python Notes:

  • The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
  • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
  • Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
  • Python string methods return new values, and DO NOT change the original string

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.