0% found this document useful (0 votes)
15 views10 pages

Numbers

The document discusses numeric data types in Python including integers, floating-point numbers, and complex numbers. It explains that numeric data types allow storing and manipulating numbers to perform calculations and solve problems. Key characteristics and operations for integers and floats are defined along with the order of operations in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

Numbers

The document discusses numeric data types in Python including integers, floating-point numbers, and complex numbers. It explains that numeric data types allow storing and manipulating numbers to perform calculations and solve problems. Key characteristics and operations for integers and floats are defined along with the order of operations in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

4/25/24, 10:29 PM Notebook.

ipynb - Colab

keyboard_arrow_down Numbers in Python


Numeric data types play a crucial role in programming as they allow us to work with numbers
and perform various mathematical operations. In Python, numeric data types represent different
kinds of numbers, such as integers, floating-point numbers, and complex numbers.
Understanding these data types is essential for performing calculations, manipulating data, and
building mathematical models in Python.

Motivation

Welcome to your journey of learning about numbers in programming! Let's explore the key
reasons why learning about numbers is valuable:

Foundational Data Type: Numbers are a fundamental data type in programming. By


understanding numbers, we gain the ability to perform mathematical calculations,
manipulate data, and solve a wide range of problems in programming.

Arithmetic and Mathematical Operations: Learning about numbers allows us to perform


different mathematical operations in our programs. We can add, subtract, multiply, divide,
and do other math operations with numbers. This helps us solve problems, make
calculations, and perform complex mathematical tasks.

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.

What are numeric data types?

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

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/3. Numbers/Noteb… 1/10


4/25/24, 10:29 PM Notebook.ipynb - Colab

(e.g., 3.14, 1.23e-4).


Complex (complex): Complex numbers consist of a real part and an imaginary part. They
are written in the form a + bj , where a represents the real part and b represents the
imaginary part. Complex numbers are useful in mathematics and scientific computations.

Importance of numeric data types in programming

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:

Mathematical operations: Numeric data types allow us to perform arithmetic operations


like addition, subtraction, multiplication, and division. These operations form the
foundation of many algorithms and calculations in programming.
Data analysis and manipulation: Numeric data types are vital for data analysis tasks, such
as computing statistics, aggregating data, and performing mathematical transformations.
They are essential for working with numerical data in scientific, financial, and engineering
applications.
Scientific and mathematical modeling: Numeric data types enable us to build models and
simulations for scientific and mathematical purposes. Whether it's simulating physical
systems, predicting future outcomes, or solving complex equations, numeric data types
provide the necessary tools.

keyboard_arrow_down Integer (int)


Integers are a fundamental numeric data type in Python that represent whole numbers without
any fractional or decimal parts. They can be positive, negative, or zero. The integer data type is
used to perform calculations involving whole numbers, countable quantities, and indices in
arrays or lists.

Key characteristics of integers in Python include:

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)

Creating integer variables


In Python, you can create integer variables by assigning an integer value to a variable name.
Here's an example:
https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/3. Numbers/Noteb… 2/10
4/25/24, 10:29 PM Notebook.ipynb - Colab

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.

keyboard_arrow_down Floating-Point (float) Data Type


Floating-point numbers, also known as floats, are a numeric data type in Python that represent
real numbers with fractional parts. They are used to perform calculations involving continuous
quantities, measurements, and scientific computations. Floating-point numbers are written with
a decimal point or in scientific notation (e.g., 3.14, 1.23e-4).

Key characteristics of floating-point numbers in Python include:

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

Creating Float variables


In Python, you can create float variables by assigning a floating-point value to a variable name.
Here's an example:

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.

keyboard_arrow_down Basic arithmetic operations


Integers and Floats support all basic arithmetic operations, allowing you to perform calculations
with ease. Here are the common arithmetic operations you can perform:

Addition ( + ): Adds two or more integers/floats together


Subtraction ( - ): Subtracts one integer/float from another

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/3. Numbers/Noteb… 3/10


4/25/24, 10:29 PM Notebook.ipynb - Colab

Multiplication ( * ): Multiplies two or more integers/floats


Division ( / ): Divides one integer/float by another, returning a float result (unless using
floor division which will be explained in the next section)
Modulo ( % ): Returns the remainder of the division between two integers/float
Exponentiation (``)**: Raises an integer/float to the power of another integer/float

Here's an example that demonstrates basic arithmetic operations for integers:

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

keyboard_arrow_down Order of Operations


When multiple arithmetic operations are present in a single expression, Python follows the order
of operations (also known as precedence) to determine the sequence in which the operations
are performed. The order of operations is as follows:

Parentheses: Operations enclosed in parentheses () are evaluated first. Parentheses


allow you to specify the order in which operations should be executed, overriding the
default precedence.
Exponentiation: Exponentiation ( ** ) is evaluated next. It represents raising a number to a
certain power.
Multiplication, Division, and Modulo: Multiplication ( * ), division ( / ), and modulo ( % )
operations are evaluated from left to right.
Addition and Subtraction: Addition ( + ) and subtraction ( - ) operations are evaluated from
left to right.

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/3. Numbers/Noteb… 4/10


4/25/24, 10:29 PM Notebook.ipynb - Colab

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

In the above example, we have the following expression: (a + b) * c - b ** 2 . Let's break


down the order of operations step by step:

(a + b) : Addition operation is performed first, resulting in 15


15 * c : Multiplication operation is performed next, with the value 15 from the previous
step and c being 2 , resulting in 30
30 - b ** 2 : Exponentiation operation b ** 2 is performed next, resulting in 25 . Then,
the subtraction operation is performed with 30 and 25 , resulting in 5 .

Thus, the final value of the expression is 5 , which is printed as the output.

keyboard_arrow_down Integer division and Floor division


Python provides two types of division operators: division ( / ) and floor division ( // ).

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

division_result = a / b # Output: 3.3333333333333335


floor_division_result = a // b # Output: 3

keyboard_arrow_down Converting between integers float data types


In Python, you can convert numeric data types to other data types and vice versa using type
conversion or casting. These conversions allow you to work with different data types and

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/3. Numbers/Noteb… 5/10


4/25/24, 10:29 PM Notebook.ipynb - Colab

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

keyboard_arrow_down integer vs. int

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.

Here's an example to illustrate the difference:

# `integer` as a concept
positive_integer = 10
negative_integer = -5

# `int` data type


x = int(3.14)
y = int("20")

print(positive_integer, type(positive_integer)) # Output: 10 <class 'int'>


print(negative_integer, type(negative_integer)) # Output: -5 <class 'int'>
print(x, type(x)) # Output: 3 <class 'int'>
print(y, type(y)) # Output: 20 <class 'int'>

10 <class 'int'>
-5 <class 'int'>

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/3. Numbers/Noteb… 6/10


4/25/24, 10:29 PM Notebook.ipynb - Colab
3 <class 'int'>
20 <class 'int'>

In the example, positive_integer and negative_integer represent integers in a general sense,


without referring to the specific data type in Python. They are used to represent whole numbers,
whether positive or negative, without any decimal or fractional part.

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.

keyboard_arrow_down float vs. float

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.

Here's an example to demonstrate the difference:

# `float` as a concept
decimal_number = 3.14

# `float` data type


x = float(10)
y = float("5.5")

print(decimal_number, type(decimal_number)) # Output: 3.14 <class 'float'>


print(x, type(x)) # Output: 10.0 <class 'float'>
print(y, type(y)) # Output: 5.5 <class 'float'>

3.14 <class 'float'>


10.0 <class 'float'>
5.5 <class 'float'>

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

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/3. Numbers/Noteb… 7/10


4/25/24, 10:29 PM Notebook.ipynb - Colab

variables demonstrate the usage of float as a specific data type provided by Python to work
with and manipulate decimal numbers.

keyboard_arrow_down Floating-Point Precision and Limitations


Floating-point numbers have limited precision due to the finite representation of real numbers in
computer systems. This can lead to some rounding errors and imprecise results in certain
calculations. It's important to be aware of these limitations when working with floats.

For example, consider the following calculation:

result = 0.1 + 0.1 + 0.1


print(result) # Output: 0.30000000000000004

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.

Here are some considerations to handle floating-point precision errors:

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:

# Floating-point precision error example


x = 0.1 + 0.1 + 0.1
print(x) # Output: 0.30000000000000004

# Rounding to manage precision error


rounded_x = round(x, 2)
print(rounded_x) # Output: 0.3

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

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/3. Numbers/Noteb… 8/10


4/25/24, 10:29 PM Notebook.ipynb - Colab

decimal places. As a result, the output 0.3 represents a more accurate representation of the
intended value.

keyboard_arrow_down NoneType vs 0

In Python, NoneType and 0 are distinct entities representing different concepts.

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

print(type(variable_a)) # Output: <class 'NoneType'>

<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 :

0 is an integer value and belongs to the int data type in Python


It represents the numeric value zero and can be used in arithmetic operations,
comparisons, and other mathematical calculations
Unlike None , 0 is a specific value and not used to indicate the absence of a value

variable_b = 0

print(type(variable_b)) # Output: <class 'int'>

<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.

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/3. Numbers/Noteb… 9/10


4/25/24, 10:29 PM Notebook.ipynb - Colab

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()

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/3. Numbers/Note… 10/10

You might also like