Open In App

Python Syntax & Core Constructs Interview Questions

Last Updated : 27 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Python is a high-level programming language known for its simplicity and readability. In interviews, fundamental concepts are often tested to evaluate your understanding of core programming practices. Below are some of the most frequently asked interview questions and answers covering key Python fundamental topics.

1. Is Python a compiled language or an interpreted language?

Whether a language is compiled or interpreted or both is not defined in the language standard. In other words, it is not a properly of a programming language. Different Python distributions (or implementations) choose to do different things (compile or interpret or both). However the most common implementations like CPython do both compile and interpret, but in different stages of its execution process.

  • Compilation: When you write Python code and run it, the source code (.py files) is first compiled into an intermediate form called bytecode (.pyc files). This bytecode is a lower-level representation of your code, but it is still not directly machine code. It’s something that the Python Virtual Machine (PVM) can understand and execute.
  • Interpretation: After Python code is compiled into bytecode, it is executed by the Python Virtual Machine (PVM), which is an interpreter. The PVM reads the bytecode and executes it line-by-line at runtime, which is why Python is considered an interpreted language in practice.

Some implementations, like PyPy, use Just-In-Time (JIT) compilation, where Python code is compiled into machine code at runtime for faster execution, blurring the lines between interpretation and compilation.

2. What is the output of bool("False") in Python and why?

The output of bool("False") in Python is True.

Reason:

  • In Python, bool() checks whether the given value is truthy or falsy.
  • An empty string "" is considered False, but any non-empty string (like "False") is considered True, regardless of the word it contains.
  • So even though the string is "False", it is non-empty - hence True.
Python
print(bool("False"))  
print(bool(""))      

Output
True
False

3. Why is Indentation Required in Python?

Indentation is required in Python because it is the way Python defines the structure and scope of code blocks (such as loops, conditionals, functions, and classes).

Unlike many other languages (C, Java, etc.) that use {} or keywords to mark code blocks, Python uses indentation (whitespace at the beginning of a line).

Reasons:

  • Readability: Forces clean, consistent code formatting.
  • Scope Definition: Determines which statements belong to a particular block (e.g., inside an if, for, or def).
  • Simplicity: Removes the need for extra symbols like {} or end.
Indentation-in-python
Python Indentation

4. What is the difference between is and == in Python?

The difference between "is" (Identity Operator) and "==" (Equality Operator) in Python is:

  • == : Compares the values of two objects. Returns True if the values are equal, even if the objects are different in memory.
  • is : Compares the memory addresses (identities) of two objects. Returns True only if both operands point to the same object in memory.
Python
a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)  # True  (values are equal)
print(a is b)  # False (different objects in memory)

Output
True
False

5. How do you floor a number in Python?

To floor a number in Python, we can use the math.floor() function, which returns the largest integer less than or equal to the given number.

Python
import math

print(math.floor(4.9))   # 4
print(math.floor(-4.9))  # -5

Output
4
-5

6. What is the difference between / and // in Python?

/ represents precise division (result is a floating point number) whereas // represents floor division (result is an integer).

Python
print(5//2)
print(5/2)

Output
2
2.5

7. Can we Pass a function as an argument in Python?

Yes, Several arguments can be passed to a function, including objects, variables (of the same or distinct data types) and functions. Functions can be passed as parameters to other functions because they are first-class objects in Python. Functions that can take other functions as arguments are known as higher-order functions.

Python
def add(x, y):
    return x + y

def apply_func(func, a, b):
    return func(a, b)

print(apply_func(add, 3, 5))

Output
8

The add function is passed as an argument to apply_func, which applies it to 3 and 5.

8. What is a dynamically typed language?

A dynamically typed language is a language where the type of a variable is determined at runtime, not at compile time. In such languages, you don’t need to declare the type of a variable explicitly, the interpreter infers it based on the value assigned. Since variables can hold values of different types at different times, type checking happens during execution rather than before. Python is a dynamically typed language.

Python
x = 10       # x is an integer
x = "Hello"  # Now x is a string

Here, the type of x changes at runtime based on the assigned value hence it shows dynamic nature of Python.

9. What is pass in Python?

  • The pass statement is a placeholder that does nothing.
  • It is used when a statement is syntactically required but no code needs to run.
  • Commonly used when defining empty functions, classes or loops during development.
Python
def fun():
    pass  # Placeholder, no functionality yet

# Call the function
fun()

Here, fun() does nothing, but the code stays syntactically correct.

10. What is a mutable default argument bug in Python?

The mutable default argument bug in Python occurs when a mutable object (like a list or dictionary) is used as a default argument in a function. Since default arguments are evaluated only once when the function is defined, the same object is reused across multiple function calls. This can lead to unexpected behavior because modifications persist between calls.

Python
def append_val(val, lst=[]):
    lst.append(val)
    return lst

print(append_val(1))  # [1]
print(append_val(2))  # [1, 2] unexpected

Fix: Use None as the default and create a new object inside the function.

Python
def append_val(val, lst=None):
    if lst is None:
        lst = []
    lst.append(val)
    return lst

11. What are *args and **kwargs?

In Python, *args and **kwargs are special symbols used to pass a variable number of arguments to a function.

  • *args (Non-keyword arguments): Allows a function to accept any number of positional arguments. Inside the function, they are stored as a tuple.
Python
def fun(*argv):
    for arg in argv:
        print(arg)

fun('Hello', 'Welcome', 'to', 'GeeksforGeeks')

Output
Hello
Welcome
to
GeeksforGeeks
  • **kwargs (Keyword arguments): Allows a function to accept any number of keyword arguments (key-value pairs). Inside the function, they are stored as a dictionary.
Python
def fun(**kwargs):
    for k, val in kwargs.items():
        print("%s == %s" % (k, val))

fun(s1='Geeks', s2='for', s3='Geeks')

Output
s1 == Geeks
s2 == for
s3 == Geeks

12. Explain the concept of Python closures.

A closure is a function object that remembers values in its enclosing scopes even if the outer function has finished executing.

Closures occur when:

  • A nested (inner) function is defined inside another (outer) function.
  • The inner function uses variables from the outer function.
  • The outer function returns the inner function.
Python
def outer(x):
    def inner(y):
        return x + y
    return inner

add_five = outer(5)
print(add_five(3))  # 8 → inner remembers x = 5

Output
8

13. Why does the following loop print 10 ten times?

Python
funcs = [lambda: i for i in range(1,11)]
print([f() for f in funcs])  # [10, 10, ..., 10]

Output
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

Explanation:

  • range(1, 11) generates numbers from 1 to 10 → so the loop runs 10 times.
  • In each iteration, a lambda lambda: i is created. But lambdas capture the variable i, not its value at that moment.
  • After the loop ends, i is left at 10.
  • When evaluating [f() for f in funcs], every function looks up i (which is now 10).

14. What is docstring in Python?

A docstring in Python is a special string literal used to document a module, class, function, or method.

  • It is written as the first statement inside the definition (enclosed in triple quotes """ """ or ''' ''').
  • The docstring can be accessed at runtime using the .__doc__ attribute or the built-in help() function.
  • Docstrings improve readability and make code easier to understand and maintain.
Python
def greet(name):
    """This function greets the person passed as an argument."""
    return f"Hello, {name}!"

print(greet("Gukesh"))

# Accessing the docstring
print(greet.__doc__)

Output
Hello, Gukesh!
This function greets the person passed as an argument.

15. What is the difference between deepcopy() and copy() in Python?

The difference between copy() and deepcopy() in Python lies in how they handle nested (compound) objects like lists of lists or objects containing other objects.

  • copy() creates a shallow copy. It creates a new object, but only copies references of nested objects, changes made to nested (mutable) objects affect both the original and the copy.
  • deepcopy() creates a deep copy. It creates a new object and recursively copies all nested objects, changes to nested objects in the copy do not affect the original.
Python
import copy

original = [[1, 2], [3, 4]]
shallow = copy.copy(original)
deep = copy.deepcopy(original)

original[0][0] = 99

print(shallow)  
print(deep)     

16. How does slicing work in Python?

Slicing is a way to extract a portion (subsequence) of a sequence type such as lists, strings, or tuples using the following syntax:

Syntax:

sequence[start : stop : step]

  • start: The index where slicing begins (inclusive). Default = 0.
  • stop: The index where slicing ends (exclusive). Default = len(sequence).
  • step: The interval between elements. Default = 1.
Python
s = "Python"
print(s[0:4])      # 'Pyth'
print(s[::2])      # 'Pto' → step of 2
print(s[::-1])     # 'nohtyP' → reverse string

Explore