Skip to content

Commit 45058d8

Browse files
authored
Create README.md
0 parents  commit 45058d8

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# #2 - Become a Python Function Ninja
2+
3+
This assignment involves writing some more functions in Python :+1:
4+
5+
Last assignment aimed to strengthen your basic function writing skills while also learning the concept of typecasting to convert one data type to another.
6+
7+
This one will take half an hour, but will teach you all cool things about functions.
8+
9+
## Notes : For help in finishing this assignment
10+
11+
### Parts of a Function - Quick Recall
12+
```
13+
def printHello(name):
14+
print "Hello" + name + "!"
15+
16+
printHello("Turtle")
17+
```
18+
19+
```
20+
def add(x, y):
21+
sum = x+y
22+
return sum
23+
24+
result = add(a, b)
25+
```
26+
27+
- Function Definiton - A header line which begins with a keyword and ends with a colon.
28+
- Function Declaration - A body consisting of one or more Python statements, each line should be indented the same amount.
29+
- Function call - Defining and Declating a new function does not make the function run. To do that we need a function call.
30+
- Parameters
31+
- These are of two types
32+
- Formal Parameters - The parameters/variables in the function definition are more specifically known as the formal parameters.
33+
Eg. def add(x, y): Here x and y are formal parameters.
34+
- Actual Parameters - A function needs certain information to do its work. These values, often called arguments or actual parameters, are passed to the function by the user. They are in the function call, eg add(a,b) , a and b are actual parameters
35+
- Return statement
36+
- A function may or may not return anything
37+
- A function that does our work but not returns anything, like this one,
38+
```
39+
def printHello(name):
40+
print "Hello" + name + "!"
41+
42+
printHello("Turtle")
43+
```
44+
are called *procedures* or non - fruitful functions
45+
- On the other hand, if it returns something, like
46+
```
47+
def add(x, y):
48+
sum = x+y
49+
return sum
50+
51+
result = add(a, b)
52+
```
53+
then the function is called a fruitful function
54+
55+
- The return statement is followed by an expression which is evaluated. Its result is returned to the caller. Okay sweet!
56+
```
57+
return a + b
58+
```
59+
a + b is an expression
60+
61+
### Inside Functions
62+
63+
- Two types of Functions
64+
- User-defined, which users write.
65+
- Built - in functions, example - print(), abs(), len() Okay!
66+
67+
- Local Scope and Global Scope [dude this is *important* :smile: ]
68+
1. In-Depth Knowledge about variables and data types in Python : [Read](http://www.python-course.eu/python3_variables.php)
69+
2. Then this one : [Read](http://www.python-course.eu/python3_global_vs_local_variables.php)
70+
3. These two articles are excellent and cover almost every intricate detail and tricks.
71+
4. These articles may take an hour or two to study, but it'll be totally worth it.
72+
- Remember if you want to know what a built-in function does, just type help(function_name) in Python interpreter
73+
Eg - help(print)
74+
help(id)
75+
help(len)
76+
help(abs)
77+
78+
79+
## Questions
80+
81+
1. Write a function which returns max of 2 numbers. We can use if, elif and else, right?
82+
2. Find the output - Concept of local and global variables
83+
```
84+
a = 60
85+
def func(z):
86+
print("z = ", z)
87+
a = 2
88+
print('Changed local a to', a)
89+
func(a)
90+
print('Global a is still', a)
91+
```
92+
3. What is the use of id(), print() function in python? You know the interpreter trick!
93+
4. What's the output:
94+
95+
```
96+
def cube(x):
97+
return x * x * x
98+
99+
x = cube(3)
100+
print x
101+
```
102+
5. Lets find the output , again! :smiley:
103+
```
104+
def foo(k):
105+
k[0] = 1
106+
q = [0]
107+
foo(q)
108+
print(q)
109+
```
110+
6. Try this, find output?
111+
```
112+
def foo(i, x=[]):
113+
x.append(x.append(i))
114+
return x
115+
for i in range(3):
116+
y = foo(i)
117+
print(y)
118+
```
119+
120+
- The trick for solving 'find the output' questions is to NOT think how the program wants to you to think. DON'T proceed with any notion of how that program is *supposed* to work, find out how that program will *exactly* work.
121+
122+
- Try doing dry run! Okay! :smile:

0 commit comments

Comments
 (0)