Basic Python interview questions and answers for beginners
A few questions and answers for interviewers to check someone's basic python knowledge. A few questions and answers for interviewers to check someone’s basic python knowledge. In Python 3.9 onwards, you can use the 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: 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: Which is the latest version of Python?
Q: How to create a virtual environment in Python?
Q: What is the purpose of
requirements.txt
file?# requirements.txt
requests==2.28.1
beautifulsoup4==4.11.1
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?
index()
method of a list to find the index of an element in a list.=
# Output: 1
Q: How to slice a list in a stepwise manner?
step
parameter of the slice()
function to slice a list in a stepwise manner.=
# Output: [2, 4, 6, 8]
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, while extend()
adds all elements from another list to the end of the current list.=
# 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?
try
block, and the code to handle the exception is placed in the except
block. This allows the program to gracefully handle errors without crashing.
# 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?
person = {'name': 'John', 'age': 30}
Q: How to merge two dictionaries in Python?
update()
method of a dictionary to merge two dictionaries.=
=
# Output: {'name': 'John', 'age': 30, 'city': 'New York', 'country': 'USA'}
|
operator to merge two dictionaries.=
=
= |
# Output: {'name': 'John', 'age': 30, 'city': 'New York', 'country': 'USA'}
Q: What is the purpose of the if __name__ == "__main__" statement?
Q: Can you explain what a list comprehension is?
=
= # <-- list comprehension
Q: What is a ternary operator?
= 10
= 20
= # <-- ternary operator
# Output: 20
Q: What is a decorator?
Q: What is a context manager?
# 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?
yield
keyword to produce a series of values over time, rather than computing them all at once and storing them in memory.
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.
= 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.= 10
= 10
# Output: True
# Output: True
= 257
= 257
# Output: False
# Output: True
Q: What is the purpose of the
pass
statement in Python?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.
pass
Q: What is the difference between
in
and not in
operators in Python?in
checks if a value is present in a sequence, while not in
checks if a value is not present in a sequence.=
# Output: True
# Output: False
=
# Output: True
# Output: False
Q: What is the purpose of the
break
statement in Python?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.
break
# Output: 0, 1, 2, 3, 4
Q: What is the purpose of the
continue
statement in Python?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.
continue
# Output: 1, 3, 5, 7, 9
Q: What is the purpose of the
finally
statement in Python?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.
# 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?
__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.__init__.py
file is often used to define default values for variables or to perform any necessary setup when the module is imported.# my_module/__init__.py
# Output: Hello, World!
Related Articles