Python Interview Questions
Explore top Python Questions to prepare for an interview in 2026, Improve your skills and explore new questions about Python.
What are local variables and global variables in Python?
Answer
Global Variables: Variables declared outside a function or in a global space are called global variables. These variables can be accessed by any function in the program.
Local Variables: Any variable declared inside a function is known as a local variable. This variable is present in the local space and not in the global space.
When to use a tuple vs list vs dictionary in Python?
Answer
Use a
tupleto store a sequence of items that will not change.Use a
listto store a sequence of items that may change.Use a
dictionarywhen you want to associate pairs of two items.
Explain some benefits of Python
Answer
Python is a dynamic-typed language. It means that you don’t need to mention the data type of variables during their declaration.
Python supports object-orientated programming as you can define classes along with the composition and inheritance.
Functions in Python are like first-class objects. It suggests you can assign them to variables, return from other methods and pass them as arguments.
Developing using Python is quick but running it is often slower than compiled languages.
Python has several usages like web-based applications, test automation, data modeling, big data analytics, and much more.
What is Lambda Functions in Python?
Answer
A Lambda Function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.
Consider:
x = lambda a : a + 10
print(x(5)) # Output: 15
How do I modify a string in python?
Answer
You can’t because strings are immutable in python. In most situations, you should simply construct a new string from the various parts you want to assemble it from. Work with them as lists; turn them into strings only when needed.
>>> s = list("Hello zorld")
>>> s
['H', 'e', 'l', 'l', 'o', ' ', 'z', 'o', 'r', 'l', 'd']
>>> s[6] = 'W'
>>> s
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
>>> "".join(s)
'Hello World'
What is a Negative Index in Python?
Answer
Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on.
How the string does get converted to a number?
Answer
To convert the string into a number the built-in functions are used like
int()a constructor. It is a data type that is used likeint (‘1’) == 1.float()is also used to show the number in the format asfloat(‘1’) = 1.The number by default are interpreted as a decimal and if it is represented by
int(‘0x1’)then it gives an error asValueError. In this theint(string,base)the function takes the parameter to convert string to number in this the process will be likeint(‘0x1’,16) == 16. If the base parameter is defined as 0 then it is indicated by octal and 0x indicates it as a hexadecimal number.There is function
eval()that can be used to convert a string into number but it is a bit slower and present many security risks
What is docstring in Python?
Answer
A documentation string or docstring is a multiline string used to document a specific code segment.
The docstring should describe what the function or method does.
Does Python have a switch-case statement?
Answer
In Python before 3.10, we do not have a switch-case statement. Here, you may write a switch function to use. Else, you may use a set of if-elif-else statements. To implement a function for this, we may use a dictionary.
def switch_demo(argument):
switcher = {
1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December"
}
print switcher.get(argument, "Invalid month")Python 3.10 (2021) introduced the match-case a statement that provides a first-class implementation of a "switch" for Python. For example:
For example:
def f(x):
match x:
case 'a':
return 1
case 'b':
return 2The match-case the statement is considerably more powerful than this simple example.
What are descriptors ?
Answer
Descriptors were introduced to Python way back in version 2.2. They provide the developer with the ability to add managed attributes to objects. The methods needed to create a descriptor are __get__, __set__ and __delete__. If you define any of these methods, then you have created a descriptor.
Descriptors power a lot of the magic of Python’s internals. They are what make properties, methods and even the super function work. They are also used to implement the new style classes that were also introduced in Python 2.2.