Python interview questions PDF
Python interview questions PDF
I'll provide you with a list of python interview questions and answers in the form of a PDF. However, please note that I will respond in English as per your request.
Here are some common Python interview questions:
Basic Questions
What is Python? How is it different from other programming languages?Answer: Python is a high-level scripting language known for its simplicity, ease of use, and large community support. It's different from other languages because it has a focus on readability, using whitespace to denote code blocks, making it easy to understand and maintain.
What are the key features of Python?Answer: Some key features of Python include:
Easy to learn High-level language Dynamic typing Object-oriented programming Extensive libraries for various tasks How do you write a basic Hello, World program in Python?Answer: A basic Hello, World program would look something like this:
print("Hello, World!")
Data Structures
What is the difference between list and tuple in Python?Answer: The main difference is that tuples are immutable, meaning they cannot be changed after creation, while lists are mutable.
How do you iterate over a dictionary in Python?Answer: You can use a for loop with the dict's items() method:
d = {"a": 1, "b": 2}
for key, value in d.items():
print(key, value)
Control Flow
How do you handle exceptions in Python? What are some common exceptions?Answer: In Python, you can use try/except blocks to catch and handle exceptions. Some common exceptions include:
TypeError ValueError NameError What is the difference between pass and break statements in a loop?Answer: The pass statement does nothing, but allows you to avoid errors due to missing code. The break statement exits the loop immediately.
Functions
How do you define and use functions in Python?Answer: You can define a function with the def keyword:
def greet(name):
print("Hello, " + name)
greet("John")
What is the difference between lambda and def keywords in Python?
Answer: The main difference is that lambda functions are anonymous (they don't have a name), while def functions can be reused with different inputs.
File Input/Output
How do you read and write files in Python?Answer: You can use the built-in open() function to read/write files:
f = open("file.txt", "r")
print(f.read())
f.close()
What is the difference between modes 'r' and 'w' when opening a file?
Answer: The 'r' mode opens a file for reading, while the 'w' mode opens a file for writing, overwriting any existing data.
Object-Oriented Programming
How do you define classes in Python?Answer: You can define classes using the class keyword:
class Person:
def init(self, name):
self.name = name
p = Person("John")
print(p.name)
What is the difference between init and str methods in a class?
Answer: The init method is used to initialize an object's attributes, while the str method returns a string representation of the object.
This is just a small sample of what you might be expected to know if you're applying for a Python developer position. I hope this helps!
Python Interview Questions GeeksforGeeks
Here are some commonly asked Python interview questions with explanations:
1. What is the difference between list()
and []
?
In Python, both list()
and []
can be used to create an empty list. The main difference is that []
creates a list object immediately, while list()
returns a list wrapper around an existing iterable (like a tuple or another list). This means that if you do list(my_tuple)
, it will create a new list from the elements of the tuple.
2. How does Python handle multiple inheritance?
Python supports multiple inheritance, where a class can inherit from multiple base classes. When this happens, Python uses a method resolution order (MRO) to determine which methods to call when you access an attribute or method on the derived class. The MRO is determined by the order in which the base classes are listed in the class definition.
3. What is the purpose of the __name__
attribute?
The __name__
attribute is a built-in attribute that contains the name of the module, function, or class it belongs to. In Python, you can use this attribute to check where a piece of code is being executed from.
4. How do you implement the Singleton design pattern in Python?
To implement the Singleton pattern in Python, you can create a class with a private constructor and a public accessor method that returns an instance of the class. The private constructor ensures that only one instance of the class can be created.
5. What is the difference between pass
and break
statements?
In Python, both pass
and break
are used to exit loops or iterate through iterables. The main difference is that pass
simply continues execution with the next iteration of the loop, while break
exits the loop entirely.
6. How do you implement a stack using Python lists?
You can implement a stack using Python lists by pushing elements onto the end of the list and popping them from the end. This allows you to efficiently add and remove elements from the top of the stack.
7. What is the purpose of the enumerate
function?
The enumerate
function in Python returns an iterator that produces tuples, where the first element of each tuple is the index of the element in the original iterable, and the second element is the value at that index.
8. How do you check if a variable exists in Python?
In Python, you can use the in
operator to check if a variable exists within a certain scope (e.g., a function or module). You can also use the dir()
function to get a list of all variables defined in the current scope.
9. What is the difference between sorted()
and sort()
?
In Python, sorted()
returns a new sorted list from an iterable, while sort()
sorts the elements of an iterable in place. This means that if you sort a list using sort()
, it will modify the original list.
10. How do you implement a recursive function in Python?
To implement a recursive function in Python, you need to define a base case that stops the recursion and a recursive case that calls itself until the base case is reached. The function should also include a mechanism for returning values or modifying state as it recurses.
These are just some examples of commonly asked Python interview questions. Remember to always test your code thoroughly and think about edge cases when writing answers.