Values and built-in data types

Back to main page

Ultimately, working with data of any kind, resorts to working with values—which can take on many forms (known as its data type or class) such as:

  • Booleans (e.g. True, False)
  • Integers (e.g. 1, 5, etc.)
  • Floats (e.g. 0.55, 1.34, etc.)
  • Strings (e.g. 'c', 'hello!', etc.)

These are not the only data types that Python is limited to. Later on, we will show how it is possible to define our own data types.

If you are unsure of the class of a value, you can use the built-in type() command:

print('type of \'hello!\'', type('hello!'))
print('type of 5', type(5))
print('type of 1.2', type(1.2))
print('type of True', type(True))

This outputs

type of 'hello!' <class 'str'>
type of 5 <class 'int'>
type of 1.2 <class 'float'>
type of True <class 'bool'>

Booleans

In Python, booleans are of type bool and they represent the notion of true/false. To be clear, however, Python expects both true and false to be capitalized. That is, True and False.

Integers

In Python, integers are implemented as arbitrary-precision integers. Accordingly, the largest integer you can feed into Python is limited only by your computer’s available resources.

Python is also capable of understanding numbers written in bases 2, 8, and 16. For base 2, prepend a 0b to the front of the integer. For base 8, prepend 0o to the integer. For base 16, prepend 0x to the integer. Example:

print(0b11)  # should print 3
print(0o11)  # should print 9
print(0x11)  # should print 17

Floats

Floating point numbers are implemented via C’s double. Accordingly, the precision is determined by your computer’s hardware. Of relevance is the IEEE 754 standard on floating point numbers.

Complex Numbers

Python also has support for complex numbers. By appending j or J to a numerical constant, Python interprets the result as an imaginary number. To obtain a complex number, we simply add a real number to an imaginary number. Example:

print(3 + 5.5j)

Strings

Strings in Python are denoted by usage of single quotes ' or double quotes ". For instance,

"Isn't this fun?"

Is a valid string. Notice also that the usage of double quotes, in this case, allows us to place a single quote in the middle of the string without causing Python to think we ended a string.

It is also possible to have multi-line strings by using triple single quotes ''' or triple double quotes """. For instance,

"""This string
is allowed to
be multiple lines
long."""

Type Conversion

In Python, it is possible to convert one data type into another through the usage of the corresponding constructors. We will explain what exactly a constructor is later on.

For instance, if we want to convert an integer into a string, we might do something of the form

str(5)

This one way that allows us to easily combine strings and integers:

print('I have ' + str(25) + 'cats')

For information, see the official Python documentation for

Exercises

  1. Convert 5.5 into an integer. What happens?

  2. Convert the string '42' into an integer.

  3. Convert 5.5 into a string.

  4. Explain why the following code has a runtime error:

    int('5.5')
    

Back to main page