|
| 1 | +# A function is a block of code which only runs when it is called. You can pass data, known |
| 2 | +# as parameters, into a function. A function can return data as a result. |
| 3 | + |
| 4 | +# Define a Python Function |
| 5 | +def hello_world(): |
| 6 | + print('Hello World!') |
| 7 | + |
| 8 | + |
| 9 | +# call a function |
| 10 | +hello_world() |
| 11 | +# Hello World! |
| 12 | + |
| 13 | + |
| 14 | +# Passing a single argument |
| 15 | +def hello_friend(friend): |
| 16 | + print(f'Hello, {friend}!') |
| 17 | + |
| 18 | + |
| 19 | +# call function passing in argument |
| 20 | +hello_friend('Mosely') |
| 21 | +hello_friend('Winnie') |
| 22 | +# Hello, Mosely! |
| 23 | +# Hello, Winnie! |
| 24 | + |
| 25 | + |
| 26 | +# Passing a list as an argument |
| 27 | +def hello_friends(names): |
| 28 | + for name in names: |
| 29 | + message = f'Hello, {name}!' |
| 30 | + print(message) |
| 31 | + |
| 32 | + |
| 33 | +# call function passing in a list |
| 34 | +hello_friends(['Winnie', 'Mosely', 'Bella', 'Mugsy']) |
| 35 | +# Hello, Winnie! |
| 36 | +# Hello, Mosely! |
| 37 | +# Hello, Bella! |
| 38 | +# Hello, Mugsy! |
| 39 | + |
| 40 | + |
| 41 | +# Use a function to modify a list |
| 42 | +def hello_friends(names): |
| 43 | + while names: |
| 44 | + name = names.pop() |
| 45 | + message = f'Hello, {name}!' |
| 46 | + print(message) |
| 47 | + |
| 48 | + |
| 49 | +original = ['Winnie', 'Mosely', 'Bella', 'Mugsy'] |
| 50 | +print(original) |
| 51 | +# ['Winnie', 'Mosely', 'Bella', 'Mugsy'] |
| 52 | + |
| 53 | +hello_friends(original) |
| 54 | +# Hello, Mugsy! |
| 55 | +# Hello, Bella! |
| 56 | +# Hello, Mosely! |
| 57 | +# Hello, Winnie! |
| 58 | + |
| 59 | +print(original) |
| 60 | +# [] |
| 61 | + |
| 62 | + |
| 63 | +# Prevent a function from modifying a list |
| 64 | +def hello_friends(names): |
| 65 | + while names: |
| 66 | + name = names.pop() |
| 67 | + message = f'Hello, {name}!' |
| 68 | + print(message) |
| 69 | + |
| 70 | + |
| 71 | +original = ['Winnie', 'Mosely', 'Bella', 'Mugsy'] |
| 72 | +copy = original[:] |
| 73 | +print(original) |
| 74 | +# ['Winnie', 'Mosely', 'Bella', 'Mugsy'] |
| 75 | + |
| 76 | +hello_friends(copy) |
| 77 | +# Hello, Mugsy! |
| 78 | +# Hello, Bella! |
| 79 | +# Hello, Mosely! |
| 80 | +# Hello, Winnie! |
| 81 | + |
| 82 | +print(original) |
| 83 | +# ['Winnie', 'Mosely', 'Bella', 'Mugsy'] |
| 84 | + |
| 85 | + |
| 86 | +# Using positional arguments |
| 87 | +def describe_car(make, model): |
| 88 | + print(f'The {make} {model} is a neat vehicle') |
| 89 | + |
| 90 | + |
| 91 | +describe_car('Subaru', 'WRX') |
| 92 | +# The Subaru WRX is a neat vehicle |
| 93 | + |
| 94 | +describe_car('Tesla', 'Model 3') |
| 95 | +# The Tesla Model 3 is a neat vehicle |
| 96 | + |
| 97 | +describe_car('Tesla', 'Cybertruck') |
| 98 | +# The Tesla Cybertruck is a neat vehicle |
| 99 | + |
| 100 | + |
| 101 | +# Using keyword arguments |
| 102 | +def describe_car(make, model): |
| 103 | + print(f'The {make} {model} is a neat vehicle') |
| 104 | + |
| 105 | + |
| 106 | +describe_car('Subaru', 'WRX') |
| 107 | +# The Subaru WRX is a neat vehicle |
| 108 | + |
| 109 | +describe_car(make='Tesla', model='Model 3') |
| 110 | +# The Tesla Model 3 is a neat vehicle |
| 111 | + |
| 112 | +describe_car(model='Corvette', make='Chevy') |
| 113 | +# The Chevy Corvette is a neat vehicle |
| 114 | + |
| 115 | + |
| 116 | +# Using a default value |
| 117 | +def describe_car(make, model='WRX'): |
| 118 | + print(f'The {make} {model} is a neat vehicle') |
| 119 | + |
| 120 | + |
| 121 | +describe_car('Subaru') |
| 122 | +# The Subaru WRX is a neat vehicle |
| 123 | + |
| 124 | + |
| 125 | +# Using None to make an argument optional |
| 126 | +def describe_car(make, model=None): |
| 127 | + if model: |
| 128 | + print(f'The {make} {model} is a neat vehicle') |
| 129 | + else: |
| 130 | + print(f'The {make} is a neat vehicle') |
| 131 | + |
| 132 | + |
| 133 | +describe_car('Subaru') |
| 134 | +# The Subaru is a neat vehicle |
| 135 | + |
| 136 | +describe_car(model='Corvette', make='Chevy') |
| 137 | +# The Chevy Corvette is a neat vehicle |
| 138 | + |
| 139 | + |
| 140 | +# Function with an arbitrary number of arguments |
| 141 | +def make_a_sandwich(type, *veggies): |
| 142 | + print(f'\nMaking a {type} Sandwich.') |
| 143 | + print('It has these veggies:') |
| 144 | + for veggie in veggies: |
| 145 | + print(f'- {veggie}') |
| 146 | + |
| 147 | + |
| 148 | +make_a_sandwich('Ham', 'Onions') |
| 149 | +# Making a Ham Sandwich. |
| 150 | +# It has these veggies: |
| 151 | +# - Onions |
| 152 | + |
| 153 | +make_a_sandwich('Roast Beef', 'Lettuce', 'Tomato') |
| 154 | +# Making a Roast Beef Sandwich. |
| 155 | +# It has these veggies: |
| 156 | +# - Lettuce |
| 157 | +# - Tomato |
| 158 | + |
| 159 | +make_a_sandwich('Turkey', 'Lettuce', 'Tomato', 'Peppers') |
| 160 | +# Making a Turkey Sandwich. |
| 161 | +# It has these veggies: |
| 162 | +# - Lettuce |
| 163 | +# - Tomato |
| 164 | +# - Peppers |
| 165 | + |
| 166 | + |
| 167 | +# Collecting an arbitrary number of keyword arguments |
| 168 | +def make_a_sandwich(type, **veggies): |
| 169 | + print(f'\nMaking a {type} Sandwich.') |
| 170 | + print('It has these veggies:') |
| 171 | + for veggie in veggies: |
| 172 | + print(f'- {veggies[veggie]}') |
| 173 | + |
| 174 | + |
| 175 | +make_a_sandwich('Ham', one='Onions') |
| 176 | +# Making a Ham Sandwich. |
| 177 | +# It has these veggies: |
| 178 | +# - Onions |
| 179 | + |
| 180 | +make_a_sandwich('Roast Beef', one='Onions', two='Peppers') |
| 181 | +# Making a Roast Beef Sandwich. |
| 182 | +# It has these veggies: |
| 183 | +# - Onions |
| 184 | +# - Peppers |
| 185 | + |
| 186 | +make_a_sandwich('Turkey', one='Olives', two='Spinach', three='Cucumbers') |
| 187 | +# Making a Turkey Sandwich. |
| 188 | +# It has these veggies: |
| 189 | +# - Olives |
| 190 | +# - Spinach |
| 191 | +# - Cucumbers |
| 192 | + |
| 193 | + |
| 194 | +# Returning a single value |
| 195 | +def get_full_name(first, last): |
| 196 | + full_name = f'{first} {last}' |
| 197 | + return full_name.title() |
| 198 | + |
| 199 | + |
| 200 | +comedian = get_full_name('ricky', 'gervais') |
| 201 | +print(comedian) |
| 202 | +# Ricky Gervais |
| 203 | + |
| 204 | + |
| 205 | +# Returning a dictionary |
| 206 | +def build_house(type, bedrooms): |
| 207 | + house = {'type': type, 'bedrooms': bedrooms} |
| 208 | + return house |
| 209 | + |
| 210 | + |
| 211 | +house = build_house('Colonial', 3) |
| 212 | +print(house) |
| 213 | +# {'type': 'Colonial', 'bedrooms': 3} |
| 214 | + |
| 215 | + |
| 216 | +# Returning a dictionary with optional values |
| 217 | +def build_house(type, bedrooms, pool=None): |
| 218 | + house = {'type': type, 'bedrooms': bedrooms} |
| 219 | + if pool: |
| 220 | + house['pool'] = pool |
| 221 | + return house |
| 222 | + |
| 223 | + |
| 224 | +house = build_house('Colonial', 3) |
| 225 | +print(house) |
| 226 | +# {'type': 'Colonial', 'bedrooms': 3} |
| 227 | + |
| 228 | +house = build_house('Colonial', 2, 'No') |
| 229 | +print(house) |
| 230 | +# {'type': 'Colonial', 'bedrooms': 2, 'pool': 'No'} |
0 commit comments