# Numbers in Python

Python provides various ways to represent numeric data in the code. It is achieved through Python's three main numeric data types, namely **Int, Float and Complex.**

## Numeric Data Types

The Numeric Data Types are used to represent numbers in the code. These Data Types are further divided into three types:

**Integer Type -**It includes positive or negative whole numbers. Integer can be of any length as there is no limit in Python on the length of an integer.x = 10 #Integer Literal print(type(x)) Output:

**Float Type -**It includes real numbers represented in a floating-point representation. It is accurate up to 15 decimal points.z = 5.5 #Float Literal print(type(z)) **Complex Type -**It includes numbers with a real and imaginary part.y = 3 + 5j #Complex Literal print(type(y)) Output:

## Number System in Python

Python can handle numbers represented through different number systems. It supports **Binary, Hexadecimal and Octal** number systems.

## Binary Numbers

Binary numbers are represented in the form of powers of 2. In Python, numbers starting with 0b are recognised as Binary numbers. Like this

` ````
```
number = 0b1100
print(number)
number = 0b110110
print(number)
number = 0b1110111
print(number)
number = 0b11001
print(number)
number = 0b100010
print(number)

Output:

## Hexadecimal Numbers

Hexadecimal numbers are represented in the form of powers of 16. In Python, numbers starting with 0x are recognised as Hexadecimal numbers. Like this

` ````
```
number = 0x15
print(number)
number = 0x131
print(number)
number = 0x1123
print(number)
number = 0x1401
print(number)
number = 0x105
print(number)

Output:

## Octal Numbers

Octal numbers are represented in the form of powers of 8. In Python, numbers starting with 0o are recognised as Octal numbers. Like this

` ````
```
number = 0o15
print(number)
number = 0o7
print(number)
number = 0o1123
print(number)
number = 0o12
print(number)
number = 0o175
print(number)

Output:

## Arithmetic Operations on Integer and Float Data Type

Arithmetic operators perform the usual arithmetic operations we are familiar with, like addition, subtraction, division, etc. Arithmetic Operators in Python come in 2 forms. These are

**Unary Operators -**These work on a single operand.**Binary Operators -**These work on two operands.

## Unary Arithmetic Operators

Operator | Operation | Example |
---|---|---|

Unary Plus (+) |
Returns the value of the operand | Let x = 10 Then +x means 10 Let x = -10 Then +x means -10 |

Unary Minus (-) |
Returns the value of the operand with the negated sign | Let x = 10 Then -x means -10 Let x = -10 Then -x means 10 |

## Binary Arithmetic Operators

Operator | Operation | Example |
---|---|---|

Addition (+) |
Returns the sum of operands | 1 + 2 = 3 5.0 + 3 = 8.0 2.5 + 3.5 = 6.0 “A” + “B” = “AB” [5,6,7] + [8,9] = [5,6,7,8,9] (1,2,3,4) + (‘Bus’,’Truck’) = (1,2,3,4,’Bus’,’Truck’) |

Subtraction (-) |
Subtracts the 2nd operand from the 1st operand |
6 - 5 = 1 1.0 - 3 = -2.0 2.5 - 3.5 = -1.0 |

Division (/) |
Divides the 1st operand by the 2nd operand and returns the quotient |
10 / 4 = 2.5 10.0 / 5 = 2.0 1.0 / 0.3 = 3.3333333333333335 |

Modulus (%) |
Divides the 1st operand by the 2nd operand and returns the remainder |
5 % 2 = 1 10.0 % 4 = 2.0 12.0 % 7.0 = 5.0 |

Multiplication (*) |
Returns the product of the two operands |
2 * 3 = 6 -2.2 * 2 = -4.4 6.2 * 4.5 = 27.900000000000002 "Hello"*2 = “HelloHello” |

Exponent (**) |
Returns the result as 1st operand raised to the power as 2nd operand |
5**2 = 25 2.5**3 = 15.625 10.5**3.2 = 1852.7027964066515 |

Truncated Division(//) |
Divides the 1st operand by the 2nd operand and returns the whole part of the quotient |
10 // 4 = 2 10.25 // 4 = 2.0 -10 // 6.0 = -1.0 |

If you notice in the above examples, the result of an operation is implicitly type casted to the type of operand which is bigger in terms of byte size. For example

5.0 * 2 = 10.0(float) ( not 10(int) )

## Arithmetic Operations on Complex Data Type

We can perform addition and subtraction operations on Complex Data types in Python. Like this

` ````
```
a = 1 + 6j
b = 7 + 24j
print(a+b)
print(a-b)

Output:

## Functions

These are the available functions that work with numbers in Python :

Function | Description |
---|---|

int |
Type casts a given float or string containing numbers to an integer type and returns an integer. |

float |
Type casts a given integer or string containing numbers to float type and returns a floating-point number. |

complex |
Type casts a given integer, float or string containing numbers to a complex type and returns a complex number with real and imaginary part. |

abs |
Returns the absolute value of the argument. |

round |
Returns the given number rounded to the given precision in decimal digits. |

pow |
Returns the given number raised to a specified power. |

hex |
Returns the hexadecimal representation of an integer. |

oct |
Returns an octal representation of an integer. |