Python Interview Questions

48 Qs

Explore top Python Questions to prepare for an interview in 2026, Improve your skills and explore new questions about Python.

1
Fresher

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.

2
Fresher

When to use a tuple vs list vs dictionary in Python?

Answer
  • Use a tuple to store a sequence of items that will not change.

  • Use a list to store a sequence of items that may change.

  • Use a dictionary when you want to associate pairs of two items.

3
Fresher

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.

4
1+ yr

What is Lambda Functions in Python?

Answer

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

 

5
1+ yr

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'

 

6
1+ yr

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.

7
1+ yr

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 like int (‘1’) == 1.

  • float() is also used to show the number in the format as float(‘1’) = 1.

  • The number by default are interpreted as a decimal and if it is represented by int(‘0x1’) then it gives an error as ValueError. In this the int(string,base) the function takes the parameter to convert string to number in this the process will be like int(‘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

8
1+ yr

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.

9
1+ yr

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 2

The match-case the statement is considerably more powerful than this simple example.

10
1+ yr

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.

More questions available

Sign in to get access to all questions, answers, and detailed explanations.