Ch8 Answers
Ch8 Answers
Ans: Like integers and strings, list is also a data type in python. An integer variable can have only one value at
a time but a list variable can have multiple values. The value in the list are known as elements. These elements
can be of different datatype.
Example of String-
student= [1,”shanasib” 98.9 “rohan”]
fruits=[‘Mango’,”Apple’,’Orange’,’Guava’,’Watermelon’]
As shown above the elements in a list are enclosed in square brackets and are separated using a comma
We can access elements in a list by specifying its index. Index of list starts with 0.
Example:
>>>>Flower= [“rose”, “sunflower” , “tulip” , “marigold” , “lily”]
“rose” “sunflower” “tulip” “marigold” “lily”
index 0 1 2 3 4
Ans: Slicing operation is used to extract a part of a list. In slicing, we specify the start and end index+1 numbers
with the step value to specify the elements to be extracted.
Syntax: string[start:stop:step]
Example- a=[‘SBI’,’UBI’,’PNB’,’AXIS’,’RBI’,’ICICI’,’HDFC’]
Expression Output
a[1:4:1] #Slice starts from index 1 to index 3 [‘UBI’,’PNB’,’AXIS’]
a[ :3:1] #Slice starts from index 0 to 2 [‘SBI’,’UBI’,’PNB’]
a[2: :1] #Slice starts from index 2 to last index [’PNB’,’AXIS’,’RBI’,’ICICI’,’HDFC’]
A[1:6:2]#Slice starts at 1 the \n 3 then 5 [’UBI’,’AXIS’,’ICICI’]
4.Differentiate between the list methods: extend() and append() ?
2. country=[‘India’,’Bhutan’,’Cananda’,’Nigeria’,’