Basic Python interview questions and answers for beginners
A few questions and answers for interviewers to check someone’s basic python knowledge.
Q: Which is the latest version of Python?
- Python 3.12
Q: How to create a virtual environment in Python?
Q: What is the purpose of requirements.txt
file?
- It is a file that contains a list of all the packages and their versions that your project requires.
- It is used to install the required packages in your project.
- Example:
# requirements.txt
requests==2.28.1
beautifulsoup4==4.11.1
- To install the required packages, run the following command:
Q: What is the difference between pip install
and pip install -r requirements.txt
?
Q: What is the difference between a list and a tuple in Python?
List | Tuple |
---|---|
Lists are mutable (can be changed after creation) and use square brackets []. | Tuples are immutable (cannot be changed after creation) and use parentheses (). |
Lists are typically used for collections of similar items that might need to be modified, while tuples are used for collections that shouldn’t change. | Tuples are used for collections that shouldn’t change. |
Example: items = [1, 12, 33, 14, 5] | bhubaneswar_coordinate = (20.27, 85.84) |
Q: How to find index of an element in a list?
- You can use the
index()
method of a list to find the index of an element in a list. - Example:
=
# Output: 1
Q: How to slice a list in a stepwise manner?
- You can use the
step
parameter of theslice()
function to slice a list in a stepwise manner. - Example:
=
# Output: [2, 4, 6, 8]
- Here
1:5:2
means start from index 1, stop at index 5, and step by 2.
Q: What are the differences between append()
and extend()
methods in Python?
append()
adds an element to the end of a list, whileextend()
adds all elements from another list to the end of the current list.- Example:
=
# my_list is now [1, 2, 3, 4]
# my_list is now [1, 2, 3, 4, 5, 6]
# my_list is now [1, 2, 3, 4, 5, 6, [7, 8]]
Q: How do you handle exceptions in Python?
- Exceptions are handled using try-except blocks. The code that might raise an exception is placed in the
try
block, and the code to handle the exception is placed in theexcept
block. This allows the program to gracefully handle errors without crashing. - Example:
# Code that might raise an exception
= 10 / 0
# Code to handle the exception
Q: What is a dictionary in Python and when would you use one?
- A dictionary is a collection of key-value pairs.
- It’s used when you need to associate values with unique keys, allowing fast lookup, insertion, and deletion.
- Use case: Dictionaries are useful for representing structured data, like a person’s attributes (name, age, address) or a configuration with settings.
- Example:
person = {'name': 'John', 'age': 30}
Q: How to merge two dictionaries in Python?
- You can use the
update()
method of a dictionary to merge two dictionaries. - Example:
=
=
# Output: {'name': 'John', 'age': 30, 'city': 'New York', 'country': 'USA'}
In Python 3.9 onwards, you can use the |
operator to merge two dictionaries.
- Example:
=
=
= |
# Output: {'name': 'John', 'age': 30, 'city': 'New York', 'country': 'USA'}
Q: What is the purpose of the if __name__ == "__main__" statement?
- This statement is used to check whether a Python script is being run directly or being imported as a module.
- Code under this statement only runs if the script is the main program, not when it’s imported as a module. It’s useful for including code that should only run when the script is executed directly.
Q: Can you explain what a list comprehension is?
- A list comprehension is a concise way to create lists in Python. It allows you to create a new list based on the values of an existing list or other iterable, often in a single line of code.
- It’s a more readable and sometimes faster alternative to using a for loop to create a list.
- Example:
=
= # <-- list comprehension
Q: What is a ternary operator?
- A ternary operator is a conditional operator in Python that allows you to write a single line of code that evaluates a condition and returns one of two values based on the result.
- It’s a shorthand way to write an if-else statement.
- Example:
= 10
= 20
= # <-- ternary operator
# Output: 20
Q: What is a decorator?
A: A decorator in Python is a special type of function that can modify or enhance another function or class without directly changing its source code. It’s a way to wrap a function, adding new functionality to an existing function.Decorators are denoted by the “@” symbol followed by the decorator name, placed on the line immediately before the function or class definition. They allow you to easily add behaviors to objects without modifying the objects themselves. Common uses for decorators include:
- Logging
- Measuring execution time
- Input validation
- Authentication and authorization
Q: What is a context manager?
- A context manager in Python is a construct that allows you to properly manage resources, such as file handles or database connections. It ensures that resources are properly acquired and released, even if errors occur during execution.
- Common use cases:
- File operations (automatically closing files)
- Database connections (ensuring connections are closed)
- Lock acquisition and release
- Managing transactions
- Example:
# Using a context manager for file operations
# File is automatically closed after the block, even if an exception occurs
Q: What are Python generators and when would you use them?
- Generators are functions that return an iterator. They use the
yield
keyword to produce a series of values over time, rather than computing them all at once and storing them in memory. - Generators are memory-efficient and useful when working with large datasets or infinite sequences.
- Example:
yield
-= 1
# Output: 5 4 3 2 1
Q: Explain the difference between *args and **kwargs in function definitions.
*args
allows a function to accept any number of positional arguments, which are packed into a tuple.**kwargs
allows a function to accept any number of keyword arguments, which are packed into a dictionary.- These are used when you don’t know in advance how many arguments might be passed to your function.
- Example:
= 0
+=
+=
return
# Output: 15
Q: What is the difference between is
and ==
operators in Python?
is
checks if two variables refer to the same object in memory, while==
checks if two variables have the same value.- Example:
= 10
= 10
# Output: True
# Output: True
= 257
= 257
# Output: False
# Output: True
Python automatically interns (reuses) small integers, which typically range from -5 to 256. This means that these integer objects are pre-allocated and stored in a global integer cache. Whenever you assign one of these values to a variable, Python uses the cached version instead of creating a new object.
Q: What is the purpose of the pass
statement in Python?
- The
pass
statement is used as a placeholder when you don’t want to add any code to a block. It’s often used as a placeholder for code that will be added later. - Example:
pass
Q: What is the difference between in
and not in
operators in Python?
in
checks if a value is present in a sequence, whilenot in
checks if a value is not present in a sequence.- Example:
=
# Output: True
# Output: False
=
# Output: True
# Output: False
Q: What is the purpose of the break
statement in Python?
- The
break
statement is used to exit a loop prematurely. It allows you to terminate the loop immediately, without executing the remaining code in the loop. - Example:
break
# Output: 0, 1, 2, 3, 4
Q: What is the purpose of the continue
statement in Python?
- The
continue
statement is used to skip the rest of the current iteration of a loop and move on to the next iteration. It allows you to skip certain iterations or elements in a loop without executing the remaining code in the loop. - Example:
continue
# Output: 1, 3, 5, 7, 9
Q: What is the purpose of the finally
statement in Python?
- The
finally
statement is used to specify a block of code to execute regardless of whether an exception is raised or not. It allows you to define a block of code that will always be executed, even if an exception occurs. - Example:
# Code that might raise an exception
= 10 / 0
# Code to handle the exception
# Code that will always be executed
Q: What is the significance of __init__.py file in Python?
- The
__init__.py
file is a special file that Python looks for when importing a module. It’s used to define the__init__
method, which is a special method that Python uses to initialize the module. - The
__init__.py
file is often used to define default values for variables or to perform any necessary setup when the module is imported. - Example:
# my_module/__init__.py
# Output: Hello, World!