0% found this document useful (0 votes)
26 views37 pages

Lectures 21 23

Uploaded by

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

Lectures 21 23

Uploaded by

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

More about

functions and
modules
Topics

• Designing a Program to Use Functions

• List methods and useful built-in functions

• math module

• random module
Defining and Calling a Function
(cont’d.)
• main function: called when the program starts
• Calls other functions when they are needed

• Defines the mainline logic of the program

• Top-down design: technique for breaking algorithm


into functions
Designing a Program to Use
Functions (cont’d.)
• Hierarchy chart: depicts relationship between
functions
• AKA structure chart

• Box for each function in the program, Lines connecting


boxes illustrate the functions called by each function
• Does not show steps taken inside a function
Designing a Program to Use
Functions (cont’d.)
Example
Example
Example

Nested version
List Methods and Useful Built-
in Functions

• We learned how to define lists and perform


some basic operations on them via indexing.
• Examples of functions we used
• len()
• append()
• list()
List Methods and Useful Built-
in Functions
• index(item): used to determine where an item is
located in a list
• Returns the index of the first element in the list containing
item
• Raises ValueError exception if item not in the list
List Methods and Useful Built-
in Functions (cont’d.)
• insert(index, item): used to insert item at
position index in the list
• sort(): used to sort the elements of the list in
ascending order
• remove(item): removes the first occurrence of
item in the list
• reverse(): reverses the order of the elements in
the list
List Methods and Useful Built-
in Functions (cont’d.)
• del statement: removes an element from a specific
index in a list
• General format: del list[i]

• min and max functions: built-in functions that


returns the item that has the lowest or highest value
in a sequence
• The sequence is passed as an argument
Example
Example
Example
Copying Lists

• To make a copy of a list you must copy each element


of the list
• Three methods to do this:
• Creating a new empty list and using a for loop to add a copy of
each element from the original list to the new list
• Creating a new empty list and concatenating the old list to the
new empty list
• Using the copy function
Example
• Wrong way: one list is modified directly but both lists
are affected
Copying Lists (cont’d.)
Example
• Correct way: using copy function
The math Module

• math module: part of standard library that contains


functions that are useful for performing
mathematical calculations
• Typically accept one or more values as arguments,
perform mathematical operation, and return the result
• Use of module requires an import math statement
The math Module (cont’d.)
The math Module (cont’d.)
• The math module defines variables pi and e, which
are assigned the mathematical values for pi and e
• Can be used in equations that require these values, to get
more accurate results
• Variables must also be called using the dot notation
• Example:
circle_area = math.pi * radius**2

More functions can be found here


https://docs.python.org/3/library/
math
.html
Examples
Examples
Examples
Examples
Examples
Generating Random Numbers
• Random number are useful in a lot of programming
tasks
• random module: includes library functions for
working with random numbers
• Dot notation: notation for calling a function
belonging to a module
• Format: module_name.function_name()
Generating Random Numbers
(cont’d.)
• randint function: generates a random number in
the range provided by the arguments
• Returns the random number to part of program that
called the function
• Returned integer can be used anywhere that an integer
would be used
• You can experiment with the function in interactive mode
Generating Random Numbers
(cont’d.)
Generating Random Numbers
(cont’d.)
Example
Generating Random Numbers
(cont’d.)
• randrange function: similar to range function,
but returns randomly selected integer from the
resulting sequence
• Same arguments as for the range function
• random function: returns a random float in the
range of 0.0 and 1.0
• Does not receive arguments
• uniform function: returns a random float but
allows user to specify range
Example
Example
Example
Example

You might also like