0% found this document useful (0 votes)
7 views2 pages

Assignment_Python

Uploaded by

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

Assignment_Python

Uploaded by

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

Tanish Garg

24/BMS/25069

Assignment: Programming using Python


ANSWERS
Q1 ANS.
 Total_no – It is a valid variable as it follows all rules.
 2nd_number- It is an invalid variable as it cannot start from a
number, or it cannot contain special character like (-)
 #amount- It is an invalid variable as it cannot start from # or
anyother special character other than underscore.
 _sum of no- It is an invalid variable as it cannot contain spaces in
btw.

Q2 a. ANS.
i) address[20:-1]- “ ,Delh”
ii) len(address)- 26
iii) address[-len(adreess):len(address)]- “H-4,5 Zone,Pitampura,Delhi”
iv) address.find(‘delhi’)-(-1)
v) write the code to replace ‘Delhi’ with ‘West Delhi’ in address string.
Ans.
address='H-4,5 Zone,Pitampura,Delhi'
new_address = address.replace('Delhi', 'West Delhi')
print(new_address)
b. Ans. CODE-:
x = ['r', 'y', 'o']
y = ['c', 'b', 'g']
combine = list(zip(x, y))
print(combine)

Q3 a. Evaluate and justify your answer.


 ‘hello’<’hi’ or ’I am fine’>’I am not fine’.
ANS. Output: True(logical OR operator (or) returns True if any of the
operands is True)

b. Print the following the following pattern.

ANS.
x = 64
for i in range(1, 5):
if i % 2 == 0:
for j in range(1, i + 1):
print("*", end='')
else:
for j in range(1, i + 1):
x += 1
print(chr(x), end='')
print()
Tanish Garg
24/BMS/25069

Q4 a. write a function that that interchanges first and last


characters of a given string.
b. Explain the shallow copy and deep copy in Python.
Q5 a. What is significance of __name__ == “__main__” in python.
Explain with example.
b. Consider a list A=[1,1,4,4,3,2,4,6,7,5,3,2,2].create a dictionary dict1
which contain a
number in list and its frequency as
dict1={1:2,2:3,3:2,4:3,5:1,6:1,7:1}.
c. form above list A generate two lists odd ad even such that odd=[ it
contains all odd
no from list A without duplicates] even=[it contains all even no
from list A without duplicates]
Q6 a. write a function mirror (string str) to generate mirror of a string . In
Mirror of a string
alphabet a is replaced by z,b is replaced y and c is replaced by x and
so on. For
example ,mirror of a string axn is zcm.
b. write a user defined function to copy a string into another string
c. Consider a list L1 as follows: L1=[15,20,50[60,70]] Give the
python statements to
create the following list: i) L2 and L3 as the shallow copy and deep
copy of the list L1
Print the elements of the list L1, L2 and L3 after the following
Modifications
i) L1[2] = 100 (ii) L1[3][1] = 110
Q7 Give the output of the following program segment. If any of the
statements has error then
write “error” giving the reasons in support of your answer
i) t1=(4,5,6)
t2=(7,8)
t3=t1+t2
t1=t1+t2
t2[1]=9
ii) t = (1,2,3,7,9,0,5)
s1={1,2,3,4}
s2={1,2,3,4,5}
print(t[ : -1]) , print(t[1 : -1])., print(s[3]))
print(s1<s2)
Q8 Consider the following program segment:
for char in "Delhi University":
if char=='i':
Statement1
else:
print(char, end=' ')
print("end loop")
What will be the output when Statement1 is replaced with each of the following
statements: a) break b) continue

You might also like