## Python Fundamentals¶

### 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 :
print "Hello Data Science"

Hello Data Science


### Numbers in python:¶

Numeric types: int, float, and bool.

In :
type(1)

Out:
int
In :
type(2.5)

Out:
float
In :
type(True)

Out:
bool
In :
type(2+3j)

Out:
complex

### String Insertion¶

Basic string insertions

In :
print "My name is {}".format('Craig')

My name is Craig

In :
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 :
place = 'NY'
print "{0} is in {1}".format(name, place)

Waldo is in NY


Try reversing order of the variables name and place.|

In :
print "{0} is in {1}".format(place, name)

NY is in Waldo


### Basic math in python¶

Some operators are pretty obvious

In :
5 + 5

Out:
10
In :
3 * 7

Out:
21

But some operators are less intuitive

In :
print "Hello " + 'World'

Hello World

In :
10 % 4  # modulo  operator(i.e. remainder)

Out:
2
In :
10 ** 2  # exponent

Out:
100
In :
1E3 + 1E-3 # exponent base 10

Out:
1000.001

Assign values to variables

In :
x = 10
y = 5
z = 4


Use math with variables

In :
x - y

Out:
5
In :
x * y

Out:
50

Try dividing x by z

In :
x/z

Out:
2

Try dividing x by 4.0

In :
x/4.0

Out:
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 :
def simplest_function():


Call function by name

In :
simplest_function()

I made a function


You can create a function that requires an input

In :
def square(x):
return x**2

In :
square(5)

Out:
25

### Line Continuation¶

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

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

In :
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 :
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 :
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 :
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 :
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 :
def square2(x):
print x**2

In :
result = square(5)
print result

25

In :
result = square2(5)
print result

25
None

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