REMEMBER PYTHON

MINTE9
2REMEMBER




Functions

p45 A function is a named sequence of statements. The expression in parantheses is called the argument. To use a function from a module, you have to import it.
 
>>> int('31')
31
>>> int(3.99999)
3
... 4 lines
˄˄˄
 
>>> import math

>>> math.pi
3.141592653589793

>>> math.sin(math.pi/2)
1.0
... 5 lines
˄˄˄

Function Definiton

p49 ! A function definition specifies the name and the statements. To end the function, you have to enter an empty line. After the function is called, the local variable is destroyed.
 RunCode
# /var/www/python/test.py

def myfunc(a):
    return a%2

# statements inside a function don't run ...
# until the function is called

print(myfunc(3)) # 1


# python test.py
... 7 lines
˄˄˄
 RunCode
def myfunc(a, b):
    c = a + b
    return c

print(myfunc(3, 4)) # 7

print(c) # NameError: name 'c' is not defined
... 5 lines
˄˄˄

Questions    
No. 4   Turtle
CARD
03
32

        A B C D E F
🔔
1/2