← Back
Question 11 Interview Prep

What is a function in Python?

A function is a reusable block of code designed to perform a specific task. Functions help improve code reusability and organization.

Example:

def greet():
print("Hello")

Functions reduce code duplication and make programs easier to maintain.

Question 12 Interview Prep

What is the difference between parameters and arguments?

Parameters are variables defined in the function definition, while arguments are the actual values passed to the function during the function call.

Example:

def greet(name): # parameter
print(name)

greet("Rahul") # argument

Question 13 Interview Prep

What is the purpose of the return statement?

The return statement is used to send a value back from a function. It allows the function result to be reused elsewhere in the program.

Example:

def add(a, b):
return a + b

Here, the function returns the sum of two numbers.

Question 14 Interview Prep

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

== checks whether two values are equal, while is checks whether two variables refer to the same memory location.

Example:

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

print(a == b) # True
print(a is b) # False

This is an important concept in Python interviews.

Question 15 Interview Prep

What are loops in Python?

Loops are used to execute a block of code repeatedly.

Python mainly supports:

for loop
while loop

Loops help automate repetitive tasks efficiently.

Question 16 Interview Prep

What is the difference between a for loop and a while loop?

A for loop is generally used when the number of iterations is known, while a while loop is used when execution depends on a condition.

Example:

for i in range(5):
print(i)
count = 0

while count < 5:
print(count)
count += 1

Question 17 Interview Prep

What is the difference between break and continue?

break immediately stops the loop, while continue skips the current iteration and moves to the next iteration.

Example:

for i in range(5):
if i == 3:
break

Here, the loop stops when i becomes 3.

Question 18 Interview Prep

What is a string in Python?

A string is a sequence of characters used to store text data.

Example:

name = "Python"

Strings are immutable in Python, meaning they cannot be changed after creation.

Question 19 Interview Prep

What is string slicing?

String slicing is used to extract a portion of a string using indexes.

Example:

text = "Python"

print(text[0:3])

Output:

Pyt

Slicing is commonly used for manipulating text data.

Question 20 Interview Prep

What is list comprehension?

List comprehension provides a shorter and cleaner way to create lists.

Example:

numbers = [x for x in range(5)]

Output:

[0, 1, 2, 3, 4]

It improves code readability and reduces the number of lines required.