This first session will cover the basics of Python, and introduce elements that will help you get familiar with Python as an interactive computational environment for exploring data. The material is presented in an interactive environment that runs within your web browser, called an IPython Notebook. It allows presentation of text and graphics to be combined with Python code that can be run interactively, with the results appearing inline. We are looking at an IPython notebook now.
Python is an interpreted programming language, also referred to as a high-level language, or as a scripting language. What this means is that when you write some commands, or statements that are meaningful in the Python language, the Python 'interpreter' reads the command, figures out what the intended computation is, and then executes it. This differs from low-level languages like C or C++, in which you generally have to compile code before you can run it, and if you find errors they have to be diagnosed and then the code re-compiled before you can run it. Interpreted languages skip the compile step, and just execute code directly, and if there are errors, they are seen at run-time.
There are several ways to work with Python code:
To begin using an IPython Notebook, you need to launch a command prompt. In Windows, you can click on 'Start' and then type into the text box 'cmd' and press enter, to launch a shell window. On a mac, you can launch the terminal app. Once you have a command prompt, navigate (change directory) to whatever directory you want to work in. If you have a place you want to store IPython notebooks, cd to that location, and type:
jupyter notebook
Once you do this, and the IPython Notebook opens up in your browser, you can either load an existing notebook or begin a new one.
The first programming command demonstrated when you are learning a programming language is usually to make the computer print 'Hello World!'. In Python, doing this is pretty simple:
In [1]:
print('Hello World!')
In [2]:
# this is a comment. the python interpreter ignores it.
# comments are just notes for humans to read to help understand the code
# best practice: add a comment for every couple lines of code to explain what's going on and why
# you'd be amazed at how quickly you forget your code's logic
In [3]:
# add two integers
1 + 1
Out[3]:
In [4]:
# multiply two integers
2 * 3
Out[4]:
In [5]:
# spaces don't matter here, but keep them consistent for readability
2*3
Out[5]:
In [6]:
# divide two integers
10/5
Out[6]:
In [7]:
# raise 2 to the 4th power
2**4
Out[7]:
In [8]:
# take the square root of 9 (by raising it to the power of 0.5)
9 ** (0.5)
Out[8]:
In [9]:
# variables, such as x here, contain values and their values can vary
x = 5
In [10]:
# what is the value of x?
x
Out[10]:
In [11]:
# you can perform operations on variables, just like you can on two numbers
x + 3
Out[11]:
In [12]:
# what is the value of x now?
x
Out[12]:
In [13]:
# to update the value of a variable, you need to do an assignment again
x = x + 3
In [14]:
# and now what is the value of x?
x
Out[14]:
In [15]:
# create a new variable y from an operation on x
x = 5
y = x * 2
y
Out[15]:
In [16]:
# outputting values only displays the last thing output
x
y
Out[16]:
In [17]:
# use print to write some value to the console
print(x)
print(y)
In [18]:
# you can comma-separate values to print multiple to the console on one line
print(x, y)
In [19]:
# you can also print the result of an expression
print(x * y)
In [20]:
# ask ipython for it by using '?'
len?
In [ ]:
# use tab-completion to fill in the rest of statements, functions, methods
prin
In [21]:
# tab-completion also works with variables you have created (ie, a variable in memory)
number_of_students = 10
In [ ]:
numbe
In [22]:
# what about errors? you can't divide by zero...
12/0
For syntax errors or how to do something, Google it! StackOverflow is a particularly good site for code snippets and troubleshooting.
Data in Python is interpreted as having a type. In low-level, compiled languages like C or C++, the programmer has to explicitly declare the type of each variable before actually using it. In Python, the type is inferred at run time, and you can always ask Python what the type of an object is:
In [23]:
# integers are whole numbers
type(125)
Out[23]:
In [24]:
# every variable has a data type, and they can be of any type
x = 125
type(x)
Out[24]:
In [25]:
# float is a floating point (aka decimal) number
some_rate = 4.3
type(some_rate)
Out[25]:
In [26]:
# strings are strings of characters
s = 'abc'
type(s)
Out[26]:
In [27]:
# a list is a collection of elements denoted by square brackets
my_list = [1, 2, 3, 4]
my_list
Out[27]:
In [28]:
type(my_list)
Out[28]:
In [29]:
# a dictionary is a collection of key:value pairs, denoted by curly braces
person = {'first_name':'Geoff', 'last_name':'Boeing'}
person
Out[29]:
In [30]:
type(person)
Out[30]:
In [31]:
# divide 2 integers, and you'll get an integer
num1 = 10
num2 = 5
num1 / num2
Out[31]:
In [32]:
# divide 2 integers
num1 = 8
num2 = 5
num1 / num2
Out[32]:
In [33]:
# we really wanted to be diving whole floating point numbers because we want a decimal result
num1 = 8.0
print(type(num1))
num2 = 5
print(type(num2))
num3 = num1 / num2
print(type(num3))
num3
Out[33]:
In [34]:
# some of the operators we saw earlier work on strings
city = 'Berkeley'
sep = ', '
state = 'California'
zip_code = '94703'
location = city + sep + state + ' ' + zip_code
print(location)
In [35]:
zip_code = 02492
In [36]:
# the zip code 02492 isn't actually a number, it's a string of numeric characters
zip_code = '02492'
In [37]:
# multiplying a string just duplicates it
zip_code * 3
Out[37]:
In [38]:
# you can get the nth element from an iterable object (like a string) with [n] indexing notation
# remember, in Python the index starts with zero not one
print(location[0])
print(location[1])
print(location[2])
In [39]:
# how many characters are in this string?
len(location)
Out[39]:
In [40]:
# get a substring from some position up to but not including a seond position
location[2:5]
Out[40]:
In [41]:
# get the first n characters from the string
location[:5]
Out[41]:
In [42]:
# get the characters from the string after the nth position
location[5:]
Out[42]:
In [43]:
# you can replace characters in a string with the replace() method
location.replace('e', 'E')
Out[43]:
In [44]:
zip_code
Out[44]:
In [45]:
# you can convert between data types
type(zip_code)
Out[45]:
In [46]:
# convert the zip code string to an integer
zip_code = int(zip_code)
zip_code
Out[46]:
In [47]:
type(zip_code)
Out[47]:
In [48]:
# the math works better now
zip_code * 2
Out[48]:
In [49]:
x = 3
print(x * 2)
y = str(x)
print(y * 2)
In [50]:
# the int function won't convert a string that looks like a floating point number
rent_str = '2500.00'
rent_int = int(rent_str)
In [51]:
# but you can daisy chain functions together to conver the string to a float then to an int
rent_int = int(float(rent_str))
rent_int
Out[51]:
In [52]:
# you cannot concatenate a string and a number
city = 'Berkeley '
zip_code = 94703
city + zip_code
In [53]:
# convert the number first, then concatenate
city + str(zip_code)
Out[53]:
In [54]:
my_list = [2, 4, 6, 8]
In [55]:
# how many elements are in this list?
len(my_list)
Out[55]:
In [56]:
# get the zero-th element in a list
my_list[0]
Out[56]:
In [57]:
# you can update elements in a list because it is mutable
my_list[2] = 100
my_list
Out[57]:
In [58]:
# add a new element with the append() method
# lists can hold elements of varying data types
my_list.append('hello')
my_list
Out[58]:
In [59]:
# you can also add lists to concatenate them
[1, 2, 3] + [4, 5, 6]
Out[59]:
In [60]:
antonyms = {'hot':'cold', 'fast':'slow', 'good':'bad'}
antonyms
Out[60]:
In [61]:
# you can access things in a dictionary using its keys
antonyms['hot']
Out[61]:
In [62]:
# you can update values in a dictionary because it is mutable
antonyms['hot'] = 'freezing'
antonyms
Out[62]:
In [63]:
# what are all the keys in this dict?
antonyms.keys()
Out[63]:
In [64]:
# what are all the values in this dict?
antonyms.values()
Out[64]:
As Allen Downey explains in Think Python, the main elements of a program are:
input: Get data from the keyboard, a file, or some other device.
output: Display data on the screen or send data to a file or other device.
math: Perform basic mathematical operations like addition and multiplication.
conditional execution: Check for certain conditions and execute the appropriate code.
repetition: Perform some action repeatedly, usually with some variation.
These are common steps that you will find to be a generic recipe for many programs, whether written in Python or any other language.