Member-only story
Python List Cheatsheet For Beginners
If you’re taking a class or course on Python, chances are that you need to deal with lists one way or another. Here’s a cheatsheet of useful list functions that may come in handy if you’re taking a Python test
Creating a List
To create a list in Python, we need to use square brackets. Note that we can put elements of whichever data type we want inside the list.
lis = [4, 5, 6] # list of integers
lis = [3.14, 2.78, 1.41] # list of floats
lis = ["apple", "orange" ,"pear"] # list of strings
lis = ["apple", 2, 3.14] # list containing multiple data types
Accessing Elements in a List
Let’s say we have this list.
lis = ["apple", "orange", "pear"]
To access the elements inside the list, we can tell Python which element we want using the order in which it appears in the list. Take note that in Python, we start counting from 0, so the first element will be at index 0, the second element will be at index 1, the third element will be at index 2 and so on.
first element --> index 0 --> "apple"
second element --> index 1 --> "orange"
third element --> index 2 --> "pear"
Also, note that if we index a list with a number that is too large, we get an error (an index…