Index
Definition
Numbers in python are considered atomic data types, which means they cannot be divided further into smaller elements.
This means that a number is not iterable so you can not loop, slice or index a number.
Oss:
Iterable objecta access their elements one by one. For example, strings, lists, tuples, and sets are iterable objects in Python.
Type
There are three numeric types in Python:
int
float
complex
Int
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
Float
Float, or “floating point number” is a number, positive or negative, containing one or more decimals.
Float can also be scientific numbers with an “e” to indicate the power of 10:
Complex
Complex numbers are written with a “j” as the imaginary part:
Type Conversion
You can convert from one type to another with the int()
, float()
, and complex()
methods:
Note:
When converting from a float to an int, the decimal part will be will be rounded down to the nearest whole number. For example,
int(2.8)
will output2
, not3
.Similarly, when converting from an int or float to a complex number, the imaginary part will be set to
0.
Python Operators
Python supports a variety of operators for performing arithmetic operations on numbers, such as +
, -
, *
, /
, %
(modulo), **
(exponentiation), and //
(floor division). For example:
Augmented Assignment Python Operators
Augmented Assignment Operators in Python are shorthand operators that combine an arithmetic operator with an assignment operator.
The following table shows the list of augmented assignment operators available in Python:
- These operators can be used with any numeric data type, such as integers, floating-point numbers, and complex numbers.
- They can also be used with strings and lists, as well as other built-in data types that support the relevant operations.
Comparison Python Operators
You can also compare numbers using comparison operators such as <
, >
, <=
, >=
, ==
(equality), and !=
(not equal). For example:
Built-in Functions
Python provides some built-in functions for working with numbers, such as abs()
, max()
, min()
, round()
, and pow()
. For example:
Slicing of a Number
No, you can not slice a number in Python because numbers are considered as atomic data types, meaning they cannot be divided further into smaller elements.
Slicing is a concept that applies to iterable objects like strings, lists, and tuples.
However, you can convert a number to a string and then slice it. For example:
Quartz Index Exampleof a Number
No, you can not index a number in Python because numbers are considered as atomic data types, meaning they cannot be divided further into smaller elements. Indexing is a concept that applies to iterable objects like strings, lists, and tuples, where you can access individual elements by their position in the sequence using an index, ad esempio:
Note that after indexing the number as a string, the result will also be a string, not a number.
Loops in a Number
you can’t loop because a number is not iterable
Modules
Random Module
Python also has a random
module for generating random numbers. For example:
Math Module
For more advanced mathematical operations, Python has a math
module. You can import the math
module and use its functions, such as sqrt()
for square roots and sin()
for trigonometric sine. For example: