Variables in Python

[ back to top ]

A variable is a name used to reference a value for later use. Think of a variable as a name pointing to some object. Assigning a value to a variables in Python is done with the assignment operator (=). For example, the following line assigns the value 2 to the the variable x.

In [7]:
x = 2
In [8]:
# Comments begin with the pound sign and continue to the end of the line
# The built-in print function will display a value on the screen
print(x)
2

Assigning variables to values is the primary way that data is stored and manipulated in Python code.

Variable names must start with a letter and can contain letters, numbers, and the underscore character ( _ ). The usual convention for variable names is to use lowercase letters with underscores to seperate words (e.g. my_favorite_number = 2). In addition, there are a number of keywords in Python that are used by the interpreter. So you'll want to avoid using these keywords as variable names. Namely,

False, None, True, and, as, assert, break, class, continue, def, del, elif, else,
except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or,
pass, raise, return, try, while, with, yield

are keywords to avoid. Now that we know variables are names that reference a value, let's look at some of the types of values that we have in Python.

Survey of some built-in types

[ back to top ]

Every value in Python has a type associated with it (NOTE: If you ever want to know the type of something, you can use the built-in type function)

In [9]:
type(x)
Out[9]:
int

Different types have different allowed operations and functionality. For example, as we'll discuss later, the addition operator is defined for integer types

In [10]:
a = 1
b = 2
print(type(a))
print(type(b))
# Use addition operator between two integers
print(a + b)
<type 'int'>
<type 'int'>
3

and an uppercase method is defined for string types

In [11]:
c = 'Madison, WI'
print(type(c))
# Use the string uppercase method to make every character uppercase
print(c.upper())
<type 'str'>
MADISON, WI

The most-commonly used built-in types in Python can be grouped into two categories: simple and compound.

Simple types

[ back to top ]

The "simple" types consist of integers (int), floating-point numbers (float), complex numbers (complex), boolean values (bool), and the None type (NoneType).

Integers

Integers represent whole numbers ( ...,-2, -1, 0, 1, 2,...). Numbers without a decimal point or exponential notation produce integers.

In [12]:
a = 2
print(type(a))
<type 'int'>

Floating-point numbers

Floating-point numbers (often called "floats") represent real numbers. Number containing a decimal point or exponential notation are used to define floating-point values.

In [13]:
b = 1.5
print(type(b))
<type 'float'>
In [14]:
b.as_integer_ratio()
Out[14]:
(3, 2)

Exponential notation (e or E) is shorthand for scientific notation. E.g. 7e3 = $7 \times 10^3$

In [15]:
c = 7e3
print(c)
print(type(c))
7000.0
<type 'float'>

Complex numbers

A complex number can be created by including a 'j' or 'J' in a number. The corresponding real and imaginary parts of the complex number can be accessed using real and imag attributes.

In [16]:
z = 7 + 4.3j
print(type(z))
<type 'complex'>
In [17]:
z.real
Out[17]:
7.0
In [18]:
z.imag
Out[18]:
4.3

Note that the real and imaginary parts for the complex type are floating-point numbers—regardless of whether or not there is a decimal point.

In [19]:
print(type(z.real))
<type 'float'>

Booleans

Booleans can take on one of two possible values: True or False. Booleans will be utilized later when we discuss the conditional statements in Python.

In [20]:
n = True
print(type(n))
<type 'bool'>
In [21]:
p = False
print(type(p))
<type 'bool'>

Note that bool values are case sensitive—the first letter needs to be capitalized.

None type

The NoneType type represents just a single value: None. None is commonly used to represent un-initialized values. In addition, functions that don't have an explicit return value will implicity return None.

In [22]:
z = None
print(type(z))
<type 'NoneType'>

Compound types

[ back to top ]

In addition to int, float, complex, bool, and NoneType, Python also has several built-in data structures that are used as containers for other types. These "compound" types consist of lists (list), tuples (tuple), strings (str), sets (set), and dictionaries (dict).

Lists

A list is a ordered, mutable collection of data (data elements are called "items"). We'll discuss mutable vs. immutable objects momentarily. Lists are constructed using square brackets with list items seperated by commas.

In [23]:
d = [2.3, 5, -43, 74.7, 5]
print(type(d))
<type 'list'>
In [24]:
print(d)
[2.3, 5, -43, 74.7, 5]

Lists have lots of built-in functionality. For example, You can use the built-in len function to get the number of items in a list

In [25]:
len(d)
Out[25]:
5

The list append method can be used to add elements to a list

In [26]:
d.append(3.1415)
print(d)
[2.3, 5, -43, 74.7, 5, 3.1415]

The list sort method will sort the items in a list into ascending order

In [27]:
d.sort()
print(d)
[-43, 2.3, 3.1415, 5, 5, 74.7]

The list reverse method will reserve the order of a list

In [28]:
d.reverse()
print(d)
[74.7, 5, 5, 3.1415, 2.3, -43]

The list count method counts the number of times an item occurs in a list

In [29]:
# Counts how many times the item 5 occurs in the list d
d.count(5)
Out[29]:
2

Note that a list can contain any type of object. The items in a list need not be homogeneous. For example,

In [30]:
crazy_list = [1, False, 23.11, [1, 2, 3], None]
print(crazy_list)
[1, False, 23.11, [1, 2, 3], None]

The items in a list can be accessed using list indexing. Indexing a list consists of adding the item index in square brackets after a list. It's also important to note that in Python list indices begin with zero. So the first item in a list has the index 0, the second item has the index 1, and so on. For example

In [31]:
d = [2.3, 5, -43, 74.7, 5]
In [32]:
d[0]
Out[32]:
2.3
In [33]:
d[1]
Out[33]:
5
In [34]:
d[2]
Out[34]:
-43

Python also supports negative indexing. This has the effect of staring from the end of the list. So the last item in a list has an idex of -1, the second to last item has an index of -2, and so on.

In [35]:
d[-1]
Out[35]:
5
In [36]:
d[-2]
Out[36]:
74.7
In [37]:
d[-3]
Out[37]:
-43

In addition to indexing a list to get back list items, you can using slicing to get back a sub-list. The syntax for list slicing is given by

list_object[starting_index : stopping_index : index_step_size]

As an example we could use 0:3 which would give us all the elements starting with the zero index item and up to but not including the third index item.

In [38]:
print(d)
print(d[0:3]) # This will return the sub-list starting from the index 0 up to, but not including, the index 3
[2.3, 5, -43, 74.7, 5]
[2.3, 5, -43]

By default, the starting index is 0 (at the beginning of the list), the stopping index corresponds to the last item, and the step size is 1.

In [39]:
print(d)
print(d[:4]) 
print(d[1:])
print(d[::2])
[2.3, 5, -43, 74.7, 5]
[2.3, 5, -43, 74.7]
[5, -43, 74.7, 5]
[2.3, -43, 5]
In [40]:
print(d[:-4]) 
[2.3]

Tuples

Tuples are ordered, immutable collection of data. Tuples can be construced in a similar way as lists, but with parenthesis instead of square brackets.

In [41]:
f = (83.2, -4 ,5e7)
print(type(f))
<type 'tuple'>

One weird quirk is that a tuple with a single item needs to have a trailing comma, e.g.

In [42]:
f = (83.2,)
print(f)
print(type(f))
(83.2,)
<type 'tuple'>

If this trailing comma is left out, then python will assume you don't actually want a tuple and will assign whatever the single item is in parenthesis to your variable. For example,

In [43]:
f = (83.2)
print(f)
print(type(f))
83.2
<type 'float'>

The number of items in a tuple can be found using the built-in len function, and they support indexing and slicing similar to lists.

In [44]:
g = (1, 3.2, False, 222, None)
print(g)
print(len(g))
print(g[1:4])
(1, 3.2, False, 222, None)
5
(3.2, False, 222)
Mutable vs. immutable objects

Up to this point, it may seem like lists and tuples aren't any different. They are both containers that can hold items, you can access the items with an index, etc. How are these things different? One of the main differences between the list type and the tuple type is that lists are mutable, while tuples are immutable. Once created, the value of a mutable object can be changed, while immutable objects cannot be changed once created. Let's look at an example.

Let's create a list

In [45]:
g = [1, 2, 3, 4]

Now let's modify the list in place. That is, let's try to change the items in the list without creating a whole new list.

In [46]:
g[0] = 99
print(g)
[99, 2, 3, 4]

As you can see, there wasn't a problem here. We assigned to the variable g the list [1, 2, 3, 4], then modified the zeroth item in g to be the number 99. Let's try the same thing with a tuple now.

In [47]:
g = (1, 2, 3, 4)
print(g)
(1, 2, 3, 4)
In [48]:
g[0] = 99
print(g)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-48-d10c25f7486a> in <module>()
----> 1 g[0] = 99
      2 print(g)

TypeError: 'tuple' object does not support item assignment

We got this error because tuples are immutable—they can't be modified once they're created.

Sets

A set is an unordered collection with no duplicate elements. They are constructed with comma separated items in curly brackets, {}.

In [49]:
s = {1, 2, 3, 4, 2, 2, 3, 1}
print(s)
set([1, 2, 3, 4])

Set objects support mathematical operations like union, intersection, difference, and symmetric difference. Set unions are done with the | operator or using the set union method. For example,

In [50]:
s1 = {1, 2, 3, 4}
s2 = {3, 4, 5, 6}
print(s1 | s2)
print(s1.union(s2))
set([1, 2, 3, 4, 5, 6])
set([1, 2, 3, 4, 5, 6])

Set intersections are done with the & operator or using the set intersection method. For example,

In [51]:
print(s1 & s2)
print(s1.intersection(s2))
set([3, 4])
set([3, 4])

As always, the number of items in a set can be found using the len function.

In [52]:
len(s1)
Out[52]:
4

Strings

Strings are used to represent a sequence of characters. Strings can be created by enclosing characters in either single or double quotes.

In [53]:
g = 'pizza'
type(g)
Out[53]:
str
In [54]:
h = "jamesbond007"
type(h)
Out[54]:
str

Strings can also be index just like lists and tuples.

In [55]:
h[0]
Out[55]:
'j'
In [56]:
h[-4]
Out[56]:
'd'
In [57]:
h[3:6]
Out[57]:
'esb'

You can also find out how many characters are in a string using the len() function

In [58]:
len(h)
Out[58]:
12

Dictionaries

Dictionaries are unordered containers for key-value pairs. That is, dictionaries store a mapping for each key to an associated value. Dictionaries are created by placing comma-separated key-value pairs inside curly brackets {}. For a key-value pair, the key and corresponding value are seperated by a colon, :.

An example might help...

In [59]:
k = {'MJ': 23, 'longitude': -53.2, 'city': 'Tokyo'}
print(type(k))
<type 'dict'>

Here, the dictionary keys are 'key1', 'key2', and 'key3', with corresponding values of 23, -53.2, and 'Tokyo'. In a similar way to sequences, you can access the values in a dicionary by giving the corresponding key in square brackets.

In [60]:
k['MJ']
Out[60]:
23
In [61]:
k['longitude']
Out[61]:
-53.2
In [62]:
k['city']
Out[62]:
'Tokyo'

The keys in a dictionary can be obtained by using the dictionary keys method

In [63]:
k.keys()
Out[63]:
['city', 'longitude', 'MJ']

The values in a dictionary can be obtained by using the dictionary values method

In [64]:
k.values()
Out[64]:
['Tokyo', -53.2, 23]

The size of a dictionary (the number of key-value pairs it contains) can be found with the built-in len() function.

In [65]:
len(k)
Out[65]:
3

It is important to note that in the previous example all the keys were strings, but this doesn't have to be the case. The only restriction on keys is that they be hashable. This means that keys must be an immutable type. For example, the following is also an acceptable dictionary.

In [66]:
m = {-23: [1, 2, 3, 4], 'desk': 3.2, 7.12: (-3, 'bird')}
In [67]:
m[-23]
Out[67]:
[1, 2, 3, 4]
In [68]:
m['desk']
Out[68]:
3.2
In [69]:
m[7.12]
Out[69]:
(-3, 'bird')

Let see what happens if I try to contruct a dictionary with a list (a mutable object) as a key

In [70]:
n = {[1, 2, 3]: 'WOO'}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-70-f5fddf5e4900> in <module>()
----> 1 n = {[1, 2, 3]: 'WOO'}

TypeError: unhashable type: 'list'

Whoops lists are mutable! So remember to always use immutable objects for dictionary keys!

Other types

Other useful types can be found in the collections module in the Python standard library.

  • namedtuple
  • Counter
  • OrderedDict
  • defaultdict

Type casting

[back to top]

Sometimes it can be useful to change the type of an object. In Python, this so-called "type-casting" can be accomplished using several built-in functions:

  • int()—casts to integer
  • float()—casts to float
  • str()—casts to string
  • bool()—casts to boolean

Let's see it in action

Casting integers to floats is fairly straight-forward

In [71]:
a = float(2)
print(a)
print(type(a))
2.0
<type 'float'>

When casting a float to an integer, Python will round down to the nearest integer

In [72]:
b = int(78.81)
print(b)
print(type(b))
78
<type 'int'>

You can even cast a number to a string! (This effectively just returns the number in quotes)

In [73]:
c = str(-1324.1)
print(c)
print(type(c))
-1324.1
<type 'str'>

Things get a little less straight-forward when casting to bool. Below are the bool casting rules:

  • Numbers: Zero of any numeric type, for example, 0, 0.0, 0+0j, cast to False. Everything else casts to True.
  • Lists: An empty list, [], will cast to False. Non-empty lists casts to True.
  • Tuples: An empty tuple, (), will cast to False. Non-empty tuples casts to True.
  • Strings: An empty string, '', will cast to False. Non-empty strings casts to True.
  • Dictionaries: The empty dictionary, {}, casts to False. Everything else casts to True.
  • NoneType: None casts to False.

Here are some examples:

In [74]:
bool(0)
Out[74]:
False
In [75]:
bool(-178.3)
Out[75]:
True
In [76]:
bool([])
Out[76]:
False
In [77]:
bool([1, 2, 3, 4, 5])
Out[77]:
True
In [78]:
bool({})
Out[78]:
False
In [79]:
bool({'key': 'value'})
Out[79]:
True

Arithmetic operations

[back to top]

The following operations are supported for several of the built-in types in Python:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Floored division: //
  • Exponentiation: **

Some examples with numerical types...

In [80]:
1+1
Out[80]:
2
In [81]:
10-3
Out[81]:
7
In [82]:
3*5.0
Out[82]:
15.0
In [83]:
5/2
Out[83]:
2
In [84]:
5//3
Out[84]:
1
In [85]:
9.0**2
Out[85]:
81.0

When performing arithmetic operations, the type of numbers does matter. According to the Python Software Foundation:

Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the 'narrower' type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex.

So, when you have a arithmetic operation with mixed numeric types, say adding an int and a float, the result will have the 'widest' type of the two numbers, in this case float. The convention is int is the most narrow type, float is wider than int, and complex is wider than float.

Some of these arithmetic operations are even defined for compound types. For example, list addition

In [86]:
list1 = [1, 2, 3, 4]
list2 = [5, 6]
summed_list = list1 + list2
print(summed_list)
[1, 2, 3, 4, 5, 6]

tuple addition

In [87]:
tup1 = (1, 2, 3, 4)
tup2 = (5, 6)
summed_tuple = tup1 + tup2
print(summed_tuple)
(1, 2, 3, 4, 5, 6)

and string addition are all defined

In [88]:
'My name is ' + 'James'
Out[88]:
'My name is James'

Comparison operations

[back to top]

In addition to using arithmetic operations to combine objects, it's also using to compare the value of objects as well. The comparison operators defined in Python are:

  • == (equal)
  • != (not Equal)
  • < (less than)
  • > (greater than)
  • <= (less than or equal to)
  • >= (greater than or equal to)

A boolean value of either True or False will be returned appropreiately from a comparison operator. For example,

In [89]:
2 == 2
Out[89]:
True
In [90]:
1 > 0.5
Out[90]:
True
In [91]:
1 < 0.5
Out[91]:
False

Python also can handle comparing different types to one another. In particular, floats and integers are compared in a natural way

In [92]:
2 == 2.0
Out[92]:
True

Multiple comparison can also be made at once.

In [93]:
a = 25
print(15 < a < 30) # Checks whether or not a is greater than 15 and less thatn 30
True

Boolean values can also be combined using the and, or, or not keywords.

In [94]:
(a < 30) and (a > 15)
Out[94]:
True
In [95]:
(a < 30) and (a == 25)
Out[95]:
True
In [96]:
(a > 30) or (a == 25)
Out[96]:
True
In [97]:
(a > 30) or (a < 15)
Out[97]:
False
In [98]:
not (a == 25)
Out[98]:
False

Control flow

[ back to top ]

Up to this point, we've explored some of the built-in types in Python and how to store values (i.e. variables) for later use. Now we'll look at using these building blocks to make a dynamic program.

Conditional statements

[ back to top ]

Conditional statements are used to execute a piece of code based on if some condition is met. Let's look at some examples.

If statements

If statements are used to execute a piece of code if a condition is met. The basic structure of an if statement is shown below.

if condition :
    # indented code block here

If statements start with the keyword if, followed by the condition to be evaluated, then the line is ended with a colon. The block of code to be evaluated if the condition is met should be indented below the if statement. The condition here should be some expression that is either a boolean value, or can be cast to a boolean value. Let's look at some examples.

In [99]:
condition = True
if condition:
    print('Condition is True')
Condition is True
In [100]:
condition = False
if condition:
    print('Condition is True')
In [101]:
a = 10
if a < 20:
    print('Condition is True')
    b = 10
    print(b)
Condition is True
10

Multiple conditions can be combined into a more complex condition using the and / or keywords.

if condition1 and condition2 :
    #code evaluated if both conditions are True
if condition1 or condition2 :
    #code evaluated if at least one of the conditions are True

For example,

In [102]:
b = 5
c = 15
if b < 10 and c < 20:
    print('Both conditions are True')
Both conditions are True

The or keywords requires that at least one of the conditions be True. For example, below the first condition is True, but the second is False.

In [103]:
if b < 10 or c < 10:
    print('At least one condition is True')
At least one condition is True

If-else statements

Sometimes more complicated situations can arise in which you would like to have, depending on if a condition is met, different pieces of code run. This leads us to the if-else statement. If-else statements consist of an if statement followed by a piece of code that will be executed if the if-statement condition is not met.

if condition :
    # code for True condition
else:
    # code for False condition
In [104]:
b = 4
if b == 5:
    print('b is 5')
else:
    print('b is not 5')
b is not 5

Elif statements

Sometimes your might like to have many if statements you would like to check

In [105]:
value = 10
if value < 10:
    print('Value was less than 10')
elif value > 10:
    print('Value was greater than 10')
else:
    print('Value neither less than or greater than 10')
Value neither less than or greater than 10
In [106]:
x = 0.
if x == 0:
    print(x, "is zero")
elif x > 0:
    print(x, "is positive")
elif x < 0:
    print(x, "is negative")
else:
    print(x, "is unlike anything I've ever seen...")
(0.0, 'is zero')

Loops

[ back to top ]

Looping in Python is done via for-loops and while-loops

For loops

The basic syntax of a for loop is shown below.

for iterating_var in iterable:
    # code using iterating_var here

We've run into several iterables already: lists, tuples, strings, and dictionaries.

In [107]:
for item in [0, 1, 2, 3, 4, 5]:
    print(item)
0
1
2
3
4
5
In [108]:
for item in (False, 3.2, 'this is a string'):
    print(item)
False
3.2
this is a string
In [109]:
for letter in 'Python':
    print(letter)
P
y
t
h
o
n

Built-in range function.

In [110]:
for item in range(3, 20):
    print(item)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
In [111]:
tup = (False, 3.2, 'this is a string')
for index, value in enumerate(tup):
    print(index, value)
(0, False)
(1, 3.2)
(2, 'this is a string')
In [112]:
k = {'MJ': 23, 'longitude': -53.2, 'city': 'Tokyo'}
for key in k:
    print(key, k[key])
('city', 'Tokyo')
('longitude', -53.2)
('MJ', 23)
In [113]:
# If using Python 2, use k.iteritems() instead of k.items()
for key, value in k.items():
    print(key, value)
('city', 'Tokyo')
('longitude', -53.2)
('MJ', 23)

While loops

Loop over code until condition is evaluated to False

while condition:
    # code using iterating_var here
In [114]:
n = 0
while n < 10:
    print(n)
    n = n + 1 
0
1
2
3
4
5
6
7
8
9

List comprehension

One way to create a list is to use the append method inside a for loop

In [115]:
squared_list = []
for i in range(5):
    value = i**2
    squared_list.append(value)
print(squared_list)
[0, 1, 4, 9, 16]

While this approach gets the job done, it's a little too verbose. Python has another, less verbose, syntax for creating lists—list comprehensions

In [116]:
squared_list = [i**2 for i in range(5)]
print(squared_list)
[0, 1, 4, 9, 16]

Functions

[ back to top ]

Functions allow for the consolidation of several pieces of code into a single reusable object. The basic syntax for defining a function is shown below.

def function_name(some_input):
    # Code utilizing input goes here
    return some_output
In [117]:
def add(value1, value2):
    total = value1 + value2
    return total
In [118]:
print(add(4, 5))
9
In [119]:
print(add(1e3, -200))
800.0
In [120]:
print(add('Python', 'rules'))
Pythonrules
In [121]:
def change_zeroth_item_to_3(parameter):
    parameter[0] = 3
    return parameter
In [122]:
change_zeroth_item_to_3([1, 2, 3, 4, 5])
Out[122]:
[3, 2, 3, 4, 5]