Python Syntax & Core Constructs Interview Questions (original) (raw)

Last Updated : 27 Aug, 2025

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.

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:

print(bool("False"))
print(bool(""))

`

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:

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:

a = [1, 2, 3] b = [1, 2, 3]

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

`

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

`

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)

`

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))

`

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?

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.

def fun(*argv): for arg in argv: print(arg)

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

`

Output

Hello Welcome to GeeksforGeeks

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:

def outer(x): def inner(y): return x + y return inner

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

`

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:

14. What is docstring in Python?

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

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.

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]

s = "Python" print(s[0:4]) # 'Pyth' print(s[::2]) # 'Pto' → step of 2 print(s[::-1]) # 'nohtyP' → reverse string

`