Assignment 4
Assignment 4
1. List
Lists in Python are used to store collection of heterogeneous items i.e you can store items of different data types in a single list.
Lists are mutable, meaning that you can add, delete, or change elements in a flexible manner.
You can recognize lists by their square brackets [ and ] that hold elements, separated by a comma ,.
Lists are built into Python: you do not need to invoke them separately.
A) Creating Lists
In [7]: # . Create an empty list with name mylist
mylist= []
Length of mylist: 0
In [21]: # Dsiplay L1
print("L1:", L1)
L1: []
In [25]: languages
Out[29]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
In [31]: # create a list which contains [10,20] 5 times using replication operator
replicated_list = [10, 20] * 5
In [33]: replicated_list
Out[33]: [10, 20, 10, 20, 10, 20, 10, 20, 10, 20]
1. Positive Indexing
2. Negative Indexing
Positive Indexing
Positive indexing begins at 0 for the leftmost/first item, and then traverses right.
In [ ]: # Retrieve the first element , then third element , then last element one by one
Out[42]: True
Out[44]: 100
Out[46]: 'C'
Negative Indexing
Contrary to positive indexing, negative indexing begins at -1 for the rightmost/last item, and then traverses left
Out[50]: 'C'
Out[56]: 'Python'
C). Slicing
In [64]: # Consider the list below. You can create it using a range
l2=list(range(1,20))
print(l2)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[1, 2, 3, 4, 5]
In [68]: #Create a list which contians elemenst from index 5 till index 10
sublist2 = l2[5:11]
print(sublist2)
In [70]: #Create a sublist that contains elements from index 5 till the last element
sublist3 = l2[5:]
print(sublist3)
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
In [72]: #create a sublist that conatins last 5 elements using negative index
sublist4 = l2[-5:]
print(sublist4)
In [76]: #Create a sublist which displays all the number at even index from l2
sublist5 = l2[::2]
print(sublist5)
D) Useful Methods
In [43]: #Add an element at the end of the list
mylist=['C++' , 12, 20, 50.5 , True ]
list2=[10,20,30,40,50]
1. append()
To add an element at the end of a list
Out[55]: ['C++', 12, 20, 50.5, True, 'python', 300, [10, 20, 30]]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[57], line 2
1 # Try to append multiple elements like append(10,20,30) to mylist
----> 2 mylist.append(10,20,30)
Out[61]: ['C++', 12, 20, 50.5, True, 'python', 300, [10, 20, 30], '10,20,30']
2. extend()
To append elements from another list to the current list, use the extend() method.
Out[65]: ['C++',
12,
20,
50.5,
True,
'python',
300,
[10, 20, 30],
'10,20,30',
10,
20,
30,
40,
50]
Out[69]: ['C++',
12,
20,
50.5,
True,
'python',
300,
[10, 20, 30],
'10,20,30',
10,
20,
30,
40,
50,
True,
False]
3. remove()
The remove() method removes the first occurence of the specified item.
Out[71]: ['C++',
12,
20,
50.5,
True,
'python',
300,
[10, 20, 30],
'10,20,30',
10,
20,
30,
40,
50,
True,
False]
Out[73]: ['C++',
12,
50.5,
True,
'python',
300,
[10, 20, 30],
'10,20,30',
10,
20,
30,
40,
50,
True,
False]
4. pop()
remove the element at the specified index , or the last element if index is not specified
Out[130… 'python'
Out[132… 'C++'
5. sort()
sorts the elements
In [138… l1=[23,6,89,45,3,15,90]
#Sort the elements in the list in ascending order
l1.sort()
l1
6. index()
returns the index of the first occurence of a specified element
Out[148… 1
7. count()
Returns the numer of occurences of an element
Out[150… 2
8. copy()
Creates a copy of a list
In [152… lang=languages.copy()
In [154… lang
9. reverse()
Reverses the order of a list
In [156… lang.reverse()
In [158… lang
10.clear()
Clear the contents of a list
In [160… languages.clear()
In [162… languages
Out[162… []
11. insert()
To insert a new list item, without replacing any of the existing values, we can use the insert() method.
In [15]: languages
In [19]: languages
Out[19]: ['C', 'HTML', 'C#', 'C++', 'java', 'PHP', 'python', 'golang', 'python']
In [75]: mylist
Out[75]: ['C++',
12,
50.5,
True,
'python',
300,
[10, 20, 30],
'10,20,30',
10,
20,
30,
40,
50,
True,
False]
12. Modification
In [21]: ## Modification
languages
Out[21]: ['C', 'HTML', 'C#', 'C++', 'java', 'PHP', 'python', 'golang', 'python']
Out[23]: ['C', 'HTML', 'C#', 'C++', 'python', 'PHP', 'python', 'golang', 'python']
In [79]: mylist
Out[79]: ['C',
12,
50.5,
True,
'python',
300,
[10, 20, 30],
'10,20,30',
10,
20,
30,
40,
50,
True,
False]
List Operators
+ Operator
In [209… human_languages=['English','Chinese','French']
In [211… languages
Out[211… ['C', 'HTML', 'C#', 'C++', 'python', 'PHP', 'python', 'golang', 'python']
In [215… languages_c=languages+human_languages
In [217… languages_c
Out[217… ['C',
'HTML',
'C#',
'C++',
'python',
'PHP',
'python',
'golang',
'python',
'English',
'Chinese',
'French']
in Operator
In [219… languages
Out[219… ['C', 'HTML', 'C#', 'C++', 'python', 'PHP', 'python', 'golang', 'python']
Out[221… True
not in Operator
Practice Programs
In [85]: ### 1. Create a list of first 20 odd numbers and find their sum
l5= [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39]
sum_off_odds= sum(l5)
print(l5)
print(sum_off_odds)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39]
400
In [9]: ### 2. Create a list of 10 integers and find the maximum/Minimum value and its index
L4=[10, 24, 5, 67,90,78,56,57,200,5, 3, 89]
max_value = max(L4)
min_value = min(L4)
max_index = L4.index(max_value)
min_index = L4.index(min_value)
print(L4)
print(max_index)
print(min_index)
In [11]: # . Write a program that finds the maximum value and its position in a given list
L4 = [10, 24, 5, 67, 90, 78, 56, 57, 200, 5, 3, 89]
max_value = max(L4)
max_index = L4.index(max_value)
List: [10, 24, 5, 67, 90, 78, 56, 57, 200, 5, 3, 89]
Maximum value: 200
Position (Index): 8
print("Integers:", int_count)
print("Floats:", float_count)
print("Strings:", str_count)
print("Booleans:", bool_count)
Integers: 9
Floats: 3
Strings: 3
Booleans: 2