0% found this document useful (0 votes)
10 views4 pages

Python Test

Uploaded by

shashikant1687
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)
10 views4 pages

Python Test

Uploaded by

shashikant1687
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

GROUP-A

Find the output of the following: (15x2=30)


1.str1 = '{2}, {1} and {0}'.format('a', 'b', 'c')
str2 = '{0}{1}{0}'.format('abra', 'cad')
print(str1, str2)

2.a = 2
b = '3.77'
c = -8
str1 = '{0:.4f} {0:3d} {2} {1}'.format(a, b, c)
print(str1)

3.line = "I'll come by then."


eline = ""
for i in line:
eline += chr(ord(i)+3)
print(eline)

4.tuple = {}
tuple[(1,2,4)] = 8
tuple[(4,2,1)] = 10
tuple[(1,2)] = 12
_sum = 0
for k in tuple:
_sum += tuple[k]
print(len(tuple) + _sum)

5.L = [1, 3, 5, 7, 9]
print(L.pop(-3), end = ' ')
print(L.remove(L[0]), end = ' ')
print(L)

6.D = {1 : 1, 2 : '2', '1' : 2, '2' : 3}


D['1'] = 2
print(D[D[D[str(D[1])]]])

7.D = {1 : [1, 2, 3], 2: (4, 6, 8)}


D[1].append(4)
print(D[1], end = " ")
L = [D[2]]
L.append(10)
D[2] = tuple(L)
print(D[2])
8.for i in [1, 2, 3, 4][::-1]:
print (i)
9.my_string = 'geeksforgeeks'
for i in range(len(my_string)):
​ my_string[i].upper()
print (my_string)

10.d = {'a': 100, 'b': 200}


d['b'] = d.get('a', 0) + d.get('c', 50)
print(d)

11.lst = [1, 2, 3, 4]
for i in lst:
lst.append(i * 2)
if len(lst) > 10:
break
print(lst)
12.from collections import Counter
s = "swiss"
c = Counter(s)
print(next(x for x in s if c[x] == 1))

13.from collections import Counter


s = "banana"
c = Counter(s)
print("".join([x for x in s if x != max(c, key=c.get)]))

14.t = (1, 2, 3, 4)
l = list(t)
l[1], l[-1] = l[-1], l[1]
print(tuple(l))

15.t = ((1, 2), (3, 4), (5, (6, 7)))


flat = sum([list(x) if isinstance(x, tuple) else [x] for x in t], [])
print(tuple(flat))

16.Answer the following questions:(5x2=10)


i)What are immutable and mutable type? List immutable and mutable types of Python.
ii)Write the difference between split() and patition() function?
iii)When should tuples be preferred over list?
iv)Write the difference between sort() and sorted() function in list manipulation.
v)Why dictionaries are more useful than a list?
17.Write a program to count the number of times a character occurs in the
given string.(without using count function) (5)
18.Write a program which replaces all vowels in the string with '*'.(without
using any functions) (5)
19.Write a program that prompts for a phone number of 10 digits and two
dashes, with dashes after the area code and the next three numbers. For
example, 017-555-1212 is a legal input. Display if the phone number entered
is valid format or not and display if the phone number is valid or not (i.e.,
contains just the digits and dash at specific places.) (5)
20.Write a program to compare two equal sized lists and print the first index
where they differ.
Enter two equal sized lists
Enter first list: [80, 60, 50, 40, 30]
Enter second list: [80, 60, 55, 42, 30]
Lists differ at index 2

21.Create a dictionary whose keys are month names and whose values are
the number of days in the corresponding months. (5)

(a) Ask the user to enter a month name and use the dictionary to tell how
many days are in the month.

(b) Print out all of the keys in alphabetical order.


(c) Print out all of the months with 31 days.
(d) Print out the (key-value) pairs sorted by the number of days in each month.
22.Write a program that asks a user for a number of years, and then
prints out the number of days, hours, minutes, and seconds in that
number of years. (5)

How many years? 10​


10.0 years is:​
3650.0 days​
87600.0 hours​
5256000.0 minutes​
315360000.0 seconds

You might also like