Skip to content

Commit d3cff7b

Browse files
committedJun 9, 2018
New Change
1 parent 09dd137 commit d3cff7b

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed
 

‎markdown-version/functions.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,55 @@ def square(num,power=2):
8989
return num **power
9090
#Even if we dont pass a value for the power it will retain its default value 2
9191
```
92+
93+
* Default parameters can help you save from errors
94+
* It makes our code more flexible
95+
* Default Parameters can be any values we can also pass functions inside of a function
96+
97+
```Python
98+
def add(a,b):
99+
return a+b
100+
#The math function takes in add function as a parameter also
101+
def math(a,b,fn=add):
102+
return add(a,b)
103+
```
104+
105+
#### Keyword Arguements
106+
* Using keyword arguements we can alter the order
107+
* The reason we use the keyword arguements is because it helps you cause less errors and makes it more explicit
108+
```Python
109+
def exponent(num,pow):
110+
return num **pow;
111+
print(exponent(pow=2,num=3)) #This will return 9
112+
```
113+
114+
#### Scopes
115+
* There are rules where are variables can be accessed
116+
* Whenever we define a variable inside of a function it will be part of the function
117+
118+
```Python
119+
x = 2
120+
def add_two(num):
121+
return x+num
122+
#In this example the x variable is available outside the function
123+
124+
def add_three(num):
125+
y = 3
126+
return num+y
127+
#In this example the y is just the part of the function and it cannot be accessed from outside
128+
```
129+
130+
* Global scope
131+
* A variable that is defined outside of the function it is global
132+
* If we want to manipulate a variable that is not defined inside of the local scope we use the keyword **global**.
133+
134+
```Python
135+
total = 0
136+
def increment():
137+
global total
138+
total +=1
139+
return total
140+
print(increment())
141+
```
142+
143+
* **non local** keyword

‎python-exercises/functions.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
#Heads & Tail
2-
from random import random
1+
# #Heads & Tail
2+
# from random import random
33

4-
def heads_tails():
5-
r = random()
6-
if r > 0.5:
7-
return "HEADS"
8-
else:
9-
return "TAILS"
4+
# def heads_tails():
5+
# r = random()
6+
# if r > 0.5:
7+
# return "HEADS"
8+
# else:
9+
# return "TAILS"
1010

11-
print(heads_tails())
11+
# print(heads_tails())
1212

1313
# This function generates even numbers
14-
def generate_evens():
15-
even = [x for x in range(1,50) if x % 2==0]
16-
return even
14+
# def generate_evens():
15+
# even = [x for x in range(1,50) if x % 2==0]
16+
# return even
1717

18-
print(generate_evens())
18+
# print(generate_evens())
19+
20+
# def add(a,b):
21+
# return a+b
22+
# def math(a,b,fn=add):
23+
# return add(a,b)
24+
# print(math(10,10))

0 commit comments

Comments
 (0)
Please sign in to comment.