Python Tutorial 2
Python Tutorial 2
What is a List
Python uses the square brackets ([]) to indicate a list. The following shows an empty list:
empty_list = []
Typically, a list contains one or more items. To separate two items, you use a comma (,). For example:
Since a list often contains many items, it’s a good practice to name it using plural nouns
e.g., numbers, colors, and shopping_carts.
numbers = [1, 3, 2, 7, 9, 4]
If you print out the list, you’ll see its representation including the square brackets. For example:
print(numbers)
Output:
[1, 3, 2, 7, 9, 4]
A list can contains other lists. The following example defines a list of lists:
Output:
Since a list is an ordered collection, you can access its element by indexes like this:
list[index]
Lists are zero-based indexes. In other words, the first element has an index of 0, the second element has
an index of 1, and so on.
For example, the following shows how to access the first element of the numbers list:
numbers = [1, 3, 2, 7, 9, 4]
print(numbers[0])
Output:
The numbers[1] will return the second element from the list:
numbers = [1, 3, 2, 7, 9, 4]
print(numbers[1])
Output:
3
The negative index allows you to access elements starting from the end of the list.
The list[-1] returns the last element. The list[-2] returns the second last element, and so on. For example:
numbers = [1, 3, 2, 7, 9, 4]
print(numbers[-1])
print(numbers[-2])
Output:
4
9
A list is dynamic. It means that you can modify elements in the list, add new elements to the list, and
remove elements from a list.
list[index] = new_value
The following example shows how to change the first element of the numbers list to 10:
numbers = [1, 3, 2, 7, 9, 4]
numbers[0] = 10
print(numbers)
Output:
[10, 3, 2, 7, 9, 4]
The following shows how to multiply the second element with 10:
numbers = [1, 3, 2, 7, 9, 4]
numbers[1] = numbers[1]*10
print(numbers)
Output:
[1, 30, 2, 7, 9, 4]
numbers = [1, 3, 2, 7, 9, 4]
numbers[2] /= 2
print(numbers)
Output:
[1, 3, 1.0, 7, 9, 4]
The append() method appends an element to the end of a list. For example:
numbers = [1, 3, 2, 7, 9, 4]
numbers.append(100)
print(numbers)
Output:
[1, 3, 2, 7, 9, 4, 100]
The insert() method adds a new element at any position in the list.
For example, the following inserts the number 100 at index 2 of the numbers list:
numbers = [1, 3, 2, 7, 9, 4]
numbers.insert(2, 100)
print(numbers)
Output:
[1, 3, 100, 2, 7, 9, 4]
The extend() method to add multiple elements at the end of the list.
print(vowels)
Output:
['a', 'e']
['a', 'e', ‘i’, 'o', 'u']
The del statement allows you to remove an element from a list by specifying the position of the element.
The following example shows how to remove the first element from the list:
numbers = [1, 3, 2, 7, 9, 4]
del numbers[0]
print(numbers)
Output:
[3, 2, 7, 9, 4]
The pop() method removes the last element from a list and returns that element:
numbers = [1, 3, 2, 7, 9, 4]
last = numbers.pop()
print(last)
print(numbers)
Output:
4
[1, 3, 2, 7, 9]
Typically, you use the pop() method when you want to remove an element from a list and still want to
access the value of that element.
To pop an element by its position, you use the pop() with the element’s index. For example:
numbers = [1, 3, 2, 7, 9, 4]
second = numbers.pop(1)
print(second)
print(numbers)
Output:
3
[1, 2, 7, 9, 4]
For example, the following removes the element with value 9 from the numbers list:
numbers = [1, 3, 2, 7, 9, 4]
numbers.remove(9)
print(numbers)
Output:
[1, 3, 2, 7, 4]
The remove() method removes the first occurrence of the element with the specified value.
4) Slicing a List in Python
In Python, we can access the elements stored in a range of indexes using the slicing operator (:). The
index put on the left side of the slicing operator is inclusive, and that mentioned on the right side is
excluded.
Slice itself means part of something. So when we want to access elements stored at some part or range in
a list we use slicing.
my_List = ['i', 'n', 't', 'v', 'i', 'e', 'w', 'b', 'i', 't']
# Accessing elements from index beginning to 5th index
print(my_List[:6])
# Accessing elements from index 3 to 6
print(my_List[3:7])
# Accessing elements from index 5 to end
print(my_List[5:])
# Accessing elements from beginning to end
print(my_List[:])
# Accessing elements from beginning to 4th index
print(my_List[:-6])
Output:
The count method in python gives us the total number of occurrences of a given value inside the list.
Example:
Output:
2
sort() method can be used in both python lists and tuples; its function is to arrange the list or tuple in
ascending order. Thus, the sort method lays down the elements in the list in increasing order.
Example 1:
print(lis1)
Output:
Example 2:
print(lis1)
Output:
Summary