Repository navigation
Expand file tree
/
Copy pathExamples_Functions.py
More file actions
91 lines (48 loc) · 1.59 KB
/
Copy pathExamples_Functions.py
File metadata and controls
91 lines (48 loc) · 1.59 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
"""
Michael Hollas - 2020-05-01
------------------------------------------------------------------------------------------------------------------------
How To use functions
------------------------------------------------------------------------------------------------------------------------
functions are created by defining them (hence def)
def functionname(input variables):
"""
def simpleAddition(a,b):
#do the thing!
return a + b # will return this value to be the output of the function - don't need to have a return
def print_hello():
print("hello")
simpleAddition(4,5)
print(simpleAddition(1,2))
# or
addValue = simpleAddition(1,2)
print(addValue)
"""
input/arguments with keywords!
"""
print(simpleAddition(a=2,b=5))
#order does not matter
print(simpleAddition(b=2,a=5))
"""
default values!
"""
def my_function(firstname = "john",lastname="smith"):
print("{0} {1}".format(firstname,lastname))
my_function("mmike","hollas")
my_function()
"""
variables inside a function usually cannot be accessed outside the function
"""
def simplemultiplaction(a,b):
x = 12
return a*b
#print(x) #<--- can't do!!!'
# functions can be called from another file!!!! :O
"""
first need to import the function
"""
from Examples_CSV_file_reader import csv_to_dict
# or
from Examples_CSV_file_reader import * # this will import all functions from this file
from Examples_CSV_file_reader import csv_to_dict as cd # can rename the function with the 'as' keyword
data = csv_to_dict("C:\Data\\test.csv")
data2 = cd("C:\Data\\test.csv")# this is the same function as above!