Python Module 2 Notes
Python Module 2 Notes
1. What is a List? Explain append( ), insert( ) and remove( ) methods with examples
June/July 2023 (8M)
A list is a value that contains multiple values in an ordered sequence.
A list value looks like this: ['cat', 'bat', 'rat', 'elephant'].
A list begins with an opening square bracket and ends with a closing square bracket,
[ ].Values inside the list are also called items and are separated with commas
To add new values to a list, use the append( ) and insert( ) methods.
The append( ) method call adds the argument to the end of the list.
Example:
The insert( ) method can insert a value at any index in the list. The first argument to insert( ) is the
index for the new value, and the second argument is the new value to be inserted.
Example:
remove ( ) methods
The remove( ) method is passed the value to be removed from the list it is called on.
Example:
If the value appears multiple times in the list, only the first instance of the value will be removed.
The remove( ) method is good when you know the value you want to remove from the list.
Example:
A for loop can iterate over the keys, values, or key-value pairs in a dictionary by using keys(),
values(), and items() methods.
The values in the dict_items value returned by the items() method are tuples of the key and value.
Example:
If we want a true list from one of these methods, pass its list-like return value to the list() function.
Example:
The list(spam.keys()) line takes the dict_keys value returned from keys() and passes it to list(),
which then returns a list value of ['color', 'age'].
We can also use the multiple assignment trick in a for loop to assign the key and value to separate
variables.
Example:
3. How is tuple different from a list and which function is used to convert list to tuple? Explain
June/July 2023 (6M )
The tuple data type is almost identical to the list data type, except in two ways.
First, tuples are typed with parentheses, ( and ), instead of square brackets, [ and ].
Example:
Second, benefit of using tuples instead of lists is that, because they are immutable and their contents
don’t change. Tuples cannot have their values modified, appended, or removed.
Example:
If you have only one value in your tuple, you can indicate this by placing a trailing comma after the
value inside the parentheses.
The functions list() and tuple() will return list and tuple versions of the values passed to them.
Converting a tuple to a list is handy if you need a mutable version of a tuple value.
5. Read N numbers from the console and create a list. Develop a program to compute and print
mean, variance and standard deviation with messages June/July 2023 (10M )
Program:
lis=[ ]
For i in range(N):
lis.append(int(input(‘Enter the element:’)))
mean = round(sum(lis)/N,2)
print(‘\nMean is ‘,mean)
lis_mean=[ ]
for i in range(N):
lis_mean.append((lis[i]-mean)**2)
variance = round(sum(lis_mean)/N,2)
print(‘Variance is’,variance)
SD = round(variance**(1/2),2)
print(‘Standard deviation is’,SD)
if SD>=1.5:
print(‘High Dispersion, Low reliability.’)
else:
print(‘Low Dispersion, Reliable.’)
Prof. Jillian Rufus J, Dr. TTIT Page 4
Introduction to Python Programming
Output:
Enter the number of elements: 8
Enter the element: 10
Enter the element: 12
Enter the element: 23
Enter the element: 23
Enter the element: 16
Enter the element: 23
Enter the element: 21
Enter the element: 16
Mean is 18.0
Variance is 24.0
Standard deviation is 4.9
High Dispersion, Low Reliability.
The first time setdefault() is called, the dictionary in spam changes to {'color': 'black', 'age': 5, 'name':
'Pooka'}. The method returns the value 'black' because this is now the value set for the key 'color'. When
spam.setdefault('color', 'white') is called next, the value for that key is not changed to 'white' because
spam already has a key named 'color'.
7. Discuss list and dictionary data structure with example for each. 6M
List
A list is a value that contains multiple values in an ordered sequence.
A list value looks like this: ['cat', 'bat', 'rat', 'elephant'].
A list begins with an opening square bracket and ends with a closing square bracket,
[ ].Values inside the list are also called items and are separated with commas
Example:
Dictionary
A dictionary is a collection of many values. Indexes for dictionaries can use many different data types,
not just integers. Indexes for dictionaries are called keys, and a key with its associated value is called a
key-value pair. A dictionary is typed with braces, {}.
Example:
This assigns a dictionary to the myCat variable. This dictionary’s keys are 'size', 'color', and
'disposition'. The values for these keys are 'fat', 'gray', and 'loud', respectively. You can access these
values through their keys:
8. Read a multi-digit number from the console. Develop a program to print the frequency of each
digit with suitable message.
Program:
import pprint
count={}
count.setdefault(character,0)
count[character] = count[character] +1
pprint.pprint(count)
print( )
for k, v in count.items( ):
Output:
123456123456
{‘1’: 2
‘2’ : 2
‘3’ : 2
‘4’ : 2
‘5’ : 2
‘6’ : 2}