Numbers
Numbers
ipynb - Colab
Motivation
Welcome to your journey of learning about numbers in programming! Let's explore the key
reasons why learning about numbers is valuable:
Problem Solving and Algorithms: Numbers are crucial for problem solving and coming up
with efficient solutions. They allow us to represent and analyze data, design step-by-step
instructions (called algorithms), and find solutions to various problems. By understanding
numbers, we can think logically and develop strategies to solve problems in different fields,
such as math, science, and computer science.
Numeric data types are a category of data types that represent numerical values
in programming languages.
These types allow us to store and manipulate numbers, perform mathematical operations, and
solve complex problems. In Python, the core numeric data types are:
Integer (int): Integers represent whole numbers without any fractional or decimal parts.
They can be positive, negative, or zero, and can be as large or small as the memory allows.
Floating-Point (float): Floating-point numbers, also known as floats, represent real
numbers with fractional parts. They are written with a decimal point or in scientific notation
Numeric data types are fundamental in programming because they enable us to work with
numbers and perform various calculations. Here are a few reasons why understanding numeric
data types is important:
Integers have unlimited precision, meaning they can be arbitrarily large or small, as long as
they fit within the available memory
They do not include decimal points or fractional parts, making them suitable for whole
number calculations and discrete values
Integer values can be represented in various numeral systems, such as decimal (base 10),
binary (base 2), octal (base 8), and hexadecimal (base 16)
x = 5
y = -10
In the above example, we have created two integer variables: x with a value of 5 and y with a
value of -10 . Once assigned, the variables x and y can be used in calculations or other
operations.
Floats can represent a wide range of values, including both very small and very large
numbers, with a finite precision determined by the system
Floating-point numbers have a limited number of significant digits and can introduce some
degree of rounding error or precision loss in calculations
They can represent both fractional and whole numbers, but the fractional part is not limited
to a fixed number of decimal places
x = 3.14
y = -2.5
In the above example, x is assigned the value 3.14 , and y is assigned the value -2.5 . Once
assigned, these variables can be used in calculations or other operations involving floating-point
numbers.
a = 10
b = 3
sum_result = a + b
difference_result = a - b
product_result = a * b
division_result = a / b
modulo_result = a % b
exponentiation_result = a ** b
print(sum_result) # Output: 13
print(difference_result) # Output: 7
print(product_result) # Output: 30
print(division_result) # Output: 3.3333333333333335
print(modulo_result) # Output: 1
print(exponentiation_result) # Output: 1000
13
7
30
3.3333333333333335
1
1000
Here's an example that showcases the order of operations using parentheses to specify the
order:
a = 10
b = 5
c = 2
result = (a + b) * c - b ** 2
print(result) # Output: 5
Thus, the final value of the expression is 5 , which is printed as the output.
Division ( / ): This operator performs normal division, which returns a float result even
when dividing two integers. For example, 10 / 3 returns 3.3333333333333335.
Floor Division ( // ): This operator performs integer division or floor division, which returns
the largest integer less than or equal to the division result. For example, 10 // 3 returns 3. It
discards the decimal part and provides an integer result.
a = 10
b = 3
perform operations that require specific data formats. Here are some common conversions
involving integers:
Converting an Integer to Float: To convert an integer to a float, use the float() function.
The float value will have the same numeric value as the integer, but with a decimal point
added.
x = 5
float_value = float(x)
print(float_value) # Output: 5.0
5.0
Converting a Float to Integer: To convert a float to an integer, use the int() function. The
float value will be rounded down to the nearest whole number.
x = 3.7
int_value = int(x)
print(int_value) # Output: 3
integer: The term "integer" is a broad concept used to describe a whole number, either
positive, negative, or zero, without any fractional or decimal part. It encompasses the
mathematical concept of integers.
int : In Python, int is the specific data type that represents integers. It is a built-in class
that provides a way to work with and manipulate whole numbers.
# `integer` as a concept
positive_integer = 10
negative_integer = -5
10 <class 'int'>
-5 <class 'int'>
On the other hand, x and y are variables assigned with values that are instances of the int
data type in Python. By using the int() function, values like 3.14 and "20" are explicitly
converted to the int data type, representing whole numbers. These variables demonstrate the
usage of int as a specific data type provided by Python to work with and manipulate integers.
float: The term "float" is a general concept used to describe a number that has a fractional
or decimal part. It represents the mathematical concept of real numbers with a decimal
component.
float : In Python, float is the specific data type used to represent floating-point numbers.
It is a built-in class that provides a way to work with and manipulate decimal numbers.
# `float` as a concept
decimal_number = 3.14
In the example, decimal_number represents a float in a general sense, without referring to the
specific data type in Python. It is used to represent a number with a fractional or decimal part,
such as 3.14 .
On the other hand, x and y are variables assigned with values that are instances of the float
data type in Python. By using the float() function, values like 10 and "5.5" are explicitly
converted to the float data type, representing numbers with decimal components. These
variables demonstrate the usage of float as a specific data type provided by Python to work
with and manipulate decimal numbers.
0.30000000000000004
In this case, the expected result should be 0.3 , but due to the inherent precision limitations of
floats, the actual result includes a small rounding error.
Avoid Comparing for Exact Equality: Due to the precision limitations, comparing floating-
point numbers for exact equality can lead to unexpected results. Instead, use comparison
techniques that take into account a tolerance or a specified precision threshold.
Consider Rounding: Depending on the application, rounding the results to an appropriate
number of decimal places can help manage precision errors and simplify the output. You
can use the round() function or other rounding techniques to achieve the desired level of
precision.
Here's an example that demonstrates the usage of the round() function to manage floating-point
precision errors:
0.30000000000000004
0.3
In the above example, the calculation 0.1 + 0.1 + 0.1 should ideally result in 0.3 . However,
due to floating-point precision limitations, the actual result includes a small rounding error. To
mitigate this precision issue, the round() function is used to round the value of x to two
decimal places. As a result, the output 0.3 represents a more accurate representation of the
intended value.
keyboard_arrow_down NoneType vs 0
NoneType is a special built-in type in Python that represents the absence of a value or the lack of
a meaningful object. It is commonly denoted by the keyword None . Here are a few key points
about NoneType :
None is often used to indicate the absence of a value or when a variable or function does
not return a meaningful result
It is not the same as the integer value 0
None is considered a "singleton" in Python, meaning that there is only one instance of None
throughout the program
variable_a = None
<class 'NoneType'>
In this example, the type() function is used to determine the type of variable_a , which has a
value of None . The output shows that the type of variable_a is <class 'NoneType'> , indicating
that it is of the NoneType .
0 is a numeric value and represents the integer zero. Here are a few key points about 0 :
variable_b = 0
<class 'int'>
In this example, the type() function is used to determine the type of variable_b , which has a
value of 0 . The output shows that the type of variable_b is <class 'int'> , indicating that it is
of the int (integer) type.
Key Takeaways
Numeric data types are fundamental in Python and are used to represent different kinds of
numbers
The int data type is used for working with whole numbers, while the float data type is
used for handling numbers with decimal or fractional parts
Python provides various arithmetic operators such as addition ( + ), subtraction ( - ),
multiplication ( * ), division ( / ), and exponentiation ( ** ) that can be used with numeric
data types to perform calculations
The NoneType represents the absence of a value, indicating when a variable does not
return a meaningful result
Converting between numeric data types and other data types can be achieved using built-in
conversion functions such as int() and float()