Data Types and Type Conversion
Overview
Teaching: 9 min
Exercises: 9 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.
Every value has a type.
- Every value in a program has a specific type.
- Integer (
int
): counting numbers like 3 or -512. - Floating point number (
float
): fractional numbers like 3.14159 or -2.5.- Integers are used to count, floats are used to measure.
- Character string (usually called “string”,
str
): text.- Written in either single quotes or double quotes (as long as they match).
- The quotation marks aren’t printed when the string is displayed.
Use the built-in function type
to find the type of a value.
- Use the built-in function
type
to find out what type a value has. - Works on variables as well.
- But remember: the value has the type — the variable is just a label.
print(type(52))
<class 'int'>
fitness = 'average'
print(type(fitness))
<class 'str'>
Types control what operations (or methods) can be performed on a given value.
- A value’s type determines what the program can do to it.
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'
You can use the “+” and “*” operators on strings.
- “Adding” character strings concatenates them.
full_name = 'Ahmed' + ' ' + 'Walsh'
print(full_name)
Ahmed Walsh
- Multiplying a character string by an integer N creates a new string that consists of that character string repeated N times.
- Since multiplication is repeated addition.
separator = '=' * 10
print(separator)
==========
Strings have a length (but numbers don’t).
- The built-in function
len
counts the number of characters in a string.
print(len(full_name))
11
- But numbers don’t have a length (not even zero).
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()
Must convert numbers to strings or vice versa when operating on them.
- Cannot add numbers and strings.
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'
- Not allowed because it’s ambiguous: should
1 + '2'
be3
or'12'
? - Some types can be converted to other types by using the type name as a function.
print(1 + int('2'))
print(str(1) + '2')
3
12
Can mix integers and floats freely in operations.
- Integers and floating-point numbers can be mixed in arithmetic.
- Python automatically converts integers to floats as needed.
print('half is', 1 / 2.0)
print('three squared is', 3.0 ** 2)
half is 0.5
three squared is 9.0
Variables only change value when something is assigned to them.
- If we make one cell in a spreadsheet depend on another, and update the latter, the former updates automatically.
- This does not happen in programming languages.
first = 1
second = 5 * first
first = 2
print('first is', first, 'and second is', second)
first is 2 and second is 5
- The computer reads the value of
first
when doing the multiplication, creates a new value, and assigns it tosecond
. - After that,
second
does not remember where it came from.
Fractions
What type of value is 3.4? How can you find out?
Solution
It is a floating-point number (often abbreviated “float”).
print(type(3.4))
<class 'float'>
Automatic Type Conversion
What type of value is 3.25 + 4?
Solution
It is a float: integers are automatically converted to floats as necessary.
result = 3.25 + 4 print(result, 'is', type(result))
7.25 is <class 'float'>
Choose a Type
What type of value (integer, floating point number, or character string) would you use to represent each of the following? Try to come up with more than one good answer for each problem. For example, in # 1, when would counting days with a floating point variable make more sense than using an integer?
- 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
In Python 3, the
//
operator performs integer (whole-number) floor division, the/
operator performs floating-point division, and the ‘%’ (or modulo) operator calculates and returns the remainder from integer division:print('5 // 3:', 5//3) print('5 / 3:', 5/3) print('5 % 3:', 5%3)
5 // 3: 1 5 / 3: 1.6666666666666667 5 % 3: 2
However in Python2 (and other languages), the
/
operator between two integer types perform a floor (//
) division. To perform a float division, we have to convert one of the integers to float.print('5 // 3:', 1) print('5 / 3:', 1 ) print('5 / float(3):', 1.6666667 ) print('float(5) / 3:', 1.6666667 ) print('float(5 / 3):', 1.0 ) print('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.Solution
Depending on requirements it might be important to detect when the number of subjects per survey doesn’t divide the number of subjects evenly. Detect it with the
%
operator and test if the remainder that it returns is greater than 0.num_subjects = 600 num_per_survey = 42 num_surveys = num_subjects // num_per_survey remainder = num_subjects % num_per_survey print(num_subjects, 'subjects,', num_per_survey, 'per survey:', num_surveys)
600 subjects, 42 per survey: 14
Strings to Numbers
Where reasonable,
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
If the conversion doesn’t make sense, however, an error message will occur
print("string to float:", float("Hello world!"))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-df3b790bf0a2> in <module>() ----> 1 print("string to float:", float("Hello world!")) ValueError: could not convert string to float: 'Hello world!'
Given this information, what do you expect the following program to do?
What does it actually do?
Why do you think it does that?
print("fractional string to int:", int("3.4"))
Solution
What do you expect this program to do? It would not be so unreasonable to expect the Python 3
int
command to convert the string “3.4” to 3.4 and an additional type conversion to 3. After all, Python 3 performs a lot of other magic - isn’t that part of its charm?However, Python 3 throws an error. Why? To be consistent, possibly. If you ask Python to perform two consecutive typecasts, you must convert it explicitly in code.
int("3.4") int(float("3.4"))
In [2]: int("3.4") --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-2-ec6729dfccdc> in <module>() ----> 1 int("3.4") ValueError: invalid literal for int() with base 10: '3.4' 3
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
Python provides complex numbers, which are written as
1.0+2.0j
. Ifval
is an imaginary number, its real and imaginary parts can be accessed using dot notation asval.real
andval.imag
.
- Why do you think Python uses
j
instead ofi
for the imaginary part?- What do you expect
1+2j + 3
to produce?- What do you expect ‘4j’ to be? What about
4 j
or `4 + j’? >Solution
- Standard mathematics treatments typically use
i
to denote an imaginary number. However, from media reports it was an early convention established from electrical engineering that now presents a technically expensive area to change. Stack Overflow provides additional explanation and discussion- 4+2j
- 4j, syntax error, depends on the value of j
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.