Skip to article frontmatterSkip to article content

A Deep Dive into Variables and Datatypes

In the prior notebook, we introduced how to write more sophisticated multi-line codes using scripts. Here, we’re going to momentarily put scripting aside and focus on introducing some basic vocabulary that we will build on throughout the series. By the end of the day, you should be able to...

  • Define the most important variable types in Pythons (int, float, string, list)
  • Use F-strings to refer to variables in printed strings
  • Use basic indexing to access elements of a list
  • Write codes that take in inputs from the command line
  • Understand the basic distinction between functions, objects, and variables

Variables in Python

In the previous notebook, we restricted our consideration of variables to simple numerical variables that we could add, subtract, multiply etc. easily in Python. However, variables are a lot more flexible than that in Python. To illustrate this point, here are four different valid variable definitions:

# define six different Python variables
x = -6
width = 7.0
animal_name = 'cat'
dogs_are_friends = True 
list_of_classes = ['french', 'galaxies', 'philosophy']
dictionary1 = {'Name': 'Steve', 'Address': '219 Prospect Street', 'Office Number': 501}

Reminder: the ‘=’ sign in each line of the above is NOT a mathematical statement. A single equals sign is the assignment operator: it takes whatever is on the righthand side and stores it in the lefthand side. As before, we are free to print any of these, e.g.,

print(list_of_classes)

which largely just prints whatever was on the righthand side of our variable declaration.

As is clear from the above examples, there’s clearly a diversity in what can be stored in a Python variable. We call the different types of things that variables can be data types. All the data types have a unique name and a set of properties. To see their names, we can try:

print(type(width))

Now that we know how to check the type of a given variable, let’s dig into the properties of each type.

Integers

Integers (int) for short are your run-of-the-mill counting numbers (.., -2, -1, 0, 1, 2.. etc). They obey all the basic arithmetic you expect:

a =  6
b = -4 
print(a+b, a-b, a * b)

In each of these cases, adding/subtracting/multiplying two integers results in another integer. This is hopefully intuitive. But what about dividing two integers?

c = 5
d = 2
e = c / d

print(e) ## print the result of the calculation above

If we now check the type of the result,

print(type(e))

We can see that it’s something else -- a float.

Floats

Floats, short for “floating point numbers,” are numbers with decimal places. As can be seen from the example above, Python automatically recognizes that it needs to use a float variable type to store the division of the variable c by the variable d. This is NOT the case in many other programming languages! In Java or C, for example, e would be an integer result equal to 2. Python is free to do this because it dynamically allocates memory to variable types, as opposed to pre-specifiying how much memory a variable needs.

If we want to specify that a number is a float, there are at least three equivalent ways of doing that:

f1 = float(24)
f2 = 24.
f3 = 24.0

print(type(f1), type(f2), type(f3))

In the first line here, float() is a function that takes whatever is in the parantheses (the so-called argument) and converts it to a float.

Strings

Strings are essentially just letters enclosed by pairs of quotes ’ '. Almost any common character can be included in a string, including things like spaces, and strings can be any length (including length 0). Some examples:

animal_name = 'cat'
blank_string = ''
net_id = 'zzz56'
print(animal_name, blank_string, net_id)

Note that both single and double quotes will both work - just be consistent.

If you want to check how many characters are in a string, you can use the len() function:

print(len(animal_name))

Another property of strings is that they can be concatenated (added together) with the “+” operator. For example,

string1 = 'hot'
string2 = 'dog'
print(string1 + string2)

Lastly, it is occasionally useful to note that you can force quantities to be strings by using the str() function. For example,

acc = str(9.8)
print(type(acc))

Lists

Lists are exactly what they sound like: they are lists of “stuff” in Python, enclosed by hard brackets [ ]; the items in the list are called elements. You can put anything within a list, including any other data type (and even lists..)

# a list of elements with all the same data types:
list_of_animals = ['cat', 'dog', 'frog']
print(list_of_animals)

# a list of elemetns with varied data types:
random_stuff = ['8', True, 'frog', (8,7)]
print(random_stuff)

# an empty list
empty_list = []
print(empty_list)

Just like we did for strings, we can find the length of a list as follows:

list1 = ['a','b','c','d']
length = len(list1)
print(length)

If we have an existing list and want to add an element to the end, we can use the function .append():

list1 = ['a','b','c','d']
print(list1, len(list1))
list1.append('e')
print(list1, len(list1)) # print again to see the effect of the change

which might come in especially handy if you want to build up an empty list, e.g.,

list_of_numbers = []
list_of_names.append('999-301-9191')

We can also do the same with remove():

list_of_animals = ['cat','cat', 'dog', 'frog','cat']
print(list_of_animals)
list_of_animals.remove('cat')
print(list_of_animals)

List indexing

An important property of lists is that they are (implictly) indexed: each element has an associated index that specifies its position. Importantly, these count up starting from 0 (not 1!!) and go up to the length of your list minus one. For example,

letters = ['a','b','c','d']
print(letters[0]) # return the first item in the list
print(letters[1]) # return the second item in the list

If you try to use an an index that is >= the length of the list, you will run into an error since that index doesn’t exist:

letters = ['a','b','c','d']
print(letters[4]) # attempt to return the 5th element of a 4-element list; this will fail

Another useful index is -1, which will return the last element of the list:

letters = ['a','b','c','d']
print(letters[-1]) # this will print 'd'

If we only want to print a subset of the list, you can use so-called list “slicing” with the colon (:). You can specify both the starting and ending index, as such:

letters = ['a','b','c','d']
print(letters[1:3]) # this will print

Here, only two elements are return because the : will not select the element with index equal to the number that comes after the colon.

We can also use functions to compute summary statistics for lists comprised only of numbers, e.g.,

grades = [71, 85, 81, 92, 97, 100, 67, 45]
worst_grade = min(grades)
best_grade = max(grades)

print(f"The worst grade on the test was {worst_grade} and the best grade was {best_grade}.")

Booleans

Booleans are essentially binary truth values: True or False.

is_physics_fun = False
today_is_Monday = True

They can naturally arise when using (in)equality operators:

print(6 > 7)
print('cat' == 'cat') # double equal sign checks for equality; single equal sign is for assignment
print('cat' != 'cat')  # != is how we say "not equal to" in Python

As with any other type of variable, you could store the output of some operation as follows:

result = (7 > 6) # evaluate the inequality, and store a boolean in the variable called result
print(result)

Booleans are very useful for controlling program flow. We’ll discuss this at length next week.

Dictionaries

A more complicated -- but very useful -- data type in Python is the dictionary. It provides a structured way to store data that can be accessed with a “key” (roughly, a column name). For example,

dictionary1 = {'Name': 'Steve', 'Address': '219 Prospect Street', 'Office Number': 501}
print(dictionary1['Name'])

In the event you need to access the list of fields, you can print dictionary1.keys().

Aside: Taking Command-Line inputs during Python scripts

A useful function when writing scripts is input(). If you include this in your code as follows,

var_to_set = input()
print(f"Your input was {var_to_set}")

your input will be recored in the variable var_to_set.

The Big Picture: Objects and Functions in Python

Python is what we call an object-oriented program language. The term objects here refer to bundles of data (attributes) and functions (which often act on that data). The structure of objects (e.g., what functions they contain) is defined by a class; this is why you see the word <class when you run type() on a variable. All Python data types are classes, and by extension, all variables are objects.. For example, let’s take the case of a list:

mylist = [5,4]
mylist.append(5)

Here, mylist is an object of type list, with a value of [5,4]. All objects of type list have a function .append() associated with them. This isn’t true for objects of type int,str, etc -- they have their own functions associated with them since they are each types defined by different classes.

A fun example of a class function associated with int() is as_integer_ratio():

var = 3
print(var.as_integer_ratio())

As a general comment, functions are almost always accompanied by a double parantheses(). Anything within those parantheses is called an argument. In the case above, the object itself (var) is an implict argument of the function (as_integer_ratio()).

We’ll take much more about functions and objects throughout this series, so don’t worry if this is a bit jargon-y and opaque at this point. For now, we will move on to introduce another important datatype called the array.