Answer any 2 questions. Each question carried 2 marks.
(2*2 = 4)
1. Define Python Interpreter/Shell.
A Python Interpreter is the program that reads and executes Python code. It translates your Python
instructions into machine-readable form line by line, so the computer can understand and run
them.
2. What is a Comment? How to write comments in Python? RA
3. Define List in Python with example. RA
4. Explain different Datatypes used in Python RA
5. Explain Control Flow Statements in Python.
Python uses conditional statements to perform decision-making, allowing programs to execute different
code blocks based on whether a condition is True or False. These are also known as control flow
statements.
Control flow statements:
1. if
2. if-else
3. if-elif
4. Nested if
If statement: The simplest form, which executes a block of code only if its condition is True.
Syntax:
if condition:
Statement 1
Ex:
age = 20
If age >= 18:
print(“You are eligible to vote.”)
if-else statement: Executes the if block if the condition is True, and an alternative else block if the
condition is False.
Syntax:
if condition:
Statement 1
else:
statement2
Ex:
age = 15
if age >= 18:
print("You can vote.")
else:
print("You are too young to vote.")
if-elif-else ladder: Used to check multiple conditions sequentially. Python checks the conditions from
the top; the first one that evaluates to True has its corresponding code block executed, and the rest of the
ladder is skipped.
Syntax:
if condition:
Statement 1
elif:
Statement 2
elif:
Statement 3
else:
Statement 4
Ex:
marks = 85
if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
else:
print("Grade: F")
Nested if statements: Involves placing an if statement inside another if or else block. This is useful for
multi-layered conditional logic.
Syntax:
if condition:
if condition:
Statement 1
else:
Statement2
else:
Statement2
Ex:
num = 10
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
6. What is a List? Explain Indexing and Slicing of List in Python. RA
7. What is a Function? Explain in detail.
A function in Python is a reusable block of code that performs a specific task, designed to promote
modularity, reusability, and readability in a program.
Function Names
Function names follow the same rules as variable names in Python:
• A function name must start with a letter or underscore
• A function name can only contain letters, numbers, and underscores
• Function names are case-sensitive (myFunction and myfunction are different)
Defining a Function
Functions are defined using the def keyword, followed by a name, parentheses (), and a colon :. The
function body is the indented block of code that follows.
Syntax:
def function_name(parameters):
Statement 1
return expression
Ex:
def add(x,y):
z=x+y
print(z)
Calling a function:
To call a function, write its function name followed by parentheses
Ex:
x=20
y=10
def add(x,y):
z=x+y
print(z)
add(x,y) #calling the function
Types of Functions in Python
1. Built-in Functions
Functions that are already available in Python.
• Print() → displays output
• Len() → returns length
• Type() → tells data type
Examples:
print("Hello")
length = len("Python")
data_type = type(10)
2. User-defined Functions
Functions created by the programmer using the def keyword.
Example:
def greet(name):
print("Hello", name)
greet("Anu")
Function within Functions
A function defined inside another function is called an inner function (or nested function).
def f1():
s = 'I love python'
def f2():
print(s)
f2()
f1()
Op:
I love python
Return Statement in Function
The return statement ends a function and sends a value back to the caller. It can return any data type,
multiple values or None if no value is given.
Syntax:
return [expression]
Ex:
def square(num):
return num**num
Print(square(2))
Print(square(-4))
Output
16
8. In detail Explain Looping Statements in Python.
Python has two primary looping statements: for loops and while loops. These loops allow a block of code
to be executed repeatedly.
[Link] Loops
A for loop is used to iterate over a sequence (such as a list, tuple, string, dictionary, or range) and
executes a block of code once for each item in the sequence.
Syntax:
for variable in sequence:
# Code block to be executed
Ex:
n=4
for i in range(0, n):
print(i)
Output
[Link] loops
A while loop repeatedly executes a block of code as long as a given condition remains True. The condition
is checked at the beginning of each iteration.
Syntax:
while condition:
# Code block to be executed
Ex:
count = 0
While (count < 3):
count = count + 1
Print(“Hello”)
Output:
Hello
Hello
Hello
Nested Loops
Python allows you to use a loop inside another loop, which is known as a nested loop. The inner loop
executes completely for every single iteration of the outer loop.
Ex:
For I in range(1,4):
For j in range(1,4):
print(i,j)
Output:
11
12
13
21
22
23
31
32
33
Loop Control Statements
Python provides special keywords to modify the normal flow of loops:
• Continue
• Break
• Pass
Continue Statement
The continue statement in Python returns the control to the beginning of the loop.
for i in range(1, 6):
If i == 3:
continue
print(i)
Output:
Break Statement
The break statement in Python brings control out of the loop.
for i in range(1, 6):
if i == 3:
break
print(i)
Output
Pass Statement
Acts as a placeholder . It does nothing, but avoids errors when a statement is required syntactically
for i in range(1, 6):
if i == 3:
pass
print(i)
Output
9. What is a String? Explain in detail with examples. RA