Python Data Types - 1
→ Integers
→ Floating-Point Numbers
→ Boolean Data Type
Data and Computing Skills, 2024
Khalil Israfilzade, PhD
Python Data Types - 1
A data type in Python is a classification that specifies which type of value a variable can
hold. In Python, data types are implicitly assigned, meaning that you don't have to
explicitly declare the data type of a variable when you create it. The interpreter
automatically assigns the data type based on the value assigned to the variable. Here are
the most common ones:
1. Numeric data types
To programme well, you don't have to be a math genius. The truth is that few
programmers require knowledge beyond simple algebra. Of course, the amount of maths
you need to know depends on the application. The level of arithmetic required to be a
programmer is generally lower than you may imagine. Although arithmetic and computer
programming aren't as closely tied as some people believe, numbers are an essential
aspect of any programming language, including Python.
In Python, there are three distinct numeric types: integers, floating point numbers, and
complex numbers. And numbers are created by numeric literals or as the result of builtin functions and operators.
Numeric data types are widely used in many mathematical, statistical, data science, and
machine learning solutions built in Python.
Integers
An integer is a whole number with no decimal places. For example, 1 is an integer,
but 1.0 isn’t. The name for the integer data type is int, which you can see with type():
You may already be familiar with how to convert a string containing an integer to a
number using int(), which is especially useful when working with user input. For
example, the following converts the string "17" to the integer 17:
#output
<class 'int'>
PAGE 2
Python Data Types - 1
Consider the following integer examples:
Output:
In Python, you can use underscores (_) to make large numbers more readable by
grouping digits, similar to how you would use commas or spaces when writing numbers
by hand. This feature is especially helpful when dealing with large numbers, as it
improves the readability of the code without affecting the actual value of the number.
Here are two valid ways to represent the number one million as an integer literal in
Python:
# Using underscores to group digits in an integer literal
one_million_1 = 1_000_000
# Without using underscores, the number can be harder to read
one_million_2 = 1000000
! Warning, do not try to use more than one underscore as shown below:
one_million_1 = 1__000__000
Output will generate a SyntaxError: invalid decimal literal
c = int(-2.7) # c will be -3, because the number is rounded down to the nearest integer
PAGE 3
Python Data Types - 1
! Python provides the integer division operator (//), also known as the floor division
operator, as an alternative to the regular division operator (/). The // operator performs
division and then rounds the result down to the nearest integer. This can be useful when
you want to perform division but only need the integer part of the result.
Here are the examples you provided:
print(9 // 3)
# Output: 3
print(5.0 // 2) # Output: 2.0
print(-3 // 2) # Output: -2
When both operands are positive, the // operator works as expected. However, when
one of the operands is negative, the result might not be what you initially expect, because
the floor division operator always rounds down. In the case of -3 // 2, the division results
in -1.5, which is rounded down to -2.
Another important point is that if one of the operands is a float, the result of the floor
division will also be a float. This is why 5.0 // 2 returns 2.0 (a float) instead of 2 (an
integer).
Using the floor division operator can help simplify your code when you need to perform
division but are only interested in the integer part of the result.
!Indeed, subtracting a negative number from another number can sometimes look
confusing due to the presence of consecutive minus signs. In this case, you are subtracting
a negative number (-5) from a positive number (1).
Of the four examples above outputs are the same. #Output: 6
You can surround -5 with parentheses to make it even clearer that the second - is
modifying 5:
PAGE 4
Python Data Types - 1
Using parentheses is a good idea because it makes your code more explicit. Computers
execute code, but humans read code. Anything you can do to make your code easier to
read and understand is a good thing.
Floating-Point Numbers
A floating-point number, or float, is a number with a decimal place that can represent real
numbers, including fractions and irrational numbers. Floats are used when you need
more precision or when working with numbers that have a decimal component. The
floating-point data type in Python is called float.
Consider the following float examples:
Output:
Using underscores to improve readability (similar to integer literals):
Num_float = 1_000_000.0
PAGE 5
Python Data Types - 1
TASK_1: BMI Calculator
Now, let's create a simple BMI calculator using Python. The BMI calculation formula is:
BMI = weight (kg) / (height (m))^2
or
Here's a Python script to calculate BMI based on user input:
# Get the user's height and weight
height = input("Enter your height in meters: ")
weight = input("Enter your weight in kilograms: ")
# Convert the input strings to float and int
new_height = float(height)
new_weight = int(weight)
# Calculate the BMI
BMI = new_weight / new_height ** 2
# Round the BMI to an integer
Bmi_new = int(BMI)
# Print the result
print(Bmi_new)
BMI weight ranges
•
•
•
•
Less than 18.5 = Underweight
Between 18.5 - 24.9 = Healthy Weight
Between 25 - 29.9 = Overweight
Over 30 = Obese
This example demonstrates the use of integers and floating-point numbers in Python, as
well as basic arithmetic operations and type conversion.
PAGE 6
Python Data Types - 1
2. Boolean Data Type
In Python, boolean is a subtype of numeric data type, and it represents one of two values:
True or False (which can also be represented by integers 1 and 0). Just as integer numbers
belong to a class int and floating-point numbers to a class float, boolean belong to a
class bool.
Boolean values are often used in control structures like conditionals (if-else statements) and
loops (while loops), where you need to evaluate expressions to make decisions or control
the flow of your program.
Python comparison operators compare two items, but they can only be used on items that
are comparable. For instance, two integers can be compared for equality, as can two
strings. But two items with different types, such as an integer and a string, cannot be
compared. If such comparisons are attempted, an error similar to TypeError: '<' not
supported between instances of 'int' and 'str' is returned.
Here are the comparison operations that can be used to evaluate a boolean expression:
Operation
x>y
x >= y
x<y
x <= y
x == y
x != y
Result (boolean)
x is greater than y
x is greater than or equal to y
x is less than y
x is less than or equal to y
x is equal to y
x is not equal to y
! Throughout following examples, do not mix up the assignment operator = with the
equality operator ==. Substituting the = operator in place of the == leads to strange and
misleading bugs.
Integer comparisons
Less than:
x = 15
y = 25
# Compare if x is less than y
result = x < y
print(result) # Output: True
PAGE 7
Python Data Types - 1
Equal to:
m = 30
n = 20
# Compare if m is equal to n
result = m == n
print(result) # Output: False
Not equal to:
p = 5
q = 10
# Compare if p is not equal to q
result = p != q
print(result) # Output: True
Greater than or equal to:
r = 7
s = 8
# Compare if r is greater than or equal to s
result = r >= s
print(result) # Output: False
Less than or equal to:
u = 12
v = 15
# Compare if u is less than or equal to v
result = u <= v
print(result) # Output: True
These examples use various comparison operators to evaluate the relationship between
two variables and store the resulting boolean value (True or False) in the result variable.
String comparisons
By default, string comparisons in Python are case-sensitive, meaning that uppercase and
lowercase letters are considered distinct. For example, the strings "Hello" and "hello"
would be treated as different when using the == operator.
Here are some more examples that compare string variables using different comparison
operators:
Case-sensitive equality:
x = "Python"
y = "python"
# Compare if x is equal to y (case-sensitive)
result = x == y
print(result) # Output: False
Not equal to:
p = "apple"
q = "orange"
# Compare if p is not equal to q
result = p != q
print(result) # Output: True
PAGE 8
Python Data Types - 1
In addition to being case-sensitive, string comparisons
are based on the lexicographic (dictionary) order of the
characters in the strings. Lexicographic order is the order
in which words or strings are arranged based on the
alphabetical order of their individual characters. This
ordering is determined by the Unicode* point of each
character. For example, in a lexicographic comparison, the
string "apple" would come before "banana" because the
character 'a' comes before 'b' in the alphabet.
*Unicode is an international character
encoding standard that provides a unique
number for every character across
languages and scripts, making almost all
characters accessible across platforms,
programs, and devices.
Less than (lexicographically):
r = "banana"
s = "grape"
# Compare if r is lexicographically less than s
result = r < s
print(result) # Output: True
Greater than (lexicographically):
u = "mango"
v = "kiwi"
# Compare if u is lexicographically greater than v
result = u > v
print(result) # Output: True
These examples use various comparison operators to evaluate the relationship between
two string variables and print the resulting boolean value (True or False). Keep in mind
that string comparisons are case-sensitive by default and are based on the lexicographic
(dictionary) order of the characters in the strings.
! In Python, the boolean values True and False are internally represented as the integers
1 and 0, respectively. This means that when you perform operations involving boolean
values and integers, Python will implicitly convert the boolean values to their
corresponding integer representations.
Here are some similar examples
demonstrating the equivalence of True
and False with 1 and 0, using different
mathematical operations:
PAGE 9
Python Data Types - 1
TASK_2: time left until retirement.
In this Python challenge, the goal is to calculate the time left until retirement based on the
user's desired retirement age and their current age. Here's a step-by-step explanation of
the code:
PAGE 10