0% found this document useful (0 votes)
11 views8 pages

Python Tutorial 2

Python programming tutorial

Uploaded by

roy3hritwik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
11 views8 pages

Python Tutorial 2

Python programming tutorial

Uploaded by

roy3hritwik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Python List

What is a List

A list is an ordered collection of items.

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:

todo_list = ['Learn Python List', 'How to manage List elements']

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.

The following example defines a list of six numbers:

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]

The following shows how to define a list of strings:


colors = ['red', 'green', 'blue']
print(colors)
Output:
['red', 'green', 'blue']

A list can contains other lists. The following example defines a list of lists:

coordinates = [[0, 0], [100, 100], [200, 200]]


print(coordinates)

Output:

[[0, 0], [100, 100], [200, 200]]

Accessing elements in a list

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

Modifying, adding, and removing elements

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.

1) Modifying elements in a list

To change an element, you assign a new value to it using this syntax:

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]

And the following divides the third element by 2:

numbers = [1, 3, 2, 7, 9, 4]
numbers[2] /= 2

print(numbers)

Output:
[1, 3, 1.0, 7, 9, 4]

2) Adding elements to the list

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.

vowels = ['a', 'e']


print(vowels)
# Extending an element in list
vowels.extend([‘i’, 'o', 'u'])

print(vowels)

Output:

['a', 'e']
['a', 'e', ‘i’, 'o', 'u']

3) Removing elements from a list

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]

To remove an element by value, you use the remove() method.

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:

['i', 'n', 't', 'v', 'i', 'e']


['v', 'i', 'e', 'w']
['e', 'w', 'b', 'i', 't']
['i', 'n', 't', 'v', 'i', 'e', 'w', 'b', 'i', 't']
['i', 'n', 't', 'v']

The count method in python gives us the total number of occurrences of a given value inside the list.

Example:

lis=["Horse","Shoe","Turtle",179, 200, 544,"Shoe"]


lis.count('Shoe')

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:

lis1=[179, 200, 544, 11, 18, 2000]


lis1.sort()

print(lis1)

Output:

[11, 18, 179, 200, 544, 2000]


The list we have given is sorted in the ascending order of the values.

Example 2:

lis1=[179, 200, 544, 11, 18, 2000]


lis1.sort(reverse=True)

print(lis1)

Output:

[2000, 544, 200, 179, 18, 11]

Summary

 A list is an ordered collection of items.


 Use square bracket notation [] to access a list element by its index. The first element has an index 0.
 Use a negative index to access a list element from the end of a list. The last element has an index -1.
 Use list[index] = new_value to modify an element from a list.
 Use append() to add a new element to the end of a list.
 Use insert() to add a new element at a position in a list.
 Use pop() to remove an element from a list and return that element.
 Use remove() to remove an element from a list.

You might also like