# Python like Javascript is a functional langauge. It allows you to pass methods. | |

# To filter my_list for even numbers you can: | |

my_list = range(1,11) # Creates a list with the number 1 – 10 | |

# Filter Syntax is filter(params to filter by, list to filter) | |

print filter( lambda x: x%2==0, my_list) | |

# Returns | |

[2, 4, 6, 8, 10] |

# Python Hax: Iterating Over a Dictionary

# Iterating over Dictionaries in Pythin is really straight forward | |

dictionary = { | |

"a" : "A", | |

"b" : "B", | |

"c" : "C", | |

"d" : "D" | |

} | |

# Get all items in the Dictionary | |

dictionary.items() | |

# Returns | |

[('a', 'A'), ('c', 'C'), ('b', 'B'), ('d', 'D')] | |

# Get all keys in the Dictionary | |

dictionary.keys() | |

# Returns | |

['a', 'c', 'b', 'd'] | |

# Get all values in the Dictionary | |

dictionary.values() | |

# Returns | |

['A', 'C', 'B', 'D'] |

# Python Hax: List Slicing / Reverse

# Python list can be manipulated as follows: | |

my_list = [1,2,3,4,5,6,7,8,9,10] | |

# my_list[start_index : end_index : stride] | |

# Default for start index is 0 | |

# Default for end index is the end of the list | |

even_num_list = my_list[1::2] | |

# Returns | |

[2, 4, 6, 8, 10] | |

odd_num_list = my_list[::2] | |

# Returns | |

[1, 3, 5, 7, 9] | |

reverse_list = my_list[::–1] | |

# Returns | |

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1] |

# Python Hax: List Comprehension Syntax

# Creating a List of numbers from 1 to 10 can be done with: | |

list_1_to_10 = range(1,11) | |

# Returns | |

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |

# But what if you wanted only the even numbers? Use the List comprehension syntax! | |

even_num_list = [x for x in range(1,11) if x % 2 == 0] | |

# Returns | |

[2, 4, 6, 8, 10] |

# Python Hax: Recursive Example for Factorials

''' | |

Recursion can be difficult for beginners. It still is difficult for me. | |

The best way to improve is to try "easy" recursion problems such as | |

factorials in this case. | |

Instructions: | |

Create a function that takes an positive interger and returns the factorial of it | |

Example factorial(3) should return 3*2*1 or 6 | |

Factorial(1) should return 1 | |

Factorial(0) should return 1 | |

''' | |

#Non Recursion Example | |

def factorial(x): | |

total = 1 | |

if x == 0 or x == 1: # Handles Edge Case of 1 or 0 | |

return total | |

while x > 1: | |

total = total * x # Multiples variable total defined outside of loop with x | |

x-=1 # Subtracts one from x so that the while loop will break when x = 1 | |

return total | |

# Recursion Example | |

def factorial(x): | |

if x == 0 or x ==1: # When x is equal to 0 or 1, it returns 1 | |

return 1 | |

return x * factorial(x–1) # multiples x with the recursion or calling itself with x – 1 | |

''' | |

If we do factorial (3) | |

It first checks if x is == 0 or x == 1 => False | |

Then it go to x * factorial(x-1) which would be 3 * factorial(2) | |

Evaluate factorial(2) | |

Check if x == 0 or x == 1 => False | |

x * factorial(x-1) or 2 * factorial(1) | |

Evaluates factorial(1) | |

Check if x == 0 or x == 1 => True | |

returns 1 | |

factorial(1) evaluates to 1 | |

returns 1 | |

So 2 * factorial(1) = 2 * 1 => 2 | |

factorial(2) evaluates to 2 | |

returns 2 | |

So factorial(2) = 2 | |

So 3 * factorial(2) = 3 * 2 => 6 | |

This function then returns 6 | |

''' | |

# Install LDA, Beautiful Soup and NLPK for Sublime REPL

# This guide is for people who have install Sublime REPL and linked their system Python to it. | |

# If you haven't but want to, refer to my previous post where I show you how. | |

# If you are using Python 3 and still have Python 2 installed on your system as default | |

# For NLTK | |

python3 –m pip install –U nltk | |

# For LDA | |

python3 –m pip install lda | |

#For Beautiful Soup | |

python3 –m pip install beautifulsoup4 | |

#________________________________________________________________________________________ | |

# python2 -m pip install PACKAGE NAME will allow you to install packages for Python 2 | |

# If you are only running one version of Python, you can just type pip install PACKAGE NAME |

# Ruby Hax: Yield

# Yield in Ruby allow you to pass a block implicitly which not only makes it faster | |

# to type out this pattern but it also faster to execute! | |

# Regular Block Syntax | |

def calculate(a, b, action) | |

action.call(a,b) | |

end | |

# Defining the lambda add and subtract | |

add = lambda {|a,b| a+b} | |

subtract = lambda {|a,b| a–b} | |

puts calculate(10,5, add) | |

puts calculate(10,5, subtract) | |

# Returns | |

"15" | |

"5" | |

#Yield Syntax | |

def calculate(a, b) | |

yield(a,b) | |

end | |

puts calculate(10,5){|a,b| a+b} #Add | |

puts calculate(10,5){|a,b| a–b} #Subtract | |

# Returns | |

"15" | |

"5" |

# Ruby Hacks: Include vs Extend for Ruby Modules

# Include give you access to a module on a instance level while | |

# extend give class level access. | |

module CanYouHearMe | |

def test | |

puts "I hear you loud and clear!" | |

end | |

end | |

class Radio | |

include CanYouHearMe | |

end | |

class Satellite | |

extend CanYouHearMe | |

end | |

# Calling the method test on the class Radio | |

Radio.test | |

# Returns | |

"private method 'test' called for Context::Radio:Class" | |

# Calling the method test on the class Satellite | |

Satellite.test | |

# Returns | |

"I hear you loud and clear!" | |

# Calling the method test on an instance of the class Radio | |

ham_radio = Radio.new | |

ham_radio.test | |

# Returns | |

"I hear you loud and clear!" | |

# Calling the method test on an instance of the class Satellite | |

sat_comm = Satellite.new | |

sat_comm.test | |

# Returns | |

"private method 'test' called for #<Context::Satellite:0x000000024ef2f8>" |

# Ruby Hacks: Class inheritance in Ruby

# Inheritance is an important idea for Object Orientated Programming OOP | |

# This example will demo a simple example of inheritance | |

class Dog | |

def talk | |

puts "Woof" | |

end | |

end | |

class JackRussell < Dog | |

end | |

class BichonFrise | |

end | |

roo = JackRussell.new | |

roo.talk | |

#Returns | |

"Woof" | |

candy = BichonFrise.new | |

candy.talk | |

#Returns | |

" undefined method `talk' " | |

# Notice that the class JackRussell inherits from the class Dog on line 10. | |

# While JackRussell has no talk method defined, it inherits the talk method | |

# from the parent or Super class of Dog. This is why roo.talk returns "Woof" | |

# Meanwhile candy.talk returns undefined method as it is talk is not defined | |

# in the class BichonFrise nor did it inherit from the class Dog (line 21) |