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.
x = 2
# 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)
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.
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)
type(x)
Different types have different allowed operations and functionality. For example, as we'll discuss later, the addition operator is defined for integer types.
a = 1
b = 2
print(type(a))
print(type(b))
# Use addition operator between two integers
print(a + b)
and an uppercase method is defined for string types
c = 'Madison, WI'
print(type(c))
# Use the string uppercase method to make every character uppercase
print(c.upper())
The most-commonly used built-in types in Python can be grouped into two categories: simple and compound.
The "simple" types consist of integers (int), floating-point numbers (float), complex numbers (complex), boolean values (bool), and the None type (NoneType).
Integers represent whole numbers ( ...,-2, -1, 0, 1, 2,...). Numbers without a decimal point or exponential notation produce integers.
a = 2
print(type(a))
Floating-point numbers (often called "floats") represent real numbers. Number containing a decimal point or exponential notation are used to define floating-point values.
b = 1.5
print(type(b))
b.as_integer_ratio()
Exponential notation (e or E) is shorthand for scientific notation. E.g. 7e3 = $7 \times 10^3$
c = 7e3
print(c)
print(type(c))
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.
z = 7 + 4.3j
print(type(z))
z.real
z.imag
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.
print(type(z.real))
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.
n = True
print(type(n))
p = False
print(type(p))
Note that bool values are case sensitive—the first letter needs to be capitalized.
z = None
print(type(z))
[ 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).
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.
d = [2.3, 5, -43, 74.7, 5]
print(type(d))
print(d)
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
len(d)
The list append method can be used to add elements to a list
d.append(3.1415)
print(d)
The list sort method will sort the items in a list into ascending order
d.sort()
print(d)
The list reverse method will reserve the order of a list
d.reverse()
print(d)
The list count method counts the number of times an item occurs in a list
# Counts how many times the item 5 occurs in the list d
d.count(5)
Note that a list can contain any type of object. The types of items in a list need not be homogeneous. For example,
crazy_list = [1, False, 23.11, [1, 2, 3], None]
print(crazy_list)
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
d = [2.3, 5, -43, 74.7, 5]
d[0]
d[1]
d[2]
Python also supports negative indexing. This has the effect of starting 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. If you are familiar with R or other languages that use a different convention for negative indices then this is important to note.
d[-1]
d[-2]
d[-3]
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.
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
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.
print(d)
print(d[:4])
print(d[1:])
print(d[::2])
print(d[:-4])
Tuples are ordered, immutable collection of data. Tuples can be construced in a similar way as lists, but with parenthesis instead of square brackets.
f = (83.2, -4 ,5e7)
print(type(f))
One weird quirk is that a tuple with a single item needs to have a trailing comma, e.g.
f = (83.2,)
print(f)
print(type(f))
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,
f = (83.2)
print(f)
print(type(f))
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.
g = (1, 3.2, False, 222, None)
print(g)
print(len(g))
print(g[1:4])
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
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.
g[0] = 99
print(g)
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.
g = (1, 2, 3, 4)
print(g)
g[0] = 99
print(g)
We got this error because tuples are immutable—they can't be modified once they're created.
Be careful though. Items within the tuple may be references to mutable objects.
f = ['a', 'b', 'c']
g = (1, 2, f, 4)
print(g)
f[1] = 'scary new data'
print(g)
A set is an unordered collection with no duplicate elements. They are constructed with comma separated items in curly brackets, {}.
s = {1, 2, 3, 4, 2, 2, 3, 1}
print(s)
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,
s1 = {1, 2, 3, 4}
s2 = {3, 4, 5, 6}
print(s1 | s2)
print(s1.union(s2))
Set intersections are done with the & operator or using the set intersection method. For example,
print(s1 & s2)
print(s1.intersection(s2))
As always, the number of items in a set can be found using the len function.
len(s1)
Strings are used to represent a sequence of characters. Strings can be created by enclosing characters in either single or double quotes.
g = 'pizza'
type(g)
h = "jamesbond007"
type(h)
Strings can also be index just like lists and tuples.
h[0]
h[-4]
h[3:6]
You can also find out how many characters are in a string using the len() function
len(h)
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...
k = {'MJ': 23, 'longitude': -53.2, 'city': 'Tokyo'}
print(type(k))
Here, the dictionary keys are 'MJ', 'longitude', and 'Tokyo', 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.
k['MJ']
k['longitude']
k['city']
The keys in a dictionary can be obtained by using the dictionary keys method
k.keys()
The values in a dictionary can be obtained by using the dictionary values method
k.values()
The size of a dictionary (the number of key-value pairs it contains) can be found with the built-in len() function.
len(k)
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.
m = {-23: [1, 2, 3, 4], 'desk': 3.2, 7.12: (-3, 'bird')}
m[-23]
m['desk']
m[7.12]
Let see what happens if I try to contruct a dictionary with a list (a mutable object) as a key
n = {[1, 2, 3]: 'WOO'}
Whoops lists are mutable! So remember to always use immutable objects for dictionary keys!
Other useful types can be found in the collections module in the Python standard library.
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:
Casting integers to floats is fairly straight-forward
a = float(2)
print(a)
print(type(a))
When casting a float to an integer, Python will round down to the nearest integer
b = int(78.81)
print(b)
print(type(b))
You can even cast a number to a string! (This effectively just returns the number in quotes)
c = str(-1324.1)
print(c)
print(type(c))
Things get a little less straight-forward when casting to bool. Below are the bool casting rules:
bool(0)
bool(-178.3)
bool([])
bool([1, 2, 3, 4, 5])
bool({})
bool({'key': 'value'})
The following operations are supported for several of the built-in types in Python:
1+1
10-3
3*5.0
5/2
5//3
9.0**2
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
list1 = [1, 2, 3, 4]
list2 = [5, 6]
summed_list = list1 + list2
print(summed_list)
tuple addition
tup1 = (1, 2, 3, 4)
tup2 = (5, 6)
summed_tuple = tup1 + tup2
print(summed_tuple)
and string addition are all defined
'My name is ' + 'James'
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:
2 == 2
1 > 0.5
1 < 0.5
Python also can handle comparing different types to one another. In particular, floats and integers are compared in a natural way
2 == 2.0
Multiple comparison can also be made at once.
a = 25
print(15 < a < 30) # Checks whether or not a is greater than 15 and less thatn 30
Boolean values can also be combined using the and, or, or not keywords.
(a < 30) and (a > 15)
(a < 30) and (a == 25)
(a > 30) or (a == 25)
(a > 30) or (a < 15)
not (a == 25)
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 are used to execute a piece of code based on if some condition is met. Let's look at some examples.
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.
condition = True
if condition:
print('Condition is True')
condition = False
if condition:
print('Condition is True')
a = 10
if a < 20:
print('Condition is True')
b = 10
print(b)
Multiple conditions can be combined into a more complex condition using the and / or keywords. The and keyword requires that both conditions are True
b = 5
c = 15
if b < 10 and c < 20:
print('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.
if b < 10 or c < 10:
print('At least one condition is True')
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.
b = 4
if b == 5:
print('b is 5')
else:
print('b is not 5')
Sometimes your might like to have many if statements you would like to check
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')
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...")
Looping in Python is done via for-loops and while-loops
The basic syntax of a for loop is shown below.
We've run into several iterables already: lists, tuples, strings, and dictionaries. Below we iterate over a list.
for item in [0, 1, 2, 3, 4, 5]:
print(item)
Below we iterate over a tuple.
for item in (False, 3.2, 'this is a string'):
print(item)
Below we iterate over a string.
for letter in 'Python':
print(letter)
The built-in range function creates a list of numbers. You specify the first number and the terminating number
for item in range(3, 20):
print(item)
The built-in enumerate function returns an iterable that contains tuples. The first element of the tuple is the index of our original iterable and the second element of the tuple is the value at that index.
tup = (False, 3.2, 'this is a string')
for index, value in enumerate(tup):
print(index, value)
For dictionaries it is possible to iterate over the keys
k = {'MJ': 23, 'longitude': -53.2, 'city': 'Tokyo'}
for key in k: # for key in k.keys()
print(key, k[key])
It is also possible to iterate over key-value pairs
for key, value in k.items():
print(key, value)
Loop over code until condition is evaluated to False
n = 0
while n < 10:
print(n)
n = n + 1
One way to create a list is to use the append method inside a for loop
squared_list = []
for i in range(5):
value = i**2
squared_list.append(value)
print(squared_list)
While this approach gets the job done, it's a little too verbose. Python has another, less verbose, syntax for creating lists—list comprehensions
squared_list = [i**2 for i in range(5)]
print(squared_list)
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 add(value1, value2):
total = value1 + value2
return total
print(add(4, 5))
print(add(1e3, -200))
print(add('Python', 'rules'))
def change_zeroth_item_to_3(parameter):
parameter[0] = 3
return parameter
change_zeroth_item_to_3([1, 2, 3, 4, 5])