0% found this document useful (0 votes)
4 views18 pages

Chapter 1 Solution

The document discusses various aspects of Python programming, including its advantages, modes of operation, and components of a program. It covers topics such as operators, expressions, variables, lists, tuples, and dictionaries, along with examples and code snippets. Additionally, it addresses specific programming tasks and their outputs, demonstrating Python's functionality.

Uploaded by

aashimakundu8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Chapter 1 Solution

The document discusses various aspects of Python programming, including its advantages, modes of operation, and components of a program. It covers topics such as operators, expressions, variables, lists, tuples, and dictionaries, along with examples and code snippets. Additionally, it addresses specific programming tasks and their outputs, demonstrating Python's functionality.

Uploaded by

aashimakundu8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Q. What are the advantages of Python programming language?

Answer =

Advantage of python language are as follow --

1. easy to use
2. expressive language
3. its completeness
4. cross platform
5. Free and open source
6. various uses

Q. In how many different ways can you work in Python?

Answer =

Two ways can we work in python --

1. Interactive mode
2. Script mode

Q. What are the advantages/disadvantages of working in interactive mode in Python?

Answer =

Advantage :-

1. In interactive mode we can get answer line by line.


2. Easy to find an error

Disadvantage :-

1. We cannot write a whole program in interactive mode.


2. we cannot save the program in it.

Q. What are the advantages/disadvantages of working in interactive mode in Python?

Answer =

Advantage :-

1. In interactive mode we can get answer line by line.


2. Easy to find an error

Disadvantage :-

1. We cannot write a whole program in interactive mode.


2. we cannot save the program in it.
Q. Write Python statement for the following in interactive mode:

(a) To display sum of 3, 8.0, 6*12

(b) To print sum of 16, 5.0, 44.0

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 =

The operation between two operands is called operator.

Function of operator:-
To operate two operand by instruction of user.

Example of unary operator:


+ unary plus , - unary minus etc.

Example of binary operator:


+ addition ,- subtraction etc.

Q. What is an expression and a statement?

Answer =

Expression is a legal combination of symbol that represent a value.


Statement is an instruction that does something.

Q. What all components can a Python program contain?

Answer =

Component of program are as follow:

1. Function
2. Expression
3. Statement
4. Comments
5. Blocks
6. Indentation

Q. What are variables? How are they important for a program?

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.

Q. Write the output of the following:

(i)

for i in '123':

print("guru99", i)

(ii)

for i in [ 100, 200, 300] :

print(i)

(iii)

for j in range (10, 6, -2):

print (j * 2)

(iv)

for x in range (1, 6):

for y in range (1, x+1) :

print (x,' ', y )

(v)

for x in range (10, 20):

if (x == 15):

break

print (x)

(vi)

for x in range (10, 20) :

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

Q. Write the output of the following program on execution if x = 50:


if x > 10 :

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.

Q. What are the various ways of creating a list?

Answer =

We can create a list by three types

(i) By using square brackets.


(ii) By using list() function.
(iii) By using eval() function.

Q. What are the similarities between strings and lists?

Answer =

Similarity:-

List and string both are used to store value /data.


Both are sequence.

Q. What are the similarities between strings and lists?

Answer =

Similarity:-
List and string both are used to store value /data.
Both are sequence.

Q. Why are lists called a mutable data type?

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.

Q. What is the difference between insert() and append() methods of a list.

Answer =

Insert() command insert the data at given index number while append() command insert the data at the
end of the list.

Q. Write a program to calculate the mean of a given list of numbers.

Answer =

lst = eval (input ("Enter the list of number : - "))


a=0
b=0
for i in lst :
a=a+i
b=b+1
print ("Mean of the list is ", a / b)

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 =

lst = eval (input ("Enter the marks of the student : - "))


tmarks = int (input ("Enter the total subjects marks : - "))
sum = 0
for i in lst :
sum = sum + i
print ("Total marks of student : - ", sum , "Percentage of student : - ", sum / tmarks * 100 )

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 =

lst = eval (input ("Enter the list : - "))


for i in range (len(lst)) :
if (i) % 2 != 0 :
lst[i] = lst[i] * 2
print(lst)

Q. Write a Program to count the frequency of an element in a given list.

Answer =

lst = eval(input("Enter a list :-"))


for i in lst :
feq = lst.count(i)
print("Frequency of ",i,"=",feq)

Output :-

Enter a list :-[1,5,3,9,4,2,0,8,6,9,4,0,3,1,7,6,3]

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.

Suppose the list is [10, 20, 30, 40]

After shifting, it should look like: [40, 10, 20, 30]

Answer =

lst = eval(input("enter the list = "))


print ("New list =", [lst[ -1] ] + lst[0 : -1] )

Q. A list Num contains the following elements:

3, 25, 13, 6, 35, 8, 14, 45

Write a function to swap the content with the next value divisible by 5 so that the resultant list will look like:

25, 3, 13, 35, 6, 8, 45, 14

Answer =

num=[3, 25, 13, 6, 35, 8, 14, 45]


for i in range(len(num) -1):
if num[i+1] % 5 == 0 :
num[i],num[i+1]=num[i+1],num[i]
print(num)

Output :-

[25, 3, 13, 35, 6, 8, 45, 14]


>>>

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 =

tup = eval(input("Enter a tuple :-"))


for i in tup :
print(i)
print(tup)
print("maximum value :- ", max(tup))
print("minimum value :- ", min(tup))

Output :-

Enter a tuple :-(1,2,3,4,5,6,7,8,9)


1
2
3
4
5
6
7
8
9
(1, 2, 3, 4, 5, 6, 7, 8, 9)
maximum value :- 9
minimum value :- 1
>>>

Q. Write a program to input any values for two tuples. Print it, interchange it and then compare them.

Answer =

tup1 = eval(input("Enter First tuple :-"))


tup2 = eval(input("Enter Second tuple :-"))

tup1,tup2 = tup2,tup1

print("First tuple :-",tup1)


print("Second tuple :- ",tup2)

print(tup1 < tup2)


print(tup1 > tup2)
print(tup1 == tup2)

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

clas = input("Enter name which you want to delete :- ")


del dic[clas]
print("Dictionary",dic)

Output :-

Enter name :- Path


Enter percentage :- 99.4
Do you want to enter more records enter (Yes/ No) :- yes

Enter name :- Walla


Enter percentage :- 85.2
Do you want to enter more records enter (Yes/ No) :- yes

Enter name :- Portal


Enter percentage :- 76.92
Do you want to enter more records enter (Yes/ No) :- No

Enter name which you want to delete :- Walla


Dictionary {'Path': 99.4, 'Portal': 76.92}
>>>

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 :-

Enter name :- Path


Enter phone number :- 123456789
Enter cost :- 89
Enter item :- Computer
Do you want to enter more records enter (Yes/ No) :- yes
Enter name :- Walla
Enter phone number :- 987654321
Enter cost :- 99.8
Enter item :- Laptop
Do you want to enter more records enter (Yes/ No) :- yes
Enter name :- Portal
Enter phone number :- 567894321
Enter cost :- 75
Enter item :- CPU
Do you want to enter more records enter (Yes/ No) :- yes
Enter name :- Express
Enter phone number :- 123498765
Enter cost :- 150
Enter item :- Mouse
Do you want to enter more records enter (Yes/ No) :- No

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 =

string = input("Enter a string :-")


new_str = string[0].upper()+string[1:-1]+string[-1].upper()
print(new_str)

Q. Write a Python program to remove duplicate characters of a given string.


Answer =

string = input("Enter a string :- ")


new_str = ""
for i in string:
if i not in new_str :
new_str += i
print(new_str)

Output :-

Enter a string :- Pathwalla


Pathwl
>>>

Enter a string :- Portal Express


Portal Expes
>>>

Q. Write a Python program to compute sum of digits of a given string.

Answer =

string = input("Enter a string :-")


sum = 0
for i in string:
sum += int(i)

print("Sum of digit :-",sum)


Q. Write a Python program to find the second most repeated word in a given string.

Answer =

string = input("Enter a string :-")


lst = string.split()
max = 0
sec = 0
for i in lst:
if lst.count(i) >= max :
max = lst.count(i)
elif lst.count(i) >= sec :
sec = lst.count(i)
secmaxvalue = i

print("Second most repeated word :-", secmaxvalue )

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 =

string = input("Enter a string :-")


new_str = string[-1] + string[1:-1] + string[0]
print(new_str)

Q. Write a Python program to multiply all the items in a list.

Answer =

lst = eval(input("Enter a list :-"))


mul = 1
for i in lst :
mul *= i
print(mul)

Q 33 = Write a Python program to get the smallest number from a list.

Answer =

lst = eval(input("Enter a list :-"))


lst.sort()
print("Smallest number :-", lst[0] )

Q. Write a Python program to append a list to the second list.

Answer =

lst1 = eval(input("Enter Frist list :-"))


lst2 = eval(input("Enter Second list :-"))
lst1 += lst2

print(lst1)

Output :-

Enter Frist list :-[ 1,2,3,4]


Enter Second list :-[5,6,7,8,9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

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)

Q. Write a Python program to get unique values from a list.

Answer =

lst = eval (input("Enter a list :-"))

for i in lst:
if lst.count(i) == 1 :
print(i)

Q. Write a Python program to convert a string to a list.

Answer =

string = list(input("Enter a string :-"))


print(string)

Q. Write a Python script to concatenate the following dictionaries to create a new one:

d1 = {‘A’: 1, ‘B’: 2, ‘C’: 3}

d2 = {‘D’: 4}

Output should be:

{‘A’: 1, ‘B’: 2, ‘C’: 3, ‘D’: 4}

Answer =
d1 = {"A": 1, "B": 2, "C": 3}
d2 = {"D": 4}

d1.update(d2)
print(d1)

Q. Write a Python script to check if a given key already exists in a dictionary.


Answer =

Dictionary have unique key only. So this program is not possible.

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)

Q. Write a Python program to sort a dictionary by key.

Answer =

dic = eval(input("Enter dictionary :- "))


newdic = {}
lst = list(dic.keys())
lst.sort()
for i in lst :
newdic [ i ] = dic[ i ]

print(newdic)

Q. Write a Python program to combine two dictionary adding values for common keys.
d1 = {'a': 100, 'b': 200, 'c': 300}

d2 = {'a': 300, 'b': 200, 'd': 400}

Sample output:

Counter ({'a': 400, 'b': 400, 'c': 300, 'd': 400})

Answer =

d1 = {'a': 100, 'b': 200, 'c': 300}


d2 = {'a': 300, 'b': 200, 'd': 400}

for i in d2 :
if i in d1 :
d1[ i ] = d1[ i ] +d2[ i ]
else :
d1[ i ] = d2[ i ]
print(d1)

Q. Write a Python program to find the highest 3 values in a dictionary.

Answer =

dic = eval(input("Enter a dictionary :-"))


val = list( dic.values() )
val.sort()
print("Highest 3 values ",val[ - 1 : - 4 : - 1])

Output :-

Enter a dictionary :-{ "Portal":16, "Express":14, "Path":15, "Walla":10,"Python":19}


Highest 3 values [19, 16, 15]

Q. Write a Python program to sort a list alphabetically in a dictionary.

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 =

dic = eval (input("Enter a Dictionary :-"))


lst = list( dic.values() )
print("Number of items :-",len(lst))

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 =

lst = eval(input("Enter a list :-"))

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 :-

Enter a list :-[105, 99, 10, 43, 62, 8]


[10, 99, 105, 43, 62, 8]
>>>

Enter a list :-[9,8,7,6,5,4,3,2,1]


[7, 8, 9, 6, 5, 4, 3, 2, 1]
>>>

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?

28, 44, 97, 34, 50, 87

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):

if l[j] > l[j + 1]:


l[j],l[j+1] = l[j+1],l[j]
print(l)
count += 1
if count == 3:
break

lst = [105, 99, 10, 43, 62, 8]


Bsort(lst)
print()
print("Upto three iterations list will look like: " ,lst)

You might also like