This lesson is being piloted (Beta version)

Data 'types' and expressions

Overview

Teaching: 25 min
Exercises: 20 min
Questions
  • How does Python categorise data?

  • What operations can Python do with data?

  • How does Python compare data?

  • What order does Python evalute expressions in?

Objectives
  • Understand how different operators function.

  • Understand that a value’s ‘type’ determine the kind of operations that can be done.

  • Convert data between different types.

  • Use type-specific built in functions.

  • Demonstrate operator overloading (e.g. ‘+’ effect on int vs str).

  • Understand order of precedence and associativity.

Operators and Expressions

We have already seen how Python can be used as a calculator:

5 - 3

This is an example of an ‘expression’.

Technically, the ‘expression’ is the entire 5 - 3.This can be broken down into ‘operators’ and ‘operands’.
The operator is the - and the data values (5 and 3) are the operands.

Expressions with ‘numbers’

We can use many other kinds of operators to construct different expressions:

Multiplication

2 * 3
6

Exponent

2 ** 3
8

Division

13 / 3
4.333333333333333

Floor Division

13 // 3
4

Rounds the answer down to the nearest integer value.

Modulo

13 % 3
1

% returns the remainder of the division.

Division expressions

What is the outcome of the following expressions?

print('5 // 3:', 5//3)
print('5 % 3:', 5%3)

Solution

5 // 3: 1
5 % 3: 2

Order of operations

Precedence and Associativity

If you had the following expressions what are the outputs?

  1. 2 + 3 * 4
  2. 5 * 2 // 3
  3. 5 * (2 // 3)
  4. 2 ** 3 ** 2
  5. (2 ** 3) ** 2

Solution

  1. Answer: 14
  2. Answer: 3
  3. Answer: 0
  4. Answer: 512
  5. Answer: 64

Value ‘types’

Data comes in different flavours … intuitively, we know that we can do different things with different kinds of data. The expression 13 % 2 makes sense to us, but what about 'Brian' % 2?

Categorising data

Group the following values into sensible categories:

10
-5
'Harry'
2.71828
'x'
3.14159265359
1.0
0
'c'
'The meaning of life'

What might be the implications, for a computer, of these different ‘types’ of data?

Although they may often feel like the same thing, it is helpful to distinguish between variables, values, and types.

a = 'a string'

In this simple example, we have the variable or identifier a, the value a string, and the type str.
You can always check the type through the built-in function type:

type(a)
<class 'str'>
a = 7.1
type(a)
<class 'float'>

Number types: Int and Float

myint = 1
print(myint)
1
myfloat = 1.0
print(myfloat)
1.0

Can also convert an int to float and vice-verse.

float(1)
int(1.0)
1.0
1

Strings

string1 = 'hello'
string2 = "hello"
string3 = '1234'
string4 = str(56789)
string5 = 'x'
string6 = "Cleese's legacy"
string7 = 'Another Monty Python quote: "Blue. No, Yellow. Arrghh!"'

Boolean Expressions - True or False

count = 3
print(count == 4)
print(count < 3)
print(count == 3)
print(count > 4)
string1 == string2
False
False
True
False
True

Special Case - NoneType

empty = None
print(empty)
None

Choose a Type

What type of value (integer, floating point number or character string) would you use to represent each of the following?

  1. Number of days since the start of the year.
  2. Time elapsed since the start of the year.
  3. Serial number of a piece of lab equipment.
  4. A lab specimen’s age.
  5. Current population of a city.
  6. Average population of a city over time.

Identify the outputs

What are the outputs from the following commands?

  1. print(type(3))
  2. print(float(3))
  3. print(int(3.9))
  4. print(bool(0))
  5. print(type(None))

Comparisons, Membership Tests and Identity Tests

Note: a single = is for variable assignment, not for comparison.

3 = 3
File "<stdin>", line 1
SyntaxError: can't assign to literal

Double == is for comparison:

3 == 3
True

Types control how values can be combined in expressions.

print(5 - 3)
2
print('hello' - 'h')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'str'

Integers and floats can be mixed freely in expressions.

print('half is', 1 / 2.0)
print('three squared is', 3.0 ** 2)
half is 0.5
three squared is 9.0

Strings can be added and multiplied.

age = 42
age + 3
45
full_name = 'Ahmed ' + 'Walsh'
print(full_name)
Ahmed Walsh
'spam' * 5

'spamspamspamspamspam'

Built in functions and data types

Python pre-defines a number of core ‘functions’ as part of the base language. We have already met one of these functions, print. We ‘call’ a function by stating the name of the function, directly followed by some data (a ‘value’ or ‘variable’) inside round brackets:

print(42)
print(string7)

The built-in function print works on all data types. This is not true of all built in functions; many only work on certain kinds of data.

print(len(full_name))
11
print(len(52))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()

Must convert numbers to strings or vice versa when operating on them.

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

Strings to Numbers

float will convert a string to a floating point number, and int will convert a floating point number to an integer:

print("string to float:", float("3.4"))
print("float to int:", int(3.4))
string to float:, 3.4
float to int:, 3

Given that, what do you expect this program to do? What does it actually do? Why do you think it does that?

print("fractional string to int:", int("3.4"))

Arithmetic with Different Types

Which of the following will print 2.0? Note: there may be more than one right answer.

first = 1.0
second = "1"
third = "1.1"
  1. first + float(second)
  2. float(second) + float(third)
  3. first + int(third)
  4. first + int(float(third))
  5. int(first) + int(float(third))
  6. 2.0 * second

Solution

Answer: 1 and 4

Think like a computer - division

Computers cannot intuitively solve problems; the problem must be broken down by the computer into a series of logical steps. How would a computer solve the following problem?

If num_subjects is the number of subjects taking part in a study, and num_per_survey is the number that can take part in a single survey, write a series of expressions that calculates the number of surveys needed to reach everyone once.

Hint: you may need to use // and %

Test your expressions with num_subjects = 600 and num_per_survey = 20.

Now test again with num_per_survey = 42.

Solution

First test if the number of subjects per survey divides evenly by the number of subjects using the % operator.

num_subjects = 600
num_per_survey = 42
remainder = num_subjects % num_per_survey
num_surveys = num_subjects // num_per_survey

print(remainder)
print(num_subjects, 'subjects,', num_per_survey, 'per survey:', num_surveys + 1, 'surveys needed')
12
600 subjects, 42 per survey: 15 surveys needed

Key Points

  • Types control what operations can be done on values.

  • Strings can be added and multiplied.

  • Strings have a length (but numbers don’t).

  • Must convert numbers to strings or vice versa when operating on them.