what is my_list.index(2) (what index do ) LOL
Answers
Answer:
Programiz
Search Programiz
Python List
In this tutorial, we'll learn everything about Python lists, how they are created, slicing of a list, adding or removing elements from them and so on.
Video: Python Lists and Tuples
Python offers a range of compound data types often referred to as sequences. List is one of the most frequently used and very versatile data types used in Python.
How to create a list?
In Python programming, a list is created by placing all the items (elements) inside square brackets [], separated by commas.
It can have any number of items and they may be of different types (integer, float, string etc.).
# empty list
my_list = []
# list of integers
my_list = [1, 2, 3]
# list with mixed data types
my_list = [1, "Hello", 3.4]
A list can also have another list as an item. This is called a nested list.
# nested list
my_list = ["mouse", [8, 4, 6], ['a']]
Access List Elements
There are various ways in which we can access the elements of a list.
List Index
We can use the index operator [] to access an item in a list. In Python, indices start at 0. So, a list having 5 elements will have an index from 0 to 4.
Trying to access indexes other than these will raise an IndexError. The index must be an integer. We can't use float or other types, this will result in TypeError.
Nested lists are accessed using nested