Вы находитесь на странице: 1из 23

INTRODUCTION TO COMPUTER

PROGRAMMING USING PYTHON

FUNCTIONS

Prepared by: Dr. Hamed Benisi


Functions Python supports expressions with math-like
functions
& A function in an expression is a function call

Modules Will explain the meaning of this later


Function expressions have the form FUN(x,y,...)

Function Argument
Name

Function calls Examples (math functions that work in Python):


round(2.34) Arguments can be
any expression
max(a+3,24)
Functions You have seen many functions already

& Type casting functions: int (), float (), bool


()
Modules Get type of a value: type ()
Exit function: exit()
Arguments go in (), but name() refers to
function in general.

Always- Python contains few built-in functions.

available Built- Many more functions are available through

in Functions
built-in modules
Functions Libraries of functions and variables.
To access a module, use the import command:
& import <module name>

Modules
Can then access functions like this:
<module name>.<function name>(<arguments>)

Example:
>>> import math
Modules >>> math.cos (2.0)
-0.4161468365471424
With Import Without Import
Functions >>> import math >>> math.cos(2.0)
& >>> math.cos(2.0)
-
Traceback (most
recent call last):
Modules 0.4161468365471424 File "<stdin>", line 1,
in <module>
NameError: name
'math' is not defined

Modules can have variables, too


Can access them like this:
Necessity of <module name>.<variable name>
import Example: >>> import math
>>> math.pi
3.141592653589793
After importing a module, can see what
Functions functions and variables are available with:
help(<module name>)
&
Modules

module help
Functions io

&
Read/write from files
random
Modules Generate random numbers
Can pick any distribution
string
Useful string functions
sys
Other Useful Information about your OS
Modules
Functions Write in a text editor
We use Komodo Edit
& but any editor will work
Modules

Making your
Own Module
Module Text

Functions # module.py

&
"""This is a simple module.
It shows how modules work""

Modules
x = 1+2
x = 3*x

Docstring (note the Triple Quotes)


Acts as a multiple-line comment

module.py
Useful for code documentation
Functions You can also import like this:
& from <module> import <function name>
Modules
Example:
>>> from math import pi
>>> pi
from command 3.141592653589793

Note that you dont need


the module name now
Functions You can also import everything from
& a module:
Modules from <module> import *

Example:
>>> from math import *
>>> pi
from command 3.141592653589793
>>> cos(pi)
-1.0 Module functions now behave
like built-in functions
Functions
&
Modules

Anatomy of a
Function
Definition
Functions
&
Modules

Anatomy of a
Function
Definition
Functions Passes a value from the function to the
& caller
Modules Format: return <expression>

Any statements after return are ignored

The return Optional (if absent, no value will be sent


back)
Statement
Functions Passes a value from the function to the
& caller
Modules Format: return <expression>

Any statements after return are ignored

The return Optional (if absent, no value will be sent


back)
Statement
Functions We will draw pictures to show what is in memory
&
Modules Function Frame: Representation of function call

Understanding
How Functions
Work
Functions Example: to_centigrade

& >>> from temperature import *


>>> to_centigrade (50.0)
Modules
PHASE 1: Set up call frame
1. Draw a frame for the call
2. Assign the argument value
Understanding to the parameter (in frame)
3. Indicate next line to execute
How Functions
Work
Generating Random number are useful in a lot of
programming tasks
Random random module: includes library functions
for working with random numbers
Numbers Dot notation: notation for calling a function
belonging to a module
Format: module_name.function_name()
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
Generating randrange function: similar to range
function, but returns randomly selected
Random integer from the resulting sequence
Randrange (beg, end, step) : This
Numbers function is also used to generate random
number but within a range specified in
its arguments. This function takes 3
arguments, beginning number (included
in generation), last number (excluded in
generation) and step ( to skip numbers in
range while selecting).
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
Generating Random number created by functions in
random module are actually pseudo-random
Random numbers
Seed value: initializes the formula that
Numbers generates random numbers
Need to use different seeds in order to
get different series of random numbers
By default uses system time for seed
Can use random.seed () function to
specify desired seed value

Random
Number Seeds
Generating Random number created by functions in
random module are actually pseudo-random
Random numbers
Seed value: initializes the formula that
Numbers generates random numbers
Need to use different seeds in order to
get different series of random numbers
By default uses system time for seed
Can use random.seed () function to
specify desired seed value

Random This function maps a particular random


number with the seed argument
Number Seeds mentioned. All random numbers called
after the seeded value returns the
mapped number.
Generating Seed value:

Random
Numbers

Random
Number Seeds

Вам также может понравиться