<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Python Basics
This material was heavily influenced by (stolen from) the UC Berkeley and NASA GSFC Python Bootcamps.
Getting Python to talk back
Step1:  Python as a calculator
There are four distinct numeric types in Python
Step2:  Defining and using variables
Step3:  Some more mathy operators
Step4:  Comparisons
Step5:  Truthiness
Step6:  0j == False
Step7:  More on variables and types
Step8:  Built-in types in python
Step9:  Backslashes ( \ ) start special (escape) characters
Step10:  <img src="https
Step11:  Strings are immutable (unlike in C), so you cannot change a string in place.
Step12:  Running a Program from Command Line
This is a good of place as any to introduce the show that you can run you don't HAVE to use the notebook.
Everyone should have a file called temperature.py, open the file in your text editor (emacs, vim, notepad) of choice at look at its content. 
You will see it the same code as from the previous example. Let's try running it with the execfile command.
Step13:  With python as in other languages you can separate scripts and programs containing python code that can be used independently or with other python programs.  The notebook is excellent for code development and presentations but not the best for production level code.  
Loops and branches in python
Python has the usual control flow statements
Step14:  IPython Notebook automatically converts tabs into spaces, but some programs do not. Be careful not to mix these up! Be consistent in your programming.
If you're working within the Python interpreter (not the IPython Notebook), you'll see this
<ASSISTANT_TASK:>
Python Code:
print "Hello World!"
print 'Hello World!'
# This is a comment.
print 'This is not a comment.'
'Something smells funny.'
Explanation: Python Basics
This material was heavily influenced by (stolen from) the UC Berkeley and NASA GSFC Python Bootcamps.
Getting Python to talk back
End of explanation
print 2 + 2
# Spaces between characters don't matter
print 2+2
2 + 2
print "2 + 2"
print 2.1 + 2            # The most precise value is a float.
(3.*10. - 26.)/5.
(3*10 - 26)/5.0
# Since our most precise value is an int, python spits out the solution as an int
(3*10 - 26)/5
# Rounding errors can creep in
2.1 + 2 == 4.0999999999999996      # two 'equals' signs asks whether something is equal
complex(1,2)
# note that python uses j to denote the imaginary part
1+2j
a = 1+2j
print a.real, a.imag
1+2j-2j
Explanation: Python as a calculator
There are four distinct numeric types in Python:
   - int (32 bit, equivalent to long in C)
   - long (unlimited precision)
   - float (equivalent to double in C)
   - complex (real + imaginary parts, both floats)
End of explanation
t = 1.0                       # declare a variable t (time)
accel = 9.8                   # acceleration in units of m/s^2
dist = 0.5*accel*t*t          # distance traveled in time t seconds is 1/2*a*t^2
print dist                    # this is the distance in meters
dist1 = accel*(t**2)/2        # note: t^2 means something very different!
print dist1
dist2 = 0.5*accel*pow(t,2)
print dist2
Explanation: Defining and using variables
End of explanation
# Integer division prints the floor; i.e., it only takes the integer digits
print 6/5
# modulo operator
6 % 5
# bitwise operators: shift left
# 1 in binary is '1', shifting left by two bits gives '100' = 4
1 << 2
# bitwise operators: shift right
# 5 in binary is '101', shifting right by one bit gives '10' = 2
5 >> 1
x = 2 ; y = 3        # multiple commands on the same line, separated by a semicolon
x | y                # bitwise OR
                     # x in binary is '10', y in binary is '11', x | y is '11' -> 3
x ^ y                # exclusive OR ('10' ^ '11' = '01' = 1)
x & y                # bitwise AND ('10' & '11' = '10' = 2)
x = x ^ y ; print x   # x has been reassigned
x += 3 ; print x      # 'x += 3' is the same as saying 'x = x+3'
                      # the equivalent holds from -, *, /
Explanation: Some more mathy operators
End of explanation
a = 3 ; b = 4
a == b                # two '=' signs for comparison, one '=' for assignment
a+1 == b
a+1.0 == b
a < 10
a < 3
a <= 3
a < (10 + 2j)
a < -2.0
a != 3.1415
Explanation: Comparisons
End of explanation
0 == False       # False is equivalent to 0, and other things
1 == True        # True is equivalent to 1, and other things
not False
1 == False
Explanation: Truthiness
End of explanation
not (10.0 - 10.0)
x = None         # None is neither True nor False
print None == False
print None == True
type(None)
Explanation: 0j == False
End of explanation
print type(1)
print type("1")
x = 2 ; type(x)
type(2) == type(1)
type(False)
type(type(1))
type(pow)
isinstance(1,int)        # check if something is a certain type
isinstance("spam",str)
isinstance(1.212,int)
Explanation: More on variables and types
End of explanation
x = "spam" ; print type(x)
print "Hello!\nI'm hungry."       # '\n' means 'new line'
# This doesn't work.
print "Hello! 
I'm hungry."
print "Hello! \n I'm hungry."
"Wah?!" == "Wah?!"
print "'Wah?!' said the student."
print ""Wah?!" said the student."
print '"Wah?!" said the student.'
print "\"Wah?!\" said the student."
Explanation: Built-in types in python: int, bool, str, float, complex, long ...
See https://docs.python.org/2/library/stdtypes.html
Strings
Strings are sequences of characters.
* They can be indexed and sliced up as if they were an array (we'll talk more about arrays later).
* You can glue strings together with + signs.
Strings can be formatted and compared.
End of explanation
# Raw strings don't recognize escape characters
print r'This is a raw string ... newlines \n are ignored. So are returns \r and tabs \t.'
# Triple quotes are useful for multiple line strings
y = '''Four score and seven minutes ago,
    you folks all learned some basic mathy stuff with Python
    and boy were you blown away!'''
print y
# Prepending 'u' makes a string "unicode"
print u"\N{BLACK HEART SUIT}"
# You can concatenate strings with the '+' sign
s = "spam" ; e = "eggs"
print s + e
print s + " and " + e
print "green " + e + " and " + s
# You can do multiple concatenations with the '*' sign
print s*3 + e
print "*"*50
# Strings can be compared
print "spam" == "good"
# s comes before z in the alphabet
"spam" < "zoo"
# 's' comes before 'spam' in the dictionary
"s" < "spam"
# 'spaa' comes before 'spam' alphabetically
"spaaaaaaaaaaaam" < "spam"
print 'I want ' + 3 + ' eggs and no ' + s
# We have to cast the 3 (an int) as a string
print 'I want ' + str(3) + ' eggs and no ' + s
pi = 3.14159     # There are easier ways to call pi, which we'll see later
print 'I want ' + str(pi) + ' eggs and no ' + s
print str(True) + ':' + ' I want ' + str(pi) + ' eggs and no ' + s
print s
print len(s)               # len() tells you the length of a string (or, more generally, an array)
print len("eggs\n")        # The newline \n counts as ONE character
print len("")              # empty string
# Strings act like arrays. We'll see more about arrays later.
s = "SPAM"
print s
print s[0]               # Python uses zero-based indexing; i.e., it starts counting at zero
print s[1]
print s[-1]
print s[-2]
# Take slices of strings
print s
print s[0:1]
print s[1:4]
print s[0:100]            # Python doesn't warn you. Be careful!
Explanation: Backslashes ( \ ) start special (escape) characters:
\n = newline
\r = return
\t = tab
\a = bell
String literals are defined with double (") or single quotes ('). <br>
The outermost type of quotation mark cannot be used inside a string -- UNLESS it's escaped with a backslash.
See http://docs.python.org/reference/lexical_analysis.html#string-literals
End of explanation
# Slice counting backwards
print s
print s[-3:-1]
# You don't have to specify both ends
print s
print s[:2]
print s[2:]
# You can slice in different steps
print s
print s[::2]
print s[::-1]
Explanation: <img src="https://raw.github.com/profjsb/python-bootcamp/master/Lectures/01_BasicTraining/spam.png">
End of explanation
mygrade = 'F+'
print mygrade
mygrade[0] = 'A'
mygrade = 'A+'
print mygrade
# Ask for user input
faren = raw_input("Enter the temperature (in Fahrenheit): ")
print "Your temperature is " + faren + " degrees."
# User input is always saved as a string
faren = raw_input("Enter the temperature in Fahrenheit): ")
cel = 5./9. * (faren - 32.)
print "The temperature in Celcius is " + cel + " degrees."
# Don't forget to convert things to the right type
faren = raw_input("Enter the temperature in Fahrenheit): ")
faren = float(faren)             # The calculation on the right gets saved to the variable on the left
cel = 5./9. * (faren - 32.)
print "The temperature in Celcius is " + str(cel) + " degrees."
x = raw_input("Enter x: ")
x = float(x)
print x
Explanation: Strings are immutable (unlike in C), so you cannot change a string in place.
End of explanation
execfile("temperature.py")
Explanation: Running a Program from Command Line
This is a good of place as any to introduce the show that you can run you don't HAVE to use the notebook.
Everyone should have a file called temperature.py, open the file in your text editor (emacs, vim, notepad) of choice at look at its content. 
You will see it the same code as from the previous example. Let's try running it with the execfile command.
End of explanation
x = 1
    print x
x = 1
if x > 0:               # colons indicate the beginning of a control statement
    print "yo"
else:                   # unindenting tells Python to move to the next case
    print "dude"
print "ok"              # unindenting also tells Python the control statement is done
Explanation: With python as in other languages you can separate scripts and programs containing python code that can be used independently or with other python programs.  The notebook is excellent for code development and presentations but not the best for production level code.  
Loops and branches in python
Python has the usual control flow statements:
- if, else, elif
- for loops
- while loops
- break, continue, pass
Indentation in Python defines where blocks begin and end.
End of explanation
# You can mix indentations between different blocks ... but this is ugly and people will judge you
x = 1
if x > 0:
    print "yo"
else:
    print "dude"
    print "dude"
# You can put everything on one line
print "yo" if x > 0 else "dude"
# Multiple cases
x = -100
if x < -10:
    print "yo"
elif x > 10:             # 'elif' is short for 'else if'
    print "dude"
else:
    print "sup"
for x in range(5,50,10):
    print x**2
for x in ("all","we","wanna","do","is","eat","your","brains"):
    print x
x = 0
while x < 5:
    print pow(2,x)
    x += 1             # don't forget to increment x!
# Multiple levels
for x in range(1,10):
    if x % 2 == 0:
        print str(x) + " is even."
    else:
        print str(x) + " is odd."
# Blocks cannot be empty
x = "fried goldfish"
if x == "spam for dinner":
    print "I will destroy the universe"
else:
    # Nothing here.
# Use a 'pass' statement, which indicates 'do nothing'
x = "fried goldfish"
if x == "spam for dinner":
    print "I will destroy the universe"
else:
    pass
# Use a 'break' statement to escape a loop
x = 0
while True:
    print x**2
    if x**2 >= 100:
        break
    x +=1
Explanation: IPython Notebook automatically converts tabs into spaces, but some programs do not. Be careful not to mix these up! Be consistent in your programming.
If you're working within the Python interpreter (not the IPython Notebook), you'll see this:
&gt;&gt;&gt; x = 1
&gt;&gt;&gt; if x &gt; 0:
...     print "yo"
... else:
...     print "dude"
... print "ok"
...
yo
ok
End of explanation
<END_TASK>