Skip to content

Commit 6908f5f

Browse files
author
Sahil
committed
Add function keyword args and scope
1 parent dbf052d commit 6908f5f

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

day3/functions.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# arguments can be of two types: with default values and without default values
2+
# those with default values are called keyword arguments
3+
# for eg the print function we use has 'sep' as a keyword argument which is '\n' by default
4+
5+
# below is also a way you can document your function using docstrings
6+
def distance_travelled(t, u = 0, g = 9.81):
7+
"""
8+
Function distance_travelled calculates the distance travelled by an object
9+
with initial speed u, in a time interval of t, where acceleration due to gravity is g
10+
11+
input: t (time taken), u (initial speed) g (acceleration due to gravity)
12+
output: distance travelled
13+
14+
Example:
15+
>>> distance_travelled(1.0)
16+
17+
4.905
18+
"""
19+
d = u*t + 0.5 * g * t**2
20+
return d
21+
22+
print(distance_travelled(2.0))
23+
print(distance_travelled(2.0, u = 1.0))
24+
print(distance_travelled(2.0, g = 9.7))
25+
print(distance_travelled(g = 9.7, t = 2.0))
26+
27+
# scope inside a function is different than the one outside it
28+
# inside function scope, you can access the outer scope variables but cannot change them
29+
30+
def set_name(input_name):
31+
name = input_name
32+
print(f"Name inside the function: {name}")
33+
34+
set_name("Scaler")
35+
# print(name) # NameError: this variable is not defined
36+
37+
"""
38+
# Exercise: What should be the output for this piece of code?
39+
def add_char_to_list(char, lst = []):
40+
lst.append(char)
41+
return lst
42+
add_char_to_list('s')
43+
add_char_to_list('c')
44+
add_char_to_list('a')
45+
add_char_to_list('l')
46+
add_char_to_list('e')
47+
print(''.join(add_char_to_list('r')))
48+
"""

day3/grid_city.py

Whitespace-only changes.

lecture3.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,83 @@ print(res)
161161
## More on Functions
162162
[Code](day3/functions.py)
163163

164-
### Keyword Arguments
164+
### Keyword Arguments and Positional Arguments
165+
- A function can have two types of arguments: ones with default values and those without it.
166+
- The arguments which have a default value are termed as `keyword arguments` and required ones are `positional arguments`.
167+
- These arguments should always be the last ones, i.e. All the *required (positional) arguments go first*. They are then *followed by the optional keyword arguments*.
168+
- Arguments without defaults are required otherwise you get a syntax error.
169+
- You can pass in none, some or all of the keyword arguments.
170+
- You can pass in parameters by keyword (name defined for argument).
165171

172+
<details>
173+
<summary>
174+
Function with keyword arguments (code)
175+
</summary>
176+
177+
```py
178+
# for eg the print function we use has 'sep' as a keyword argument which is '\n' by default
179+
180+
# below is also a way you can document your function using docstrings
181+
def distance_travelled(t, u = 0, g = 9.81):
182+
"""
183+
Function distance_travelled calculates the distance travelled by an object
184+
with initial speed u, in a time interval of t, where acceleration due to gravity is g
185+
186+
input: t (time taken), u (initial speed) g (acceleration due to gravity)
187+
output: distance travelled
188+
189+
Example:
190+
>>> distance_travelled(1.0)
191+
192+
4.905
193+
"""
194+
d = u*t + 0.5 * g * t**2
195+
return d
196+
197+
print(distance_travelled(2.0))
198+
print(distance_travelled(2.0, u = 1.0))
199+
print(distance_travelled(2.0, g = 9.7))
200+
print(distance_travelled(g = 9.7, t = 2.0))
201+
```
202+
203+
</details>
166204

167205
### Function scope
168206

207+
```py
208+
# scope inside a function is different than the one outside it
209+
# inside function scope, you can access the outer scope variables but cannot change them
210+
211+
def set_name(input_name):
212+
name = input_name
213+
print(f"Name inside the function: {name}")
214+
215+
set_name("Scaler")
216+
# print(name) # NameError: this variable is not defined
217+
```
218+
219+
### Exercise
220+
221+
<details>
222+
<summary>
223+
Guess output of the code
224+
</summary>
225+
226+
```py
227+
# Exercise: What should be the output for this piece of code?
228+
# NEVER USE ARGUMENTS WITH LISTS AS DEFAULT VALUES!!!
229+
def add_char_to_list(char, lst = []):
230+
lst.append(char)
231+
return lst
232+
add_char_to_list('s')
233+
add_char_to_list('c')
234+
add_char_to_list('a')
235+
add_char_to_list('l')
236+
add_char_to_list('e')
237+
print(''.join(add_char_to_list('r')))
238+
```
239+
240+
</details>
169241

170242
## Containers
171243

0 commit comments

Comments
 (0)