#Python workshop - NICAR 2015 Source docs
###Software Miniconda
###Tools SublimeText
Terminal or PowerShell(windows)
###Step 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.
- Click on New -> Notebook
- Type in
print "Hello NICAR!"
- Click the button that looks like a play symbol. (It means "run.")
- Variable assignment: Type
my_string = "We're going to learn Python today at NICAR."
- Run. *Nothing happens! Bc we only assigned.
- Type
print my_string
and run- We're going to learn Python today at NICAR. is printed!
- Type
print len(my_string)
and run. *43 is printed. This is the length of my_string, called by the len() function. - Type
print type(my_string)
and run.- <type 'str'> is returned. The type function tells us my_string is a string object.
- 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.
- Print my_string again. Note that the string did not change to lowercase.
- 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!
- 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.)
- Import libraries/modules
- Always import libraries at the top of your file.
- Assign a
file_name
variable to give the downloaded file a name. - Assign a new
target_file
variable to suck down the csv and name it"banklist.csv"
- 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')
- Line 14 is equivalent to
- Create a variable called csv_data and use the csv module's reader handler to open the file
- 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.
- 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.
- 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