1. What are Jump statements?
Explain with appropriate syntax and
examples.
Jump Statements in Python:
Jump statements are used to control the flow of execution in a program
by "jumping" to a different part of the code. They allow skipping or
terminating certain blocks of code.
Types of Jump Statements:
1. break:
o Purpose: Exits from the current loop (for or while)
immediately.
o Syntax:
o break
o Example:
o for i in range(5):
o if i == 3:
o break # Exit loop when i is 3
o print(i)
o # Output: 0 1 2
2. continue:
o Purpose: Skips the current iteration of a loop and moves to
the next iteration.
o Syntax:
o continue
o Example:
o for i in range(5):
o if i == 3:
o continue # Skip the rest of the loop when i is 3
o print(i)
o # Output: 0 1 2 4
3. pass:
o Purpose: Does nothing. It’s a placeholder when a statement
is syntactically required but no action is needed.
o Syntax:
o pass
o Example:
o for i in range(5):
o if i == 3:
o pass # Do nothing when i is 3
o else:
o print(i)
o # Output: 0 1 2 4
Summary:
• break: Exits a loop.
• continue: Skips the current loop iteration.
• pass: Does nothing (used as a placeholder).
2. What are endless loops? Why do such loops occur?
Endless Loops (Infinite Loops)
• Definition: An endless or infinite loop is a loop that runs
indefinitely and never stops, unless externally interrupted (e.g., by
a user or a program termination).
• Why Do Endless Loops Occur?:
1. Incorrect Loop Condition: The loop condition always
evaluates to True, leading to an infinite cycle.
▪ Example:
▪ while True:
▪ print("This will run forever!")
2. Lack of Exit Condition: The loop does not contain a
statement to change the loop condition or exit the loop.
▪ Example:
▪ i=0
▪ while i < 5:
▪ print(i) # No increment of 'i', so the loop never ends
3. Incorrect Increment/Decrement in Loops: The loop's control
variable is not updated correctly, keeping the condition true.
▪ Example:
▪ i=0
▪ while i < 10:
▪ print(i) # 'i' is not updated, causing an infinite loop
Summary:
• Endless Loops occur due to faulty loop conditions or
missing/incorrect updates to loop variables.
• These loops can cause the program to hang, consuming resources
indefinitely.
3. What is the difference between else clause of if-else and else clause
of python loops?
Difference Between else Clause in if-else and Python Loops:
Aspect else in if-else else in Loops
Purpose Executes when the if Executes after the loop
condition is False finishes, if no break occurs
Condition for Executes when the if Executes after the loop
Execution condition fails completes normally
(without break)
Usage Used to define an Used to run code after the
alternative code block loop completes, unless
when if is not true interrupted by break
Example (if- ```python ```python
else)
if condition: for i in range(5):
# Code for if condition print(i)
else: else:
# Code for else when print("Loop completed
condition is false without break")
4. A store charges $120 per item if you buy less than 10 items. If you
buy between 10 and 99 items , the cost is $100 per month. If you buy
100 or more items, the cost is $70 per item. Write a program that asks
the user how many items they are buying and print the total costs.
Enter the number of items you are buying: 25
The total cost for 25 items is: $2500
5. Program that searches for prime numbers from 50 through 65.
Prime numbers from 50 to 65:
53
59
61
6. Write programs using nested loops to produce the following patterns
0
22
444
6666
88888
1
22
333
7. Write a python script to print Fibonacci series first 20 elements. Some
initial elements of the Fibonacci series are : 0 1 1 2 3 5 8….. and so on
8. Following code is meant to be an interactive grade calculation script
for an entrance test that converts from a percentage into a letter grade:
FOR EXAMPLE: 90 and above is A+ 80-90 is A 60-80 is A- And everything
below is fail .
9. What is python conditions and if statements? What is the importance
of python conditions in real life?
Python Conditions and If Statements
• Conditions: Expressions that evaluate to True or False.
• If Statement: Executes code only if a condition is True.
Examples
x = 10
if x > 5:
print("x is greater than 5") # Runs because condition is True
Types of If Statements
1. Simple If → Executes if condition is True.
2. If-Else → Executes one block if True, another if False.
3. If-Elif-Else → Handles multiple conditions.
4. Nested If → If statements inside another if statement.
Importance of Python Conditions in Real Life
• Automated Decision Making → Loan approvals, fraud detection.
• Data Validation → Checking passwords, emails, form inputs.
• Traffic Control → Adjusting signals based on vehicle count.
• Smart Homes → Turning lights on/off based on motion detection.
• Gaming Logic → Winning/losing conditions, scoring systems.
10. Explain types of control in python with example.
Types of Control in Python
Python has three main types of control structures that help direct the
flow of execution:
1. Conditional Control (Decision-Making)
2. Loop Control (Repetition)
3. Branching Control (Jump Statements)
1. Conditional Control (Decision-Making)
Conditional statements allow a program to make decisions based on
conditions.
Types of Conditional Statements:
(a) if Statement
Executes a block of code if the condition is True.
age = 18
if age >= 18:
print("You are eligible to vote.")
(b) if-else Statement
Executes one block if the condition is True and another if False.
num = 10
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
(c) if-elif-else Statement
Handles multiple conditions.
marks = 85
if marks >= 90:
print("Grade: A+")
elif marks >= 80:
print("Grade: A")
elif marks >= 60:
print("Grade: B")
else:
print("Fail")
2. Loop Control (Repetition)
Loops are used to execute a block of code multiple times.
Types of Loops in Python:
(a) for Loop
Iterates over a sequence (list, tuple, string, range, etc.).
for i in range(1, 6):
print(i) # Prints numbers from 1 to 5
(b) while Loop
Repeats a block of code while a condition is True.
count = 1
while count <= 5:
print(count)
count += 1
3. Branching Control (Jump Statements)
Branching statements are used to change the normal flow of a program.
Types of Branching Statements:
(a) break Statement
Exits the loop when a certain condition is met.
for i in range(1, 10):
if i == 5:
break # Stops the loop at 5
print(i)
(b) continue Statement
Skips the current iteration and continues with the next one.
for i in range(1, 6):
if i == 3:
continue # Skips 3 and moves to the next iteration
print(i)
(c) pass Statement
Used as a placeholder when a statement is required but no action is
needed.
for i in range(5):
if i == 3:
pass # Placeholder, does nothing
else:
print(i)
11. Differentiate between for loop and while loop.
Difference Between for Loop and while Loop
Feature for Loop while Loop
Usage Used when the number Used when the number of
of repetitions is known. repetitions is unknown and
depends on a condition.
Syntax Uses an iterator (like Runs based on a condition.
range()).
Condition Checks condition before Checks condition before each
Checking each iteration using a iteration and stops when it's
sequence. False.
Best For Iterating over sequences Running loops until a certain
(lists, strings, tuples, condition is met.
etc.).
Loop Automatically updates Needs manual update inside
Control the loop variable. the loop (e.g., count += 1).
Example for i in range(5): print(i) count = 0 while count < 5:
print(count); count += 1
When to When the number of When the number of
Use? iterations is fixed. iterations is not fixed.
Key Takeaway:
• Use for loop when you know how many times to loop.
• Use while loop when you don’t know the exact number of
repetitions.
12. What is range( ) function in python.
range() Function in Python
The range() function generates a sequence of numbers and is
commonly used in loops.
Key Points:
1. Generates a sequence of numbers.
2. Syntax: range(start, stop, step)
o start → (Optional) Beginning number (default is 0).
o stop → (Required) The sequence stops before this number.
o step → (Optional) The increment (default is 1).
3. Example Usage:
for i in range(1, 6):
print(i) # Output: 1 2 3 4 5
4. Memory Efficient:
o range() does not create a list, making it efficient for large
numbers.
5. print(range(5)) # Output: range(0, 5)
print(list(range(5))) # Output: [0, 1, 2, 3, 4]
6. Supports Negative Steps:
for i in range(10, 0, -2):
print(i) # Output: 10 8 6 4 2
13. Python has two primitive loop commands. Explain each with an
example.
Two Primitive Loop Commands in Python
Python has two main types of loops:
1. for Loop (Used for Iteration)
• Used when the number of iterations is known.
• Iterates over sequences (lists, tuples, strings, range(), etc.).
Example:
for i in range(1, 6):
print(i) # Output: 1 2 3 4 5
2. while Loop (Used for Condition-Based Repetition)
• Used when the number of iterations is unknown and based on a
condition.
• Runs until the condition becomes False.
Example:
count = 1
while count <= 5:
print(count)
count += 1 # Output: 1 2 3 4 5
Key Differences:
Feature for Loop while Loop
Usage Used when the number Used when the number of
of iterations is fixed. iterations is unknown.
Condition Uses an iterator (like Uses a condition that must
Checking range()). be updated inside the loop.
Best For Iterating over Running loops until a certain
sequences (lists, strings, condition is met.
etc.).
14. Write the syntax for the following terms:
i)if statement
ii) if-else statement
iii)nested if-else statement
iv)python elif
v)break – for loop, while
vi)continue- for loop, while
vii)While
Syntax for Different Python Control Statements
i) if Statement
if condition:
# Code to execute if condition is True
ii) if-else Statement
if condition:
# Code to execute if condition is True
else:
# Code to execute if condition is False
iii) Nested if-else Statement
if condition1:
if condition2:
# Code to execute if both conditions are True
else:
# Code to execute if condition1 is True but condition2 is False
else:
# Code to execute if condition1 is False
iv) elif Statement (Multiple Conditions)
if condition1:
# Code for condition1
elif condition2:
# Code for condition2
else:
# Code if none of the conditions are True
v) break Statement in for and while Loops
# break in for loop
for i in range(5):
if i == 3:
break # Stops the loop at 3
print(i)
# break in while loop
count = 0
while count < 5:
if count == 3:
break # Stops the loop at 3
print(count)
count += 1
vi) continue Statement in for and while Loops
# continue in for loop
for i in range(5):
if i == 3:
continue # Skips 3 and moves to the next iteration
print(i)
# continue in while loop
count = 0
while count < 5:
count += 1
if count == 3:
continue # Skips printing 3
print(count)
vii) while Loop
while condition:
# Code to execute while the condition is True
# Ensure condition changes to avoid infinite loop
15. Accept three numbers from the user and display the largest number.
Enter first number: 12
Enter second number: 45
Enter third number: 7
The largest number is: 45