You are on page 1of 6

Department of Software Engineering

CS-114: Fundamentals of Programming


Class: BE(SE)-11A

Lab 09: Functions and Collections

Date: December 15, 2020

Time: 10:00 am -12:50 pm

Instructor: Dr. Arham Muslim

Lab Engineer: Ms. Sara Mehmood


Lab 08: Functions

Objectives
In this lab, the students will learn and practice to develop an understanding of python
functions.

Tools/Software Requirement
Python IDLE

Description
Follow the lab manual step by step. As you proceed, you will be asked to add screenshots/snaps
of your results inside the provided output windows within this manual for grading purposes.

Functions

A function is a block of organized & reusable code that performs a specific task. Using functions
we can write modular programs that have minimal code duplication.

 Function blocks begin with the keyword def followed by the function name and
parentheses ()
 Any input parameters/arguments should be placed within these parentheses
 The code block within every function starts with a colon (:) and is indented
 The return [expression] statement can be used to exit a function. Optionally, you can
pass back an expression to the caller. A return statement with no arguments is the same
as a return None. A function without any return statement will execute all its
statements. See an example below for the usage of the return statement

Collections - List

Lists are used to store multiple ordered items in a single variable. Python has several compound
data types, used to group together other values. The most versatile is the list, which can be
written as a list of comma-separated values (items) between square brackets. Lists might
contain items of different types, but usually, the items all have the same type. For example:

In Python, lists are mutable, meaning that the items in a list can be changed, added, or
removed. Available methods to change a list include:
Lab Tasks:
Perform the following tasks using only the programming techniques you have learned so far.

Note: All the tasks of this lab should be performed in Python scripted mode only. You should
use sensible and self-explanatory names for the variables you are using. Add appropriate
comments to explain your code.

1. Write an interactive calculator that allows the user to type in mathematical expressions.
The program should evaluate the entered expression and print the results. The program
should allow the user to keep on performing the calculation unless one of the following
two conditions satisfies:
a. The program crashes due to an incorrect expression
b. The user enters Quit as a sentinel value. The sentinel value is Quit, not “Quit”

Solution:

Insert code here

Insert screen shot for output here


2. Create a module that contains area calculation functions of three shapes (triangle, circle,
and rectangle). Utilize this module in your main program that continuously prompts the
user to enter a shape type and depending on the user input it should return the
accurate area calculation results

Next, you need to create a function within your main program that calculates the
surface area of a cylinder. The cylinder should also be one of the shapes the user can
input to get the area.

Solution:

Insert code here

Insert screen shot for output here


3. Write a modular program that keeps on getting numbers from the user until he/she
enters a value “X”. All the received numbers should be stored in a list. After that menu
should be shown to the user from where the user can request the following information
to be produced based on a selection:
a. Enter the list of numbers again
b. Maximum and minimum numbers in the list (should not use max() and min())
c. Sum of the squares of each number in the list (anonymous functions should be
used)
d. Sort the list in ascending order (should not use sort())
e. Exit the program

Solution:

Insert code here

Insert screen shot for output here

You might also like