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?

python -m venv myenv

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:
pip install -r requirements.txt

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?

ListTuple
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:
friends = ["Alice", "Bob", "Charlie", "David"]
print(friends.index("Bob"))  # Output: 1

Q: How to slice a list in a stepwise manner?

  • You can use the step parameter of the slice() function to slice a list in a stepwise manner.
  • Example:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(numbers[1:5:2])  # 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, while extend() adds all elements from another list to the end of the current list.
  • Example:
my_list = [1, 2, 3]
my_list.append(4)  # my_list is now [1, 2, 3, 4]
my_list.extend([5, 6])  # my_list is now [1, 2, 3, 4, 5, 6]
my_list.append([7, 8])  # 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 the except block. This allows the program to gracefully handle errors without crashing.
  • Example:
try:
    # Code that might raise an exception
    result = 10 / 0
except ZeroDivisionError:
    # Code to handle the exception
    print("Cannot divide by zero")

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:
dict1 = {'name': 'John', 'age': 30}
dict2 = {'city': 'New York', 'country': 'USA'}
dict1.update(dict2)
print(dict1)  # 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:
dict1 = {'name': 'John', 'age': 30}
dict2 = {'city': 'New York', 'country': 'USA'}
dict3 = dict1 | dict2
print(dict3)  # 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:
numbers = [1, 2, 3, 4, 5]
squared_numbers = [x**2 for x in numbers]  # <-- list comprehension
print(squared_numbers)

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:
x = 10
y = 20
z = x if x > y else y  # <-- ternary operator
print(z)  # 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
with open('example.txt', 'w') as file:
    file.write('Hello, World!')
# 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:
def countdown(n):
    while n > 0:
        yield n
        n -= 1

for i in countdown(5):
    print(i) # 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:
def add_numbers(*args, **kwargs):
    total = 0
    for arg in args:
        total += arg
    for key, value in kwargs.items():
        total += value
    return total

print(add_numbers(1, 2, 3, a=4, b=5))  # 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:
x = 10
y = 10
print(x is y)  # Output: True
print(x == y)  # Output: True

x = 257
y = 257
print(x is y)  # Output: False
print(x == y)  # 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:
def my_function():
    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.
  • Example:
fruits = ['apple', 'banana', 'cherry']
print('apple' in fruits)  # Output: True
print('orange' in fruits)  # Output: False

numbers = [1, 2, 3, 4, 5]
print(2 in numbers)  # Output: True
print(6 in numbers)  # 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:
for i in range(10):
    if i == 5:
        break
    print(i)  # 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:
for i in range(10):
    if i % 2 == 0:
        continue
    print(i)  # 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:
try:
    # Code that might raise an exception
    result = 10 / 0
except ZeroDivisionError:
    # Code to handle the exception
    print("Cannot divide by zero")
finally:
    # Code that will always be executed
    print("This code 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

def my_function():
    print("Hello, World!")

my_function()  # Output: Hello, World!