-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2018_12_08_functions.py
More file actions
65 lines (52 loc) · 1.6 KB
/
Copy path2018_12_08_functions.py
File metadata and controls
65 lines (52 loc) · 1.6 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 8 12:43:57 2018
@author: domenjemec
@title: 2018-12-08 Functions
"""
#Function type
f=""
#define a basic function
def basic_function():
global f
f="basic"
print("I am the " + f + " function")
#function that takes arguments
def basic_arg_function(arg1, arg2):
global f
f="basicArg"
print("I am the " + f + " function with inputs 1:" + arg1 + " 2:" + arg2)
#function that returns a value
def basic_func_w_return(arg1):
global f
f="basicArgWValue"
y="Int " if isinstance(arg1,int) else "String "
return "I am the " + f + " function with inputs 1:" + y+ str(arg1)
#function that with default value for an argument
def power(num,x=1):
result = 1
for i in range(x):
result = result * num
return "I am the defalutArgWReturn function value: " + str(result)
#function with variable number of arguments
def multi_add(*args):
result = ""
for s in args:
result = s if result =="" else result + ", " + s
return result
#function with variable and non variable args (note var has to be last)
def multi_add_prefixed(start, *args):
n = multi_add(args)
return start + str(n)
#executes
#basic_function()
#basic_arg_function(arg1="value1",arg2="value2")
#print(basic_func_w_return(1))
#print(power(9,4))
#print(power(x=14, num=2))
#String parsing / array indexing fun
#x="9^4"
#print(power(int(x.split("^")[0]),int(x.split("^")[1])))
#print("Variable formula: " + multi_add("A", "B", "C"))
print(multi_add_prefixed("This is the start string for a mixed formula: ", "A", "B", "C"))