top of page
Anchor 1
Frequently Asked C Interview Question & Answers

What is a closure in Python?

A closure is said to occur when a nested function references a value in its enclosing scope. The whole point here is that it remembers the value.

>>> def A(x):

def B():

print(x)

return B

>>> A(7)()

7

Close

What is a lambda function?

An anonymous function is known as a lambda function. This function can have any number of parameters but, can have just one statement.

Example:

a = lambda x,y : x+y

print(a(5, 6))

Output: 11

Close

What is self in Python?

Self is an instance or an object of a class. In Python, this is explicitly included as the first parameter. However, this is not the case in Java where it’s optional.  It helps to differentiate between the methods and attributes of a class with local variables.

The self variable in the init method refers to the newly created object while in other methods, it refers to the object whose method was called.

Close

What does [::-1} do?

 [::-1] is used to reverse the order of an array or a sequence.

For example:

import array as arr

My_Array=arr.array('i',[1,2,3,4,5])

My_Array[::-1]

Output: array(‘i’, [5, 4, 3, 2, 1])

[::-1] reprints a reversed copy of ordered data structures such as an array or a list. the original array or list remains unchanged.

Close

Explain the //, %, and ** operators in Python.

The // operator performs floor division. It will return the integer part of the result on division.

>>> 7//2

3

Normal division would return 3.5 here.

 

Similarly, ** performs exponentiation. a**b returns the value of a raised to the power b.

>>> 2**10

1024

Finally, % is for modulus. This gives us the value left after the highest achievable division.

>>> 13%7

6

>>> 3.5%1.5

0.5

Close

How many kinds of operators do we have in Python? Explain arithmetic operators.

This type of Python Interview Questions and Answers can decide your knowledge in Python. Answer the Python Interview Questions with some good Examples.

Here in Python, we have 7 kinds of operators: arithmetic, relational, assignment, logical, membership, identity, and bitwise.

We have seven arithmetic operators. These allow us to perform arithmetic operations on values:

1. Addition (+) This adds two values.

>>> 7+8

15

2. Subtraction (-) This subtracts the second value from the first.

>>> 7-8

-1

3. Multiplication (*) This multiplies two numbers.

>>> 7*8

56

4. Division (/) This divides the first value by the second.

>>> 7/8

0.875

>>> 1/1

1.0

For floor division, modulus, and exponentiation refer to the previous question.

Close

Explain relational operators in Python.

Relational operators compare values.

1. Less than (<) If the value on the left is lesser, it returns True.

>>> 'hi'<'Hi'

False

2. Greater than (>) If the value on the left is greater, it returns True.

>>> 1.1+2.2>3.3

True
This is because of the flawed floating-point arithmetic in Python, due to hardware dependencies.

3. Less than or equal to (<=) If the value on the left is lesser than or equal to, it returns True.

>>> 3.0<=3

True

4. Greater than or equal to (>=) If the value on the left is greater than or equal to, it returns True.

>>> True>=False

True

5. Equal to (==) If the two values are equal, it returns True.

>>> {1,3,2,2}=={1,2,3}

True

6. Not equal to (!=) If the two values are unequal, it returns True.

>>> True!=0.1

True

>>> False!=0.1

True

Close

What are assignment operators in Python?

This one is an Important Interview question in Python Interview.

We can combine all arithmetic operators with the assignment symbol.

>>> a=7

>>> a+=1

>>> a

8

>>> a-=1

>>> a

7

>>> a*=2

>>> a

14

>>> a/=2

>>> a

7.0

>>> a**=2

>>> a

49.0

>>> a//=3

>>> a

16.0

>>> a%=4

>>> a

0.0

Close

Explain logical operators in Python.

We have three logical operators- and, or, not.

>>> False and True

False

>>> 7<7 or True

True

>>> not 2==2

False

Close

What are membership, operators?

With the operators ‘in’ and ‘not in’, we can confirm if a value is a member in another.

>>> 'me' in 'disappointment'

True

>>> 'us' not in 'disappointment'

True

Close
bottom of page