You are currently viewing Numbers in Python: Everything you should clearly know as a beginner.

Numbers in Python: Everything you should clearly know as a beginner.

  • Post author:
  • Post category:Python

In Python, there are three types of numeric data: integers, floating-point numbers, and complex numbers.

my_number = 7 # int
my_float = 7.0 # float
my_complex = 5+7j # complex

Integers

In programming whole numbers like 1, and 2, and 3 are called integers, or an int. They can be zero, positive or negative.

x = 8
n = -50
y = 56453565

All integers are objects of the int class. Here, variable x is of int class.

x = 8
print(type(x)) # <class 'int'>

Integers values can be binary, octal, and hexadecimal.

Binary

Binary numbers are numbers with base of 2 and are represented in the combination of 0s and 1s. 0b is the Python prefix for the representation of binary numbers. We can convert integers into binary number using bin().

x = 0b110110
print(x) # 54


n = 54
b = bin(n)
print(b) #0b110110

Octal

An octal number is a number with base of eight. The octal system uses digits from 0 to 7. 0o or 0O is the Python prefix for the representation of octal numbers. We can convert integers into binary number using oct().

x =0o117
print(x) # 79


n = 79
b = oct(n)
print(b) # 0o117

Hexadecimal

A Hexadecimal is a number with base of 16. The hexadecimal system uses digits from 0 to 15. 0x is the Python prefix for the representation of hexadecimal numbers. We can convert integers into binary number using hex().

x=0x112
print(x) # 274


n = 274
b = hex(n)
print(b) # 0x112

Leading zeros

Leading zeros in non-zero integers are not allowed in Python. For example, 00789 is an invalid number and 000 is 0.

x = 0789
print(x) # SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers

x = 000
print(x) # 0

Python Delimiter

Python does not allow comma as number delimiter. We use underscore _ as a delimiter instead.

x = 1_000_000
print(x) # 1000000

Converting different data types to integer

To convert different data type to integer, we use the int() function.

# convert float to int
print(int(9.5)) # 9

# convert string to int
print(int("7")) # 7

Float

Decimal numbers are called floats in Python. For example, 4.5 is called a float in Python.

n = 4.5
h = 89.7804

All decimal values are object of float class

x = 4.5
print(type(x)) # <class 'float'>

Exponential Notation in Float

Floating-point values can be represented using e-notation. Numbers in e-notation are represented by digits that is followed by an e or an E. The e or an E is then followed by the number that represents the number of times the given number should be multiplied by 10.

val = 1e3
print(val) # 1000.0

p = 3.14e2
print(p) # 314.0

Maximum size for Float

The maximum size for float is referred as “inf”, “Inf”, “INFINITY”, or “infinity”. 2e400 will be considered as infinity for most systems.

print((2e400)) # inf

Converting different data type to Float

To convert different data type to integer, we use the float() function. String infinity’ or ‘inf’ give float inf value and ‘nan’ or ‘NaN’ give float nan value. Any other non-numeric string value apart from these will throw an error.

print(float(45)) # 45.0
print(float(True)) # 1.0
print(float("-4.54")) # -4.54
print(float('infinity')) # inf
print(float('inf')) # inf
print(float('nan')) # nan
print(float('NaN')) #nan
print(float('hi')) # error

Complex Number

A complex number is a number with real and imaginary components. A complex number is a number of the form x + yi, where x and y are real numbers and i is imaginary number. In python, the imaginary number i is represented as j.

my_complex = 5+7j
print(type(my_complex)) # <class 'complex'>

Converting real numbers to complex number

We can convert real numbers x and y into complex using complex(x,y). The real part can be accessed using real() and imaginary part can be accessed by imag().To get these functions we will import cmath.

import cmath
  
# Initializing real numbers
x = 5
y = 7
  
# converting x and y into complex number
z = complex(x,y)
  
# printing real and imaginary part of complex number
print (z.real) # 5.0

print (z.imag) # 7.0

Arithmetic Operators

OperatorsDescription
+ (Addition)Produces the sum of operands.
– (Subtraction)Produces the difference of operands.
* (Multiplication) Produces the product of the operands.
/ (Division) Produces the quotient of the operands.
% (Modulus)Returns the remainder left of one operand divided by a second operand
** (Exponent)Returns the result of raising the first operand to the power of the second operand.
// (Floor Division)Adjusts whole number to the left-hand side in the number line.

Arithmetic operations on Integers and Floats

a = 7
b = 8.0

print(b/a)  # 1.1428571428571428
print(b//a) # 1.0
print(b-a) # 1.0
print(b+a) # 15.0
print(b**a) # 2097152.0

Arithmetic operations on complex numbers

# arithmetic operations on one complex number
c=5+7j

print(c+2)  #(7+7j)
print(c-2)  # (3+7j)
print(c*2)  # (10+14j)
print(c/2)  # (2.5+3.5j)
print(c**2) # (-24+70j)

# arithmetic operations on two complex numbers
a=5+7j
b=3+5j

print(a+b)

print(a-b) # (2+2j)
print(a*b) # (-20+46j)
print(a/b) # (1.4705882352941178-0.11764705882352938j)
OperatorsDescription
int()Converts data type to int
float()Converts data type to float
complex()Converts data type to complex
real()Returns real part from the complex number
imag()Returns imaginery part from the complex number
bin() Converts integers into binary number
oct() Converts integers into octal number
hex() Converts integers into hexadecimal number
type()Returns the data type class
round()Returns the rounded number.
pow()Returns the power of a number
abs()Returns the absolute value of a number