0% found this document useful (0 votes)
4 views

Python Interview Questions and Answers

The document provides a series of Python interview questions and answers covering topics such as whether Python is compiled or interpreted, list concatenation methods, loop differences, and the use of lambda functions. It explains key concepts like dynamic typing, the pass statement, and argument passing in Python. Each question is accompanied by examples to illustrate the answers effectively.

Uploaded by

waghmithu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Interview Questions and Answers

The document provides a series of Python interview questions and answers covering topics such as whether Python is compiled or interpreted, list concatenation methods, loop differences, and the use of lambda functions. It explains key concepts like dynamic typing, the pass statement, and argument passing in Python. Each question is accompanied by examples to illustrate the answers effectively.

Uploaded by

waghmithu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Interview Questions and Answers

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

Please remember one thing, 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. How can you concatenate two lists in Python?

We can concatenate two lists in Python using the +operator or the extend() method.

1. Using the + operator:

This creates a new list by joining two lists together.

a = [1, 2, 3]

b = [4, 5, 6]

res = a + b

print(res)

Output

[1, 2, 3, 4, 5, 6]

2. Using the extend() method:


This adds all the elements of the second list to the first list in-place.

a = [1, 2, 3]

b = [4, 5, 6]

a.extend(b)

print(a)

Output

[1, 2, 3, 4, 5, 6]

3. Difference between for loop and while loop in Python

• For loop: Used when we know how many times to repeat, often with lists, tuples,
sets, or dictionaries.

• While loop: Used when we only have an end condition and don’t know exactly
how many times it will repeat.

for i in range(5):

print(i)

c=0

while c < 5:

print(c)

c += 1

4. How do you floor a number in Python?

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

• floor() method in Python returns the floor of x i.e., the largest integer not greater
than x.

• Also, The method ceil(x) in Python returns a ceiling value of x i.e., the smallest
integer greater than or equal to x.

import math

n = 3.7
F_num = math.floor(n)

print(F_num)

Output

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 objects. Higher-order functions are functions that can
take other functions as arguments.

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?

• In a dynamically typed language, the data type of a variable is determined at


runtime, not at compile time.

• No need to declare data types manually; Python automatically detects it based


on the assigned value.

• Examples of dynamically typed languages: Python, JavaScript.

• Examples of statically typed languages: C, C++, Java.

• Dynamically typed languages are easier and faster to code.

• Statically typed languages are usually faster to execute due to type checking at
compile time.
Example:

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.

def fun():

pass # Placeholder, no functionality yet

# Call the function

fun()

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

10. How are arguments passed by value or by reference in Python?

• Python’s argument-passing model is neither “Pass by Value” nor “Pass by


Reference” but it is “Pass by Object Reference”.

• Depending on the type of object you pass in the function, the function behaves
differently. Immutable objects show “pass by value” whereas mutable objects
show “pass by reference”.

You can check the difference between pass-by-value and pass-by-reference in the
example below:

def call_by_val(x):

x=x*2

print("value updated to", x)

return

def call_by_ref(b):
b.append("D")

print("list updated to", b)

return

a = ["E"]

num = 6

call_by_val(num)

call_by_ref(a)

Output

value updated to 12

list updated to ['E', 'D']

11. What is a lambda function?

A lambda function is an anonymous function. This function can have any number of
parameters but, can have just one statement.

In the example, we defined a lambda function(upper) to convert a string to its upper


case using upper().

s1 = 'GeeksforGeeks'

s2 = lambda func: func.upper()

print(s2(s1))

Output

GEEKSFORGEEKS

You might also like