Skip to content

Instantly share code, notes, and snippets.

@rebekahmonson
Last active August 29, 2015 14:16
Show Gist options
  • Save rebekahmonson/19ae08d08b63f2a6b014 to your computer and use it in GitHub Desktop.
Save rebekahmonson/19ae08d08b63f2a6b014 to your computer and use it in GitHub Desktop.

#Python workshop - NICAR 2015 Source docs

###Software Miniconda

iPython notebook

###Tools SublimeText

Terminal or PowerShell(windows)

###Step 1

  1. Start iPython notebook in Terminal: ipython notebook
    • What happens: Starts local server at http://localhost:8888/tree which will allow us to edit and run code in the browser, and use some special shortcuts and interactivity features.
  2. Click on New -> Notebook
  3. Type in print "Hello NICAR!"
  4. Click the button that looks like a play symbol. (It means "run.")
  5. Variable assignment: Type my_string = "We're going to learn Python today at NICAR."
  6. Run. *Nothing happens! Bc we only assigned.
  7. Type print my_string and run
    • We're going to learn Python today at NICAR. is printed!
  8. Type print len(my_string) and run. *43 is printed. This is the length of my_string, called by the len() function.
  9. Type print type(my_string) and run.
    • <type 'str'> is returned. The type function tells us my_string is a string object.
  10. Type print my_string.lower() and run.
    • we're going to learn python today at nicar. is printed. You applied the lower function to make everything lower case.
  11. Print my_string again. Note that the string did not change to lowercase.
  12. Run print my_string + my_string
    • We're going to learn Python today at NICAR.We're going to learn Python today at NICAR. is returned. You just concatenated a string!
  13. Run through the same processes with an integer and try common operators.

Convert string to integer

bar = "10" 
bar = int(bar)

Create a list

my_list = ["Wow, we're learning Python.", 10]

Get the first item in my_list. (List index starts at zero.)

print my_list[0]

Concatenate a list

my_list_2 = [11,12]
my_list_3 = my_list + my_list_2
print my_list_3

Result: ["Wow, we're learning Python.", 10, 11, 12]

Error messages when dividing by zero:

a = 10
b = 9
c = 8
d = 0

print a / 2
print b / 3
print c / d

Returns: ZeroDivisionError: integer division or modulo by zero because a number divided by zero is infinite.

if/else:

print a / 2
print b / 3
if d == 0:
    print "You can't do this to me!"
else:
    print c / d

Returns:

5
3
You can't do this to me!

Logical operators and not and or are logical operators

Other Tips:

  • Keep spaces between vars operators and integers for readability.
  • Booleans are capitalized in Python … True, not true … False not false
  • String objects NEVER equal integer objects
  • = is for assignment. == is for comaparisons.
  • White space is very important in Python bc they are interpreted as part of the code. Indents must be 4 spaces.

###Step 2 We're now using the step_2.py file (Follow-along in the completed step 2 file using SublimeText.)

  1. Import libraries/modules
    • Always import libraries at the top of your file.
  2. Assign a file_name variable to give the downloaded file a name.
  3. Assign a new target_file variable to suck down the csv and name it "banklist.csv"
  4. Use the open function with read-only ('rb') access so we can't overwrite it.
    • Line 14 is equivalent to file = open(file_name, 'rb')
  5. Create a variable called csv_data and use the csv module's reader handler to open the file
  6. Loop through the rows in the data.
    • You're implicitly declaring row within the for loop.
    • csv.data automatically stops at the end of the csv file, so you don't need an end state for your loop.
  7. In SublimeText, go to Tools > Build … Your code will run, and all the rows in the file will print to the console at the bottom of the window.
  8. Note that each row is itself a list.

Other Tips

Comments in Python look like this:

# this is a Python note
# And a second line of notes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment