Screenshot 2024-04-15 at 2.20.06 PM
Screenshot 2024-04-15 at 2.20.06 PM
1. Introduction: When one starts writing big programs, it is preferable to avoid accumulating
large quantities of lines of code in a single file. An effective approach is to decompose the
program into subprograms with a well-defined and more elementary task. Furthermore, if the
same block of instructions appears multiple times in a program, it is better to define it once
and for all and avoid systematically repeating it. One solution in the Python language is to
define functions.
A.
Application 1: Run the code cell below to perform the creation of different functions:
>>> def Hello(): >>> def square(x):
print("Hello!") return x*x
def square_and_cube(number):
square = number ** 2
cube = number ** 3
return square, cube
# function use
result_square, result_cube = square_and_cube(5)
print("Square:", result_square)
print("Cube:", result_cube)
1
FERHAT ABBAS Sétif-1 University 1st year Engineering
Faculty of Technology Module: Computing 2
Department of Basic Education in Technology Year: 2023/2024
Remarks:
1. The definition of a function starts with the keyword "def" and continues with the
name of the function. You should avoid reserved words (such as if, while...) and
special or accented characters.
2. the execution of a function's code only occurs when you explicitly call (invoke) the
function
3. The use of a function involves two steps:
a) Function Definition:
b) Function Call:
Application 2: Run the code cell below then try to comment every statement:
import math
def area(r):
return math.pi * sqr(r)
2
FERHAT ABBAS Sétif-1 University 1st year Engineering
Faculty of Technology Module: Computing 2
Department of Basic Education in Technology Year: 2023/2024
Exercise 1 : Define a function named power that takes two parameters (base and exponent) and
returns the result of raising the base to the power of the exponent
In Python, variables can have different scopes, and the two main types of variable scopes are global
and local.
a) Global Variables: Variables that are created outside of a function (as in all of
the examples above) are known as global variables. Global variables can be
used by everyone, both inside of functions and outside.
print("Python is " + x)
# 3) If you use the global keyword, the # 4) To change the value of a global variable
variable belongs to the global scope: inside a function, refer to the variable by using
the global keyword:
def myfunc():
global x x = "awesome"
x = "fantastic" def myfunc():
global x
myfunc() x = "fantastic"
print("Python is " + x)
3
FERHAT ABBAS Sétif-1 University 1st year Engineering
Faculty of Technology Module: Computing 2
Department of Basic Education in Technology Year: 2023/2024
b) Local Variables: Local variables are declared inside a function and have a local scope.
result = calculate_sum(3, 4)
print(local_variable) # error
print(result) # Output: 7
4. External functions: In Python, external functions typically refer to functions that are
defined outside the current script or module and are accessed through imports. There are
several ways to use external functions in Python. Remember to ensure that the external files or
libraries are in the same directory.
a) Importing Modules:
1 Define the functions in a separate Python file (module).
2 Use the import statement in your script to bring in the module.
3 Access the functions using the module name as a prefix.
4
FERHAT ABBAS Sétif-1 University 1st year Engineering
Faculty of Technology Module: Computing 2
Department of Basic Education in Technology Year: 2023/2024
Application 5: Write and save the following code in a file named “ mymodule.py”
# External module: mymodule.py
def my_function():
print("Hello from my_external_function!")
Application 6: Write the following code in new file then Run it.
# Main script
import mymodule
mymodule.my_function()
my_function()
result = math.sqrt(25)
print(result)
5
FERHAT ABBAS Sétif-1 University 1st year Engineering
Faculty of Technology Module: Computing 2
Department of Basic Education in Technology Year: 2023/2024
Write a function called factorial that takes a non-negative integer as a parameter and returns its
factorial.
Write a function called calculator that takes two numbers and an operator (+, -, *, /) as
parameters and performs the corresponding operation. The function should return the result.
Write a function called is_prime that takes a positive integer as a parameter and returns True
if it is a prime number and False otherwise.
Exercise 5:
Write a Python script that accomplishes the following tasks:
1. : This function reads real numbers from the keyboard and stores them in a
list,
2. : This function calculates the average ̅ of the numbers in the list using
the formula:.
̅ ∑
∑ ̅
6
FERHAT ABBAS Sétif-1 University 1st year Engineering
Faculty of Technology Module: Computing 2
Department of Basic Education in Technology Year: 2023/2024
Exercise 6 :
Write a Python program that displays all narcissistic numbers less than .
Example:
Decompose(a) : This function decomposes the number a into its digits (using the string type to
facilitate the task of decomposition) and stores the result in a list object.
Is_narcissistic(a) : This function checks if a number is narcissistic.
Narcissistics(a, b) : which displays all narcissistic numbers in the interval [a, b].
Homework
Write a Python script that propose the following menu to the user :
Reminder :