A programing language is a bundle of keywords, syntax and rules that can specify instructions to create a desired output using a computer: think about it like a cooking recipe of sorts, rather than a human language.
Python is one type of programming languages (others include Perl, javascript, C, Java). It doesn't need to be compiled into machine code, it emphasizes readability and is often less wordy than other languages. Python is good for visualizing data, scraping websites, and building software.
To start writing code, you just need a text editor. For today, most of the work will be done in this jupyter notebook.
You can find the official documentation here: https://docs.python.org/2/
There are a variety of NICAR Python-related tipsheets here: https://ire.org/resource-center/tipsheets/?q=python
StackOverflow is a great place to get help with Python and other programming languages: https://stackoverflow.com/
On your machine, open up the command line: Start --> All programs --> Accessories --> Command Prompt (Windows) or the Terminal (Mac). The command line is how you can send commands to your computer to do various things. This is where we'll run a sample Python script we've included called sample.py
.
First, we'll go to the correct directory: cd Desktop/nicar-2018-python
Then, we'll run the script: python sample.py
Jupyter Notebook is a relatively easy way to test, comment and display Python, R and Julia code. We're going to use it today to demonstrate some Python basics.
In your command line, you should already be in this class' directory. If you're not, type cd Desktop/nicar-2018-python
to get there. Then type jupyter notebook
to open up a local notebook. This should open up a tab in a web browser and from there, you can click on nicar_python_class.ipynb
to get to this notebook.
Hitting shift + enter will run a code block.
Python can be used as a really powerful calculator. Use the command line (type python
to enter a python shell), this notebook or IDLE to do some math:
# This is a comment. Any line without a # before it is code.
# Remember, hit shift + enter to run this code block and get an answer.
6 + 4
10
749 / 24
31.208333333333332
# You don't have to worry about PEMDAS! Yay!
67 * 93 + 32 / 8
6235.0
# % is a symbol that indicates remainder after division.
6 % 4
2
The Python interpreter reads spaces and certain words in order to understand what to do. So, indentation matters a lot and some words are out-of-bounds to use as variables: def, import, from, in, while, for, if, elif, else, print, input, and many more.
Packages are typically specified to a certain function and allow you to use Python to do different things. Some examples:
pandas for data analysis
beautifulsoup for scraping
regex for regular expressions and matching
numpy for advanced math
# This is how you install a package.
import pandas
# If you only want to import a specific capability from a package, do it like this:
from package import module
# So, here's an example:
from datetime import timedelta
# This is a simple script with variables and printing.
# Replace the 'name' input with your name and hit shift + enter.
welcome = 'Hi, '
name = 'friend.'
print (welcome + name)
Hi, friend.
# Variables are names assigned to a value, string or set of values or strings.
# You should try to be specific in how you name your variables, so the person reading them can get
# a sense of what is stored within them.
# You're doing a story on Chicago's far north community areas.
# Which variable names are better?
communityareas = ['Rogers Park', 'Edgewater', 'Uptown', 'Forest Glen']
numbers = [1, 77, 3, 12]
for q, a in zip(communityareas, numbers):
print('What number is {0}? It is {1}.'.format(q, a))
foo = ['Rogers Park', 'Edgewater', 'Uptown', 'Forest Glen']
bar = [1, 77, 3, 12]
for q, a in zip(foo, bar):
print('What number is {0}? It is {1}.'.format(q, a))
What number is Rogers Park? It is 1. What number is Edgewater? It is 77. What number is Uptown? It is 3. What number is Forest Glen? It is 12. What number is Rogers Park? It is 1. What number is Edgewater? It is 77. What number is Uptown? It is 3. What number is Forest Glen? It is 12.
# Types: integers, floats, strings, duples
# Naming conventions exist for having variables of a certain type. For example, booleans (which can be true or false)
# are often named with an "if" in front of them.
# Uncomment each of the lines, one by one, and hit shift + enter to see what kind of value is assigned to each variable.
counter = 100
miles = 1000.0
name = "100"
print (type(counter))
print (type(miles))
print (type(name))
<class 'int'> <class 'float'> <class 'str'>
# Functions
# What do you think this function does?
def greet(name):
print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
What's your name?
# While loop: "while a certain condition is met, do a certain thing."
time = 5
while time > 0:
print (time)
time = time - 1
print("Blastoff!")
# FUN FACT: This can create an infinite loop that never stops running until your computer hypothetically explodes*.
# *This is an exaggeration. Still pretty cool.
5 4 3 2 1 Blastoff!
# For loop: "for each of these things, run the script in the indented area."
languages = ['Python', 'R', 'JavaScript', 'Ruby']
for language in languages:
print (language + " is a programming language.")
Python is a programming language. R is a programming language. JavaScript is a programming language. Ruby is a programming language.
# If, elif, else: "if a certain condition is met, do a certain thing; if a certain other condition is met, do something
# else; or, last resort, do a certain other thing."
chicago_streets = ['wacker', 'lasalle', 'state', 'michigan', '95th', 'garfield', 'midway plaisance', 'broadway', 'devon', 'cicero', 'kedzie', 'fullerton', 'oak']
counter = 0
for street in chicago_streets:
if street == 'devon':
print (street)
elif 'th' in street:
print (street)
else:
print (counter)
counter = counter + 1
0 1 2 3 95th 4 5 6 devon 7 8 9 10
# Write your own loop!
# Your counter is at 0.
# You have a list of sources. (given)
# For each source in the list,
# print "thank you"
# and add one to the counter.
# When the loop is complete, print the counter.
counter = 0
sources = ['Emanuel', 'Duckworth', 'Pritzker', 'Preckwinkle']
for source in sources:
print ("Thank you!")
counter = counter + 1
print (counter)
Thank you! Thank you! Thank you! Thank you! 4
# Lists
list = [ 'ct', 606 , 3.14, 'juan', 20.1 ]
tinylist = [000, 'jamila']
print (list) # Prints complete list
print (list[1:3]) # Prints elements starting from 2nd till 3rd
print (list[2:]) # Prints elements starting from 3rd element
print (tinylist * 2) # Prints list two times
print (list + tinylist) # Prints concatenated lists
['ct', 606, 3.14, 'juan', 20.1] [606, 3.14] [3.14, 'juan', 20.1] [0, 'jamila', 0, 'jamila'] ['ct', 606, 3.14, 'juan', 20.1, 0, 'jamila']
# Dicts
phone_numbers = {'Kori': 4098, 'Chad': 4139, 'Ryan': 4265}
print(phone_numbers['Chad'])
print(phone_numbers.keys())
print('Cecilia' in phone_numbers)
4139 dict_keys(['Kori', 'Chad', 'Ryan']) False
# Indentation error
foods = ['pizza', 'naan', 'tacos']
for x in foods:
print(x)
# "print(x)" should be indented because it's within a for block.
File "<ipython-input-41-187910de3fc4>", line 5 print(x) ^ IndentationError: expected an indented block
# Syntax error
foods = ['pizza', 'naan', 'tacos']
for x in foods
print(x)
# There should be a colon after "for x in foods" because syntax.
File "<ipython-input-43-02bd3875beeb>", line 4 for x in foods ^ SyntaxError: invalid syntax
# Scope errors
foods = ['pizza', 'naan', 'tacos']
def print_foods():
foods = foods + ['paczki']
print (foods)
print_foods()
# The variable "foods" should be defined within the function "print_foods".
# Type errors
is_this_right = "Probably not"+1
# You can't concatenate a string and an integer.
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-5-6f18fc78ba51> in <module>() 1 # Type errors 2 ----> 3 is_this_right = "Probably not"+1 TypeError: cannot concatenate 'str' and 'int' objects