Open and Reproducible Neuroscience

Data Types and Type Conversion

Overview

Teaching: 10 min
Exercises: 10 min
Questions
  • 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.

Use the built-in function type to find the type of a value.

type(52)
<class 'int'>
disease = 'schizophrenia'
type(disease)
str

Types control what operations can be done on values.

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'

Strings can be added and multiplied.

roi_label = 'fronto' + 'temporal'
roi_label
fronto temporal
dna_repeat_sequence = 'CAG' * 10
dna_repeat_sequence
CAGCAGCAGCAGCAGCAGCAGCAGCAGCAG

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

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()

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

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 + int('2')
3
str(1) + '2'
12

Can mix integers and floats freely in operations.

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

Variables only change value when something is assigned to them.

brain_volume = 1246
brain_volume_corrected = brain_volume * 1.2
brain_volume = 1400
brain_volume
1400
brain_volume_corrected
1495.2

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?

  1. Number of days since the start of the year.
  2. Time elapsed since the start of the year.
  3. Brain volume.
  4. Number of scan sequences used in a scan session.
  5. Disease state.
  6. Scan sequence name.

Type Conversions with Numbers

float will convert a string to a floating point number, and int 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