Types

Type Introduction

Let’s try and do the following operation:

5 + 'Hello There'

Any guesses as to the outcome? It won’t work. That’s obvious, right? Python doesn’t know how to add the words “Hello There” with a number, 5. And that makes sense – addition is only defined for numbers.

This introduces the basic concept of types. There are several kinds of types. Let’s start with the three basic, or primitive types.

Broadly, this simply defines the difference between numbers and words. “String” is just a fancy computer science term for words, except that strings can include symbols like “!” or “?”. In fact, a string includes basically everything that your keyboard can type.

In many languages, though not Python, there is another type called character, which is a single symbol. For example, if “Hello there” is a string, ‘H’ is a character (and so is ‘e’, etc). Although it is not a formal type in Python, it is still quite common to refer to single elements of strings as characters. In fact, it’s so common that the Python function to convert the integer 6 to the string “6” is called chr, for character.

In short: “a” is a character, while “all” is a string.

Let’s talk about the other two primitive types: Integer and Float. So why do we have two different types to represent numbers? A float includes pretty much every real number, from 3.14159 to 4.8 and every other number in existance. Integer types on the other hand, do not include any numbers with decimal points. Examples are -4, 0, and 4.

So what does Python tell us exactly when we try 5 + 'Hello There'?

>>> 5 + 'a'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The last line is the most important. It tells us there is a TypeError, which is what happens when types don’t match up. Specifically, it’s saying that for the operator +, it does not know how to use an int and a str – which is the short way of saying Integer and String. In otherwords, it does not know how to add numbers and strings. Which makes sense.

Changing Types

In some languages, you are forced to declare the type of every single variable. For example, if you wanted to create an int named a, you would have to do the following:

int a = 5;

In these languages, a can only ever be an int and nothing else!

Python, luckily, offers more flexibility for us. We can simply do

a = 5

But at a later time, if we want a to be a string instead, we can just as easily do

a = '#WorstClassEver! #JKWeLoveFreshmen'

This offers an awesome flexibility, but it puts the onus on you, the programmer, to keep track of what each variable is at each moment in time. Otherwise, you’ll end up adding a string to an integer and run into a problem!

Expressions

It’s not just variables that have types. As we saw in the above example, 5 + 'Hello There!' has no variables, but we still ran into a signature TypeError! So what gives?

Types are not just limited to variables. Expressions and functions also have types. Python has a builtin type function which tells you the type of anything you give it.

>>> type(5 + 4)
<type 'int'>

Sure enough, the expression 5 + 4 returns 9, which is of type int.

So what is an expression anyways? An expression is any combination of values, variables, and operators. This includes any value, even if it’s just by itself with no variables or operators. Even just the slightly rude literal "Puck Frinceton" is an expression.

A statement, on the other hand, is a section of code that represents a command or action. Let’s take an example.

truth = "Why do all goth kids who want to non-confrom dress the same?"

It’s a simple statement that just assigns the string to the variable truth. Within this statement, the string is an expression, and so is the variable truth. The critical point is that the entire statement as a whole is not an expression; it is a statement, which is composed of two expressions.

Boolean Type

There is actually a surprise fourth primitive type, called boolean. Unlike the other types which have many, many values, this type only has two: True and False. That’s it!

So what are some examples of expressions that have a boolean type?

>>> 5 > 4
True

Miraculous. As it turns out, five is in fact greater than four. :)

Let’s try some more tricky ones.

>>> "Dartmouth" > "Cornell"
True

Darn right it is. This is a silly example, but a tricky one. How Python compares strings will be explained later on, so you don’t have to understand this right now.

How about testing whether two things are equal? That’s the == operator.

>>> 5 - 3 == 2
True

That wraps up primitive types… for now. (Yes, there are more surprise types! But that’s for a later time.)