
'''
PYTHON REFRESHER
Jeff Thompson | 2018 | jeffreythompson.org

Welcome back to Python! This is an example script meant as to
remind you of the basic syntax.

WHY ARE WE USING PYTHON?
Python is a lovely language in general, but we're using it for
our machine learning examples because, well, everyone else does
too! Python is really great at working with vectors (lists of
values), it runs well on large clusters and supercomputers, and,
while it used to be known as a rather slow langauge, Python is now
fast enough to be used for all kinds of projects. In particular,
Python is now the go-to language for machine learning work, and
most of the great libraries are either written specifically for
Python, or have Python "wrappers" allowing you to use them
in your Python code.

RUNNING YOUR CODE
Unlike in earlier projects, we'll be running Python inside
Anaconda, which lets us install libraries and settings especially
for machine learning projects, keeping your regular install of
Python clean and tidy!

To run you code:
1. Open Anaconda Navigator
2. Go to "Environments", select the one we made
3. Click the play button, and select "Open in Terminal" (or the
   equivelant on your operating system)
4. In your terminal window, navigate to your code's folder using
   the 'cd' command (Mac/Linux) or 'dir' command (Windows)
5. Type 'python FilenameOfYourScript.py' and hit enter!
6. Note that to see changes in your code, you have to save it
   in the text editor, then run it again.

COMMAND LINE REFRESHER?
+ Mac/Linux: 
    http://www.dummies.com/computers/macs/mac-operating-systems/
    how-to-use-basic-unix-commands-to-work-in-terminal-on-your-mac
+ Windows: 
    https://commandwindows.com/command3.htm

'''

# VARIABLES
# variables have no declared types!
pi =    3.14				# note no semi-colons, either :)
hello = 'hello, world!'		# string have single or double quotes


# LISTS
# lists (called "arrays") in other languages, are a fundamental
# unit in Python, and you can do a ton of stuff with them
animals = [ 'dog', 'cat', 'fish', 'toad' ]
print len(animals)

# items in lists can be accessed using their index
first_animal =      animals[0]
last_animal =       animals[-1]
first_two_animals = animals[0:2]

# we can also have 2D and many-dimensional lists, though
# in a bit we'll use the numpy library for this instead
tick_tack_toe = [
	[ 'X', 'O', 'X' ],
	[ 'O', ' ', 'X' ],
	[ 'O', 'X', 'O' ]
]


# LOOPS
# we can iterate lists very easily!
plants = [ 'fern', 'tree', 'flower', 'grass' ]
for plant in plants:	# note the colon instead of { }
	print plant 		# and indentation to note nested blocks!

# we can iterate part of a list, too
for plant in plants[0:len(plants)/2]:
	print plant

# you may see example code that uses "list comprehension", where
# a new list is generated by iterating another, all in one line
# it's not the easiest code to read, but there are times when it's
# helpful (or at least to identify)
numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]	# list to work with
doubled = [ (num*2) for num in numbers ]	# new list, all #s doubled
print doubled

# fancier, include an if statement inside!
# (keeps only numbers that are evenly divisible by 2)
even_nums = [ num for num in numbers if num % 2 == 0 ]
print even_nums


# CONDITIONALS
# just like loops, nested code uses colons and indentation
num_pizzas = 3
if num_pizzas < 1:
	print 'no pizzas :('
elif num_pizzas >= 1 and num_pizzas < 10:	# and/or instead of &&/||
	print 'we have *some* pizza'
else:
	print 'we have a LOT of pizza!'


# FUNCTIONS
# like in all languages, functions clean up our code, and make it
# more flexible and re-usable
def print_pi():
	print 3.14159

# functions can also take arguments and return values
def times_two(i):
	i = i * 2
	return i
print times_two(213)

# functions in Python can also have optional arguments that have
# default values already indicated
def mult(i, j, print_info=False):
	k = i * j
	if print_info:
		print 'multiplying ' + str(i) + ' x ' + str(j) + '...'
	return k
mult(2, 3)			# run without the print_info argument
mult(4, 9, True)	# run with the arg set to True

# note that functions must appear above code where they are used!

