1. Is Python a compiled language or an interpreted language?
Python is primarily an interpreted language. When you run a Python program, the source code is first compiled into bytecode by the Python interpreter (e.g., CPython), and then this bytecode is executed by the Python Virtual Machine (PVM). This compilation to bytecode happens automatically and is not like traditional compiled languages where you explicitly compile to machine code. However, the execution is interpretive, as the bytecode is run line-by-line or in chunks by the interpreter.
Note: While there is a compilation step to bytecode (.pyc files), Python is generally classified as interpreted because the code is not directly compiled to native machine code before runtime.
2. How can you concatenate two lists in Python?
You can concatenate two lists in Python using the + operator, the extend() method, or list unpacking with the * operator. The + operator creates a new list, while extend() modifies the original list in place.
# Using + operator (creates a new list)
list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated = list1 + list2 # [1, 2, 3, 4, 5, 6]
# Using extend() method (modifies list1)
list1.extend(list2) # list1 becomes [1, 2, 3, 4, 5, 6]
# Using list unpacking (Python 3.5+)
concatenated = [*list1, *list2] # [1, 2, 3, 4, 5, 6]
Common pitfall: Using append() would add the second list as a nested list, not concatenate.
3. What is the difference between / and // in Python?
In Python, / is the true division operator, which performs floating-point division and always returns a float result, even if both operands are integers. On the other hand, // is the floor division operator, which performs integer division and returns the largest integer less than or equal to the quotient (floored result).
# True division (/)
result1 = 5 / 2 # 2.5 (float)
# Floor division (//)
result2 = 5 // 2 # 2 (int)
# With negative numbers
result3 = -5 // 2 # -3 (floors towards negative infinity)
Note: In Python 2, / behaved like // for integers, but in Python 3, it's always true division.
4. Is indentation required in Python?
Yes, indentation is required in Python. Unlike many other programming languages that use braces {} to define code blocks, Python uses indentation (typically 4 spaces or a tab) to indicate the structure and scope of code blocks, such as in functions, loops, conditionals, and classes. Incorrect indentation will result in an IndentationError.
def example_function():
if True:
print("Indented block") # This line must be indented
print("Still in function") # Consistent indentation
# Incorrect indentation would raise IndentationError
Best practice: Use spaces over tabs for consistency, as recommended by PEP 8.
5. Can you pass a function as an argument in Python?
Yes, in Python, functions are first-class objects, meaning you can pass them as arguments to other functions, return them from functions, or assign them to variables. This enables higher-order functions and functional programming paradigms.
def greet(name):
return f"Hello, {name}!"
def call_function(func, arg):
return func(arg) # Passing 'greet' as an argument
result = call_function(greet, "Alice") # "Hello, Alice!"
Common use: Built-in functions like map(), filter(), and sorted() accept functions as arguments.
6. What is a dynamically typed language?
A dynamically typed language is one where the type of a variable is determined at runtime rather than at compile time. In such languages, you don't need to explicitly declare the data type of a variable; it is inferred based on the value assigned to it. Python is a dynamically typed language, which allows for flexibility but can lead to runtime type errors.
# No type declaration needed
x = 5 # x is int
x = "text" # Now x is str (type changes dynamically)
Contrast with statically typed languages like Java, where types must be declared upfront.
7. What is pass in Python?
The pass statement in Python is a null operation; it does nothing when executed. It is used as a placeholder in code blocks where a statement is syntactically required, but no action is needed or the code is to be added later. This is common in function definitions, loops, or class definitions during development.
def unfinished_function():
pass # Placeholder; no error raised
for i in range(5):
if i % 2 == 0:
pass # Skip even numbers without action
else:
print(i)
Without pass, an empty block would raise an IndentationError.
8. How are arguments passed by value or by reference in Python?
In Python, arguments are passed by object reference, often described as "pass by assignment." When you pass a variable to a function, you're passing a reference to the object it points to. If the object is mutable (e.g., list), modifications inside the function affect the original object. If immutable (e.g., int, str), reassigning the parameter creates a new object without affecting the original.
def modify_list(lst):
lst.append(4) # Modifies original (mutable)
def modify_int(num):
num = 10 # Doesn't affect original (immutable, reassignment)
original_list = [1, 2, 3]
modify_list(original_list) # original_list becomes [1, 2, 3, 4]
original_num = 5
modify_int(original_num) # original_num remains 5
Pitfall: Confusing reassignment with mutation; always check if the object is mutable.
9. What is a lambda function?
A lambda function in Python is an anonymous, small function defined using the lambda keyword. It can take any number of arguments but can only have a single expression. Lambdas are often used for short, throwaway functions, especially with higher-order functions like map(), filter(), or sorted().
# Lambda syntax: lambda arguments: expression
add = lambda x, y: x + y
print(add(3, 5)) # 8
# With map()
numbers = [1, 2, 3]
squared = list(map(lambda x: x**2, numbers)) # [1, 4, 9]
Limitation: Lambdas cannot contain statements or multiple expressions; use def for complex functions.
10. What is list comprehension?
List comprehension is a concise way to create lists in Python by applying an expression to each item in an iterable, optionally with conditions. It provides a more readable and efficient alternative to using for loops and append(). The syntax is [expression for item in iterable if condition].
# Basic list comprehension
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]
# With condition
evens = [x for x in range(10) if x % 2 == 0] # [0, 2, 4, 6, 8]
# Nested comprehension (matrix flattening)
matrix = [[1, 2], [3, 4]]
flat = [num for row in matrix for num in row] # [1, 2, 3, 4]
Best practice: Use for simple transformations; avoid overly complex comprehensions for readability.
