Python Imp 2
Python Imp 2
2. What is list and dictionary? What are the differences between List & Dictionary?
3. Explain the methods that are used to delete items from the list
7. Develop suitable Python programs with nested lists to explain copy.copy( ) and
copy.deepcopy( ) methods.
8. Explain the use of in operator and not in operators in list with suitable examples.
Programs
1. Read a multi-digit number (as chars) from the console. Develop a program to print the
frequency of each digit with suitable message.
3. Write a python program to accept n numbers and store them in a list. Then print the list
without ODD numbers in it.
List: A list is an ordered data structure with elements separated by a comma and
enclosed within square brackets. A list is a value that contains multiple values in
an ordered sequence.
A list value looks like this: ['cat', 'bat', 'rat', 'elephant'].
Just as string values are typed with quote characters to mark where the string
begins and ends, a list begins with an opening square bracket and ends with a
closing square bracket, []. Values inside the list are also called items. Items are
separated with commas.
Examples: [1, 2, 3], ['hello', 3.1415, True, None, 42],
spam = ['cat', 'bat', 'rat', 'elephant']
3. Explain the methods that are used to delete items from the list.
Removing Values from Lists with del Statements
The del statement will delete values at an index in a list. All of the values in
the list after the deleted value will be moved up one index. For example
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat']
insert():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.
>>> spam = ['cat', 'dog', 'bat']
>>> spam.insert(1, 'chicken')
>>> spam
['cat', 'chicken', 'dog', 'bat']
sort():Lists of number values or lists of strings can be sorted with the sort()
method. For example, enter the following into the interactive shell:
>>> spam = [2, 5, 3.14, 1, -7]
>>> spam.sort()
>>> spam
[-7, 1, 2, 3.14, 5]
>>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
>>> spam.sort()
>>> spam
['ants', 'badgers', 'cats', 'dogs', 'elephants']
The setdefault() method offers a way to do this in one line of code. The first
argument passed to the method is the key to check for, and the second
argument is the value to set at that key if the key does not exist. If the key does
exist, the setdefault() method returns the key’s value.
numbers = [1,2,3,4,5,1,4,5]
Sum = sum(numbers)
print(Sum)
Output: 25
(iii)max():The max() function returns the item with the highest value, or the
item with the highest value in a list.
a = [1, 5, 3, 9]
x = max(a)
print(x)
output: 9
(iv)min():The min() function returns the item with the lowest value, or the
item with the lowest value in an iterable.
If the values are strings, an alphabetically comparison is done.
a = [1, 5, 3, 9]
x = min(a)
print(x)
output: 1
7. Develop suitable Python programs with nested lists to explain copy.copy()
and copy.deepcopy( ) methods.
If the function modifies the list or dictionary that is passed, you may not want
these changes in the original list or dictionary value. For this, Python provides
a module named copy that provides both the copy () and deepcopy() functions.
The first of these, copy.copy(), can be used to make a duplicate copy of a
mutable value like a list or dictionary, not just a copy of a reference.
>>> import copy
>>> spam = ['A', 'B', 'C', 'D']
>>> cheese = copy.copy(spam)
>>> cheese[1] = 42
>>> spam
['A', 'B', 'C', 'D']
>>> cheese
['A', 42, 'C', 'D']
Now the spam and cheese variables refer to separate lists, which is why only
the list in cheese is modified when you assign 42 at index 7. As you can see
in Figure , the reference ID numbers are no longer the same for both variables
because the variables refer to independent list.
If the list you need to copy contains lists, then use the copy.deepcopy()
function instead of copy.copy(). The deepcopy() function will copy these
inner lists as well.
8. Explain the use of in operator and not in operators in list with suitable
examples.
The in and not in Operators
You can determine whether a value is or isn’t in a list with the in and not in
Operators.
Like other operators, in and not in are used in expressions and connect two
values: a value to look for in a list and the list where it may be found.
These expressions will evaluate to a Boolean value.
>>> 'howdy' in ['hello', 'hi', 'howdy', 'heyas']
True
>>> spam = ['hello', 'hi', 'howdy', 'heyas']
>>> 'cat' in spam
False
>>> 'howdy' not in spam
False
>>> 'cat' not in spam
True
string = "Java"
Input: Java
Output: jAVA