Chapter 1 Solution
Chapter 1 Solution
Answer =
1. easy to use
2. expressive language
3. its completeness
4. cross platform
5. Free and open source
6. various uses
Answer =
1. Interactive mode
2. Script mode
Answer =
Advantage :-
Disadvantage :-
Answer =
Advantage :-
Disadvantage :-
Answer =
(a) 3 + 8.0 + 6 * 12
(b) print (16 + 5.0 + 44.0)
Q. What are operators? Give examples of some unary and binary operators.
Answer =
Function of operator:-
To operate two operand by instruction of user.
Answer =
Answer =
1. Function
2. Expression
3. Statement
4. Comments
5. Blocks
6. Indentation
Answer =
Variable is a label that can used in process of program. It can contain numeric values, character, string is called
variable.
Variable play very important role in programming because they enable programmers to write flexible programs.
Without variable we cannot enter the data directly in the program.
(i)
for i in '123':
print("guru99", i)
(ii)
print(i)
(iii)
print (j * 2)
(iv)
(v)
if (x == 15):
break
print (x)
(vi)
if (x % 2 == 0) :
continue
print (x)
Answer =
Outputs: -
(i)
guru99 1
guru99 2
guru99 3
(ii)
100
200
300
(iii)
20
16
(iv)
1 1
2 1
2 2
3 1
3 2
3 3
4 1
4 2
4 3
4 4
5 1
5 2
5 3
5 4
5 5
(v)
10
11
12
13
14
(vi)
11
13
15
17
19
if x > 25 :
print("ok")
if x > 60 :
print("good")
elif x > 40 :
print("average")
else:
print("no output")
Output:-
ok
>>>
Because if 'if' statement becomes true then 'elif' and 'else' statements will not execute.
Answer =
Answer =
Similarity:-
Answer =
Similarity:-
List and string both are used to store value /data.
Both are sequence.
Answer =
Lists called a mutable data type because we can change element of a list in place.
In other words, the memory address of a list will not change even after change in its values.
Answer =
Insert() command insert the data at given index number while append() command insert the data at the
end of the list.
Answer =
Q. Write a code to calculate and display total marks and percentage of a student from a given list storing
the marks of a student.
Answer =
Q. Write a Program to multiply an element by 2 if it is an odd index for a given list containing both
numbers and strings.
Answer =
Answer =
Output :-
Frequency of 1 = 2
Frequency of 5 = 1
Frequency of 3 = 3
Frequency of 9 = 2
Frequency of 4 = 2
Frequency of 2 = 1
Frequency of 0 = 2
Frequency of 8 = 1
Frequency of 6 = 2
Frequency of 9 = 2
Frequency of 4 = 2
Frequency of 0 = 2
Frequency of 3 = 3
Frequency of 1 = 2
Frequency of 7 = 1
Frequency of 6 = 2
Frequency of 3 = 3
>>>
Q. Write a Program to shift elements of a list so that the first element moves to the second index and
second index moves to the third index, and so on, and the last element shifts to the first position.
Answer =
Write a function to swap the content with the next value divisible by 5 so that the resultant list will look like:
Answer =
Output :-
Q. Write a program to accept values from a user in a tuple. Add a tuple to it and display its elements one
by one. Also display its maximum and minimum value.
Answer =
Output :-
Q. Write a program to input any values for two tuples. Print it, interchange it and then compare them.
Answer =
tup1,tup2 = tup2,tup1
Q. Write a Python program to input 'n' classes and names of their class teachers to store them in a
dictionary and display the same. Also accept a particular class from the user and display the name of the
class teacher of that class.
Answer =
dic = {}
while True :
clas = int(input("Enter class :-"))
teach = input("Enter class teacher name :-")
dic[clas]= teach
a = input("Do you want to enter more records enter (Yes/ No)")
if a == "No" or a == "no":
break
clas = int(input("Enter class which you want to search"))
print("class teacher name",dic[clas])
Q. Write a program to store student names and their percentage in a dictionary and delete a particular
student name from the dictionary. Also display the dictionary after deletion.
Answer =
dic = {}
while True :
nam = input("Enter name :- ")
per = float(input("Enter percentage :- "))
dic[nam] = per
a = input("Do you want to enter more records enter (Yes/ No) :- ")
print()
if a == "No" or a == "no":
break
Output :-
Q. Write a Python program to input names of 'n' customers and their details like items bought, cost and
phone number, etc., store them in a dictionary and display all the details in a tabular form.
Answer =
dic = {}
while True :
nam = input("Enter name :- ")
phone = float(input("Enter phone number :- "))
cost = float(input("Enter cost :- "))
item = input("Enter item :- ")
dic[ nam ] = [ phone , item , cost ]
a = input("Do you want to enter more records enter (Yes/ No) :- ")
if a == "No" or a == "no":
break
for i in dic :
print()
print("Name : ", i )
print("Phone : ", dic[ i ][ 0 ] , "\t", "Cost : ", dic[ i ][ 1 ] , "\t", "Item : ", dic[ i ][ 2 ])
Output :-
Name : Path
Phone : 123456789.0 Cost : Computer Item : 89.0
Name : Walla
Phone : 987654321.0 Cost : Laptop Item : 99.8
Name : Portal
Phone : 567894321.0 Cost : CPU Item : 75.0
Name : Express
Phone : 123498765.0 Cost : Mouse Item : 150.0
>>>
Q. Write a Python program to capitalize first and last letters of each word of a given string.
Answer =
Output :-
Answer =
Answer =
Output :-
Enter a string :-This is Pathwalla website This provide you solution of sumita arora class 11 , 12 This is our
pleasure.
Second most repeated word :- is
>>>
Here "This" repeated 3 times and "is" repeated 2 times.
Q. Write a Python program to change a given string to a new string where the first and last chars have
been exchanged.
Answer =
Answer =
Answer =
Answer =
print(lst1)
Output :-
Q. Write a Python program to generate and print a list of first and last 5 elements where the values are
square of numbers between 1 and 30 (both included).
Answer =
lst = [ ]
for i in range(1,6):
lst += [ i**2 ]
for i in range(6,26):
lst += [ i ]
for i in range(26,31):
lst += [ i**2 ]
print(lst)
Answer =
for i in lst:
if lst.count(i) == 1 :
print(i)
Answer =
Q. Write a Python script to concatenate the following dictionaries to create a new one:
d2 = {‘D’: 4}
Answer =
d1 = {"A": 1, "B": 2, "C": 3}
d2 = {"D": 4}
d1.update(d2)
print(d1)
Q. Write a Python script to print a dictionary where the keys are numbers between 1 and 15 (both
included) and the values are square of keys.
Sample Dictionary
{1:1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}
Answer =
dic={}
for i in range(1,16):
dic[i] = i**2
print(dic)
Answer =
print(newdic)
Q. Write a Python program to combine two dictionary adding values for common keys.
d1 = {'a': 100, 'b': 200, 'c': 300}
Sample output:
Answer =
for i in d2 :
if i in d1 :
d1[ i ] = d1[ i ] +d2[ i ]
else :
d1[ i ] = d2[ i ]
print(d1)
Answer =
Output :-
Answer =
dic = {}
lst = eval (input("Enter a alphabetically list :-"))
lst.sort()
for i in range(len(lst)):
dic[ i + 1 ] = lst [ i ]
print(dic)
Q. Write a Python program to count number of items in a dictionary value that is a list.
Answer =
Q. Consider the following unsorted list: 105, 99, 10, 43, 62, 8. Write the passes of bubble sort for sorting
the list in ascending order till the 3rd iteration.
Answer =
for j in range ( 3 ):
for i in range ( 2) :
if lst [ i ] > lst [ i + 1 ] :
lst [ i ] , lst [ i + 1 ] = lst [ i + 1 ] , lst [ i ]
print(lst)
Output :-
Q. What will be the status of the following list after the First, Second and Third pass of the insertion sort
method used for arranging the following elements in descending order?
Note: Show the status of all the elements after each pass very clearly underlining the changes.
Answer =
def Bsort(l):
count = 0
for i in range(len(l) - 1, 0, -1):
for j in range(i):