Lab - 10 - User Defined Built Functions - Modules
Lab - 10 - User Defined Built Functions - Modules
Introduction
The purpose of this lab is to get familiar with usage built-in and user defined functions in Python.
Tools/Software Requirement
Python IDLE
Description:
Functions in Python:
A functions is a block of organized & reusable code that performs a specific task. Using functions we
can write modular programs that have minimal code duplication.
Description:
Function blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).
Any input parameters or arguments should be placed within these parentheses. You can
also define parameters inside these parentheses.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function, optionally passing back an expression
to the caller. A return statement with no arguments is the same as return None.
Note: Write programs in the form of functions and use function calls to get the desired results.
Task 1:
Write an interactive Python calculator program. The program should allow the user to type in a
mathematical expression, and then it should evaluate and print the resultant value of the
expression. Include a loop so that the user can perform many calculations one after the other.
Note: To quit early, the user can make the program crash by typing a bad expression (e.g., (2
+ 2) * 5 ! 4) (You’ll learn better ways of terminating interactive programs in later lessons)
or a user can terminate it simply by entering Quit as sentinel value.
Task 1
#Add your Python Script code here. [1.5 Marks]
#Add the snap of tasks execution here. [0.5 Mark]
Task 2:
Write a temperature converter program that uses lambda functions to convert from Fahrenheit to Celsius
and vice versa.
Task 2
#Add your Python Script code here. [1 Marks]
#Add the snap of tasks execution here. [0.5 Mark]
Task 3:
Write a function distance that calculates the distance between two points (x1, y1) and (x2,
y2). All numbers and return values should be of floating type. Use this function in your program.
The values of the points shall be provided by the end user.
Task 3
#Add your Python Script code here. [1.5 Marks]
#Add the snap of tasks execution here. [0.5 Mark]
Task 4:
Task 4
#Add your Python Module code here. [1.5 Marks]
# Add your Main Program code here. [1.5 Marks]
#Add the snap of tasks execution here. [0.5 Mark]
Task 5:
Write a function that accepts an integer value and returns the binary, octal and hexadecimal equivalent
representation to the console.
Note: You may want to have a look at the Python’s built in functions to complete this task.
Task 5
#Add your Python Script code here. [0.5 Mark]
#Add the snap of tasks execution here. [0.5 Mark]
Grade Criteria
This lab is graded. Min marks: 0. Max marks: 10.