List and tuple datatype in Python (1)
List and tuple datatype in Python (1)
Ques : How are the indexes allotted in a List in Python? How can we access individual
characters in a List in Python?
Python indexes the list element from left to right and from right end to left. From left to right,
the first element of a list has the index 0 and from right end to left, the extreme right index
element of a list is –1. Individual elements in a list can be accessed by specifying the list name
followed by a number in square brackets ([]).
Ques : Explain how do you use slice operator to replace list elements?
Assignment operator (=) is used to replace the elements specified with slice operator. For
example:
L1 = ['English', 65 , 'Maths' , 97, 'Science', 89 ]
L1[1] = 90 ['English', 90, 'Maths', 97, 'Science', 89] Replaces the element at index 1 to 90
print(L1) Prints the entire List
L1[2:4] = [‘IP’,99] ['English', 90, 'IP', 99, 'Science', 89] Replaces elements from index 2 to 3
print(L1) Prints the entire List
L1[1:3] = [ ] ['English', 99, 'Science', 89] Replaces elements from 1 to 2 with [ ]
print(L1) Prints the entire List
is Is operator evaluate to True if the variables on either side of the operator point to the
same object (i.e having same ids) and False otherwise.
L1 = [1 , 15 , 10 , 5, - 99, 1 00]
L2 = L1
pri n t( L1) # [1 , 15 , 10 , 5, - 99, 1 00]
pri n t( L2) # [1 , 15 , 10 , 5, - 99, 1 00]
pri n t(i d(L1 )) # 57 902 504
pri n t(i d(L2 )) # 57 902 504
pr i nt (L 1 i s L 2 ) # T ru e
L2[ 0] =1 0
pri n t( L1) # [ 10 , 1 5, 1 0, 5 , - 99, 1 00]
pri n t( L2) # [ 10 , 1 5, 1 0, 5 , - 99, 1 00]
is not Is not operator evaluate to True if the variables on either side of the operator point to
the different objects and False otherwise.
L1 = [1 , 15 , 10 , 5, - 99, 1 00]
L2 = [1 , 15 , 10 , 5, - 99, 1 00]
pri n t(i d(L1 )) # 6 524 257 6
pri n t(i d(L2 )) # 1 772 250 4
pr i nt (L 1 i s not L2 ) # T ru e
Page 3 of 10
List and Tuple datatype in Python
Ques: Explain the use of del statement in Python for removing a list element
The del statement is used to delete elements from a list. del statement deletes the element but
does not return the removed item. For example:
Name = ['Anmol', 'Kiran', 'Vimal', 'Sidharth', 'Riya', 'Vimal']
del Name[0] # deletes the first element, i.e. 'Anmol'
print (Name) # returns: ['Kiran', 'Vimal', 'Sidharth', 'Riya', 'Vimal']
Num = [23, 54, 34, 44, 35, 66, 27, 88, 69, 54]
del Num[4] # deletes the 5th element, i.e., 35
print (Num) # returns: [23, 54, 34, 44, 66, 27, 88, 69, 54]
L1 = [‘Eng’, 99,’Sc’,89]
del L1[3] # Removes the element at index 3
print(L1) # ['Eng', 99,’Sc.’]
del [ 2: ] # Removes all elements from index 2 onwards
print(L1) # ['Eng', 90]
del L1 # Removes the entire list
0 1 2
0 1 2 0 1
red yellow blue Pink cyan teal
-3 -2 -1 -2 -1
-3 -2 -1
Page 5 of 10
List and Tuple datatype in Python
Ques: Consider the following lists and predict the output of the following?
L1 = [‘Siddhant’, 12, ‘Monica’, 34 ]
L2 = [ ‘Sarika’, 54 ]
____________________________________________________________________________
Ques: Write a single statement to create a list L3 containing 5 zeros using Repetition
operator (*).
____________________________________________________________________
print(L1[2:5])
print(L1[2:22])
print(L1[1:-2])
print(L1[ :4])
print(L1[2: ])
print( L1 [:] )
L1[1] = 90
print (L1)
del L1[3]
print (L1)
Ques:To print the 4th value in the list L1, Of the following statements, which one are
correct? Justify.
print ( L1[3] )
print ( L1[2+1] )
print ( L1[3.0] )
___________________________________________________________________
Page 6 of 10
List and Tuple datatype in Python
Ques:Write the statement to replace ‘Mathematics’, 87, ‘Science’ with empty List [ ]
___________________________________________________________________.
Ques: How can you get a List of Sub Strings from a given String?
split() method returns a list of sub strings after breaking the given string by the specified
separator.
str.split (separator, maxsplit)
separator : The string splits at the specified separator. If is not provided then any white space
(space, newline, tab) is a separator.
maxsplit : It is the maximum number of splits to be done. If it is not provided then there is no
limit.
For Example:
Page 7 of 10
List and Tuple datatype in Python
breakfast = 'Menu- Bread Butter Buiscuit Banana'
print (breakfast.split()) ['Menu-', 'Bread', 'Butter', 'Buiscuit', 'Banana']
print (breakfast.split(' ',4)) ['Menu-', 'Bread', 'Butter', 'Buiscuit', 'Banana']
print (breakfast.split(' ',3)) ['Menu-', 'Bread', 'Butter', 'Buiscuit Banana']
print (breakfast.split(' ',2)) ['Menu-', 'Bread', 'Butter Buiscuit Banana' ]
print (breakfast.split(' ',1)) ['Menu-', 'Bread Butter Buiscuit Banana' ]
print (breakfast.split(' ',0)) ['Menu- Bread Butter Buiscuit Banana' ]
print (breakfast.split('B')) ['Menu- ', 'read ', 'utter ', 'uiscuit ', 'anana']
print (breakfast.split('B',4)) ['Menu- ', 'read ', 'utter ', 'uiscuit ', 'anana']
print (breakfast.split('B',3)) ['Menu- ', 'read ', 'utter ', 'uiscuit Banana']
print (breakfast.split('B',2)) ['Menu- ', 'read ', 'utter Buiscuit Banana' ]
print (breakfast.split('B',1)) ['Menu- ', 'read Butter Buiscuit Banana' ]
print (breakfast.split('B',0)) ['Menu- Bread Butter Buiscuit Banana' ]
color = "red:green:blue"
a,b,c = col or.spli t(':')
print(a,c)
grocery = 'cheese,eggs,tea,coffee'
grocery.split(',')
grocery.split(',',1)
grocery.split(',',2)
grocery.split(',',3)
txt = "apple#banana#cherry#orange"
x = txt.split("#",2)
print(x)
Page 8 of 10
List and Tuple datatype in Python
Ques: What is a tuple in Python?
A tuple is a sequence of comma separated values (items) enclosed in round brackets. Items in
the tuple may have different data types.
For Example:
T1 = (‘Aman’, 12, 4500, ‘Jatin’, 34, 8793) # A tuple with integers and strings
T2 = (12, ‘Monica’, 89.8, ‘Shilpa’, (45, 67), 78.5) # A Nested tuple
T3 = (‘E001’, [‘D01’, T05’], 23, 12) # A tuple having a list
T4 = ( ) # An empty tuple with no contents
T5 = ( 23, ) # Tuple with a single value has a comma at the end
Page 9 of 10
List and Tuple datatype in Python
Slicing operation Result Explanation
print(T1[1] ) 12 Prints the second item in the tuple
print(T1[1:4]) (12, 'seema', (13, 17)) Prints elements from index 1 to 3
print(T1[1:-2]) (12,) Prints element from 1, stops before -2
print(T1[ : 3]) ('monica', 12, 'seema') Prints elements from index 0 to index 2
print(T1[2: ] ) ('seema', (13, 17)) Prints elements from index 2 till the end
print (T1 [ 1: 4 :2]) (12, (13, 17)) Prints every 2nd character from 1st to 3rd character
print (T1 [ : : 2 ]) ('monica', 'seema') Prints every 2nd character from 0th to last character
print (T1 [ : :-2]) ((13, 17), 12) Prints every 2nd character from last to beginning character
T1[2] = 'garima' Error Because tuple is immutable, values cannot be changed
0 1 2 3
0 1
10 20 30 40 50
-2 -1
-4 -3 -2 -1
a = (10,20,[30,40],50)
a[0] =100 # error, because tuple value cannot be changed
a[2][0] =60 # list value changed
print(a) # (10, 20, [60, 40], 50)
Ques: Explain the use of del statement in Python for removing tuple elements
Page 10 of 10