You are on page 1of 13

Data types & Variables

Data Types & Variables


The two basic building blocks of programming are
code and data. Code is a set of instructions that tell
the computer what to perform and how it should
perform. But I want to start our discussion with data.
Data refers to the quantities, characters, and/or
symbols on which operations are performed with a
computer. Anything you need the computer to
remember is a piece of data. Simple examples of
data include the number of students in class, grade
point average, name, whether a switch is in an on or
off position, and so on.

Data types & Variables


Types of Data
The four basic types of data are called integer
numbers, floating-point numbers, strings, and
Booleans. This section explains and provides
examples of each of these types of data.

Data types & Variables


Types of Data
Integers: Integer numbers (or simply, integers) are
counting numbers, like 1, 2, 3, but also include 0
and negative numbers.
The following are examples of data that is
expressed as integers:
•Number of people in a room
• Personal or team score in a game
• Course number
• Date in a month
• Temperature (in terms of number of degrees)

Data types & Variables


Types of Data
Floats: Floating-point numbers (or simply
floats) are numbers that have a decimal point
in them. The following are examples of data
that is expressed as floating-point numbers:
• Grade point average
• Price of something
• Percentages
• Irrational numbers, like pi

Data types & Variables


Types of Data
Strings: Strings (also called text) are any
sequences of characters.
Examples of data that is expressed as strings
include the following:
• Name
• Address
• Course name
• Title of a book, song, or movie
• Sentence
• Name of a file on a computer

Data types & Variables


Types of Data

Booleans: Booleans are a type of data that can only


have one of two values: True or False. Booleans tell
us about the state of a data, whether they’re true or
false.
The following are some examples of data that can be
expressed as Booleans:
• The state of a light switch: True for on, False for off
• Inside or outside: True for inside, False for outside
• Whether someone is alive or not: True for alive,
False for dead
• If someone is listening: True for listening, False for
not listening
Data types & Variables
Types of Data
Note: It might seem that integer and floating-point
data have overlaps. For example, there is an integer 0
and there is a floating-point 0.0. There is an integer 1
and a floating-point 1.0. Although these may appear to
be the same thing to us humans, integers and floats are
handled very differently inside the computer. Without
getting too wrapped up in the details, it is easier for
the computer to represent and operate with integers.
But when we have a value with a decimal point, we
need to use a floating-point number instead.
Whenever we represent a value, we choose the
appropriate numeric data type.

Data types & Variables


Types of Data
Examples of Data
Now let’s take a look at what the actual data looks like for each
of the four different data types.
• Integer numbers are whole or counting numbers. These are
some examples: 12, 50, 0, -3, -25
• Floating-point numbers are any numbers that contain a
decimal point. These are some examples: 1.5, .5, -3.21, 1.0, 0.0
• Strings represent textual data or any sequence of characters.
String data is always represented with quote characters before
and after the sequence of characters. ‘Folake', ‘Amaka',
“Folake", “Amaka", 'This is some string data', "OK"
• Boolean data can only have one of two values: True or False.
Note: The words True and False must be spelled with this
capitalization: True False

Data types & Variables


Python Variables
Variables are containers for storing data values.
It is a way to refer to some piece of data. Here are
some simple examples.
thatPerson = “Mike” Here we have a variable called
thatPerson and assigned to the value Mike. We use
the equals operator to assign what is on the left of
the equation (thatPerson) to what is on the right of
the equation (“Mike”).

Data types & Variables


Python Variables
A variable consists of 3 elements; name, type and value.
A variable is created the moment you first assign a value
to it.
x=5
y = "John"
print (x)
print (y)
Variables do not need to be declared with any particular
type , and can even change type after they have been set.
x = 4 # x is of type int
x = "Sally" # x is now of type str print (x)

Data types & Variables


Types of Data
Note: It might seem that integer and floating-point
data have overlaps. For example, there is an integer 0
and there is a floating-point 0.0. There is an integer 1
and a floating-point 1.0. Although these may appear to
be the same thing to us humans, integers and floats are
handled very differently inside the computer. Without
getting too wrapped up in the details, it is easier for
the computer to represent and operate with integers.
But when we have a value with a decimal point, we
need to use a floating-point number instead. Whenever
we represent a value, we choose the appropriate
numeric data type.

Data types & Variables


Data types &
Variables

You might also like