-
Notifications
You must be signed in to change notification settings - Fork 5
/
functions.py
97 lines (69 loc) · 1.92 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#Simple function with doc string
def greet_user():
"""Greet me"""
print("Hello!")
greet_user()
#Simple function with parameter
def greet_user(name):
print(f"Hello, {name}!")
greet_user('Adam')
#Keyword arguments
greet_user(name='Ben')
#Simple function with two parameters, one with a default value
def greet_user(name, user_type='admin'):
print(f"Hello, {name}! You are an {user_type}.")
greet_user('Sam')
#Returning a value
def format_name(name):
return name.title()
name = format_name('Sam the man')
print(name)
#The contents of lists and dictionaries can be modfied via the reference passed
def modify_person(person):
person['last-name'] = 'Gates'
person = {'first-name': 'Bill', 'last-name': 'Watterson'}
print(person)
modify_person(person)
print(person)
#Arbitary number of arguments. Arguments are stored in a tuple
def print_arguments(*arguments):
print(arguments)
print_arguments('1', '2', '3')
#Arbitary keyword arguments can be assembled into a dictionary
def build_person(**attributes):
print(attributes)
build_person(first_name='Adam', last_name='Long')
import modules
#Functions can be used from modules
modules.make_pizza(16, 'cheese', 'pepperoni')
from modules import make_pizza
#They can be referenced via their namespace or directly if specifically imported.
make_pizza(16, 'cheese', 'sausage')
import modules as m
from modules import make_pizza as mp
#Modules and functions from modules can be assigned an alias
m.make_pizza(16, 'cheese', 'meatballs')
mp(16, 'cheese', 'olives')
from modules import *
#All functions can be imported from a module into the script's namespace
make_pizza(16, 'cheese', 'sausage', 'pepperoni')
#Call Stack Example
def a():
print('a() starts')
b()
d()
print('a() returns')
def b():
print('b() starts')
c()
print('b() returns')
def c():
print('c() starts')
print('c() returns')
def d():
print('d() starts')
print('d() returns')
a()
#End program early
import sys
sys.exit()