Python Programming – Structure Of A Python Program

You can learn about Functions in Python Programs with Outputs helped you to understand the language better.

Python Programming – Structure Of A Python Program

In Python, all function definitions are given at the top followed by statements also called a top-level statements. Internally Python gives a special name to top-level statements as _main_. A python begins the execution of a program from top-level statements, i.e., from _main_. def statements are also read but ignored until called. The top-level statements are not indented at all. In the following example lines 5, 6, 7, and 8 are called top-level statements. Let’s study the program given in Example 3 to see how statements are executed one at a time.

(a) The first statement of a function can be an optional statement – the documentation string of the function or docstring. Python starts execution from the first statement, i.e., line 1. Since it is a comment line so ignored by the browser.

(b) the def statement is also read but ignored until called. So, in line 2, Python just executes the function header line to determine that it is the proper function header and the entire function body, i.e., line 3 and line 4 are not executed by Python.

(c) So actual program execution begins with the first statement of _main_ segment, i.e., line 5 and it will print testing…on the console.

(d) Next line 6 will be executed which is again a print statement so, it will print passing the value 2 on the console.

(e) Statement 7, i.e., z = f(2) is a function call statement. Whenever a function call statement is encountered, the function call will send the control flow to the function definition, i.e., line 2, and the value from _main_ i.e 2 is passed to it. Now the function f(x) receives the value 2 in variable x.

(f) Now the statements (line 3 and line 4) in the function body are executed and with the return statement or the last statement of the function body, the control returns to the statement from where the function was called. Since the last statement of the function body is returned statement returning a value of y, i.e., 4, which will be given back to _main_. Now the value returned will be stored in the variable z.

(g) Now the statement in line 8 which is the print statement will be executed and it will display the result: the function returns 4

Example
Program for exponentiation (power) calculation.

# Program for exponentiation (power) calculation                                                  # line 1
def f(x):                                                                                                                     # line 2
y = x**x                                                                                                                    # line 3
return y                                                                                                                    # line 4
print(“testing…”)                                                                                                       # line 5
print(“passing the value 2”)                                                                                      # line 6
z = f(2)                                                                                                                      # line 7
print(“the function returns”, z)                                                                                  # line 8RUN
>>>
testing . . .
passing the value 2
the function returns 4
>>>

Let’s consider another example. Assume you want to calculate powers, just like the built-in function pow, or the operator **. You can define the (integer) power of a number in several different ways, but let’s start with a simple one: power(x,n) (x to the power of n) is the number x multiplied by itself n-1 times (so that x is used as a factor n times). So power(2,3) is 2 multiplied with itself twice, or 2 X 2 X 2 = 8.

Example
program of power calculation.

def power(x, n):
result = 1
for i in range(n):
result *= x
return result
a = int(input(“Enter number “))
b = int(input(“Raise to power “))
pw = power(a, b)
print(a, “raise to power”, b, “is”, pw)RUN
>>>
Enter number 2
Raise to power 3
2 raised to power 3 is 8
>>>
>>>
Enter number 3
Raise to power 4
3 raised to power 4 is 81
>>>

 

Leave a Reply

Your email address will not be published. Required fields are marked *