Control Flow
Control Flow
In this case, "Large" will be printed because y is greater than 5000. If y were between 1001
and 5000, "Medium" would print, and if y were 1000 or less, "Small" would print.
for
Loop
The for statement in Python iterates over items in a sequence, such as a
list, string, or dictionary, in the order they appear. Unlike for loops in
languages like C, Python's for loop does not require initialization,
incrementing, or stopping criteria—it simply goes through each item in the
sequence.
Example 1: Looping Over Characters in a String
• # Define a string H
e
• hello_string = "Hello World" l
l
o
• # Loop through each character W
o
• for char in hello_string: r
• print(char) l
d
Example 2: Looping Over Items in a
List
• # Define a list of fruits
• fruits = ["apple", "orange", "banana", "mango"]
OUTPUT
0
# Initialize a variable 2
i=0 4
6
# Loop while i is less than or equal to 100 ...
while i <= 100: 96
print(i) 98
i += 2 # Increment by 2 to get the next even number 100
range
Function
The range function in Python generates a sequence of
numbers in arithmetic progression. It is commonly used in
loops to generate numbers in a specific range.
range Function Examples
Example 1: Generate a List of Numbers from 0 to 9
python
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Break /
continue
The break and continue statements in Python allow for control over loop
execution:
•break: Exits the for or while loop entirely when a specified condition is
met.
•continue: Skips the current iteration and moves to the next iteration in
the loop.
break Statement
Example
This example demonstrates using break to stop the
loop if y equals 512.
print(y)
continue Statement
Example
This example demonstrates using continue to skip over an item in the list
("banana") and continue with the next iteration.
# Define a list of fruits
fruits = ["apple", "orange", "banana", "mango"]
A function in Python is a block of reusable code that takes information as input (in
the form of parameters), performs a computation, and returns a result based on the
input information. Functions in Python begin with the def keyword, followed by the
function name and parentheses, which enclose any parameters. The code block within a
function starts after a colon and may include an optional documentation string (docstring)
to describe the function.
Example of a Function in Python
• # Dictionary of students with grades
• students = {
• "1": {"name": "Bob", "grade": 2.5},
• "2": {"name": "Mary", "grade": 3.5},
• "3": {"name": "David", "grade": 4.2},
• "4": {"name": "John", "grade": 4.1},
• "5": {"name": "Alice", "grade": 3.8}
•}
• # Function to calculate average grade
• def averageGrade(students):
• """This function computes the average grade."""
• total = 0
• for key in students:
• total += students[key]["grade"]
• average = total / len(students)
• return average
# Calling the function with different numbers of arguments Student Name: Bob
student("Nav") Age: 30
student("Amy", "Age: 14") Major: CS
student("Bob", "Age: 30", "Major: CS")
Modules in Python
A module in Python is a file that contains Python definitions and statements.
These definitions and statements can include functions, classes, and variables
that can be used across different Python scripts. Organizing code into modules
enhances readability and reusability, and it is also easier to maintain large
projects.
To use a module in another Python script, you can import it using the import
keyword. Python comes with many built-in modules, but you can also create your
own.
Importing the Student Module