0% found this document useful (0 votes)
2 views3 pages

Top 10 Python Interview Questions W

The document lists the top 10 Python interview questions along with their answers, covering key topics such as data types, list comprehensions, and exception handling. It explains concepts like decorators, generators, and lambda functions, as well as differences between various list methods. Additionally, it discusses the Global Interpreter Lock (GIL) and its impact on multithreading in Python.

Uploaded by

faxi.dev06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Top 10 Python Interview Questions W

The document lists the top 10 Python interview questions along with their answers, covering key topics such as data types, list comprehensions, and exception handling. It explains concepts like decorators, generators, and lambda functions, as well as differences between various list methods. Additionally, it discusses the Global Interpreter Lock (GIL) and its impact on multithreading in Python.

Uploaded by

faxi.dev06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Top 10 Python interview questions with answers:

1. What are Python's key data types?

Solution:

Numeric types: int, float, complex

Text type: str

Sequence types: list, tuple

Mapping type: dict

Set types: set, frozenset

Boolean type: bool

2. What is a list comprehension in Python?

Solution:
A concise way to create lists using a single line of code.
Example:

squares = [x**2 for x in range(10)] # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

3. What is the difference between == and is in Python?

Solution:

== checks for value equality.

is checks for object identity (whether two references point to the same object).

a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True, values are equal
print(a is b) # False, different objects

4. How do you handle exceptions in Python?

Solution:
Using try, except, else, and finally blocks.
Example:

try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
else:
print("No error occurred.")
finally:
print("This block runs regardless of an error.")
5. What are Python decorators and why are they used?

Solution:
Decorators are functions that modify the behavior of other functions or methods.
They are used for adding functionality without changing the original function's
code. Example:

def my_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
func()
print("Something is happening after the function is called.")
return wrapper

@my_decorator
def say_hello():
print("Hello!")

say_hello()

6. What is a Python generator?

Solution:
A generator is a function that uses yield to return an iterator, which generates
values on the fly without storing them in memory. Example:

def my_generator():
yield 1
yield 2
yield 3

gen = my_generator()
for value in gen:
print(value)

7. How do you create a dictionary in Python?

Solution:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

8. What is the difference between append() and extend() in Python?

Solution:

append(): Adds a single element to the end of a list.

extend(): Adds all elements from an iterable to the end of a list.

my_list = [1, 2, 3]
my_list.append([4, 5]) # [1, 2, 3, [4, 5]]
my_list.extend([6, 7]) # [1, 2, 3, [4, 5], 6, 7]

9. What is a lambda function in Python?

Solution:
A lambda function is an anonymous function defined using the lambda keyword. It's
often used for short, simple operations. Example:

square = lambda x: x**2


print(square(5)) # 25

10. What is the Global Interpreter Lock (GIL)?

Solution:
The GIL is a mutex in CPython (the standard Python implementation) that prevents
multiple native threads from executing Python bytecode at the same time. This can
limit the performance of multithreaded Python programs in CPU-bound operations but
not in I/O-bound operations.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy