Numerics
To make good use of Built-in Objects called Numeric Types, you need to know what Literals are in these Built-in Objects and what operators they provide. Let’s learn about these.
Numeric Literals
Integers
The Integer type is a string written with digits. An example is below.
# 24, 0 ,-123234556
Python 3.x vs Python 2.x
Python’s Integer has no precision limit. However ,
There is a big difference between Integer in Python 3.x version and 2.X version.
In 2.x version, Integer is divided into normal type and long type. A normal integer represents a decimal digit that can be expressed in 32 bits. However, the long type has no precision limit, and the decimal digit ends with l or L. If it exceeds the allowed bit of 32, it is automatically converted to a long integer. There is no need for the programmer to type ‘L’.
2147689795
>>>2147689795L
However, in 3.x version, normal type and long type of Integer are merged. Thus, integers no longer print l or L at the end.
2147689795
>>>2147689795
Hex,Binary,Oct
So far, all of the Integers seen above have been Integers coded with decimal digits. Integer can be coded as decimal as well as hexadecimal, octal, and binary. HexaDecimal is coded in the format of (0x or 0X)(A-F and 0-9).
a= 0xAF
print(a)
# 175
b = 0XAF
print(b)
It is literally just coding, and since it is an Integer, the output is an Integer.
Octal is coded in the format of (0o or 0O)(0-7). In the 2.x version, the format was 0(0-7), but it was changed in the 3.x version because it was confused with the decimal representation.
a = 0o10
print(a)
# 8
b= 0O10
print(b)
# 8
Binary is coded in the format of (0b or 0B)(0-1).
a = 0b11
print(a)
# 3
b = 0B11
print(b)
# 3
Create Integers Using Built-in Calls
The format of Integer used above can be created with the Built-in method. The format of Integer used above can be created with the Built-in method.
c = hex(16)
print(c)
print(type(c))
# 0x10
# <class 'str'>
c = oct(8)
print(c)
print(type(c))
# 0o10
# <class 'str'>
c = bin(2)
print(c)
print(type(c))
# 0b10
# <class 'str'>
It can be seen that hex,oct,bin receives an Integer as an argument and returns the corresponding representation as a string.
You can create Integer objects with literal expressions, but you can also create Integer Objects with built-in methods.
a = int("10",10)
print(f"a : {a}")
print(f" type of a : {type(a)}")
b = int("10",2)
print(f"b : {b}")
print(f" type of a : {type(b)}")
c = int("10",8)
print(f"c : {c}")
print(f" type of a : {type(c)}")
d = int("10" , 16)
print(f"d : {d}")
print(f" type of a : {type(d)}")
e = int("10",3)
print(f"e : {e}")
print(f" type of a : {type(e)}")
k = int(6)
print(f"k : {k}")
print(f" type of a : {type(k)}")
# a : 10
# type of a : <class 'int'>
# b : 2
# type of a : <class 'int'>
# c : 8
# type of a : <class 'int'>
# d : 16
# type of a : <class 'int'>
# e : 3
# type of a : <class 'int'>
# k : 6
# type of a : <class 'int'>
The int method is defined as int(str,base) or int(digit) .
- int(str,base)
- After checking the representation through the base, return the corresponding integer objects
- int(digit)
- Returns an integer object corresponding to digit.
Floating Numbers
- Returns an integer object corresponding to digit.
floatnumber ::= pointfloat | exponentfloat
pointfloat ::= [digitpart] fraction | digitpart "."
exponentfloat ::= (digitpart | pointfloat) exponent
digitpart ::= digit (["_"] digit)*
fraction ::= "." digitpart
exponent ::= ("e" | "E") ["+" | "-"] digitpart
The lexical definition of floating literal is as above. A floatnumber consists of two literals, pointfloat and exponentfloat. A pointfloat , i.e. a floatnumber consisting only of a decimal part and a ‘.’. exponentfloat means to include the part that is additionally exponent.
When using a float literal in Python, make it a float object, and when a floating object is used in an expression, floating-point math is used. An example of a floating object is shown below.
print(3.14)
print(10.)
print(.0001)
print(1e100)
print(3.15e-10)
print(0e0)
# 3.14
# 10.0
# 0.0001
# 1e+100
# 3.15e-10
# 0.0
Floating-point numbers are implemented as “doubles” in C in standard C python. Thus, you get as much precision as the C compiler provides for doubles.
Complex Numbers
A complex number is a number that has real and imaginary numbers.
The lexical definition of an imaginary number is as follows.
imagnumber ::= (floatnumber | digitpart) ("j" | "J")
Since the real part of a complex number is optional, you can omit it. An example of a complex number is shown below.
print(3+5j)
print(type(3+5j))
b= 3.15j
print(b)
print(type(b))
b = 10.j
print(b)
print(type(b))
b = 10j
print(b)
print(type(b))
b=.001j
print(b)
print(type(b))
b=1e100j
print(b)
print(type(b))
b=3.14e-10j
print(b)
print(type(b))
b=3.14_15_93j
print(b)
print(type(b))
# (3+5j)
# <class 'complex'>
# 3.15j
# <class 'complex'>
# 10j
# <class 'complex'>
# 10j
# <class 'complex'>
# 0.001j
# <class 'complex'>
# 1e+100j
# <class 'complex'>
# 3.14e-10j
# <class 'complex'>
# 3.141593j
# <class 'complex'>
For example, ,3 + 5j is a complex number with a real part 3 and an imaginary part 5j. The rest are complex numbers with only imaginary numbers.
Handling Numeric Types Using Built-in tools
we now have defined Numeric Types objects. So , you can deal with Numeric type objects by defining methods or operators for these objects.
Expression Operator
python-operator-list-and-precedence
The list of Python Operators and their priorities will be attached as a link to the documents site. The higher the precedence in the table in Documents, the higher the precedence, and if the precedence is the same, the expression proceeds from left to right.
The Python Expression Operator has several characteristics to be aware of.
- The expression enclosed in parentheses is executed first.
- When performing an expression on operands of different types, the type is converted to operands of a more complex type. Then, the math operation is performed. ```python b = 40 + 3.14
print(type(b))
<class ‘float’>
Of course, you can explicitly cast the type.
```python
a = int(3.1425)
print(type(a))
print(a)
b= float(3)
print(b)
print(type(b))
# <class 'int'>
# 3
# <class 'float'>
#3.0
In other words, there is no need to explicitly convert the type, since it automatically converts the type.
Built-in math functions and utitlity modules
Built-in math functions include pow , trunc,round , int , and so on, utility modules include random, math, and so on.
I’ll attach the document link for math-modules.
References
Learning-Python
Leave a comment