Python Interview Questions 11

81. Example of key value
names = {'a': 1, 'b': 2, 'c': 3}
print(names)
x = names.keys()
print(x)
print(names['c'])

82. Program for fibonacci
def fib(n):
    a,b = 0,1
    while b < n:
        print(b)
        a,b = b , a+b

fib(3)

83. Program to check vowel 
letter = 'o'
if letter == 'a' or letter == 'e' or letter == 'i' \
or letter == 'o' or letter == 'u':
    print (letter, 'is a vowel')
else:
    print(letter, 'is not a vowel')
   
84. Example of multi-dimensional array
rows = range(1,4)
cols = range(1,3)

cells = [(row,col) for row in rows for col in cols]
for cell in cells:
    print(cell)

No comments:

Post a Comment