Python Flow Control Functions
Python Flow Control Functions
• Includes:
• • Decision Making
• • Loops
• • Nested Loops
Decision Making
• Python uses if, elif, and else for decisions.
• Example:
• age = 18
• if age >= 18:
• print("Adult")
• else:
• print("Minor")
Types of Loops
• 1. for loop: Used for iterating over a sequence.
• 2. while loop: Repeats while a condition is
True.
• 3. nested loop: Loop inside another loop.
for Loop Example
• for i in range(5):
• print(i)
• # Outputs: 0 to 4
while Loop Example
• count = 0
• while count < 5:
• print(count)
• count += 1
• # Outputs: 0 to 4
Nested Loops
• Used for working with multi-level data.
• Example:
• for i in range(3):
• for j in range(2):
• print(i, j)
Functions in Python
• A function is a block of code that performs a
task.
• Example:
• def greet():
• print("Hello!")
Function Calling
• After defining a function, you call it using its
name.
• Example:
• def greet():
• print("Hello")
• greet()
Function Arguments
• Functions can accept values (arguments).
• Example:
• def greet(name):
• print("Hello", name)
• greet("Janani")
Recursive Function
• A function that calls itself.
• Example:
• def factorial(n):
• if n == 0:
• return 1
• return n * factorial(n-1)
Multiple Return Values
• A function can return more than one value.
• Example:
• def calc(a, b):
• return a+b, a*b