Numbers in Python and it’s Examples

0
636

Introduction To Numbers In Python

Numbers in Python

Python contains integers, floating-point numbers and complex numbers. They are defined as INT, Float and COMPLEX in Python.
Integers and floating are separated by the decimal point. For example, 2 is an integer and 2.0 is float number.
Complex numbers are written in the form, X+Yi, where X is the real number and Y is the imaginary number.
Here we use type() to check the type of the number and instance()function is used to check if it belongs to a particular class.

Example:

X= 10
 print(type(X))
 print(type(5.0))

Here Output of First Print is:- Number
and Output of Second Print is:-Float.
because we pass in the first print X =10 which is the number and 5.0 is the float.

Complex number

com = 2+5i
  print(com + 5)
Here Output is (7+5i)

While numbers can be of any length, a float number is True only up to 15 decimal places (the 16th place is False).
The numbers we use every day are the decimal (base 10) number.
computer programmers generally need to work with binary (base 2), hexadecimal (base 16) and octal (base 8) number systems.
In Python, we use these numbers by appropriately placing a prefix before that number.
lists of Number System:-

System Prefix

Examples are:-

print(0b1101011)
output=107.
print(0xFB + 0b10)
Output: 253 (251 + 2).
print(0o15)
Output: 13.

Type Conversion

We can convert one type of number into another this is known as type conversion.

Operations like addition, Subtraction of number with float can be done automatically.
For Example:

  2+3.0
= 5
Here we add float + Number.

We can use a built-in function int(),float(),complex() these function is used to covert the type explicitly.
For Example:-

int(5.2)
o/p
5
float(6)
o/p
6.0
Complex(‘2+6k’)
o/p
(2+6k)

When converting from float to integer, the number gets deleted (decimal parts are removed).

Mathematics In Python

To perform operations on number python provides some mathematical libraries with the help of these libraries we can perform multiple operations on it.

Import math

This library is used to import the functions of math. Like pi, log, power etc.

If you want to import a log from the math library. Steps to import function from the library.
Example:-

Step 1:- import math
Step 2:- math.log(7) Or
Step 3:- From math import log
Step 4:- log(7)
Output:- 1.9459

Widget not in any sidebars

Import random

This library is used to import the random number function.

If you want to work with the random values make sure you import random library.
Steps to Import the random library.

Step 1:- From NumPy import random
Step 2:- import random.randint
Step 3:- randint(1,50,10)
Output:- 1,5,22,23,41,49,17,32,9,10

Here are the 10 random numbers from 1 to 50.

Arithmetic Operation On Numbers in Python

In python, we can perform arithmetic operations on a number and their types.

Arithmetic Operators in Python

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Power
  • Modulus

Example of Addition on two numbers

100+200=300
Bypassing a variable:
A=100
B=200
C=A+B
C=300
Now check the type of number:
type(c)
o/p:- Int

Example of Subtraction on two numbers

200-100=100
Bypassing a variable:
A=100
B=200
C=B-A
C=100
Now check the type of number:
type(c)
o/p:- Int

Example of Multiplication on two numbers:-

105 Bypassing a variable:- A=10 B=5 C=105
C=50
Now check the type of number:
type(c)
o/p:- Int

Example of division on two numbers

200/5=40
Bypassing a variable:
A=200
B=5
C=A/B
C=40
Now check the type of number:
type(c)
o/p:- Int

Example of power on two numbers

52 Bypassing a variable:- A=52
A=25
Now check the type of number:
type(A)
o/p:- Int

Example of Modulus on two numbers

100%30=10
Bypassing a variable:
A=100
B=30
C=A%B
C=10
Now check the type of number:
type(c)
o/p:- Int

Data Types in which we use the numbers

Like int, string, float there some more Data types in python which is used to structure the data.
Here we are going to which are those data types in which we are using numbers.

  • A number of items in LIST data type.
  • A Pair of keys and Values use in Dictionary data type.
  • A number of items in LIST data type

In a list data type, we can pass multiple items by using numbers and another data type.
For example:-

A=[1,2,3,5,6]

Here A is the list in which we pass multiple numbers of items like in this dictionary we pass 1,2,3,5,6 there are 5 items.

To access the data from the list we can use a positional number of the list, from above example if we want to retrieve the 3 from the list so the position of the number three is 2, so we have to pass like A[2].
Then we get an output =3.

  • A Pair of keys and Values use in Dictionary data type.

In a dictionary data type, we can create a pair of key and value , in which we can pass the numbers,]
For example:-

A={1:100,2:200,3:300}

Here we create a dictionary where left side of the colon is the key and right side of the colon is value.
To access the value from a dictionary we have to pass the key of the dictionary in the above example if I want to retrieve the 200 so I have to pass the key 2 like A[2]
Then we get an output =200.


Widget not in any sidebars

Conclusion:

In this blog, you can get detail knowledge of python numbers, conversions, data types, mathematics operations, Arithmetic operators.

LEAVE A REPLY

Please enter your comment!
Please enter your name here