100 Python Interview Questions and Answers
1. What is Python?
Python is an interpreted, high-level, general-purpose programming language. Its design philosophy
emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines
of code than other languages.
2. What are Python's key features?
Python's key features include:
- Interpreted language
- Dynamically typed
- High-level
- Extensive standard library
- Supports object-oriented and functional programming paradigms.
3. Explain Python's memory management.
Python uses automatic memory management, which includes a private heap containing all Python
objects. It also uses reference counting and garbage collection for memory management.
4. What are Python namespaces?
A namespace ensures that object names are unique and can be used without conflict. Python
implements namespaces as dictionaries where names are mapped to objects.
5. What is the difference between a list and a tuple?
A list is mutable, while a tuple is immutable. Lists are generally slower but can be modified, whereas
tuples are faster and cannot be changed after creation.
6. Write a Python function to reverse a string.
def reverse_string(s):
return s[::-1]
7. Write a Python program to check if a number is prime.
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
8. What is PEP 8, and why is it important?
PEP 8 is Python's style guide that provides coding conventions to write more readable and
consistent code.
9. What are *args and **kwargs?
*args allows you to pass a variable number of positional arguments to a function, while **kwargs
allows you to pass a variable number of keyword arguments.
10. Explain the difference between deep copy and shallow copy in Python.
A shallow copy creates a new object but inserts references into it to the objects found in the original.
A deep copy creates a new object and recursively copies all objects found in the original.
11. Write a Python function to find the factorial of a number using recursion.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
12. Write a Python program to remove duplicates from a list.
def remove_duplicates(lst):
return list(set(lst))
13. What is the Global Interpreter Lock (GIL)?
The GIL ensures that only one thread can execute Python bytecode at a time. This makes Python
threads safe but can limit performance in CPU-bound multithreaded programs.
14. How does Python handle exceptions? What are try, except, and finally blocks?
Python uses try-except blocks to handle exceptions. The 'finally' block ensures that code executes
whether or not an exception occurs.
15. Write a Python function to find the Fibonacci sequence up to 'n' terms.
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
16. What is a Python generator?
Generators are functions that return an iterable set of items, one at a time, in a special way. They
use the 'yield' keyword instead of 'return'.
17. Explain the use of 'with' statement in Python.
The 'with' statement in Python is used to wrap the execution of a block of code. It ensures that
resources are properly managed, such as file streams being closed after use.
18. What are Python decorators?
Decorators are a way to modify or enhance the behavior of functions or methods without changing
their actual code. They are often used for logging, authorization, and timing.
19. How do you create a virtual environment in Python?
You can create a virtual environment using 'python -m venv myenv'. This isolates dependencies and
packages for projects.
20. What is the difference between 'is' and '==' in Python?
'is' checks if two variables point to the same object in memory, while '==' checks if the values of two
variables are equal.
1. What is Python?
Python is an interpreted, high-level, general-purpose programming language. Its design philosophy
emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines
of code than other languages.
2. What are Python's key features?
Python's key features include:
- Interpreted language
- Dynamically typed
- High-level
- Extensive standard library
- Supports object-oriented and functional programming paradigms.
3. Explain Python's memory management.
Python uses automatic memory management, which includes a private heap containing all Python
objects. It also uses reference counting and garbage collection for memory management.
4. What are Python namespaces?
A namespace ensures that object names are unique and can be used without conflict. Python
implements namespaces as dictionaries where names are mapped to objects.
5. What is the difference between a list and a tuple?
A list is mutable, while a tuple is immutable. Lists are generally slower but can be modified, whereas
tuples are faster and cannot be changed after creation.
6. Write a Python function to reverse a string.
def reverse_string(s):
return s[::-1]
7. Write a Python program to check if a number is prime.
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
8. What is PEP 8, and why is it important?
PEP 8 is Python's style guide that provides coding conventions to write more readable and
consistent code.
9. What are *args and **kwargs?
*args allows you to pass a variable number of positional arguments to a function, while **kwargs
allows you to pass a variable number of keyword arguments.
10. Explain the difference between deep copy and shallow copy in Python.
A shallow copy creates a new object but inserts references into it to the objects found in the original.
A deep copy creates a new object and recursively copies all objects found in the original.
11. Write a Python function to find the factorial of a number using recursion.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
12. Write a Python program to remove duplicates from a list.
def remove_duplicates(lst):
return list(set(lst))
13. What is the Global Interpreter Lock (GIL)?
The GIL ensures that only one thread can execute Python bytecode at a time. This makes Python
threads safe but can limit performance in CPU-bound multithreaded programs.
14. How does Python handle exceptions? What are try, except, and finally blocks?
Python uses try-except blocks to handle exceptions. The 'finally' block ensures that code executes
whether or not an exception occurs.
15. Write a Python function to find the Fibonacci sequence up to 'n' terms.
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
16. What is a Python generator?
Generators are functions that return an iterable set of items, one at a time, in a special way. They
use the 'yield' keyword instead of 'return'.
17. Explain the use of 'with' statement in Python.
The 'with' statement in Python is used to wrap the execution of a block of code. It ensures that
resources are properly managed, such as file streams being closed after use.
18. What are Python decorators?
Decorators are a way to modify or enhance the behavior of functions or methods without changing
their actual code. They are often used for logging, authorization, and timing.
19. How do you create a virtual environment in Python?
You can create a virtual environment using 'python -m venv myenv'. This isolates dependencies and
packages for projects.
20. What is the difference between 'is' and '==' in Python?
'is' checks if two variables point to the same object in memory, while '==' checks if the values of two
variables are equal.
1. What is Python?
Python is an interpreted, high-level, general-purpose programming language. Its design philosophy
emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines
of code than other languages.
2. What are Python's key features?
Python's key features include:
- Interpreted language
- Dynamically typed
- High-level
- Extensive standard library
- Supports object-oriented and functional programming paradigms.
3. Explain Python's memory management.
Python uses automatic memory management, which includes a private heap containing all Python
objects. It also uses reference counting and garbage collection for memory management.
4. What are Python namespaces?
A namespace ensures that object names are unique and can be used without conflict. Python
implements namespaces as dictionaries where names are mapped to objects.
5. What is the difference between a list and a tuple?
A list is mutable, while a tuple is immutable. Lists are generally slower but can be modified, whereas
tuples are faster and cannot be changed after creation.
6. Write a Python function to reverse a string.
def reverse_string(s):
return s[::-1]
7. Write a Python program to check if a number is prime.
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
8. What is PEP 8, and why is it important?
PEP 8 is Python's style guide that provides coding conventions to write more readable and
consistent code.
9. What are *args and **kwargs?
*args allows you to pass a variable number of positional arguments to a function, while **kwargs
allows you to pass a variable number of keyword arguments.
10. Explain the difference between deep copy and shallow copy in Python.
A shallow copy creates a new object but inserts references into it to the objects found in the original.
A deep copy creates a new object and recursively copies all objects found in the original.
11. Write a Python function to find the factorial of a number using recursion.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
12. Write a Python program to remove duplicates from a list.
def remove_duplicates(lst):
return list(set(lst))
13. What is the Global Interpreter Lock (GIL)?
The GIL ensures that only one thread can execute Python bytecode at a time. This makes Python
threads safe but can limit performance in CPU-bound multithreaded programs.
14. How does Python handle exceptions? What are try, except, and finally blocks?
Python uses try-except blocks to handle exceptions. The 'finally' block ensures that code executes
whether or not an exception occurs.
15. Write a Python function to find the Fibonacci sequence up to 'n' terms.
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
16. What is a Python generator?
Generators are functions that return an iterable set of items, one at a time, in a special way. They
use the 'yield' keyword instead of 'return'.
17. Explain the use of 'with' statement in Python.
The 'with' statement in Python is used to wrap the execution of a block of code. It ensures that
resources are properly managed, such as file streams being closed after use.
18. What are Python decorators?
Decorators are a way to modify or enhance the behavior of functions or methods without changing
their actual code. They are often used for logging, authorization, and timing.
19. How do you create a virtual environment in Python?
You can create a virtual environment using 'python -m venv myenv'. This isolates dependencies and
packages for projects.
20. What is the difference between 'is' and '==' in Python?
'is' checks if two variables point to the same object in memory, while '==' checks if the values of two
variables are equal.
1. What is Python?
Python is an interpreted, high-level, general-purpose programming language. Its design philosophy
emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines
of code than other languages.
2. What are Python's key features?
Python's key features include:
- Interpreted language
- Dynamically typed
- High-level
- Extensive standard library
- Supports object-oriented and functional programming paradigms.
3. Explain Python's memory management.
Python uses automatic memory management, which includes a private heap containing all Python
objects. It also uses reference counting and garbage collection for memory management.
4. What are Python namespaces?
A namespace ensures that object names are unique and can be used without conflict. Python
implements namespaces as dictionaries where names are mapped to objects.
5. What is the difference between a list and a tuple?
A list is mutable, while a tuple is immutable. Lists are generally slower but can be modified, whereas
tuples are faster and cannot be changed after creation.
6. Write a Python function to reverse a string.
def reverse_string(s):
return s[::-1]
7. Write a Python program to check if a number is prime.
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
8. What is PEP 8, and why is it important?
PEP 8 is Python's style guide that provides coding conventions to write more readable and
consistent code.
9. What are *args and **kwargs?
*args allows you to pass a variable number of positional arguments to a function, while **kwargs
allows you to pass a variable number of keyword arguments.
10. Explain the difference between deep copy and shallow copy in Python.
A shallow copy creates a new object but inserts references into it to the objects found in the original.
A deep copy creates a new object and recursively copies all objects found in the original.
11. Write a Python function to find the factorial of a number using recursion.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
12. Write a Python program to remove duplicates from a list.
def remove_duplicates(lst):
return list(set(lst))
13. What is the Global Interpreter Lock (GIL)?
The GIL ensures that only one thread can execute Python bytecode at a time. This makes Python
threads safe but can limit performance in CPU-bound multithreaded programs.
14. How does Python handle exceptions? What are try, except, and finally blocks?
Python uses try-except blocks to handle exceptions. The 'finally' block ensures that code executes
whether or not an exception occurs.
15. Write a Python function to find the Fibonacci sequence up to 'n' terms.
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
16. What is a Python generator?
Generators are functions that return an iterable set of items, one at a time, in a special way. They
use the 'yield' keyword instead of 'return'.
17. Explain the use of 'with' statement in Python.
The 'with' statement in Python is used to wrap the execution of a block of code. It ensures that
resources are properly managed, such as file streams being closed after use.
18. What are Python decorators?
Decorators are a way to modify or enhance the behavior of functions or methods without changing
their actual code. They are often used for logging, authorization, and timing.
19. How do you create a virtual environment in Python?
You can create a virtual environment using 'python -m venv myenv'. This isolates dependencies and
packages for projects.
20. What is the difference between 'is' and '==' in Python?
'is' checks if two variables point to the same object in memory, while '==' checks if the values of two
variables are equal.
1. What is Python?
Python is an interpreted, high-level, general-purpose programming language. Its design philosophy
emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines
of code than other languages.
2. What are Python's key features?
Python's key features include:
- Interpreted language
- Dynamically typed
- High-level
- Extensive standard library
- Supports object-oriented and functional programming paradigms.
3. Explain Python's memory management.
Python uses automatic memory management, which includes a private heap containing all Python
objects. It also uses reference counting and garbage collection for memory management.
4. What are Python namespaces?
A namespace ensures that object names are unique and can be used without conflict. Python
implements namespaces as dictionaries where names are mapped to objects.
5. What is the difference between a list and a tuple?
A list is mutable, while a tuple is immutable. Lists are generally slower but can be modified, whereas
tuples are faster and cannot be changed after creation.
6. Write a Python function to reverse a string.
def reverse_string(s):
return s[::-1]
7. Write a Python program to check if a number is prime.
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
8. What is PEP 8, and why is it important?
PEP 8 is Python's style guide that provides coding conventions to write more readable and
consistent code.
9. What are *args and **kwargs?
*args allows you to pass a variable number of positional arguments to a function, while **kwargs
allows you to pass a variable number of keyword arguments.
10. Explain the difference between deep copy and shallow copy in Python.
A shallow copy creates a new object but inserts references into it to the objects found in the original.
A deep copy creates a new object and recursively copies all objects found in the original.
11. Write a Python function to find the factorial of a number using recursion.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
12. Write a Python program to remove duplicates from a list.
def remove_duplicates(lst):
return list(set(lst))
13. What is the Global Interpreter Lock (GIL)?
The GIL ensures that only one thread can execute Python bytecode at a time. This makes Python
threads safe but can limit performance in CPU-bound multithreaded programs.
14. How does Python handle exceptions? What are try, except, and finally blocks?
Python uses try-except blocks to handle exceptions. The 'finally' block ensures that code executes
whether or not an exception occurs.
15. Write a Python function to find the Fibonacci sequence up to 'n' terms.
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
16. What is a Python generator?
Generators are functions that return an iterable set of items, one at a time, in a special way. They
use the 'yield' keyword instead of 'return'.
17. Explain the use of 'with' statement in Python.
The 'with' statement in Python is used to wrap the execution of a block of code. It ensures that
resources are properly managed, such as file streams being closed after use.
18. What are Python decorators?
Decorators are a way to modify or enhance the behavior of functions or methods without changing
their actual code. They are often used for logging, authorization, and timing.
19. How do you create a virtual environment in Python?
You can create a virtual environment using 'python -m venv myenv'. This isolates dependencies and
packages for projects.
20. What is the difference between 'is' and '==' in Python?
'is' checks if two variables point to the same object in memory, while '==' checks if the values of two
variables are equal.