8000 Shorten functions notes by thinking about how it will look in the des… · VixyMan/complete-python-course@d049698 · GitHub
[go: up one dir, main page]

Skip to content

Commit d049698

Browse files
committed
Shorten functions notes by thinking about how it will look in the design and relying on notes on code blocks instead of bullet points and explanations.
1 parent 2a9f64b commit d049698

File tree

1 file changed

+35
-85
lines changed

1 file changed

+35
-85
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,23 @@
11
# Python functions
22

3-
Defining a function:
3+
Define a function using the `def` keyword. Once defined, it can be called (or executed) by using the function name and a pair of brackets.
44

55
```python
66
def greet():
77
name = input("Enter your name: ")
88
print(f"Hello, {name}!")
9-
```
10-
11-
To call a function use the function's name followed by parentheses:
12-
13-
```python
14-
# defining the function
15-
def greet():
16-
name = input("Enter your name: ")
17-
print(f"Hello, {name}!")
189

1910

20-
# calling the function
2111
greet()
2212
```
2313

24-
## The `def` keyword
25-
26-
A function is defined by 4 elements:
27-
- the `def` keyword
28-
- the name of the function
29-
- parentheses and
30-
- colon
31-
32-
Syntax: `def name():`
33-
34-
## Indentation
35-
36-
- To write code inside the function, you must increase the indentation level with one
37-
- When you reduce the indentation level, you exit the function
38-
39-
Example:
40-
41-
```python
42-
def greet():
43-
name = input("Enter your name: ") # inside the function
44-
print(f"Hello, {name}!") # inside the function
14+
> Could show small callouts beside each section of the shown code sample.
15+
> E.g. beside `def greet()` show "Defining the function name"
16+
> Then beside the function body, show "Function body, will run when we execute the function".
17+
> Then beside `greet()`, show "Executing the function starts the function body"
18+
> Highlight the difference in indentation level with a callout.
19+
> Signal that when we have less indentation, we exit the function.
4520
46-
print("Outside the function")
47-
48-
greet()
49-
```
5021

5122
## The `return` keyword
5223

@@ -59,56 +30,40 @@ def greet():
5930
pass # Do nothing
6031

6132

62-
# print the function's return
33+
# print the function's return value
6334
print(greet()) # None
6435
```
6536

66-
- `pass` it's a keyword, and it means "do nothing"
37+
- `pass` is a keyword which means "do nothing". It's necessary since a function needs a body.
6738
- you can control what a function returns using the `return` keyword.
6839

6940
Example:
7041

7142
```python
72-
def greet():
43+
def greet_user():
7344
name = "John Doe"
7445
return f"Hello, {name}!"
7546

76-
# printing the function
77-
print(greet()) # John Doe
47+
greeting = greet()
48+
print(greeting) # John Doe
7849
```
7950

80-
A function can return anything.
51+
A function can return any value.
8152

82-
## Parameters
53+
## Arguments and parameters
8354

84-
A parameter is the variable defined inside the function's definition parentheses.
55+
Functions can accept values from the caller, and return a value in their place.
8556

86-
Example:
87-
88-
```python
57+
```py
8958
def greet(name):
90-
return f"Hello, {name}!"
91-
```
92-
93-
You can have any number of parameters in a function.
59+
return f"Hello, {name}
9460

95-
96-
## Arguments
97-
98-
- When a function is called, an argument is the value that is passed to it
99-
- To call a function that has a parameter, you have to provide an argument
100-
101-
Example:
102-
103-
```python
104-
def greet(name):
105-
return f"Hello, {name}!"
106-
107-
108-
print(greet("John Doe")) # Hello, John Doe!
61+
greeting = greet("Rolf Smith")
62+
print(greeting) # Hello, Rolf Smith
10963
```
11064

111-
The variable `name` will hold whatever it was passed inside the function's parentheses upon function call.
65+
> Here we could have a diagram showing the flow of data using arrows.
66+
> From the string "Rolf Smith" to the function, then through the return value to the `greeting` variable.
11267
11368
## Number of parameters
11469

@@ -144,20 +99,11 @@ def greet(name="John Doe"):
14499

145100

146101
print(greet()) # Hello, John Doe!
102+
print(greet("Rolf Smith")) # Hello, Rolf Smith
147103
```
148104

149105
When passing an argument to the function, the default parameter value will be overwritten by the given argument.
150106

151-
For example:
152-
153-
```python
154-
def greet(name="John Doe"):
155-
return f"Hello, {name}!"
156-
157-
158-
print(greet("Jimmy Boy")) # Hello, Jimmy Boy!
159-
```
160-
161107
If the function has a mix of normal and default parameters, the default parameters have to be passed last.
162108

163109
Correct:
@@ -186,29 +132,30 @@ Read more about Python functions [here](https://www.teclado.com/30-days-of-pytho
186132

187133
- Lambda expressions are an alternative syntax for defining simple functions.
188134

189-
Syntax: `lambda arguments: expression`
135+
Example: `lambda name: f"Hello, {name}"`
190136

191-
- they don't have a name
192-
- they can be given a name by assigning them to a variable name
193-
- they're not so easy to read
137+
- No name
138+
- Can be assigned to a variable
139+
- Can be difficult to read
194140

195-
Example:
141+
Calling a lambda inline requires multiple pairs of brackets:
196142

197143
```python
198144
result = (lambda x, y: x + y)(15, 3)
199145
```
200146

201-
Read more about lambda expressions and first-class functions [here](https://www.teclado.com/30-days-of-python/python-30-day-16-lambda-expressions).
202-
203147
# First Class Functions
204148

205149
- Functions in Python are "first class citizens".
206-
- This means they are objects that can be stored in data structures and passed as arguments, and stored in variables.
150+
- They can be stored in data structures, passed as arguments, and stored in variables.
207151

208152
Example:
209153

210154
```python
211-
# Each of avg, sum, max are functions, and since they are First Class Citizens, they can be stored in this dictionary.
155+
def avg(*args):
156+
return sum(args) / len(args)
157+
158+
212159
operations = {
213160
"average": avg,
214161
"total": sum,
@@ -220,3 +167,6 @@ selection = "average"
220167
operation = operations[selection]
221168
operation() # This is acts the same as simply calling avg()
222169
```
170+
171+
172+
Read more about lambda expressions and first-class functions [here](https://www.teclado.com/30-days-of-python/python-30-day-16-lambda-expressions).

0 commit comments

Comments
 (0)
0