Python Fundamentals

© QuantSprout

iPython Notebook Magic

Short cut keys:

  • comment out a line of code = command + /
  • run block of code = shift + enter
  • autocomplete text = tab
  • help for function = shift + tab
  • view shortcut menu - esc then h

Web based viewer for ipython notebooks

http://nbviewer.ipython.org/

Write your first python program

print "Hello Data Science"
Hello Data Science

Numbers in python:

Numeric types: int, float, and bool.

type(1)
int
type(2.5)
float
type(True)
bool
type(2+3j)
complex

String Insertion

Basic string insertions

print "My name is {}".format('Craig')
My name is Craig
name = "Waldo"
print "Where in the world is {}".format(name)
Where in the world is Waldo

You can insert multiple values into strings but requires labels

place = 'NY'
print "{0} is in {1}".format(name, place)
Waldo is in NY

Try reversing order of the variables name and place.|

print "{0} is in {1}".format(place, name)
NY is in Waldo

Basic math in python

Some operators are pretty obvious

5 + 5
10
3 * 7
21

But some operators are less intuitive

print "Hello " + 'World'  
Hello World
10 % 4  # modulo  operator(i.e. remainder)
2
10 ** 2  # exponent
100
1E3 + 1E-3 # exponent base 10
1000.001

Assign values to variables

x = 10
y = 5
z = 4

Use math with variables

x - y
5
x * y
50

Try dividing x by z

x/z
2

Try dividing x by 4.0

x/4.0
2.5

Functions

Syntax requires 4 things:

  • prefix of 'def'
  • name for function
  • parentheses followed by a colon
  • indented code on the next line

Create a simple function

def simplest_function():
    print "I made a function"

Call function by name

simplest_function()
I made a function

You can create a function that requires an input

def square(x):
    return x**2
square(5)
25

Line Continuation

Python recognizes that code lines that end with a comma are incomplete

numbers = [1, 2, 3,
           4, 5, 6,
           7, 8, 9]
print numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9]

You can also use the backslash

long_string = 'This is a really, really, really '\
              'long sentence'
print long_string
This is a really, really, really long sentence

Exercise: Build a Function

1. Create a function that converts Celsius to Fahrenheit. Results should be accurate to at least one decimal point.

def convert_temp(degrees_c):
    degrees_f = degrees_c * 9.0 / 5.0 + 32.0
    return degrees_f
print convert_temp(0)
print convert_temp(100)
print convert_temp(30)
32.0
212.0
86.0

2. Update your function to return a sentence (string type) with the Celsius and Fahrenheit values inserted into the string.

def convert_temp_message(degrees_c):
    degrees_f = degrees_c * 9.0 / 5.0 + 32.0
    message = "{0} degrees Celsius = {1} degrees Fahrenheit".format(degrees_c, degrees_f)
    return message
print convert_temp_message(0)
print convert_temp_message(100)
print convert_temp_message(30)
0 degrees Celsius = 32.0 degrees Fahrenheit
100 degrees Celsius = 212.0 degrees Fahrenheit
30 degrees Celsius = 86.0 degrees Fahrenheit
def convert_temp_message2(degrees_c):
    degrees_f = convert_temp(degrees_c)
    message = "{0} degrees Celsius = {1} degrees Fahrenheit".format(degrees_c, degrees_f)
    return message
degrees_f = 1000
print convert_temp_message2(0)
print convert_temp_message2(100)
print convert_temp_message2(30)
print degrees_f

msg = convert_temp_message2(100)
print msg
0 degrees Celsius = 32.0 degrees Fahrenheit
100 degrees Celsius = 212.0 degrees Fahrenheit
30 degrees Celsius = 86.0 degrees Fahrenheit
1000
100 degrees Celsius = 212.0 degrees Fahrenheit
def square2(x):
    print x**2
result = square(5)
print result
25
result = square2(5)
print result
25
None
print convert_temp_message2('cat')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-62062733500c> in <module>()
----> 1 print convert_temp_message2('cat')

<ipython-input-34-9016118976ab> in convert_temp_message2(degrees_c)
      1 def convert_temp_message2(degrees_c):
----> 2     degrees_f = convert_temp(degrees_c)
      3     message = "{0} degrees Celsius = {1} degrees Fahrenheit".format(degrees_c, degrees_f)
      4     return message

<ipython-input-30-a7b254e0dfb1> in convert_temp(degrees_c)
      1 def convert_temp(degrees_c):
----> 2     degrees_f = degrees_c * 9.0 / 5.0 + 32.0
      3     return degrees_f

TypeError: can't multiply sequence by non-int of type 'float'