250+ Pyhton Interview Questions

1. Python Fundamentals (25 Questions)

Q1. What is Python and why is it called an interpreted language?

Python is a simple and popular programming language created by Guido van Rossum. It is called an interpreted language because the Python interpreter runs the code line by line directly, without compiling the whole program first. This makes coding and debugging faster.

Q2. What are the main features of Python?
  • Simple and readable syntax
  • Supports object-oriented programming (OOP)
  • Large standard library (lots of ready-to-use tools)
  • Dynamically typed (no need to declare data types)
  • Cross-platform (works on Windows, Mac, and Linux)
  • Automatic memory management (garbage collection)

Q3. What is PEP 8 and why is it important?

PEP 8 is the official style guide for Python code. It gives rules for formatting code like indentation, spacing, and naming. Following PEP 8 makes your code clean, consistent, and easier for others to read and maintain.

Q4. How do you check the version of Python installed on your system?

You can check the Python version by running the command python –version or python -V in the terminal/command prompt. Alternatively, inside Python code you can use:
//Python
import sys
print(sys.version)

Q5. What are keywords in Python? How many keywords are there approximately?

Keywords are reserved words in Python that have special meaning and cannot be used as variable names. There are 35 keywords in Python 3. Examples include if, else, for, while, def, class, import, True, False, None, etc.

Q6. What is the difference between a statement and an expression in Python?

An expression is a piece of code that evaluates to a value (e.g., 2 + 3, x > 5). A statement is an instruction that performs an action but does not return a value (e.g., x = 10, if condition, for loop, print()). Expressions can be part of statements.

Q7. Explain the difference between Python 2 and Python 3.

Python 3 is the current and future version. Major differences include: print is a function in Python 3 (print(“hello”)) but a statement in Python 2, integer division returns float in Python 3 (5/2 = 2.5), better Unicode support, and many syntax improvements. Python 2 is no longer supported.

Q8. What is the Zen of Python?

The Zen of Python is a collection of 19 guiding principles for writing good Python code. You can see it by typing import this in the Python interpreter. It includes lines like “Beautiful is better than ugly”, “Simple is better than complex”, and “Readability counts”.

Q9. How do you write comments in Python?

Single-line comments start with #. Multi-line comments can be created using triple quotes (”’ or “””).
Example:
# This is a single line comment
“””
This is a
multi-line comment
“””

Q10. What is the purpose of indentation in Python?

Unlike other languages that use braces {} , Python uses indentation (usually 4 spaces) to define blocks of code (like inside functions, loops, if statements). Incorrect indentation causes IndentationError.

Q11. Explain the difference between print() and return in Python.

print() displays output on the screen but does not return any value (it returns None). return sends a value back from a function to the caller and stops the function execution.

Q12. What are identifiers in Python? What are the rules for naming them?

Identifiers are names given to variables, functions, classes, etc. Rules: must start with a letter or underscore, can contain letters, digits, and underscores, cannot be a keyword, and are case-sensitive.

Q13. What is a variable in Python? How is it different from other languages?

A variable is a name that refers to a value stored in memory. In Python, you don’t need to declare the data type. The type is determined at runtime (dynamically typed). Example: x = 10 (x becomes int), later x = “hello” (x becomes string).

Q14. Explain mutable vs immutable objects in Python with examples.

Mutable objects can be changed after creation (e.g., list, dictionary, set). Immutable objects cannot be changed (e.g., int, float, string, tuple). Example:
//Python
a = [1, 2, 3]   # list is mutable
a.append(4)     # works
b = (1, 2, 3)   # tuple is immutable
# b.append(4)   # will give error

Q15. What does the id() function do in Python?

The id() function returns the unique identity (memory address) of an object. It helps check if two variables point to the same object in memory. Example: print(id(x))

Q16. Explain the difference between == and is operator.

== checks if two objects have the same value. is checks if two objects are the same object in memory (same id).
Example:
a = [1,2]
b = [1,2]
print(a == b)   # True (same value)
print(a is b)   # False (different objects)

Q17. What is None in Python?

None is a special constant that represents the absence of a value or null. It is the only object of type NoneType.

Q18. How can you take input from the user in Python?

Use the input() function. It always returns a string. Example:
name = input(“Enter your name: “)
age = int(input(“Enter your age: “))

Q19. What is type conversion in Python? Explain with example.

Type conversion (casting) changes one data type to another. Python has int(), float(), str(), etc.
Example:
x = “25”
y = int(x)      # string to integer
print(y + 10)   # 35

Q20. Explain the difference between single, double, and triple quotes.

Single (‘ ‘) and double (” “) quotes are used for strings and work the same. Triple quotes (”’ ”’ or “”” “””) are used for multi-line strings and docstrings.

Q21. What are the different ways to format strings in Python?

Python provides three main ways to format strings:

  1. f-strings (Recommended – Python 3.6 and above)

name = “Nilesh”
age = 25
print(f”My name is {name} and I am {age} years old.”)

  1. .format() method

print(“My name is {} and I am {} years old.”.format(“Nilesh”, 25))

  1. % operator (Old style)

print(“My name is %s and I am %d years old.” % (“Nilesh”, 25))

Q22. What is a docstring in Python?

A docstring is a string literal that appears as the first statement in a module, function, class, or method. It is used to document the code and can be accessed using __doc__ attribute.

Q23. How do you check the data type of a variable in Python?

Use the type() function. Example: print(type(10)) → <class ‘int’>

Q24. What is the difference between shallow copy and deep copy?

Shallow copy (copy.copy()) creates a new object but references the same nested objects. Deep copy (copy.deepcopy()) creates a completely new object with new nested objects too. This matters mainly with mutable nested structures like lists inside lists.

Q25. Write a simple “Hello World” program and explain each line.

# This is a comment
print(“Hello World”)   # print() function displays the text on screen
This is the simplest Python program. The print() function is built-in and outputs the string to the console.

Read the complete eBook on our Android app.

This is just a demo preview. Download our app to unlock all chapters with full access and learn anytime, anywhere.

Leave a Reply

Your email address will not be published. Required fields are marked *

Read the complete eBook on our Android app.

This is just a demo preview.
Download our app to unlock all chapters with full access and learn anytime, anywhere.

Related ebooks

Continue reading