Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Addis abeba institute of technology(AAIT)

Computer programing individual assignment


section 11

Name:Meron Birhanu
ID: UGR\2314\15

instructor: daniel abebe


submission date: july 21 2023

Python program flow of execution


The flow of execution basically refers to the order in which statements are executed. Execution always
starts at the first statement of the program. Moreover, statements execute one at a time. It happens in
order, from top to bottom.
• Further, functions definitions do not alter the flow of execution of the program. However, it
remembers the statements inside the function do not execute until the functions are called.
• Moreover, function calls are like a bypass in the flow of execution. Thus, instead of going to the
next statement, the flow will jump to the first line of the called function. Then, it will execute all
the statements there. After that, it will come back to pick up where it left off.
• It is essential to remember that when reading a program, do not read it from top to bottom.
Instead, keep following the flow of execution. This will ensure that one reads the def statements
as they are scanning from top to bottom.
• However, one must skip the statements of the function definition until they reach a point where
that function is called.
The flow of execution in python program refers to the order in which the statements and expressions
and executed. The program starts from the first line and proceeds sequentially, unless there are control
flow statement that changes the normal order of execution.
Here is the general flow of execution in a python program:
• Importing modules: the program may start by importing modules and libraries that are needed
for the program. This is usually done at the beginning of the program.
• Variable and object initialization: next, any variable or object that will be used in the program are
installed. This includes assigning values to variables or creating instances of objects.
• Execution of statements and expressions: the program then executes statements and
expressions in the order they appear in the code. These statements and expressions an include
variable assignments, functions calls, conditional statements (is-else statements), loops (for or
while loops), and other control flow statements
• Function and method calls: if there are any function or method calls in the code, the program
will jump to the corresponding function or method and execute the statements and expressions
inside it. Once the function or method finishes executing the program returns to the point where
the function or method was calles
• Exception handling: if an exception/error occurs during the execution of the program, it can be
caught and handled using try-except blocks. If an exception is not caught and handled, the
program will terminate with an error message.
• Termination or continuation: finally, the program will either terminate after executing all the
statements and reaching the end of the code, or it may repeat certain parts of the code if there
are loops or it may be terminated prematurely using control flow of execution
Here is an example code snippet to demonstrate the flow of execution:
# importing Modules
import math
# variable initialization
radius = 5
area = 0
# calculate area of the circle
area = math.pi * radius**2
#print the area
print(“the area of the circle is”,area)
#function definition
def calculate_area(radius):
return math.pi * radius **2
# function call
Circle_area = calculate_area(7)
Print(“the area of the circle is:”,circle_area
In this example, the program starts from importing the ‘math’ module. It then initializes the ‘radius’ and
‘area’ variables. Next it calculates the area of the circle using the initialized ‘radius’ variables and assigns
it to the area variable. The program prints the calculated area.
The code also includes a function definition for ‘calculate_area()’ that calculates the area of a circle given
a radius. The program calls this function with a radius value of 7 and assigns the return value to the
‘circle_area’ variable. Finally, it prints out value of ‘circle_area’
Some types of python program flow of execution
• Sequential execution:
This is default flow where statements are executed one after another in the order they appear. Here's an
example:
Print(“Hello”)
Print(“World”)
Output
Hello
world
2. conditional execution:
In the flow, are executed only if a certain condition is true.
Here's an example
x = 10
If x > 0
Print(“positive number”)
Else
Print(“negative number”)
Output
Positive number
3. loop execution
Statements are repeated until a certain condition is met.
Examples: for i in range(5)
Print(i)

4. Nested Conditional Execution:


Here, conditional statements are nested within each other to create complex conditions. Here's an
example:

```python
x = 10
if x > 0:
if x % 2 == 0:
print(" positive number")
else:
print("Odd positive number")
else:
print("Negative number")
output
Even positive number
5.loop with statements within a loop but can be terminated prematurely if a certain condition
is met.
Here is an example:
i=1
while i <= 5:
if i == 3:
break
print(i)
i += 1
Output
1
2
6. loop with continue
Like the flow, but instead of terminating the loop, it continues to the next iteration. Here is an
example
i=1
while i <= 5:
if i == 3:
i += 1
continue
print(i)
i += 1
Output
1
2
4
5
7. Function call
Flow jumps to a function when a function is called and returns to the calling point after the function
finishes executing. Here is an example:
def greet(name):
print(f"Hello, {name}!")

greet("Alice`

Output:
Hello, Allice!

8. Exception Handling:
Statements within a `try` block are executed normally, but if an error occurs, the flow jumps to the
`except` block. Here's an example:

```python
try:
x = 10 / 0
except ZeroDivisionError:
print("Error: Division by zero")
Output:
Error: Division by zero
9. Recursive execution:
A function calls itself to repeat a certain task until a base condition is met. Here's an example of
calculating factorial using recursion:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

result = factorial(5)
print(result)
Output
120
10. Parallel execution:
In python, parallel execution can be achieved using threads or processes, allowing multiple tasks to run
simultaneously. Here's an example using concurrent. Futures module:
import concurrent.futures

def square(x):
return x ** 2

with concurrent.futures.ThreadPoolExecutor() as executor:


results = executor.map(square, [1, 2, 3 4, 5])

for result in results:


print(result)
Output
1
4
9
16
25
To control the flow of execution of a program there are three categories of statements in
python. They are:
1. Selection statements
2. iteration statements
3. Jump statements
• Selection Statements
Decision making is valuable when something we want to do depends on some user input or some other
value that is not known when we write our program. This is quite often the case and Python, along with
all interesting programming languages, can compare values and then take one action or another
depending on that outcome.
• Iteration Statements
Iteration refers to repeating the same thing repeatedly. In the case of string sequences, you can write
code that will be executed for each character in the string. The same code is executed for each character
in a string. However, the result of executing the code might depend on the current character in the
string. To iterate over each element of a sequence you may write a for loop.
• Jump Statements
In Python:
• Pass: Does nothing at all: it‘s an empty statement placeholder
• Break: Jumps out of the closest enclosing loop (past the entire loop statement)
• Continue: Jumps to the top of the closest enclosing loop (to the loop ‘s header line)

You might also like