70e6aa18 C7af 49d5 A5fb 9cc3de3ef1c5 ASSESSMENT2
70e6aa18 C7af 49d5 A5fb 9cc3de3ef1c5 ASSESSMENT2
70e6aa18 C7af 49d5 A5fb 9cc3de3ef1c5 ASSESSMENT2
ASSESSMENT -2
(a)
y = str(123)
x = "hello" * 3
print (x , y)
x = "hello" + "world"
y = len(x)
print (y, x)
(b)
x = "hello" +
"to Python" +
"world"
for chr in x :
y = chr
print (y, ":", end = ' ')
(c)
x = "hello world"
print (x[:2], x[:-2], x[-2:])
print (x[6], x[2:4])
print (x[2:-3], x[-4:-2])
Q2. Write a short Python code segment that adds up the lengths of
all the words in a list and then prints the average (mean) length.
Q3. Predict the output of the following code snippet?
a = [1, 2, 3, 4, 5]
print(a[3:0:-1])
(a)
arr = [1, 2, 3, 4, 5, 6]
for i in range(1, 6) :
for i in range(0, 6) :
(b)
print ()
(b)
t = [1, "a", 9.2]
t[0] = 6
(c)
t = [1, "a", 9.2]
t[4] = 6
(d)
t = 'hello'
t[0] = "H"
(e)
for Name in [Amar, Shveta, Parag]
IF Name[0] = 'S' :
print(Name)
Q6. Assuming words is a valid list of words, the program below tries
to print the list in reverse. Does it have an error? If so, why? (Hint.
There are two problems with the code.)
(a, b, c, d) = ntpl
print ( "a is:", a)
print("b is:", b)
print("c is:", c)
print("d is:", d)
ntpl = (a, b, c, d)
print (ntpl[0][0] + ntpl[1][1], ntpl[1])
(a) 0
(b) 1
(c) False
(d) True
Q9. What will be the output of the following code snippet?
(a) True
(b) False
(c) 1
(d) Exception
my_dict = {}
my_dict[(1,2,4)] = 8
my_dict[(4,2,1)] = 10
my_dict[(1,2)]= 12
sum = 0
for k in my_dict :
sum += my_dict[k]
print (sum)
print(my_dict)