Back to Interview PrepGet interview ready with AILearn each topic the way interviewers ask it, then answer like you would in the interview.

Chapter 11 of 11

Top 50 Python Interview Questions

Core Python concepts - data types, operators, control flow, functions, OOP, advanced Python, exception handling, modules, and built-in functions - organized by topic and difficulty, with explanations that go beyond a one-line definition and a runnable code example for each one. Search or filter to find what you need, and share a direct link to any question.

Level:

Showing 50 of 50 questions

Questions reviewed0/50 · 0%

The first 15 of 50 questions are free to read. Sign up free to unlock every answer and track your progress.

Unlock all 50

Python is a high-level, interpreted, object-oriented programming language known for its simple and readable syntax, which makes it a popular first language and a productive choice for scripting, web backends, data science, and automation alike.

Example

print("Hello, Python!")

Python is easy to learn and use, an interpreted language (no separate compile step), dynamically typed (no need to declare variable types), object-oriented, backed by a large standard library, and cross-platform. Together these make it beginner-friendly, versatile, and widely used across web development, AI, data science, and automation.

Interpreted languages (like Python and JavaScript) execute code line by line with no separate compilation step, which makes iterating faster but execution somewhat slower. Compiled languages (like C, C++, and Java) convert the entire program into machine code before running it, which takes an extra build step but usually runs faster.

PEP 8 is Python's official style guide - a set of conventions for writing clean, readable, and consistent code (naming conventions, indentation, line length, and more). Following it improves code readability, keeps a codebase consistent, and makes collaborating with a team much easier.

Variables are names used to store data values in memory. Python is dynamically typed, so you never declare a type up front - the type is inferred from whatever value you assign, and it can even change if you reassign the variable to a value of a different type.

Example

x = 10        # x is integer
name = "Python"  # name is string
is_fun = True    # is_fun is boolean
print(x, name, is_fun)

Python has several built-in data types: `int`, `float`, and `complex` for numbers; `str` for text; `list`, `tuple`, and `set` for collections; `dict` for key-value pairs; `bool` for True/False; and `NoneType` for the absence of a value (`None`).

A list is mutable (its contents can be changed after creation) and uses square brackets `[ ]`; it's slightly slower and offers more built-in methods. A tuple is immutable (cannot be changed after creation), uses parentheses `( )`, and is faster with fewer methods - useful for data that should never change, like coordinates or fixed configuration values.

Example

lst = [1, 2, 3]
lst[0] = 10       # OK - lists are mutable

tup = (1, 2, 3)
tup[0] = 10       # TypeError - tuples are immutable

A dictionary stores data as key-value pairs, letting you look up a value instantly by its key instead of searching through a list by position. Keys must be unique and hashable (strings, numbers, or tuples); values can be anything.

Example

student = {"name": "John", "age": 22}
print(student["name"])   # John
student["age"] = 23
print(student)            # {'name': 'John', 'age': 23}

A set is an unordered collection of unique elements - duplicates are automatically discarded when you build one. Sets support fast membership tests and operations like union, intersection, and difference, and are commonly used to de-duplicate data or compare two collections.

Example

numbers = {1, 2, 2, 3, 3, 4}
print(numbers)     # {1, 2, 3, 4}
numbers.add(5)
numbers.remove(2)
print(numbers)     # {1, 3, 4, 5}

Type casting is the process of converting a value from one data type into another, using functions like `int()`, `float()`, `str()`, `list()`, `tuple()`, and `set()`. It is commonly needed when combining user input (always a string) with numeric calculations.

Example

x = "10"
y = int(x)      # type casting to int
z = float(y)    # type casting to float
print(x, type(x))  # 10 <class 'str'>
print(y, type(y))  # 10 <class 'int'>
print(z, type(z))  # 10.0 <class 'float'>

Arithmetic operators perform mathematical operations: `+` addition, `-` subtraction, `*` multiplication, `/` division (always returns a float), `%` modulus (remainder), `**` exponent (power), and `//` floor division (division rounded down to the nearest whole number).

Example

print(5 + 3)   # 8
print(5 - 3)   # 2
print(5 * 3)   # 15
print(5 / 2)   # 2.5
print(5 % 2)   # 1
print(2 ** 3)  # 8
print(5 // 2)  # 2

`=` is the assignment operator - it stores a value in a variable. `==` is the comparison operator - it checks whether two values are equal and returns a boolean. Mixing them up (writing `x = y` inside a condition instead of `x == y`) is a classic beginner syntax error.

Example

x = 10   # assignment (stores value)
y = 10   # assignment
print(x == y)  # True (comparison)

Logical operators combine conditional statements: `and` is True only if both sides are True, `or` is True if at least one side is True, and `not` reverses (negates) the result of a condition.

Example

print(True and False)  # False
print(True or False)   # True
print(not True)         # False

The `%` operator returns the remainder after division. It's frequently used to check whether a number is even or odd, or to wrap an index around a fixed range. In Python, the result of `%` always has the same sign as the divisor, so the remainder for a negative dividend is non-negative when the divisor is positive.

Example

print(10 % 3)    # 1
print(25 % 4)    # 1
print(-10 % 3)   # 2 (remainder is always non-negative here)

Operator precedence determines the order in which operations are evaluated in an expression - higher-precedence operators run first. From highest to lowest: parentheses `()`, exponent `**`, unary +/-, then `*` `/` `//` `%`, then `+` `-`, then comparisons, then `not`, `and`, `or`, and finally assignment operators. When in doubt, parentheses make the intended order explicit.

Example

x = 2 + 3 * 4 ** 2
# Step 1: 4 ** 2 = 16
# Step 2: 3 * 16 = 48
# Step 3: 2 + 48 = 50
print(x)  # 50
16.What is an if statement?17.What is the difference between a for loop and a while loop?18.What does break do?19.What does continue do?20.What is a nested loop?21.What is a function?22.What is the difference between parameters and arguments?23.What is a return statement?24.What are default arguments?25.What is a lambda function?26.What is OOP?27.What is a class?28.What is an object?29.What is inheritance?30.What is polymorphism?31.What is a decorator?32.What is a generator?33.What is list comprehension?34.What is the difference between copy and deepcopy?35.What is *args and **kwargs?36.What is an exception?37.What is a try-except block?38.What is the purpose of the finally block?39.What is the use of the raise keyword?40.What is the difference between Syntax Errors and Exceptions?41.What is a module?42.How do you import a module?43.What is a package?44.How do you import from a package?45.What is the difference between a module and a package?46.What are some commonly used built-in functions in Python?47.Explain enumerate() with an example.48.What is the difference between list() and tuple()?49.How do you use map(), filter(), and reduce()?50.What are some Python best practices?
You're viewing a free lesson. Sign up free to track your progress and unlock the full course.Sign up freeSign in