0% found this document useful (0 votes)
2 views5 pages

Python Unit 2 Notes

The document outlines Python's flow control statements, which include conditional statements, looping statements, and control flow altering statements. It provides examples of each type, such as if statements, for and while loops, and the use of break, continue, and pass statements. Additionally, it introduces the match-case statement for decision making based on pattern matching.
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)
2 views5 pages

Python Unit 2 Notes

The document outlines Python's flow control statements, which include conditional statements, looping statements, and control flow altering statements. It provides examples of each type, such as if statements, for and while loops, and the use of break, continue, and pass statements. Additionally, it introduces the match-case statement for decision making based on pattern matching.
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

Python – Unit -2

Python's flow control statements


Python's flow control statements regulate the order in which a program's instructions are executed,
moving beyond the default top-to-bottom sequence. They are categorized into conditional
statements, looping statements, and control flow altering statements.

Conditional Statements (Decision Making)

These statements allow a program to execute specific code blocks based on whether a condition
is True or False.

if statement: Executes a block of code if its condition is True.

example:

age = 20

if age >= 18:

print("Eligible to vote.") # This line executes

if-else statement: Executes the if block if the condition is True, and the else block if it is False

example:

age = 16

if age >= 18:

print("You are an adult.")

else:

print("You are a minor.") # This line executes

if-elif-else chain: Checks multiple conditions sequentially. The code block for the first True condition
is executed. The final else block runs if none of the preceding conditions are met.

example:

score = 75

if score >= 90:

print("Grade: A")

elif score >= 80:

print("Grade: B")

elif score >= 70:

KANPUR INSTITUTE OF TECHNOLOGY PROFESSOR -ASHISH TRIPATHI ( Dept. CSE)


Python – Unit -2

print("Grade: C") # This line executes

else:

print("Grade: D or F")

nested if statement in Python


example :

username = "Emil"

password = "python123"

is_active = True

if username:

if password:

if is_active:

print("Login successful")

else:

print("Account is not active")

else:

print("Password required")

else:

print("Username required")

Looping Statements (Iteration)


These statements allow a block of code to be executed repeatedly as long as a certain condition is
met or for each item in a sequence.

for loop: Iterates over the items of any sequence (a list, tuple, string, or range).

example:

for num in range(1, 6):

if num % 2 != 0:

continue # Skip odd numbers

KANPUR INSTITUTE OF TECHNOLOGY PROFESSOR -ASHISH TRIPATHI ( Dept. CSE)


Python – Unit -2

print(f"{num} is even")

while loop: Repeats a block of code as long as its specified condition remains True

example:

count = 0

while count < 5:

print("Count is:", count)

count += 1

else clause on loops: A unique Python feature, the else block can be used with loops. It executes
after the loop finishes naturally (i.e., without encountering a break statement).

example:

for a in range(5):

print(a)

else:

print(“loop finished”)

nested loop in Python

A nested loop in Python is a loop placed inside the body of another loop. The inner loop runs
completely for every single iteration of the outer loop. This structure is useful for working with
multi-dimensional data structures (like lists of lists), generating combinations, and printing
pattern

example:

# Print a right-aligned triangle of stars

n=5

for i in range(n):

# Inner loop for spaces

for j in range(n - i - 1):

print(" ", end="")

KANPUR INSTITUTE OF TECHNOLOGY PROFESSOR -ASHISH TRIPATHI ( Dept. CSE)


Python – Unit -2

# Inner loop for stars

for k in range(i + 1):

print("*", end="")

# Newline after each row

print()

Control Flow Altering Statements (Jump Statements)

These keywords alter the normal flow of a loop's execution.

Unstop

• break statement: Immediately terminates the entire enclosing loop and transfers control to
the next statement after the loop.

example:

x=0

while x < 10:

print("x:", x)

if x == 5:

print("Breaking...")

break

x += 1

print("End")

• continue statement: Skips the rest of the current iteration of the loop and continues with the
next iteration.

example:

for letter in "Python":

# continue when letter is 'h'

if letter == "h":

continue

print("Current Letter :", letter)

KANPUR INSTITUTE OF TECHNOLOGY PROFESSOR -ASHISH TRIPATHI ( Dept. CSE)


Python – Unit -2

• pass statement: A null operation; nothing happens when it executes. It is used as a


placeholder where a statement is syntactically required but no action is needed.

example:

a = 33

b = 200

if b > a:

pass # No action needed if b is greater than a

else:

print("b is not greater than a")

The match Statement

Python supports Match-Case statement, which can also be used as a part of decision making. If a
pattern matches the expression, the code under that case will execute.

example:

def checkVowel(n):

match n:

case 'a': return "Vowel alphabet"

case 'e': return "Vowel alphabet"

case 'i': return "Vowel alphabet"

case 'o': return "Vowel alphabet"

case 'u': return "Vowel alphabet"

case _: return "Simple alphabet"

print (checkVowel('a'))

print (checkVowel('m'))

print (checkVowel('o'))

KANPUR INSTITUTE OF TECHNOLOGY PROFESSOR -ASHISH TRIPATHI ( Dept. CSE)

You might also like