Escolar Documentos
Profissional Documentos
Cultura Documentos
def
hello(): Program:
print 'Hello' Output:
def thing():
print 'Fun' print ('Hello’)
print ('Fun’) Hello
hello() Fun
thing()
print ('Zip’) Zip
print “Zip” thing()
Hello
Fun
hello()
We call these reusable pieces of code “functions”.
Python Functions
def max(inp):
blah
“Hello world” blah ‘w’
for x in y: (a string)
(a string) blah
blah
Type Conversions
>>> print (float(99) / 100)
0.99
>>> i = 42
• When you put an integer and
floating point in an expression the >>> type(i)
integer is implicitly converted to a <type 'int'>
float >>> f = float(i)
• You can control this with the built in >>> print (f)
functions int() and float()
42.0
>>> type(f)
<type 'float'>
>>> print (1 + 2 * float(3) / 4 – 5)
-2.5
>>>
String >>> sval = '123'
def print_lyrics():
print ("I'm happy.”)
print ('I m happy and fine.‘)
print ("I'm okay.”)
print_lyrics():
print ('I m happy.‘)
x=5
print ('Hello‘)
def print_lyrics():
print ("I'm okay.”)
Hello
print ('I m happy.‘)
Yo
print ('Yo‘)
7
x=x+2
print (x )
Definitions and Uses
def print_lyrics():
print ("I'm okay.”)
print ('I m happy.‘)
print 'Yo'
print_lyrics()
x=x+2 Hello
print x Yo
I'm okay I’m happy
7
Arguments
• An argument is a value we pass into the function as its input when we call the
function
• We use arguments so we can direct the function to do different kinds of work
when we call it at different times
• We put the arguments in parenthesis after the name of the function
• Often a function will take its arguments, do some computation and return a
value to be used as the value of the function call in the calling expression. The
return keyword is used for this.
def greet():
return "Hello” Hello Glenn
Hello Sally
print greet(), "Glenn”
print greet(), "Sally"
Return Value >>> def greet(lang):
... if lang == 'es':
... return 'Hola’
A “fruitful” function is one that ... elif lang == 'fr':
produces a result (or return ... return 'Bonjour’
value) ... else:
The return statement ends the ... return 'Hello’
function execution and “sends
back” the result of the function ... >>> print greet('en'),'Glenn’
Hello Glenn
>>> print greet('es'),'Sally’
Hola Sally
>>> print greet('fr'),'Michael’
Bonjour Michael
>>>
Arguments, Parameters, and
Results
>>> big = max('Hello world') Parameter
>>> print big'w'
def max(inp):
blah
“Hello world” blah ‘w’
for x in y:
Argument blah
Result
blah
return ‘w’
Formal and Actual Parameters
An identifier is a name used for a class, a variable, a method,
or a parameter.
The following definitions are useful:
formal parameter — the identifier used in a method to stand for the value
that is passed into the method by a caller.
•For example, amount is a formal parameter of processDeposit
actual parameter — the actual value that is passed into the method by
a caller.
•For example, the 200 used when processDeposit is called is an
actual parameter.
•actual parameters are often called arguments
Note: formal parameters are bound to an actual value only as long
as their method is active. When a method returns to its caller, the
formal parameters no longer contain any values. They cannot be
used to store the state of an object.
What is the Difference Between
Actual and Formal Parameters?
Actual Parameters
• Values passed to a function when it is Invoked
• The actual parameters are passed by the calling function
• In actual parameters, there is no mentioning of data types. Only the value is mentioned.
Difference?
Formal Parameters
The Formal Parameters are the variables defined by the function
that receives values when the function is called
The formal parameters are in the called function.
In formal parameters, the data types of the receiving values should
be included.
Parameters….
• Functions
• Built-In Functions
• Type conversion (int, float)
• Math functions (sin, sqrt)
• Try / except (again)
• Arguments
• Parameters