-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctiondemo.py
More file actions
54 lines (39 loc) · 1.25 KB
/
functiondemo.py
File metadata and controls
54 lines (39 loc) · 1.25 KB
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
def functiononly():
print("hello world")
functiononly()
def add_number(a, b):
""" Add two variable together
Keyword arguments:
a - first value
b - second value
"""
return a + b
result = add_number(5, 6)
print(result)
print(add_number("abc", "def"))
# parameter with default value make it optional when calling
def greet(name, message="Hello"):
print(f"{message}, {name}")
# position argument
greet("Tom", "Have had your dinner?")
greet("Jerry")
# keyword argument
greet(message="I am hungry", name="Landy")
# Arbitrary Argument
def make_pizza(*toppings):
print("making a pizza with the following toppings:")
for topping in toppings:
print(topping+" ", end="")
make_pizza("pepperoni", "mushrooms", "green peppers", "cheese")
print()
# Arbitrary Keyword Argument
def build_profile(first, last, **user_info):
profile = {'first_name': first, 'last_name': last}
profile.update(user_info)
return profile
user_profile = build_profile("Michael", "Chen", location='Brisbane', role='Student', age=20)
print(user_profile)
# return data to caller
print(build_profile("Michael", "Chen", location='Brisbane', role='Student', age=20)["last_name"])
# print docstring for add_number function
print(add_number.__doc__)