Overview
Teaching: 5 min Exercises: 5 minQuestions
What kinds of data do programs store?
How can I convert one type to another?
Objectives
Explain key differences between integers and floating point numbers.
Explain key differences between numbers and character strings.
Use built-in functions to convert between integers, floating point numbers, and strings.
int
): counting numbers like 3 or -512.float
): fractional numbers like 3.14159 or -2.5.
str
): text.
type
to find the type of a value.type
to find out what type a value has.print(type(52))
<class 'int'>
fitness = 'average'
print(type(fitness))
<class 'str'>
print(5 - 3)
2
print('hello' - 'h')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-67f5626a1e07> in <module>()
----> 1 print('hello' - 'h')
TypeError: unsupported operand type(s) for -: 'str' and 'str'
full_name = 'Ahmed' + ' ' + 'Walsh'
print(full_name)
Ahmed Walsh
separator = '=' * 10
print(separator)
==========
len
counts the number of characters in a string.print(len(full_name))
11
print(len(52))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-f769e8e8097d> in <module>()
----> 1 print(len(52))
TypeError: object of type 'int' has no len()
print(1 + 'A')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-fe4f54a023c6> in <module>()
----> 1 print(1 + '2')
TypeError: unsupported operand type(s) for +: 'int' and 'str'
1 + '2'
be 3
or '12'
?print(1 + int('2'))
print(str(1) + '2')
3
12
print('half is', 1 / 2.0)
print('three squared is', 3.0 ** 2)
half is 0.5
three squared is 9.0
first = 1
second = 5 * first
first = 2
print('first is', first, 'and second is', second)
first is 2 and second is 5
first
when doing the multiplication,
creates a new value, and assigns it to second
.second
does not remember where it came from.Choose a Type
What type of value (integer, floating point number, or character string) would you use to represent each of the following?
- Number of days since the start of the year.
- Time elapsed since the start of the year.
- Serial number of a piece of lab equipment.
- A lab specimen’s age.
- Current population of a city.
- Average population of a city over time.
Division Types
The
//
operator calculates the whole-number result of division, while the ‘%’ operator calculates the remainder from division:print('5 // 3:', 5//3) print('5 % 3:', 5%3)
5 // 3: 1 5 % 3: 2
If
num_subjects
is the number of subjects taking part in a study, andnum_per_survey
is the number that can take part in a single survey, write an expression that calculates the number of surveys needed to reach everyone once.
Strings to Numbers
float
will convert a string to a floating point number, andint
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"
first + float(second)
float(second) + float(third)
first + int(third)
first + int(float(third))
int(first) + int(float(third))
2.0 * second
Solution
Answer: 1 and 4
Complex Numbers
FIXME: introduce complex numbers
Key Points
Every value has a type.
Use the built-in function
type
to find the type of a value.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.
Can mix integers and floats freely in operations.
Variables only change value when something is assigned to them.