0% found this document useful (0 votes)
32 views4 pages

Ch8 Answers

This document provides examples of list methods in Python like max(), min(), count() and examples of adding elements to lists using input, eval() function, and appending only even numbers from one list to another. It also answers questions about lists - what are lists, how to access elements, difference between lists and strings, list slicing, and difference between list methods append() and extend(). It ends with lab activities - calculating sum of odd numbers from a user input list and slicing a country list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

Ch8 Answers

This document provides examples of list methods in Python like max(), min(), count() and examples of adding elements to lists using input, eval() function, and appending only even numbers from one list to another. It also answers questions about lists - what are lists, how to access elements, difference between lists and strings, list slicing, and difference between list methods append() and extend(). It ends with lab activities - calculating sum of odd numbers from a user input list and slicing a country list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Notes:-

More Functions in Python:

Example list- num=[34,56,83,92,52,83]


List Method & Description Example Output
max() – Returns max number in list print(num.max()) 92
min() - Returns minimum number in list print(num.min()) 36
count()- returns count of given value in list Print(num.count(83)) 2
#Adding elements of the list – by taking input at runtime
marks=[]
for I in range(0,7): # Taking 6 subject marks
m=int(input (“Marks = “))
marks.append(m)
print(marks) #Print all the 6 subject marks
#Adding elements of the list – by using eval() function- give input by enclosing in []
items=eval(input(“Enter elements of list”))
print(items)
#Program to add only even of a list to another list
list1=[48,23,67,24,89,44]
list2=[]
for num in list1:
if num%2==0:
list2.append(nu0
print(list2) #prints [48,24,44]
1.What are lists in python? how can you access the different element of a list.

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

>>>>print(flower[0]) # prints - rose


>>>>print(flower[2]) #primts- tulip
>>>>print(flower[4]) #prints - Lily
2.Give the difference between list and string?
Ans: Both list and strings belong to the sequence type in python. But there are some difference between two.
Lists Strings
Lists are mutable Strings are immutable
Lists is a collection of Element that may be of String is a collection of characters.
different type
3. What is lists slicing? Give an example.

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() ?

function/method description example


append() Add the element passed as an a=[11,22,33,44,55]
argument at the end of the list val=67
Adds single element at end of list a.append(val)
print (a)
Output:
[11,22,33,44,55,67]
extend() Accepts a list as an argument and a=[11,22,33,44,55]
adds all elements of this list to the b=[66,77,88]
end of the given list a.extend(b)
print (a)
Adds multiple element at end of list Output:
[11,22,33,44,55,66,77,88]
Lab Activity

1. list1=eval(input(“Enter a list of numbers”))


sum=0
for num in list1:
if num%2==1:
sum=sum+num
print(sum)

2. country=[‘India’,’Bhutan’,’Cananda’,’Nigeria’,’

You might also like