Objects, Types, Operators, and Expressions

To do anything useful in our programs, Python will need to represent data such as numbers, words and booleans. We will learn more about these in Units 2 and 3.

For now we will introduce the most common 4 built-in data types.

1. Objects and Types

In Python, everything you work with is an object. An object can be a number, a word, or any other type of data. Each object has a specific type, which tells Python what kind of data it is and what operations can be done with it.

Python has several built-in data types, which it uses to represent data internally. We'll start by looking at four common ones:

Data TypeDescription
intAn integer. This is a whole number, it can be positive, negative or zero. e.g. 5
floatA decimal number. e.g. 3.14
strText. It consists of individual characters. Strings are enclosed in single quotation marks ' or double quotation marks ". e.g. "Hello World"
boolThe values True or False. Used to make decisions, more in Unit 3

A very useful function in python that we can use is type(). The type() function is a useful tool to check what type of data you're working with. Understanding the type of an object helps you know what operations can be performed on it and how Python will handle it in your program.

x = type(10)
print(x)

Here we are assigning the result of type(10) to the variable x. Basically this stores the result in memory in a name called x. We can then print out what is stored in x.

You could do this in one line like so.

print(type(10))

We will stick to using x as it is more readable.

If you run either of these you will see the output:

<class 'int'>

This is python telling you that 10 is an object of type int. For now, read class as type.

Try it for these other objects.

x = 10.3
print(x)
print(type(x))
x = "This is a string"
print(x)
print(type(x))
x = True
print(x)
print(type(x))

This first gets the type of the object, here a str and then passes that to the print() function.

2. Operators and Expressions

We can combine objects with operators to form expressions. When evaluated, these expressions produce a new object.

For example, we can combine the objects 10 and 5 with the + operator,

x = 10 + 5              # new object 15 assigned to x
print(x)                # prints out 15
print(type(x))          # prints out <class 'int'>

to create a new object 15 of type int.

All built-in data types have operators that we can use to form expressions.

For example, we can test if two expressions are equal with the == comparison operator.

x = 10 + 5 == 3 * 5     # assign the result of comparing 10 + 5 and 3 * 5. 
print(x)                # prints out True
print(type(x))          # prints out <class 'bool'>

This will return True. Python knows how to test whether two numbers are equal and returns you a new object of type bool.

We will cover operators for numbers, strings, and bools in their respective lessons in this unit.

3. Help Function

The help() function gets information about an object (it can also be used for other things.)

Try typing the following into the terminal or a Python file:

help(str)

You will need to click in the terminal and press Ctrl + c to exit this.

You can also type python into the terminal to enter the Python Interative Shell and then type help(str) and press Enter. You can open this by typing python into a terminal.


=== TASK ===

Create a new Python file.

Edit the file to output the type of the following expressions. You will need to use the print() and the type() function.

Make sure you have read all of the above before attempting this (especially the end of Section 1).

12 + 5
5 + 3.0
12 / 5
12 + 5 == 5 * 3.0

The output of your program should be:

<class 'int'>
<class 'float'>
<class 'float'>
<class 'bool'>