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

python ans

The document explains Python's indentation rules, emphasizing the use of spaces instead of braces for code blocks. It details control flow structures including conditional statements (if, if-else, if-elif-else) and looping statements (for, while), as well as control statements like break and continue. Examples are provided for each type of statement to illustrate their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

python ans

The document explains Python's indentation rules, emphasizing the use of spaces instead of braces for code blocks. It details control flow structures including conditional statements (if, if-else, if-elif-else) and looping statements (for, while), as well as control statements like break and continue. Examples are provided for each type of statement to illustrate their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1 st ans

In Python, indentation means adding spaces at the beginning of a line to show which
code belongs together.
It is used in loops, functions, and conditions. Python does not use `{}` like other
languages; it uses
spaces instead. Usually, we use **four spaces** for indentation. If the spaces are
not correct, Python gives an error.

2 nd ans
1. break Statement
The break statement is used to stop a loop immediately when a certain condition is
met.
Syntax:
for/while loop:
if condition:
break # Exits the loop
Example:
for i in range(1, 6):
if i == 3:
break # Stops the loop when i is 3
print(i)
Output:
1
2
2. continue Statement
The continue statement is used to skip the current iteration of the loop and move
to the next one.

Syntax:
for/while loop:
if condition:
continue # Skips the rest of the code in the current loop iteration
Example:
for i in range(1, 6):
if i == 3:
continue # Skips when i is 3
print(i)
Output:
1
2
4
5

3 rd ans
Control flow structures determine the execution order of statements in a Python
program. Python provides
three main types of control flow structures:
Conditional Statements (Decision Making)
Looping Statements (Iteration)
Control Statements (Loop Control)
Each of these structures helps manage the flow of execution, making programs
efficient and dynamic.
1. Conditional Statements (Decision Making)
Conditional statements allow a program to execute different code blocks based on
conditions.

a) if Statement
The if statement executes a block of code only if a specified condition evaluates
to True.
If the condition is False, the code inside the if block is skipped.
Syntax:
if condition:
# Code block executed if the condition is True
Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
b) if-else Statement
The if-else statement provides an alternative block of code when the condition is
False.
Syntax:
if condition:
# Executes if condition is True
else:
# Executes if condition is False
Example:
age = 16
if age >= 18:
print("You can vote.")
else:
print("You cannot vote yet.")
c) if-elif-else Statement
The if-elif-else statement checks multiple conditions.
If one condition is True, its corresponding block executes, and the rest are
skipped.
Syntax:
if condition1:
# Code block executed if condition1 is True
elif condition2:
# Code block executed if condition1 is False but condition2 is True
else:
# Code block executed if both conditions are False
Example:
marks = 75
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Grade: F")
2. Looping Statements (Iteration)
Looping statements allow executing a block of code multiple times. Python provides
two main types of loops:

for loop
while loop
a) for Loop
The for loop is used to iterate over a sequence (list, tuple, string, range, etc.).
It repeats for a fixed number of iterations.
Syntax:
for variable in sequence:
# Code block to be executed in each iteration
Example:
for i in range(1, 6): # Loops from 1 to 5
print(i)
b) while Loop
The while loop executes a block of code as long as the condition is True.
It is useful when the number of iterations is unknown in advance.
Syntax:
while condition:
# Code block executed while condition is True
Example:
x = 1
while x <= 5:
print(x)
x += 1
3. Control Statements (Loop Control)
Control statements modify the normal execution of loops.

a) break Statement
The break statement immediately exits a loop, even if the condition is still True.
Example:
for i in range(1, 6):
if i == 4:
break # Stops the loop when i is 4
print(i)
b) continue Statement
The continue statement skips the current iteration and moves to the next one.
Example:
for i in range(1, 6):
if i == 3:
continue # Skips when i is 3
print(i)

You might also like