summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xdatatypes.py33
-rwxr-xr-xfibonacci.py17
2 files changed, 50 insertions, 0 deletions
diff --git a/datatypes.py b/datatypes.py
new file mode 100755
index 0000000..8072582
--- /dev/null
+++ b/datatypes.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+
+# below snippet demonstrates list datatype in python (akin to arrays)
+# lists are ordered, mutable, hold arbitrary objects, expand dynamically
+l = [21, 'foo', 13, True, 21, [0, 1.0]]
+l.append({'ipv4': '8.8.8.8'})
+l.extend(['12', '33'])
+l.insert(0, '1st')
+# len is 10, the list or dict count as 1 element
+print('len is: ' + str(len(l)) + ', ' + str(l))
+bar = l.pop(2) # bar = 'foo'
+ip = l[-3]['ipv4'] # 3rd from last index
+print('bar = ' + bar + ', ip: ' + ip)
+
+# tuple, ordered, immutable, const-like object with no methods
+t = ('foo', 'bar', 1, False, [1,2,3])
+t[-1].extend([4,5]) # while tuples are constant and cannot be changed
+print(t) # the list at index -1 can still be mutated
+
+# set, unordered
+s = {1,2,3, 'string'}
+ss = {1,2, "bar"}
+sss = s.union(ss) # values 1 and 2 won't show up as duplicates
+print(sss)
+
+# control flow
+x = 1 if True else 2 # x = 1, this is example of ternary operator, akin to ? in C
+print(x)
+
+# List Comprehensions
+mysum = sum([i for i in range(1, 10) if i%2==0])
+squares = [x**2 for x in range(10)]
+squares = list(map(lambda x: x**2, range(10)))
diff --git a/fibonacci.py b/fibonacci.py
new file mode 100755
index 0000000..bf1ea4a
--- /dev/null
+++ b/fibonacci.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+
+def fibonacci(n):
+ if n == 0:
+ return 0
+ elif n == 1:
+ return 1
+ else:
+ return fibonacci(n - 1) + fibonacci(n - 2)
+
+# code below will execute only if this .py file is ran directly instead of being imported
+if __name__ == "__main__":
+ import sys
+ if (len(sys.argv) == 2):
+ print('fibonacci of ' + str(int(sys.argv[1])) + ' is ' + str(fibonacci(int(sys.argv[1]))));
+ else:
+ print('usage: fibonacci.py <number>');