Revision of Python Basics
Revision of Python Basics
2
List
List: It is mutable data type in Python. The content of List can be enclosed
inside a pair of square braces [].
3
Assignment and Traversal
4
Concatenation
5
Concatenation
6
Repetition
7
Concatenation
L=[]
for I in range(4):
To add new content (concatenation)
L+=[int(input(">>"))]
>>23
>>45
>>56
>>87
[23, 45, 56, 87]
8
Concatenation
L=[]
for I in range(4):
To add new content (concatenation)
S=input("Name:")
Name:AMAR
Name:AKBAR
Name:ANTHONY
Name:ROMA
['AMAR', 'AKBAR', 'ANTHONY', 'ROMA']
9
List Mutability
10
List Mutability
Note: We can change the
A=[23,45,67,78]
content of an individual
print(A)
element of the list.
A[0]=38;A[2]=65
11
List Membership
12
List - Functions len(), list(), append()
13
List - Functions len(), list(), append()
A=[34,"AM",[1,"PM"],['A','B','C'],4.3]
for I in A:
if type(I)==list: type() - returns the type of the data
print(I,end="*")
15
List - Functions append() and extend()
A=[] A=[]
A.append(32) # Correct A.append([32,45]) # Correct
A.extend(32) # TypeError A.extend([32,45]) # Correct
In the above example on the right side append() will add a single value
whereas extend() will extend the list by two elements
16
List - Functions len(), list(), extend()
17
List - Function insert()
18
List - Function insert()
19
List - Function count()
2
1
0
0
20
List - Function index()
21
Quick Review - insert(), append(), extend()
22
List - Function remove() and pop()
remove() - deletes the value passed as
A=[23,56,89,56,76]
parameter. Raises ValueError if the value
A.remove(23)
not found
print(A)
pop() - deletes the value as per the index
23
List - del
A=[34,56,78,32,62,91]
del A[3],A[0] del - it is a keyword used to delete an element
print(A) or multiple elements from a list. Raises
24
Quick Review - remove(), pop(), del
25
List - reverse()
26
List - min(),max(),sum() and sort()
A=["Anu","Zen","Jak","Bob"]
print(A)
print("Low:",min(A))
A=["A",25,"CA",56]
print(A)
print("Low:",min(A)) min(),max(),sort() -
30
Logic for finding min and max values
A=[25,67,43,56,12] A=[25,67,43,56,12]
MIN=A[0] MIN=0
MAX=A[0] MAX=0
31
List - slicing
A=[23,43,56,89,65,32]
[23, 43, 56, 89]
print(A[0:4])
[23, 43, 56, 89, 65, 32]
print(A[:6])
32
Logic for finding a Value from a List
A=[56,23,12,78,98,43]
D=int(input("Data:")) Data:65
if D in A: Value: 65 Not Found
print("Value:",D,"Found!") 65 Not Found
34
List -Nested lists
A=[[1,2,3],[3,4,5],[5,6,7]]
for I in A:
print(I) [1, 2, 3]
35
List -Nested lists
A=[[1,2,3],[3,4,5],[5,6,7]]
for I in A:
print(I)
36
List -Nested lists
L=[0,1,2,3,4]
for i in L:
i+=10 # int, float, str - immutable
print(i,end="* ")
37
List -Nested lists
A=[[7,3],[2,1,4],[4,1,5],[8,4,6,1],[1,9]]
A.sort() #Sorting on First value of each
print(A)
[[1, 9], [2, 1, 4], [4, 1, 5], [7, 3], [8, 4, 6, 1]]
[1, 4, 5]
[[1, 9], [2, 1, 4], [1, 4, 5], [7, 3], [8, 4, 6, 1]]
38
List - To find frequency of values in a List
A=[1,2,1,4,3,2,4,5,1]
MIN=min(A) 1 Frequency 3
MAX=max(A) 2 Frequency 2
AVG=sum(A)/len(A)
39
List - To swap the first half of list with second
A=[11,22,33,44,55,66]
0 1 2 3 4 5
L=len(A)
for i in range(L//2): 11 22 33 44 55 66
40
List - To swap the first half of list with second
A=[11,22,33,44,55,66]
L=len(A) Python Way
A[:L//2],A[L//2:]=A[L//2:],A[:L//2]
41
List - To swap the the alternate values
A=[11,22,33,44,55,66]
0 1 2 3 4 5
L=len(A)
print(A) 11 22 33 44 55 66
42
List - To accumulate sum of previous values
A=[11,22,33,44,55,66]
L=len(A)
print(A)
43
List - To find min of 1st half and max of 2nd
A=[1,4,8,3,7,2,5,9,0,5]
L=len(A)
Min of [1, 4, 8, 3, 7] is 1
Max of [2, 5, 9, 0, 5] is 9
44
List - To find sum of 1st half and avg of 2nd
A=[1,4,8,3,7,2,5,9,0,5]
L=len(A)
Sum of [1, 4, 8, 3, 7] is 23
Avg of [2, 5, 9, 0, 5] is 4.2
45
Happy Learning…
46