Python Q&A

Python Q&A - Part3

93) What is a variable?

Answer: Variable is used to store data

94) How to change global variable inside function?

Answer: Use the global keyword

95) What are the values of x and y?
x = 10;y = 20
def fn():
global x
x = 30
y=40

fn()
print(‘x is:’,x)
print(‘y is:’,y)

Answer: x is 30 and y is 20

96) What is the output of the following?
l=lambda n: n*n
print(l(8))

Answer: 64

97) What is the output of the following?
l=lambda n1,n2:n1*n2
print(l(8,5))

Answer: 40

98) How are anonymous functions defined?

Answer: Using the lambda keyword

99) How many arguments can lambda have?

Answer: Lambda can have any number of arguments

100) How many expressions can lambda have?

Answer: Lambda can have one expression

101) How is lambda generally used in Python?

Answer: As an argument to a higher-order function

102) print(‘1’ + ‘2’)

Answer: 12

103) print(1+2)

Answer: 3

104) What is a filter?

Answer: Filters filters the iterable with help of function

105) What are the parameters of filter?

Answer: Function and iterable

106) What is the output of the following?
lst=[2,3,4,5,6,7,8]
def fn(n):
    if n%2==0:
        return True
lst1=filter(fn,lst)
for x in lst1:
      print(x)

Answer: 2,4,6,8

107) Dump the list [‘a’,’b’,’c’] to a file using json

Answer:

lst=[‘a’,’b’,’c’]
import json
str=json.dumps(lst)
file1 = open(“C:\\Test\\file1.txt”,’w’)
json.dump(lst,file1)

108) print(float(“2”))

Answer: 2.0

109) Is Set a mutable data type?

Answer: Yes

110) Does Set have ordered values?

Answer: No

111) Does Set have duplicate values?

Answer: No

112) Write code with map() function to square the numbers in the list
lst=[2,3,4]

Answer:

lst=[2,3,4]
def fn(n):
       return n*n
lst1=map(fn,lst)
for x in lst1:
      print(x)

113) What is map()

Answer: Map processes items in an iterable

114) Which is faster Map() or loop()?

Answer: Map is faster

115) What is difference between map and filter?

Answer: Map transforms elements in iterable, while filter removes elements in the iterable

116) What is difference between list and tuple?

Answer: Lists are mutable, while tuples are immutable

117) How should an identifier begin?

Answer: Character A-Z or a-z or underscore

118) Is python case sensitive

Answer: Yes

119) How can you display all members of object?

Answer: dir()

120) What does keys() do?

Answer: Gets list of keys in dictionary

121) How can you retrieve part of list or tuple?

Answer: Using slicing

122) What is isalnum()?

Answer: Checks if characters in string are alphanumeric

123) What does this do?

import os

print(os.getcwd())

Answer: Prints the current working directory path

124) lst=[9,8,7,6]
print(lst[::-1])

Answer: [6, 7, 8, 9]

125) print(max(‘Python’))

Answer: y

126) print(complex(1,2))

Answer: (1+2j)

127) How do you get data from keyboard?

Answer:

Use input().

Input(“What is your name:”)

128) print(1>2 and 2>1)

Answer: false

129) print(1>2 or 2>1)

Answer: true

130) print(not 1>2 and 2>1)

Answer: true

131) print(‘trend’ in ‘Python is trend’)

Answer: true

132) print(‘trend’ not in ‘Python is trend’)

Answer: false

133) What is recursion?

Answer: A function calling itself is recursion

134) print([x for x in range(1,7)])

Answer: [1, 2, 3, 4, 5, 6]

135) print([x for x in range(1,7,2)])

Answer: [1, 3, 5]

136) What is the use of inheritance?

Answer: Provides code reusability and it is transitive

137) How many types of inheritance in Python?

Answer: 4 types of inheritance in Python

138) What is single inheritance?

Answer: Derived class inherits from single parent class

139) What is multiple inheritance?

Answer: Derived class inherit from more than 1 base class

140) What is multi-level inheritance?

Answer: Base, derived classes inherit to new derived class

141) What is hierarchical inheritance?

Answer: Create more than 1 derived class from single base

142) What are Python libraries?

Answer: They are a collection of Python packages

143) Give some examples of Python libraries

Answer: Numpy, Pandas,Matplotlib etc