Professional Documents
Culture Documents
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:
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:
Solution: