Skip to main content

Elementary Data Types

In computers, data is stored in binary, or a series of ones and zeroes. Computers must convert these ones and zeroes into information that you want. To do this, it must be aware of the type of the data it is storing.

For example, a computer won't know the difference between the integer 42 from the entire Harry Potter Novel Series, unless we tell it which to look for.

info

We can assign different types of data to variables.

In Python, the type of a variable is determined when it has been assigned. We don't have to tell Python the type of the variable when assigning a value to it, and instead, we provide that information implicitly depending on how we write our variable assignment.

Let's start off simple. We will introduce some elementary data types below.

These will cover the most common data types in Python:

  • Integers 12345
  • Floats 4.75
  • Booleans True/False
  • Strings "Hello, World!"

Notice how each data type has a slightly different format. That is, strings are encased by a starting and ending quote ", integers are just numbers, floats are just numbers with decimals, and booleans are either True or False.

Python will pick up on these cues, and work its magic behind-the-scenes to keep track of the types of the variables in any given program.

The rest of this chapter will include more complete information. You may wish to come back here later in case you need to use it as a reference.

Integers

info

Integers are whole numbers. They can be positive, negative, or zero.

Integers are a fundamental data type in Python. Referred to as an integer or int, integers have countless uses in programming, and can be any number of digits long.

Examples

integers.py
x = 0
y = -98
palindrome_prime = 14741

Floats

info

Floats are decimal numbers. They can be positive, negative, or zero.

Floats are important when you need to store decimals in Python. Floats stand for floating-point numbers, and are generally referred to just as float.

Floats are defined with a decimal point, and can be any number of digits long.

Examples

floats.py
pi = 3.14159265
temperature = -12.5
zero = 0.0 # This is a float, but it is equal to zero

Booleans

info

Booleans can only be either True or False.

Examples

booleans.py
light_switch_on = True
is_raining = False

Strings

info

Strings in Python are surrounded by quotation marks. These can either be single or double quotation marks.

'Elispy' (single quotes) and "Elispy" (double quotes) are two different ways to write the same string.

The primary use case of strings are for when you want to store text in your code.

Why are there two different ways of writing the same string?

One handy concequence of these two different ways of writing strings is that you can include apostrophes , or quotation marks ('Troy said "Go!"') in your strings.

When you need to use apostrophes ('), use double quotes (") around your string.

"Sally's Dog"

When you need to use quotation marks ("), use single quotes (') around your string.

'Troy said "Go!"'

When you need to use both single and double quotes (' and "), use triple quotes (""") around your string.

"""Troy told Sally's dog "Go!" loudly."""

Examples

string-examples.py
website_name = 'Elispy'
famous_quote = '"Knowledge is power"'
example_string = """Troy told Sally's dog "Go!" loudly."""