Professional Documents
Culture Documents
Chapter 003
Chapter 003
Chapter 003
Aman. W()
Department of Physics
University of Gondar
Contents
gpa = 2.5
if x == 3:
print X equals 3.
elif x < 3:
print X less than 3.
elif x > 3:
print X greater than 3.
else:
print X equals something else.
Comparison Operators
if value in sequence:
statements An alternative if form
returns a value. This can
The sequence can be simplify your code
a range,
string, Example:
tuple, or list
Examples: >> return x+1 if x < 0 else x
x=3 -1
if x in range(0, 10):
if a > 5: if a > 5:
if b < 10: if b < 10:
print(green) print(green)
else: else:
print(blue) print(blue)
Functions
def times(x,y):
return x*y
def creates a function and assigns it a name
'main' code appears below functions
Statements inside a function must be indented
return sends a result back to the caller
Arguments are passed by assignment
Arguments and return types are not declared
Function
What all can I do within the body of a function?
Define local variables
Call other functions ---- print(a) , print(b)
Define new functions
Functions come up in two ways in programs
Function Definition
Where the function is created
Starts with def in python
Function Call
Where the function is used
result = myFunction(argument1,argument2)
Function
Functions have an important feature
They can receive and return values
Values are passed to a function inside of ( ) when
called and returned using a special instruction---
def myFunction(input_values):
output_value= m* input_values
return output_value
If a function needs to return a value back to the place in
the code where it is called (it uses a return statement)
Function Call
Once the function is defined it can be called as many times as one likes. If
the function has a return it can be stored into a variable at the call
The syntax for a function call is:
>>> def myfun(x, y):
return x * y
>>> myfun(3, 4)
12
>>> a= myfun(4, 5)
20
Arguments are bound at the call site
Call site: a point in the program where the function is called
The binding is active up until the return of the function
Parameters in Python are Call by Assignment
Old values for the variables that are parameter names are
hidden, and these variables are simply made to refer to the
new values
All assignment in Python, including binding function
parameters, uses reference semantics.
All functions in Python have a return value, even if no return
line inside the code
Functions without a return the special value None
None is a special constant in the language
Variables and Functions: Local Variables
y=1
myFun(4)
print(a)
print(y)
Variables and Functions
return b + c
>>> myfun(5,3,hello)
>>> myfun(5,3)
>>> myfun(5)
Keyword Arguments
Can call a function with some/all of its arguments out of order as long as you specify
their names
>>> def foo(x,y,z):
return(2*x,4*y,8*z)
>>> foo(2,3,4)
(4, 12, 32)
>>> foo(z=4, y=2, x=3)
(6, 8, 32)
>>> foo(-2, z=-4, y=-3)
(-4, -12, -32)
return a+b
Consider the following three cases:
One of these cases will throw an error. This behavior is
defined by the code inside the function
res = sumOfTwo(1,2)
res = sumOfTwo(Hello , World)
res = sumOfTwo(Hello, 1)
Function Arguments
There are two ways to handle this difficulty
1.Tell everyone what the function expects
2.Include checks inside the function to ensure the arguments
are what is expected
A combination of both techniques should be used(Function
Documentation)
29
Example: math module
To use many of these commands, you must write the
following at the top of your Python program:
>>> from math import * or
>>> import math
>>> dir(math)
['__doc__', '__name__', 'acos', 'asin',
'atan', 'atan2', 'ceil', 'cos', 'cosh',
'degrees', 'e', 'exp', 'fabs', 'floor',
'fmod', 'frexp', 'hypot', 'ldexp', 'log',
'log10', 'modf', 'pi', 'pow', 'radians',
'sin', 'sinh', 'sqrt', 'tan', 'tanh']
>>> math.pi
3.1415926535897931
>>> math.sin(7)
0.65698659871878906
Using the Math Library