Interview Questions

Get ready for your next interview with our comprehensive question library

Python Interview Questions

Filter by Difficulty

1.

What is the difference between Python 2 and Python 3?

beginner

Python 3 introduced several breaking changes from Python 2:

  • Print function: print "hello" vs print("hello")
  • Unicode strings: Default string type is Unicode in Python 3
  • Integer division: 5/2 returns 2.5 in Python 3, 2 in Python 2
  • Range function: range() returns an iterator in Python 3
  • Input function: raw_input() was renamed to input()

Python 2 reached end-of-life in 2020, so Python 3 is the current standard.

2.

What is PEP 8?

beginner

PEP 8 is the official style guide for Python code. It provides conventions for:

  • Indentation: Use 4 spaces per indentation level
  • Line length: Maximum 79 characters
  • Naming conventions: snake_case for variables, UPPER_CASE for constants
  • Import organization: Standard library, third-party, local imports
  • Whitespace: Proper spacing around operators and after commas
3.

Explain Python's indentation and why it matters.

beginner

Python uses indentation to define code blocks instead of braces {}. This enforces consistent code structure and improves readability.

if x > 0:
    print("Positive")  # 4 spaces indentation
    if x > 10:
        print("Greater than 10")  # 8 spaces indentation
else:
    print("Non-positive")

Incorrect indentation results in IndentationError.

4.

What are Python keywords and identifiers?

beginner

Keywords are reserved words with special meaning: if, else, for, while, def, class, import, etc.

Identifiers are names for variables, functions, classes, etc. Rules:

  • Start with letter or underscore
  • Contain letters, digits, underscores
  • Case-sensitive
  • Cannot be keywords
# Valid identifiers
variable_name = 10
_private_var = 20
ClassName = "example"

# Invalid identifiers
# 123variable = 10  # Cannot start with digit
# class = "test"    # Cannot use keyword
5.

Explain Python's built-in data types.

beginner

Python has several built-in data types:

Numeric: int, float, complex
Sequence: str, list, tuple
Set: set, frozenset
Mapping: dict
Boolean: bool
None: NoneType

integer = 42
floating = 3.14
string = "Hello"
boolean = True
none_value = None
6.

What is the difference between list and tuple?

beginner
List Tuple
Mutable (can be changed) Immutable (cannot be changed)
Uses square brackets [] Uses parentheses ()
Slower performance Faster performance
More memory usage Less memory usage
# List - mutable
my_list = [1, 2, 3]
my_list[0] = 10  # Works

# Tuple - immutable
my_tuple = (1, 2, 3)
# my_tuple[0] = 10  # TypeError
7.

Explain dictionary and its characteristics.

beginner

Dictionary is a mutable, unordered collection of key-value pairs. Keys must be immutable and unique.

student = {
    "name": "John",
    "age": 25,
    "grades": [85, 90, 78]
}

# Accessing values
print(student["name"])  # John
print(student.get("age"))  # 25

# Adding/updating
student["email"] = "john@email.com"
8.

Explain the different types of loops in Python.

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
9.

What are `break`, `continue`, and `pass` statements?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
10.

Explain conditional statements in Python.

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
11.

How do you define and call functions in Python?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
12.

Explain classes and objects in Python.

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
13.

Explain exception handling in Python.

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
14.

What is the difference between `except` and `finally`?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
15.

What is the difference between modules and packages?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
16.

Explain different ways to import modules.

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
17.

What is the difference between `range()` and `xrange()`?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
18.

What are Python's built-in functions you should know?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
19.

Explain Python's `__name__ == "__main__"` idiom.

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
20.

Explain Python's file handling modes.

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 52 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime