# Dragon Notes

UNDER CONSTRUCTION
Latest content:
 Apr 05 Deep Learning Mar 19 Anomaly Detection - ML Mar 13 +Data Tables Mar 08 Clustering - Machine Learning Feb 28 Support Vector Machines - ML Feb 20 Regression - Data Science

# Python:Commands

String functions

print(", ".join(["spam", "eggs", "ham"]))
print("Hello ME".replace("ME", "world"))
print("This is a sentence.".startswith("This"))
print("This is a sentence.".endswith("sentience."))
print("woot".upper())
print("FEEL THE PYTHON".lower())
print("spam, eggs, ham".split(", "))

nums = [4, 5, 6]
print("Numbers: {0} {1} {2}".format(nums[0], nums[1], nums[2]))
print("Numbers: {1} {0} {2}".format(nums[0], nums[1], nums[2]))
print("Numbers: {1} {0} {2}".format(nums[2], nums[0], nums[1]))

print("{x},{y}".format(x=5, y=12))

# >>> spam, eggs, ham # >>> Hello world # >>> True # >>> False # >>> WOOT # >>> feel the python # >>> ['spam','eggs','ham'] # >>> Numbers: 4, 5, 6 # >>> Numbers: 5, 4, 6 # >>> Numbers: 4, 6, 5 # >>> 5, 12

map(func, iterable)
- takes a function and an interable as arguments, and returns a new iterable with the function applied to each argument
return x + 5

nums = [11, 22, 33, 44, 55]
print(result)

>>> [16, 27, 38, 49, 60]

# has to be explicitly converted to list type to be printed

filter(predicate, iterable)
- filters an iterable by removing items that don't match a predicate (a function that returns a Boolean)
nums = [11, 22, 33, 44, 55]
res = list(filter(lambda x: x%2==0, nums))
print(res)

>>> [22, 44]

# has to be explicitly converted to list type to be printed

generator function
- returns a result to its caller without destroying local variables
- is a type of iterable
- can be infinite
- doesn't allow arbitrary indexing, but can be iterated through with for loops
- improves performance by generating values on-demand (one per call, rather than all at once), which lowers memory usage
- doesn't require waiting until all elements have been generated to start using them
def countdown():
i=5
while i > 0:
yield i
i -= 1

for i in countdown():
print(i)

>>> 5
4
3
2
1

# defines a generator, replacing the return of a function # i is now 0 (preserved), and the generator doesn't # execute when called

def countdown(i):
while i > 0:
yield i
i -= 1

for i in countdown(5):
print(list(countdown(i)))

>>> [5, 4, 3, 2, 1]
[4, 3, 2, 1]
[3, 2, 1]
[2, 1]
[1]

decorator
- a way to modify functions using other functions
- useful for extending functionality of functions without modifying them (directly)
def decor(func):
def wrap():
print("============")
func()
print("============")
return wrap

def print_text():
print("Hello world!")

print_text = decor(print_text)
print_text()

>>>
============
Hello world!
============

# line 11 # newlined for display

@decor
@decor
def print_text():
print("Hello world!")

print_text()

>>>
============
============
Hello world!
============
============

# prepending the decorator to the function using '@' # has the same effect as line 11, with unlimited use

assertion
- tests an expression, throws an AssertionError exception if the result is False
- Stops code execution upon exception (unless handled by try-except)
- Used at start of functions to check for valid input, at end of functions to check for valid output
- Takes a second argument that is passed to the AssertionError, if raised
- Second argument useful for checking for anticipated errors
print(1)
assert 2 + 2 == 4
print(2)
assert 1 + 1 == 3
print(3)

>>> 1
2
AssertionError
temp = -10
assert (temp=>0), "Colder than absolute zero!"

>>> AssertionError: Colder than absolute zero!

recursion
- functions calling themselves (self-referencing)
- Usually involves a base case, which terminates recursion; lacking one may yield infinite loops
def factorial(x):
if x==1:
return 1
else:
return x*factorial(x-1)

print(factorial(5))

>>> 120

# base case # simplified example without input # checks for zero & negatives

sets
- data structures, similar to lists/dictionaries
 - Unordered; can't be indexed - Cannot contain duplicate elements - Used for membership testing and elimination of duplicate entries - add to add to a set (instead of append) - remove to remove a specific element - pop to remove the first (left-most) element - len = # of elements in set; in to check if an item is present - union, |, combines sets, forming one containing items in either - intersection, &, gets items only in both - difference, -, gets items in the 1st set but not in the 2nd - symmetric difference, ^, gets items in either set, but not in both
nums = {1, 2, 1, 3, 1, 4, 5, 6}
print(nums)
nums.remove(3)
print(nums)

>>> {1, 2, 3, 4, 5, 6}
{1, 2, 4, 5, 6, -7}
first = {1, 2, 3, 4, 5, 6}
second = {4, 5, 6, 7, 8, 9}

print(first | second)
print(first & second)
print(first - second)
print(second - first)
print(first ^ second)

# >>> {1, 2, 3, 4, 5, 6, 7, 8, 9} # >>> {4, 5, 6} # >>> {1, 2, 3} # >>> {8, 9, 7} # (non-indexed) # >>> {1, 2, 3, 7, 8, 9}