-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
executable file
·96 lines (63 loc) · 1.26 KB
/
functions.py
File metadata and controls
executable file
·96 lines (63 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 9 22:36:15 2021
@author: maherme
"""
#%%
# Built in functions.
s = [1, 2, 3]
print(len(s))
#%%
# Library functions.
from math import sqrt
print(sqrt(4))
#%%
import math
print(math.pi)
print(math.exp(1))
#%%
# User defined functions.
def func_1():
print('running func_1')
#%%
# This not call the function, this returns the function, is the function.
func_1
#%%
# This is a function call.
func_1()
#%%
# Use of parameters.
def func_2(a: int, b: int):
return a * b
#%%
# The ": int" before is only information, we can pass a non integer parameter
# to the function. Notice that python is very polymorphic.
print(func_2(2, 3))
print(func_2('a', 3))
print(func_2([1, 2], 3))
#%%
# You can return a function.
def func_3():
return func_4()
def func_4():
return 'running func_4'
print(func_3())
#%%
# Be careful where a function is called.
def func_5():
return func_6()
func_5()
def func_6():
print('running func_6')
#%%
# You can assign function to a variable.
my_func = func_4
print(func_4())
print(my_func())
#%%
# You can also define a function as a lambda function. This is like a one line
# or an anonymous function.
fn1 = lambda x: x**2
print(fn1(2))
#%%