0% found this document useful (0 votes)
57 views

Lab - 10 - User Defined Built Functions - Modules

This document provides instructions for a lab assignment on functions in Python. The lab contains 5 tasks involving writing functions to create a calculator, convert temperatures, calculate distance between points, create a module with shape area calculations, and write a function to output number representations in different bases. Students are asked to write code for each task, include snapshots of their work, and submit a single Word document with their solutions and snapshots by the provided deadline. The lab is worth a maximum of 10 marks and will be graded based on criteria for each task.

Uploaded by

Waseem Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Lab - 10 - User Defined Built Functions - Modules

This document provides instructions for a lab assignment on functions in Python. The lab contains 5 tasks involving writing functions to create a calculator, convert temperatures, calculate distance between points, create a module with shape area calculations, and write a function to output number representations in different bases. Students are asked to write code for each task, include snapshots of their work, and submit a single Word document with their solutions and snapshots by the provided deadline. The lab is worth a maximum of 10 marks and will be graded based on criteria for each task.

Uploaded by

Waseem Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of Computing

CS114: Fundamentals of Programming


Class: BESE 10 AB

Lab 10: Functions In Python


CLO4: Adopt the latest IDEs and other supplementary tools to aid code
implementation and management

Instructor: Ms. Hania Aslam

Date: November 29th , 2019


Time: 9:00am -12:00pm and 02:00pm -05:00pm

CS114: Fundamentals of Programming Page 1


Lab 10 : User Defined & Built-in Functions

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.

>>> def quadratic(a, b, c, x):


first = a * x ** 2
second = b * x
third = c
return first + second + third

>>> quadratic(2, 3, 4, 0.5)


6.0
>>> quadratic(2, 3, 4, 1.5)
13.0

Note: Write programs in the form of functions and use function calls to get the desired results.

CS114: Fundamentals of Programming Page 2


Lab Tasks:

Perform the following tasks:

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:

CS114: Fundamentals of Programming Page 3


Using Python create a module that contains area calculation functions of different shapes
(triangle, circle and rectangle). You need to utilize this module in your main program that
prompts user to enter the shape type and depending on user input it should return the accurate
area calculation results.
Next, you need to create functions within your main program that calculate and display the
volume and surface area of a cylinder.
Note: Avoid duplication to gain maximum marks. Make use of functions written inside module
where ever possible to ensure reusability and modularity.

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]

CS114: Fundamentals of Programming Page 4


Deliverables
Compile a single Word document by filling in the solution/answer part (as directed) along with
the snapshots. Name your submission file as given below and submit this Word file on LMS
before the deadline.

Name – Registration No. – Section

Grade Criteria
This lab is graded. Min marks: 0. Max marks: 10.

Activity Minimum Maximum


Documentation with clearly defined Fail Pass
understanding of the lab task and approach
Task 1 0 2
Task 2 0 1.5
Task 3 0 2
Task 4 0 3.5
Task 5 0 1

CS114: Fundamentals of Programming Page 5

You might also like