Session 1: Introduction to Python and Data Types

The IPython Notebook

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.

Python Environments

There are several ways to work with Python code:

  1. Starting Python at the command line, by typing 'python' at the command prompt
  2. Running a Python script file (.py)
  3. The way we will generally interact with Python is through IPython Notebooks, a handy interface that runs in your web browser. This is the environment you are looking at now, with a mixture of headings, text, and code embedded in an IPython Notebook.

Starting an IPython Notebook

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!')


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

Math in Python


In [3]:
# add two integers
1 + 1


Out[3]:
2

In [4]:
# multiply two integers
2 * 3


Out[4]:
6

In [5]:
# spaces don't matter here, but keep them consistent for readability
2*3


Out[5]:
6

In [6]:
# divide two integers
10/5


Out[6]:
2.0

In [7]:
# raise 2 to the 4th power
2**4


Out[7]:
16

In [8]:
# take the square root of 9 (by raising it to the power of 0.5)
9 ** (0.5)


Out[8]:
3.0

Working with variables


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]:
5

In [11]:
# you can perform operations on variables, just like you can on two numbers
x + 3


Out[11]:
8

In [12]:
# what is the value of x now?
x


Out[12]:
5

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]:
8

In [15]:
# create a new variable y from an operation on x
x = 5
y = x * 2
y


Out[15]:
10

In [16]:
# outputting values only displays the last thing output
x
y


Out[16]:
10

In [17]:
# use print to write some value to the console
print(x)
print(y)


5
10

In [18]:
# you can comma-separate values to print multiple to the console on one line
print(x, y)


5 10

In [19]:
# you can also print the result of an expression
print(x * y)


50

Getting help


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


---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-22-e838db9e68ea> in <module>()
      1 # what about errors? you can't divide by zero...
----> 2 12/0

ZeroDivisionError: division by zero

For syntax errors or how to do something, Google it! StackOverflow is a particularly good site for code snippets and troubleshooting.

Basic data types

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]:
int

In [24]:
# every variable has a data type, and they can be of any type
x = 125
type(x)


Out[24]:
int

In [25]:
# float is a floating point (aka decimal) number
some_rate = 4.3
type(some_rate)


Out[25]:
float

In [26]:
# strings are strings of characters
s = 'abc'
type(s)


Out[26]:
str

In [27]:
# a list is a collection of elements denoted by square brackets
my_list = [1, 2, 3, 4]
my_list


Out[27]:
[1, 2, 3, 4]

In [28]:
type(my_list)


Out[28]:
list

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]:
{'first_name': 'Geoff', 'last_name': 'Boeing'}

In [30]:
type(person)


Out[30]:
dict

Working with data types


In [31]:
# divide 2 integers, and you'll get an integer
num1 = 10
num2 = 5
num1 / num2


Out[31]:
2.0

In [32]:
# divide 2 integers
num1 = 8
num2 = 5
num1 / num2


Out[32]:
1.6

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


<class 'float'>
<class 'int'>
<class 'float'>
Out[33]:
1.6

Strings


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)


Berkeley, California 94703

In [35]:
zip_code = 02492


  File "<ipython-input-35-af35510fb7ab>", line 1
    zip_code = 02492
                   ^
SyntaxError: invalid token

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]:
'024920249202492'

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])


B
e
r

In [39]:
# how many characters are in this string?
len(location)


Out[39]:
26

In [40]:
# get a substring from some position up to but not including a seond position
location[2:5]


Out[40]:
'rke'

In [41]:
# get the first n characters from the string
location[:5]


Out[41]:
'Berke'

In [42]:
# get the characters from the string after the nth position
location[5:]


Out[42]:
'ley, California 94703'

In [43]:
# you can replace characters in a string with the replace() method
location.replace('e', 'E')


Out[43]:
'BErkElEy, California 94703'

Converting between types


In [44]:
zip_code


Out[44]:
'02492'

In [45]:
# you can convert between data types
type(zip_code)


Out[45]:
str

In [46]:
# convert the zip code string to an integer
zip_code = int(zip_code)
zip_code


Out[46]:
2492

In [47]:
type(zip_code)


Out[47]:
int

In [48]:
# the math works better now
zip_code * 2


Out[48]:
4984

In [49]:
x = 3
print(x * 2)
y = str(x)
print(y * 2)


6
33

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)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-50-9cecf6941b9a> in <module>()
      1 # the int function won't convert a string that looks like a floating point number
      2 rent_str = '2500.00'
----> 3 rent_int = int(rent_str)

ValueError: invalid literal for int() with base 10: '2500.00'

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]:
2500

In [52]:
# you cannot concatenate a string and a number
city = 'Berkeley '
zip_code = 94703
city + zip_code


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-52-a3898a3d1189> in <module>()
      2 city = 'Berkeley '
      3 zip_code = 94703
----> 4 city + zip_code

TypeError: Can't convert 'int' object to str implicitly

In [53]:
# convert the number first, then concatenate
city + str(zip_code)


Out[53]:
'Berkeley 94703'

Lists


In [54]:
my_list = [2, 4, 6, 8]

In [55]:
# how many elements are in this list?
len(my_list)


Out[55]:
4

In [56]:
# get the zero-th element in a list
my_list[0]


Out[56]:
2

In [57]:
# you can update elements in a list because it is mutable
my_list[2] = 100
my_list


Out[57]:
[2, 4, 100, 8]

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]:
[2, 4, 100, 8, 'hello']

In [59]:
# you can also add lists to concatenate them
[1, 2, 3] + [4, 5, 6]


Out[59]:
[1, 2, 3, 4, 5, 6]

Dictionaries


In [60]:
antonyms = {'hot':'cold', 'fast':'slow', 'good':'bad'}
antonyms


Out[60]:
{'fast': 'slow', 'good': 'bad', 'hot': 'cold'}

In [61]:
# you can access things in a dictionary using its keys
antonyms['hot']


Out[61]:
'cold'

In [62]:
# you can update values in a dictionary because it is mutable
antonyms['hot'] = 'freezing'
antonyms


Out[62]:
{'fast': 'slow', 'good': 'bad', 'hot': 'freezing'}

In [63]:
# what are all the keys in this dict?
antonyms.keys()


Out[63]:
dict_keys(['hot', 'good', 'fast'])

In [64]:
# what are all the values in this dict?
antonyms.values()


Out[64]:
dict_values(['freezing', 'bad', 'slow'])

What is a program?

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.