Wednesday, March 18, 2020
Saturday, February 1, 2020
Dictionaries Manipulation Practice
Dictionaries Manipulation Practice
Question:
Write a program to print the worker’s informations (Name age, salary) in records format.
Answer:
Employees = {‘Rohan’ : {‘age’ : 20, ‘salary’ : 10000}, ‘Mohan’ : {‘age’ : 30, ‘salary’ : 15000}}
for key in Employees :
print “Employee”, key + ‘:’
print ‘Age :’ + str (Employees [key] [‘age’]
print ‘Salary :’ + str (Employees [key] [‘salary’])
Write a program to print the worker’s informations (Name age, salary) in records format.
Answer:
Employees = {‘Rohan’ : {‘age’ : 20, ‘salary’ : 10000}, ‘Mohan’ : {‘age’ : 30, ‘salary’ : 15000}}
for key in Employees :
print “Employee”, key + ‘:’
print ‘Age :’ + str (Employees [key] [‘age’]
print ‘Salary :’ + str (Employees [key] [‘salary’])
Question:
What is a key-value pair with reference to Python dictionary ?
Answer:
A dictionary is a mapping between a set of indices (which are called keys) and a set of values. Each key maps a value. The association of a key and a value is called a key-value pair.
What is a key-value pair with reference to Python dictionary ?
Answer:
A dictionary is a mapping between a set of indices (which are called keys) and a set of values. Each key maps a value. The association of a key and a value is called a key-value pair.
Question:
What are the characteristics of Python Dictionaries ?
Answer:
The 3 main characteristics of a dictionary are :
What are the characteristics of Python Dictionaries ?
Answer:
The 3 main characteristics of a dictionary are :
1.
Dictionaries are Unordered : The
dictionary elements (key-value pairs) are not in ordered form.
2.
Dictionary Keys are Case Sensitive :
The same key name but with different case are treated as different keys in
Python dictionaries.
3.
No duplicate key is allowed : When
duplicate keys encountered during assignment, the last assignment wins.
4.
Keys must be immutable : We can use
strings, numbers or tuples as dictionary keys but something like [‘key’] is not
allowed.
Question:
Write a Python program to input ‘n’ names and phone numbers to store it in a dictionary and to input any name and to print the phone number of the particular name.
Answer:
phonebook = dict( )
Write a Python program to input ‘n’ names and phone numbers to store it in a dictionary and to input any name and to print the phone number of the particular name.
Answer:
phonebook = dict( )
n =
int(input("Enter total number of friends"))
i = 1
while
i<=n:
a = input("Enter name")
b = input("Enter phone number")
phonebook[a] = b
i = i + 1
print("Dictionary
is:- ")
print(phonebook)
name =
input("Enter name to search")
if name
in phonebook.keys():
print("Phone number =",
phonebook[name])
else:
print("Given name not exist")
Tuple Manipulation Practice
Tuple Manipulation Practice
Question: Write the output of the
given python code :
(a) tup1 = (12, 34.56);
tup2 = (‘abc’, ‘xyz’);
#Following action is not valid for tuples
#tup1 [0] = 100;
#So let’s create a new tuple as follows
tup3 = tup1 + tup2;
print tup3;
Answer:
(12,34.56, ’abc’, ‘xyz’)
(a) tup1 = (12, 34.56);
tup2 = (‘abc’, ‘xyz’);
#Following action is not valid for tuples
#tup1 [0] = 100;
#So let’s create a new tuple as follows
tup3 = tup1 + tup2;
print tup3;
Answer:
(12,34.56, ’abc’, ‘xyz’)
(b) tuple1, tuple2 = (123, ‘xyz’, ‘zara’,
‘abc’), (456, 700, 200)
print “min value element : “, min (tuple1);
print “min value element : “, min (tuple2);
Answer:
min value element : Error
min value element : 200
print “min value element : “, min (tuple1);
print “min value element : “, min (tuple2);
Answer:
min value element : Error
min value element : 200
Question:
What is a tuple ?
Answer:
Tuple is a sequence of immutable Python objects
What is a tuple ?
Answer:
Tuple is a sequence of immutable Python objects
Question:
Can we remove individual tuple elements ?
Answer:
No, we cannot remove individual tuple elements.
Can we remove individual tuple elements ?
Answer:
No, we cannot remove individual tuple elements.
Question:
Which function gives the total length of the tuple.
Answer:
len(tuple)
Which function gives the total length of the tuple.
Answer:
len(tuple)
Question:
Which function returns item from the tuple with max value.
Answer:
max(tuple)
Which function returns item from the tuple with max value.
Answer:
max(tuple)
Question: Write a program to input any
two tuples and interchange the tupel values.
Answer:
t1 =
tuple()
t2 =
tuple()
n =
int(input("Total number of values n in first tuple"))
for i
in range (n):
a = input("Enter elements")
t1 = t1 + (a, )
m =
int(input("Total number of values m in second tuple"))
for i
in range (m):
a = input("Enter elements")
t2 = t2 + (a, )
print("First
tuple")
print(t1)
print("Second
tuple")
print(t2)
t1, t2
= t2, t1
print("After
swapping")
print("First
tuple")
print(t1)
print("Second
tuple")
print(t2)
Question: Write a program to input ‘n’
numbers and separate the tuple in the following manner.
Example
T=(10,20,30,40,50,60)
Tl=(10,30,50)
T2=(20,40,60)
Answer:
t=tuple( )
n=input(” Enter number of values:”)
for i in range(n):
a=input(“enter elements”)
t=t+(a,)
t1=t2=tuple( )
for i in t:
if i%2 == 0:
t2= t2 + (i,)
else:
tl=tl+(i,)
print “First Tuple”
print t1
print “Second Tuple”
print t2
Example
T=(10,20,30,40,50,60)
Tl=(10,30,50)
T2=(20,40,60)
Answer:
t=tuple( )
n=input(” Enter number of values:”)
for i in range(n):
a=input(“enter elements”)
t=t+(a,)
t1=t2=tuple( )
for i in t:
if i%2 == 0:
t2= t2 + (i,)
else:
tl=tl+(i,)
print “First Tuple”
print t1
print “Second Tuple”
print t2
List Manipulation practice
List
Manipulation
Question: Write the output of the given Python
code.
(a) list1 = [‘physics’, ‘chemistry’, 1997,
2000];
list2 = [1,2,3,4,5,6, 7];
print “list1[0]”, list1[0]
list2 = [1,2,3,4,5,6, 7];
print “list1[0]”, list1[0]
Answer:
list1[0]:
physics
(b) listl = [‘physics’, ‘chemistry’,
1997,2000];
list2 = [1,2, 3,4,5, 6, 7];
print “list2[l:5[ :”, list2[l:5]
list2 = [1,2, 3,4,5, 6, 7];
print “list2[l:5[ :”, list2[l:5]
Answer:
list2[1:5[:
[2,3,4,5]
(c) list1 = [‘physics’, ‘chemistry’, 1997,
2000];
list2 = [1, 2, 3, 4, 5, 6, 7];
print “list1 [0]:”,
list1 [0] print “list2[1 :5]:”, list2[1:5]
list2 = [1, 2, 3, 4, 5, 6, 7];
print “list1 [0]:”,
list1 [0] print “list2[1 :5]:”, list2[1:5]
Answer
list1 [0]:
physics
list2[1:5] : [2, 3, 4, 5]
list2[1:5] : [2, 3, 4, 5]
(d) listl = [‘physics’, ‘chemistry’, 1997,
2000];
print “Value available at index 2 :”
print list[2];
list[2] = 2001;
print “New value available at index 2 :”
print list [2];
print “Value available at index 2 :”
print list[2];
list[2] = 2001;
print “New value available at index 2 :”
print list [2];
Answer
Value
available at index 2 : 1997
New value available at index 2 : 2001
New value available at index 2 : 2001
(e) list1 = [‘physics’, ‘chemistry’, 1997,
2000];
print list1;
del list1 [2];
print “After deleting value at index 2 :”
print list1;
print list1;
del list1 [2];
print “After deleting value at index 2 :”
print list1;
Answer
[‘physics’,
‘chemistry’, 1997, 2000];
After deleting value at index 2;
[‘physics’, ‘chemistry’, 2000]
After deleting value at index 2;
[‘physics’, ‘chemistry’, 2000]
(f)
aList
= [123, ‘xyz’, ‘zara’, ‘abc’, 123];
bList = [2009, ‘manni’];
aList.extend (bList)
print “Extended List :”, aList;
bList = [2009, ‘manni’];
aList.extend (bList)
print “Extended List :”, aList;
Answer
Extended List : [123, ‘xyz’, ‘zara’,
‘abc’, 123, 2009, ‘manni’]
(g) aList1 = [123, ‘xvz’, zara’, abc’];
print “Index for xyz : ” aList. index( ‘xyz’);
print “Index for zara :”, aList. index(‘zara’);
print “Index for xyz : ” aList. index( ‘xyz’);
print “Index for zara :”, aList. index(‘zara’);
Answer
Index for xyz : 1 Index for xxx : 2
(h) aList = [123, ‘xyz’, ‘zara’, ‘abc’];
aList.insert (3,2009)
aList.insert (3,2009)
print “Final List:”, aList
Answer:
Final List: [123, ‘xyz’, ‘zara’, 2009, ‘abc’]
Final List: [123, ‘xyz’, ‘zara’, 2009, ‘abc’]
(i)
aList1
= [123, ‘xyz’, ‘zara’, ‘abc’];
print “A List:”, aList.pop()
print “B List:”, aList.pop(2)
print “A List:”, aList.pop()
print “B List:”, aList.pop(2)
Answer:
A List: abc
B List: zara
(j)
A =
[2, 4, 6, 8,10]
L = len (A)
S = o
for I in range (1, L, 2):
S + = A[I]
print “Sum=”, S
L = len (A)
S = o
for I in range (1, L, 2):
S + = A[I]
print “Sum=”, S
Answer:
Sum = 12
(k) A= (10:1000,20:2000,30:3000,40:4000,
50:5000}
print A.items()
print A.keys()
print A.values()
print A.items()
print A.keys()
print A.values()
Answer:
[(40,
4000), (10, 1000), (20, 2000), (50, 5000), (30, 3000)]
[40,10, 20, 50, 30]
[4000,1000, 2000,5000,3000]
[40,10, 20, 50, 30]
[4000,1000, 2000,5000,3000]
Question:
How can we remove list element ?
Answer:
To remove a list element, you can use either the del statement or the remove method
How can we remove list element ?
Answer:
To remove a list element, you can use either the del statement or the remove method
Question:
What is the use of len(list)) in Python?
Answer:
It gives the total length of the list.
What is the use of len(list)) in Python?
Answer:
It gives the total length of the list.
Question:
Describe max(list) method.
Answer:
It returns item from the list with max value.
Describe max(list) method.
Answer:
It returns item from the list with max value.
Question:
Describe min(list) method.
Answer:
It returns item from the list with min value.
Describe min(list) method.
Answer:
It returns item from the list with min value.
Question:
What is the use of list(seq) in Python ?
Answer:
It converts a tuple into list.
What is the use of list(seq) in Python ?
Answer:
It converts a tuple into list.
Question:
What do you mean by list.append(obj)
Answer:
Appends object obj to list
What do you mean by list.append(obj)
Answer:
Appends object obj to list
Question:
What do you mean by list.count(obj)
Answer:
Returns count how many times obj occurs in list
What do you mean by list.count(obj)
Answer:
Returns count how many times obj occurs in list
Question:
Explain list.extend(seq)
Answer:
Appends the contents of seq to list.
Explain list.extend(seq)
Answer:
Appends the contents of seq to list.
Question:
Is list.reverse() method return a value?
Answer:
This method does not return any value but reverse the given object from the list
Is list.reverse() method return a value?
Answer:
This method does not return any value but reverse the given object from the list
Question:
Which method is used to sort objects of list.
Answer:
sort ()
Which method is used to sort objects of list.
Answer:
sort ()
Question:
Which function is used to reverse objects of list in place.
Answer:
list.reverse ()
Which function is used to reverse objects of list in place.
Answer:
list.reverse ()
Question:
Which function is use for removing object obj from list.
Answer:
list.remove(obj)
Which function is use for removing object obj from list.
Answer:
list.remove(obj)
Question:
Which function is use for returning the lowest index in list that obj appears.
Answer:
List index (obj).
Which function is use for returning the lowest index in list that obj appears.
Answer:
List index (obj).
Question:
How are lists different from strings
when both are sequences ?
Answer:
The lists and strings are different in following ways :
The lists and strings are different in following ways :
(a)
The lists are mutable sequences while strings are immutable.
(b) Strings store single type of elements, all characters while lists can store elements belonging to different types.
(c) In consecutive locations, strings store the individual characters while list stores the references of its elements.
(b) Strings store single type of elements, all characters while lists can store elements belonging to different types.
(c) In consecutive locations, strings store the individual characters while list stores the references of its elements.
Question:
Write a program to calculate and display the sum of all the odd numbers in the list.
Answer:
Write a program to calculate and display the sum of all the odd numbers in the list.
Answer:
pos
= 0
sum = 0
while pos < len (L):
if L[pos] %2 = = 1 :
sum = sum + L [pos]
pos = pos + 1
print sum
sum = 0
while pos < len (L):
if L[pos] %2 = = 1 :
sum = sum + L [pos]
pos = pos + 1
print sum
Question:
Write a program to perform various list operations after displaying menu.
Answer:
Write a program to perform various list operations after displaying menu.
Answer:
ch
= 0
list = [ ]
while true :
print “List Menu”
print “1. Adding”
print “2.Modify”
print “3.Delete”
print “4.Sort list”
print “5.Display list”
print “6.Exit”
ch = int (input (“Enter your choice :”))
list = [ ]
while true :
print “List Menu”
print “1. Adding”
print “2.Modify”
print “3.Delete”
print “4.Sort list”
print “5.Display list”
print “6.Exit”
ch = int (input (“Enter your choice :”))
if
ch = = 1 :
print “1.Add element”
print “2.Add a List”
ch1 = int (input(“Enter choice 1 or 2:”))
if chi = = 1:
item = int(input (“Enter element:”))
pos = int (input(“Enter position to add : “))
list.insert (pos, item)
elif chi = = 2 :
list2 = eval (input (“Enter list:”))
list.extend (lst2)
else:
print “Valid choices are 1 or 2”
print “Successfully added”
print “1.Add element”
print “2.Add a List”
ch1 = int (input(“Enter choice 1 or 2:”))
if chi = = 1:
item = int(input (“Enter element:”))
pos = int (input(“Enter position to add : “))
list.insert (pos, item)
elif chi = = 2 :
list2 = eval (input (“Enter list:”))
list.extend (lst2)
else:
print “Valid choices are 1 or 2”
print “Successfully added”
elif
ch = = 2 :
pos = int (input(“Enter position :”))
intem = int (input(“Enter new value :”))
old = list[pos]
list[.pos] = item
print old, ‘modified with vlaue”, item
pos = int (input(“Enter position :”))
intem = int (input(“Enter new value :”))
old = list[pos]
list[.pos] = item
print old, ‘modified with vlaue”, item
elif
ch = = 3:
print “1.Delete element by position”
print “2.Delete element by value”
ch1 = int (input (“Enter you choice 1 or 2’))
if chi = = 1:
pos = int (input (“Enter position :”))
item = list.pop (pos)
print item, “deleted”
elif ch1 = = 2:
item = int (input (“Enter element:”))
pos = list.remove (item)
print “Succcessfully delted”
else :
print “Valid choices are 1 or 2”
elif ch = = 4 :
print “l.Ascending”
print “1.Delete element by position”
print “2.Delete element by value”
ch1 = int (input (“Enter you choice 1 or 2’))
if chi = = 1:
pos = int (input (“Enter position :”))
item = list.pop (pos)
print item, “deleted”
elif ch1 = = 2:
item = int (input (“Enter element:”))
pos = list.remove (item)
print “Succcessfully delted”
else :
print “Valid choices are 1 or 2”
elif ch = = 4 :
print “l.Ascending”
print “2.Descending”
chi = int (input (“Enter choice 1 or 2”))
if chi = = 1:
list. sot ()
chi = int (input (“Enter choice 1 or 2”))
if chi = = 1:
list. sot ()
elif ch1 = = 2:
list.sort (reverse = True)
else :
list.sort (reverse = True)
else :
print “Valid choices are 1 or 2”
elif
ch = = 5:
print list
print list
elif
ch = = 6:
break
else :
print “valid choice 1 to 6”
break
else :
print “valid choice 1 to 6”
Subscribe to:
Posts (Atom)