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

In [4]:
print "Hello Data Science"
Hello Data Science

Numbers in python:

Numeric types: int, float, and bool.

In [5]:
type(1)
Out[5]:
int
In [6]:
type(2.5)
Out[6]:
float
In [7]:
type(True)
Out[7]:
bool
In [8]:
type(2+3j)
Out[8]:
complex

String Insertion

Basic string insertions

In [9]:
print "My name is {}".format('Craig')
My name is Craig
In [10]:
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

In [11]:
place = 'NY'
print "{0} is in {1}".format(name, place)
Waldo is in NY

Try reversing order of the variables name and place.|

In [12]:
print "{0} is in {1}".format(place, name)
NY is in Waldo

Basic math in python

Some operators are pretty obvious

In [13]:
5 + 5
Out[13]:
10
In [14]:
3 * 7
Out[14]:
21

But some operators are less intuitive

In [15]:
print "Hello " + 'World'  
Hello World
In [16]:
10 % 4  # modulo  operator(i.e. remainder)
Out[16]:
2
In [17]:
10 ** 2  # exponent
Out[17]:
100
In [18]:
1E3 + 1E-3 # exponent base 10
Out[18]:
1000.001

Assign values to variables

In [19]:
x = 10
y = 5
z = 4

Use math with variables

In [20]:
x - y
Out[20]:
5
In [21]:
x * y
Out[21]:
50

Try dividing x by z

In [22]:
x/z
Out[22]:
2

Try dividing x by 4.0

In [23]:
x/4.0
Out[23]:
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

In [24]:
def simplest_function():
    print "I made a function"

Call function by name

In [25]:
simplest_function()
I made a function

You can create a function that requires an input

In [26]:
def square(x):
    return x**2
In [27]:
square(5)
Out[27]:
25

Line Continuation

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

In [28]:
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

In [29]:
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.

In [30]:
def convert_temp(degrees_c):
    degrees_f = degrees_c * 9.0 / 5.0 + 32.0
    return degrees_f
In [31]:
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.

In [32]:
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
In [33]:
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
In [34]:
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
In [40]:
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
In [36]:
def square2(x):
    print x**2
In [37]:
result = square(5)
print result
25
In [38]:
result = square2(5)
print result
25
None
In [39]:
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'
In [ ]: