-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionsPartTwo.py
85 lines (55 loc) · 2.21 KB
/
functionsPartTwo.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
# Objectives
# using the * and ** operators as parameters to a function and outside of a function
# leverage dictionary and tuple unpacking to create more flexible function
# *args are special operators we can pass to a function as a parameter.
# Gathers remaining arguments as a tuple
# this is just a parameter - you can call it whatever you want
# Example
'''
def sum_all_nums(*args):
total = 0
for num in args:
total += num
return total
sum_all_nums(1, 2, 3) # 6
sum_all_nums(1, 2, 3, 4, 5) # 15
def ensure_correct_info(*args):
if "Peter" in args and "Steyer" in args:
return "Welcome back!"
return "Not sure who you are..."
ensure_correct_info() # not sure who you are...
ensure_correct_info(1, True, "Steyer", "Peter")
# Excercise
def contains_purple(*args):
if "purple" in args:
return True # Dont need else part of the condition, since we are returnbing out of the function on the line before. Soo the only way the return False line runs is if the above line didnt run. It's an implicit else.
return False
'''
# **kwargs keyword args
# A special operator we can pass to functions
# Gathers remaining keyword arguments as a dictionary
# Just a parameter - call it whatever
def fav_colors(**kwargs):
print(kwargs) # prints it as a dictionary
# {'peter': 'Purple', 'ruby': 'red', 'ethel': 'teal'}
fav_colors(peter="Purple", ruby="red", ethel="teal")
def fav_colors(**kwargs):
for person, color in kwargs.items():
print(f"{person}'s favorit color is {color}")
fav_colors(peter="Purple", ruby="red", ethel="teal")
fav_colors(peter="Purple", ruby="red", ethel="teal", ted="blue")
fav_colors(peter="forest deep amazing green")
def special_greeting(**kwargs):
if "David" in kwargs and kwargs["David"] == "special":
return "You get a special greeting David!"
elif "David" in kwargs:
return f"{kwargs['David']} David!"
return "Not sure who this is"
print(special_greeting(David='Hello')) # Hello David!
print(special_greeting(Bob='hello')) # Not sure who this is...
print(special_greeting(David='special')) # you get a special greeting David!
# Parameter Ordering
# 1. Parameters
# 2. *args
# 3. default parameters
# 4. **kwargs