Overview
Teaching: 10 min
Exercises: 10 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.type(52)
<class 'int'>
disease = 'schizophrenia'
type(disease)
str
5 - 3
2
'frontotemporal' - 'fronto'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-67f5626a1e07> in <module>()
----> 1 print('frontotemporal' - 'fronto')
TypeError: unsupported operand type(s) for -: 'str' and 'str'
roi_label = 'fronto' + 'temporal'
roi_label
fronto temporal
dna_repeat_sequence = 'CAG' * 10
dna_repeat_sequence
CAGCAGCAGCAGCAGCAGCAGCAGCAGCAG
len
counts the number of characters in a string.len(dna_repeat_sequence)
30
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()
1 + '2'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-fe4f54a023c6> in <module>()
----> 1 1 + '2')
TypeError: unsupported operand type(s) for +: 'int' and 'str'
1 + '2'
be 3
or '12'
?1 + int('2')
3
str(1) + '2'
12
1 / 2.0
0.5
Division Types
The
//
operator calculates the whole-number result of division, while the ‘%’ operator calculates the remainder from division:5 // 3
1
5 % 3
2
# three squared is:
3.0 ** 2
# three squared is :
9.0
brain_volume = 1246
brain_volume_corrected = brain_volume * 1.2
brain_volume = 1400
brain_volume
1400
brain_volume_corrected
1495.2
brain_volume
when doing the multiplication,
creates a new value, and assigns it to brain_volume_corrected
.brain_volume_corrected
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?
- Number of days since the start of the year.
- Time elapsed since the start of the year.
- Brain volume.
- Number of scan sequences used in a scan session.
- Disease state.
- Scan sequence name.
Type Conversions with Numbers
float
will convert a string to a floating point number, andint
will convert a floating point number to an integer:float("3.4")
3.4
int(3.4)
3
Given that, what do you expect this program to do? What does it actually do? Why do you think it does that?
int("3.4")
Solution
We would expect to return an integer value of 3. However, it returns the error:
---------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-92-ec6729dfccdc> in <module>() ----> 1 int("3.4") ValueError: invalid literal for int() with base 10: '3.4'
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.