Lab 03
Lab 03
1. Python Indentation
Indentation refers to the spaces at the beginning of a code line. The indentation in Python is very
important, and it indicate a block of code.
- Example:
- You have to use the same number of spaces in the same block of code, otherwise Python will
give you an error:
3. Python Functions
- Definitions:
o A function is a block of code which only runs when it is called.
o A function is a group of related statements that performs a specific task.
- Adavantages:
o Functions help break our program into smaller and modular chunks. As our program
grows larger and larger, functions make it more organized and manageable.
o It avoids repetition and makes the code reusable.
- To call a function, use the function name followed by parenthesis, and then data can be passed
inside the parentheses.
print(“sum of a and b:”)
range(1, 10, 3)
def myPrint():
print("abcdxyz")
print("12345")
myPrint()
- Positional arguments:
By default, a function must be called with the correct number of arguments. Meaning that if
your function expects 2 arguments, you have to call the function with 2 arguments, not more,
and not less.
The above arguments fname and lname are called positional arguments. Positional arguments
must be included in the correct order.
- See more “parameters vs arguments” in:
https://www.w3schools.com/python/gloss_python_function_arguments.asp
my_sum(1, 15)
- Keyword Arguments:
You can also send arguments with the key = value syntax. This way the order of the arguments
does not matter.
- Default arguments:
A default argument is an argument that assumes a default value if a value is not provided in the
function call for that argument.
- Practice examples:
Define a Python function for each of the following functions:
( ) √
( )
import math
def f1_sqrt(x):
return math.sqrt(x)
def f2_fraction(x):
res = (x + 1) / (x - 1)
return res
#main
print( f1_sqrt(4) )
print( f2_fraction(2) )
Exercise 1
Define a Python function for each of the following functions:
Exercise 2
Define a Python function for each of the following functions, and then find the range (minimum
and maximum values) of the functions:
( ) ( ) { [ ]
Hint:
import math
import numpy as np
def fx_2e(x):
if x >= 0:
return x
else:
return -x
def cau2e():
for x in np.arange( -3, 3.1, 0.1 ):
#find minimum and maximum values of fx_2e
print( round (fx_2e(x), 5) )
# ...
#main
cau2e()
While normal functions are defined using the def keyword in Python, anonymous functions are
defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.
Lambda functions can have any number of arguments but only one expression. The expression
is evaluated and returned.
f1_double = lambda x: x * 2
print( f1_double(5) )
In the above program, x is the argument and x * 2 is the expression that gets evaluated and
returned. This function returns a function object which is assigned to the identifier f1_double.
The statement
f1_double = lambda x: x * 2
def f1_double(x):
return x * 2
- Other examples:
f2 = lambda a, b: (a + b) / (a - b)
print( f2(5, 1) )
f = lambda x: x**2
x = np.arange(-3, 3.1, 1)
y = list( map(f, x) )
print(x)
print(y)
- https://www.programiz.com/python-programming/anonymous-function
Exercise 3
Write a computer program to compute the composites of function. Meanwhile,
.
5. Python Matplotlib
Matplotlib is a low level graph plotting library in python that serves as a visualization utility.
5.1 Installation of Matplotlib
If you have Python and PIP already installed on a system, then install it using this command:
#main
x_array = np.arange(-10, 10.1, 0.1)
y_array = list( map(fx_4i, x_array) ) #y = f(x)
Exercise 4
Write a Python program to plot each of the following functions and specify the intervals over which
the function is increasing and the intervals where it is decreasing:
Exercise 5
Write a Python program to plot the functions:
def cau6a():
k = np.arange(...)
x = np.arange(...)
for ki in k:
y = []
for xi in x:
y.append(...)
plt.title("Cau 6a")
plt.legend()
plt.show()
#6h: ( ) ( ) ( )
#6i: ( ) ( ) ( )
#6j: ( ) ( ) ( )
Exercise 7
Write a Python program to check whether the function ( ) is one-to-one function or not:
6. References
- Python Tutorial on the W3schools website: https://www.w3schools.com/python/default.asp
- Python Tutorial on the Tutorials Point website:
https://www.tutorialspoint.com/python/index.htm
-- THE END --