print("Hello World")

"""
Explanation: Welcome to the interactive Python session.
This notebook series is intended as a very high-level introduction to python and tools, for a more comprehensive set of material refer to the following reference:
https://wiki.python.org/moin/BeginnersGuide/Programmers
Also, consider working through the notes provided by Robert Johansson:
https://github.com/jrjohansson/scientific-python-lectures

Q. Print hello world to the screen:
End of explanation
"""


name = "Nathan Faggian"
name

yourname = name

id(name), id(yourname)

"""
Explanation: Q. Assign a value to a variable (references, most things are mutable)
End of explanation
"""


title = "Dr " + name
title

"""
Explanation: Q. Mutate a string.
End of explanation
"""


container = ("Nathan", 182)
container

"""
Explanation: Some core data types: Tuples, Lists, Dictionaries and Sets.
Q. How do you form a container for information? Tuples.
End of explanation
"""


container = set(["nathan", 182])
container

182 in container

"""
Explanation: Sets are also appropriate if you don't want order to be preserved.
End of explanation
"""


table = [("Nathan", 182), ("Michael", 175), ("Sam", 190)]
table

sorted(table)

sorted(table, key=lambda x:x[1], reverse=True)

"""
Explanation: Q. Sorting tabular datasets. Lists of Tuples and the sorted built-in.
End of explanation
"""


heights = {"Nathan": 182, "Michael": 175, "Sam": 190}
heights

heights["Michael"]

"""
Explanation: Q. How do you describe a relationship or a mapping of data? You can use lists of tuples but dictionaries are better.
End of explanation
"""


import operator

sorted(heights.items(), key=operator.itemgetter(0), reverse=True)

"""
Explanation: Q. Who is the tallest person?
End of explanation
"""


year = 2015 

year > 2020

(year > 2010) and (year < 2020)

(2010 < year < 2020)

"""
Explanation: Basic boolean expressions, selection and iteration.
End of explanation
"""


def is_leap_year(year):
    """
    Returns if the year is a leap year.
    """
    if year % 100 == 0:
        return year % 400 == 0
    else:
        return year % 4 == 0

"""
Explanation: Basic example of selection:
Python
if (condition):
    operation
else
    operation

Q. Is the year 2000 a, gregorian calendar, leap year?
Leap years: 

The year is evenly divisible by 4 
If the year can be evenly divided by 100, it is NOT a leap year, unless the year is also evenly divisible by 400.
End of explanation
"""


years = [2010, 2011, 2016, 2020]
for year in years:
    if is_leap_year(year):
        print("{} is a leap year!".format(year))

"""
Explanation: Basic example of iteration:
Python
for data in iterable:
    operation
End of explanation
"""


def greet(first_name, last_name):
    """Returns a string"""
    return "Hello {} {}.".format(first_name, last_name)

greet("Nathan","Faggian")

"""
Explanation: Functions and Classes
Q. Write a greeting function that takes two arguments and returns a string.
End of explanation
"""


class Wallet(object):
    """
    A Wallet contains integers of dollars and cents.
    """
    
    def __init__(self, dollars, cents):
        self.dollars = dollars
        self.cents = cents
        
    def __repr__(self):
        return "Wallet({},{})".format(self.dollars, self.cents)
    
    def __del__(self):
        pass
    
pouch = Wallet(1000,0)
print("A wallet: {}, with {} dollars and {} cents.".format(pouch, 
                                                           pouch.dollars, 
                                                           pouch.cents))


"""
Explanation: Q. Write a basic class that encapsulates some data.
End of explanation
"""


import this

"""
Explanation: Tip: Python allows you to use both functional and imperative styles and lets the user determine which approach is best. 
What makes Python different from other languages?
End of explanation
"""


data = [1, 2, 3, 4]
sum(data)

"""
Explanation: Q. Form an array of values and sum then together using the sum built-in.
End of explanation
"""


sum([x for x in data if x > 2])

"""
Explanation: For simple things the R language looks similar, especially math.
R
data &lt;- c(1,2,3,4)
sum(data)
Q. Sum an array of values only if the values are greater than 2.
End of explanation
"""


total = 0
for x in data:
    if x > 2:
        total += x 
total

"""
Explanation: For loops are also fine but more verbose:
End of explanation
"""


[x for x in data if x > 2]

"""
Explanation: Influences from functional programming make code easier to read and understand - list comprehensions.
End of explanation
"""


{k:v for k,v in [("a", 2), ("b",4)] if v > 2}

"""
Explanation: There are also dictionary comprehensions.
End of explanation
"""


with open('test.file', 'w') as file_handle:
    file_handle.write("Hello")

"""
Explanation: Context managers, can take care of handling the opening and closing of files.
End of explanation
"""


def fib():
   a, b = 0, 1
   while True:
      yield b
      a, b = b, a + b

fib_generator = fib()

value = next(fib_generator)
value

"""
Explanation: Q. An infinite fibonacci sequence. A. Using generators.
End of explanation
"""


