- Complete Python Bootcamp: Go from zero to hero in Python
- Complete Python Web Course: Build 8 Python Web Apps
Python supports collections known as lists. A list is defined using square brackets. Let us define as simple list:
c = [5,2,10,48,32,16,49,10,11,32,64,55,34,45,41,23,26,27,72,18]
In this case we have a list defined by the variable c. We defined a purely random set of numbers above.
To access individual elements, we use the same brackets. To print the first element (Python starts counting from zero):
To print the second element:
To print the last element, you can count from the back using the minus sign.
Size of the list
You can get the length of the list using the len function. Example code:
c = [5,2,10,48,32,16,49,10,11,32,64,55,34,45,41,23,26,27,72,18] print(len(c))
List may contain various types of variables in the same list including text, whole numbers, floating point numbers and so on. For example, we may define a list with text variables:
fears = ["Spiders","Ghosts","Dracula"]