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

 

Numbers in python:

Numeric types: int, float, and bool.

 
 
 
 

String Insertion

Basic string insertions

 
 

You can insert multiple values into strings but requires labels

 

Try reversing order of the variables name and place.|

 

Basic math in python

Some operators are pretty obvious

 
 

But some operators are less intuitive

 
 
 
 

Assign values to variables

 

Use math with variables

 
 

Try dividing x by z

 

Try dividing x by 4.0

 

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

 

Call function by name

 

You can create a function that requires an input and returns a result

 

Line Continuation

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

 

You can also use the backslash

 

Exercise: Build a Function

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

 

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