minte9
LearnRemember



Definition

A function is a named sequence of statements.
 
""" Function definition
A function is a named sequence of statements
To end the function, you have to enter an empty line
Split a single instruction on multple lines with \
"""

def myfunc(a): 
    return a%2

def myprint(x):
    return

print(myfunc(3)) # 1
print(myprint("2")) # None

print("Hello " + \
    "World") # Hello World

Import

To use a function from a module, you have to import it.
 
""" Import modules
To use a function from a module, you have to import it
When you save your scripts don't use 
Python modules names like math, sys, csv
"""

import math

print(math.pi) # 3.141592653589793
print(math.sin(math.pi/2)) # 1.0

Local variable

After the function is called, the local variable is destroyed.
 
""" Local, global variables
Local variable is destroyed after the function is called
To modify a global variable from within a function, use global statement
"""

def myfunc(a, b):
    c = a + b # Look Here
    return c

n = 0
def parse():
    global n # Look Here
    for i in range(10):
        n = i

print(myfunc(3,4))  # 7
parse(); print(n)   # 9

try:
    print(c)
except Exception as e:
    print(e) # name c not defined



  Last update: 211 days ago