How did Python Numbers work?

Share Your Love

Python In Numbers:

There are three numeric types in python which handle different mathematical calculations through the data.

  • Int [Integer]
  • float [Float]
  • complex

Then variables are numeric types created when assign on data on it. Like,

x = 16    # int
y = 2.48  # float
z = 10j   # complex

So to verify the type of each variable we use type() function,

x = 1
y = 2.8
z = 1j

print(type(x));
print(type(y));
print(type(z));
#Output
<class 'int'>
<class 'float'>
<class 'complex'>

Int:

The int, or integer, is a whole number or natural numbers, positive or negative, without decimals, and unlimited length.

x = 1
y = 356567775548877
z = -3377722

print(type(x))
print(type(y))
print(type(z))

The output of above code,

#Output
<class 'int'>
<class 'int'>
<class 'int'>

Float:

The float or floating-point is a number, is a positive or negative number, without decimals, of unlimited length.

x = 1.10
y = 1.0
z = -35.59

print(type(x))
print(type(y))
print(type(z))

The output of above code,

<class 'float'>
<class 'float'>
<class 'float'>

The float has scientific numbers ‘e’ to indicate the power of 10.

x = 35e3
y = 12E4
z = -87.7e100

print(type(x))
print(type(y))
print(type(z))

The output of above code,

#Output
<class 'float'>
<class 'float'>
<class 'float'>

Complex:

The Complex numbers are written with a alphabet “j” as called as imaginary part:

x = 3+5j
y = 5j
z = -5j

print(type(x))
print(type(y))
print(type(z))

The output of above code,

#Output
<class 'complex'>
<class 'complex'>
<class 'complex'>

Type Conversion:

In type conversion to convert from one type to another we use int()float(), and complex() methods:

#Convert from one type to another:
x = float(10)

#convert from float to int:
y = int(2.80)

#convert from int to complex:
z = complex(x)

print(x)
print(y)
print(z)

print(type(x))
print(type(y))
print(type(z))

The output of above code,

#Output:
1.0
2
(1+0j)
<class 'float'>
<class 'int'>
<class 'complex'>

You can’t convert complex number to any other number type.

Random Number:

Python doesn’t have a random() function to make a random number, but python has built-in module called random that can be used to make random numbers.

#Import inbuilt random to display random numbers Form 1 to 9
import random

print(random.randrange(1, 10))

The output of above code,

#Output:
9

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 411

Newsletter Updates

Enter your email address below to subscribe to our newsletter