File tree Expand file tree Collapse file tree 2 files changed +71
-13
lines changed Expand file tree Collapse file tree 2 files changed +71
-13
lines changed Original file line number Diff line number Diff line change @@ -89,3 +89,55 @@ def square(num,power=2):
89
89
return num ** power
90
90
# Even if we dont pass a value for the power it will retain its default value 2
91
91
```
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
Original file line number Diff line number Diff line change 1
- #Heads & Tail
2
- from random import random
1
+ # # Heads & Tail
2
+ # from random import random
3
3
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"
10
10
11
- print (heads_tails ())
11
+ # print(heads_tails())
12
12
13
13
# 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
17
17
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))
You can’t perform that action at this time.
0 commit comments