Screenshot 2024-03-04 at 2.26.06 PM
Screenshot 2024-03-04 at 2.26.06 PM
Screenshot 2024-03-04 at 2.26.06 PM
Python Lists
1. Definition:
A list is a data structure that contains a series of values of different types (for example integer
and string), stored in a single variable.
Examples of lists: languages = ['Python', 'Pascal', 'C++'], fruits = ['apple', 'banana', 'orange']
2. Create a Python List:
We create a list by placing elements inside a square bracket '[',']' separated by camas’,’ .
3. Operations on lists:
3.1. Access List Elements
Each element in a list is associated with a number, known as a list index. The index always starts
from 0, meaning the first element of a list is at index 0, the second element is at index 1, and so on.
Exemple2:
>>>languages = ['Python', 'Pascal', 'C++','Java']
# Access item at index 0 # items from index 1 to end
>>>print(languages[0]) # Python >>>print(languages[1:]) # 'Pascal', 'C++', 'Java'
# Access item at index 2 # items from index [start:stop:step]
>>>print(languages[2]) # C++ >>>print(languages[0:4 :2]) #'Python', 'C++'
# Access the last item # Reversing a list
>>>print(languages[-1]) # Java >>>print(languages[::-1])
# items from index 1 to index 2 #IndexError
>>>print(languages[1:3]) # 'Pascal', 'C++' >>>print(languages[4])
Page 1 / 5
Ferhat Abbas-Sétif-1 University 1st year Engineering
Faculty of Technologies Module: Inf I
Department of Basic Education in Technology Year: 2023/2024
Exemple3:
>>>languages = ['Python', 'Pascal', 'C++','Java']
>>>languages.append('c') >>>x = [1, 2, 3]; y = [4, 5, 6]
#'Python', 'Pascal', 'C++', 'Java', 'c' >>>x+y # [1, 2, 3, 4, 5, 6]
>>>languages= languages+['R'] >>>x = [1, 2]
# 'Python', 'Pascal', 'C++', 'Java', 'c', 'R' >>>x*5 # [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
>>>languages= languages+'R' >>> [0] * 5
#TypeError (not "str") to list #initialization of a list[0, 0, 0, 0, 0]
Exemple4:
>>>languages = ['Python', 'Pascal', 'C++','Java']
>>>languages[2]='C' >>> languages[1:]=['P','C','J']
#'Python', 'Pascal', 'C', 'Java' # 'Python', 'P', 'C', 'J'
Exemple5:
>>>languages = ['Python', 'Pascal', 'C++','Java']
>>>languages.remove('C++') >>> languages = languages[0:1]+languages[2:]
#'Python', 'Pascal', 'Java' # 'Python', 'Java' remove 'Pascal' from the list.
Exemple6:
>>>languages = ['Python', 'Pascal', 'Java']
>>>langages.insert(2,'c') >>> languages[2:3]=['C','C++']
# 'Python', 'Pascal', 'c', 'Java' # ['Python', 'Pascal', 'C', 'C++', 'Java']
Page 2 / 5
Ferhat Abbas-Sétif-1 University 1st year Engineering
Faculty of Technologies Module: Inf I
Department of Basic Education in Technology Year: 2023/2024
Exemple7:
>>>languages = ['Python', 'Pascal', 'Java']
>>>languages.clear() >>> languages=[]
# [] # []
Exemple8:
>>>languages = ['Python', 'Pascal', 'Java']
>>>for x in languages: >>> for i in range(len(languages)):
print(x) print(languages [i])
#'Python', 'Pascal', 'Java' #'Python', 'Pascal', 'Java'
5. Sort Lists:
List Python has a sort() method that will sort the list alphanumerically, ascending, by default:
Exemple9:
>>>languages = ['Python', 'C++', 'Java']
>>>languages.sort() >>> languages.sort(reverse = True)
>>>print(languages) >>>print(languages)
#'C++', 'Java', 'Python' 'Python', 'Java', 'C++'
6. Copy a List:
You cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference to
list1, and changes made in list1 will automatically also be made in list2. There are ways to make a
copy, one way is to use the built-in List method copy().
Exemple10:
>>>L1= ['Python', 'C++', 'Java']; L2=[5,10,22]
>>>L1=L2 >>>L1[0]='C' >>>L1=L2.copy()
>>> print(L1),print(L2) >>> print(L1),print(L2) >>>L1[0]='C++'
# L1 5, 10, 22 #L1 'C', 10, 22 #L1 'C++', 10, 22
# L2 5, 10, 22 #L2 'C', 10, 22 #L2 'C', 10, 22
Page 3 / 5
Ferhat Abbas-Sétif-1 University 1st year Engineering
Faculty of Technologies Module: Inf I
Department of Basic Education in Technology Year: 2023/2024
7. List Methods:
Python has a set of built-in methods that you can use on lists.
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the
current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
8. Exercises
8.1. Ex1:
Let L1 and L2 be tow lists like L1 = [8.3, 27.2, 42, 17, 4.1]; L2= [15, 5.9, 59, 10.1, 2].
Give the Python script to create the lists L3 and L4:
L3: identical to L1 in which the first value has been deleted ([ 27.2, 42, 17, 4.1]).
L4: L4 containing the L1 values then those of L2([8.3, 27.2, 42, 17, 4.1,15, 5.9, 59, 10.1, 2]).
8.2. Ex2:
We have a non-empty list L of numbers. Create LC a list containing the squares of the numbers in
the list L.
Exp: L = [8, 27, 42, 17, 4], LC=[ 64. 729. 1764. 289. 16.]
8.3. Ex3:
Write a script that displays whether a list is symmetrical (list identical to list upside down).
Exp: [5,7,3,7,5] or [8,10,13,13,10,8]
8.4. Ex4:
Give a script that counts the number of multiples of 3 present in a list L entered by keyboard.
Page 4 / 5
Ferhat Abbas-Sétif-1 University 1st year Engineering
Faculty of Technologies Module: Inf I
Department of Basic Education in Technology Year: 2023/2024
8.5. Ex5:
Write a Python script to exchange the first element with the last element of a given list.
Exp: L = ["Python", "Java", "C ++", "Javascript"] ["Javascript", "Java", "C ++", "Python"]
8.6. Ex6:
Write a Python script that returns DN a list of the divisors of a given integer N.
Exp N = 18 DN=[1, 2, 3, 6, 9, 18]
8.7. Ex7:
T=[12,8,10,6,10,5,11]
Write a Python script which: day=['Sat','Sun','Mon','Tue','Wed','Thu','Fri']
• Create a list that contains the temperatures of the week.
Sat▒▒▒▒▒▒▒▒▒▒▒▒
• Calculates and displays: Sun▒▒▒▒▒▒▒▒
1. The temperature of the hottest day and its index. Mon▒▒▒▒▒▒▒▒▒▒
2. The temperature of the coldest day and its index. Tue▒▒▒▒▒▒
3. The average temperature. Wed▒▒▒▒▒▒▒▒▒▒
4. Displays the temperature histogram.(use chr(9618)). Thu▒▒▒▒▒
5. display the temperatures of the week in descending order. Fri▒▒▒▒▒▒▒▒▒▒▒
Page 5 / 5