Python Numbers

Python Numbers


Python contains three numerical types :

  • int
  • float
  • complex

When you assign a value to a numeric type variable, it is created :

Example :-

x = 1    # int
y = 2.8  # float
z = 1j   # complex

Example :-

In Python, use the type() function to determine the type of any object :

x = 1
y = 2.8
z = 1j
print(type(x))
print(type(y))
print(type(z))

Output :-

<class 'int'>
<class 'float'>
<class 'complex'>

You can also search for these topics, python numbers of exercises, python number of items in list, pattern number program in python, type of python numbers, define the python numbers between 1 to 10, what are the bases, bits and bytes in python, search the equality of python numbers, find the error of python numbers, define the python method even or odd, elements in the list using python numbers, python ends with numbers.

Int

An integer or Int is a whole number that can be positive or negative, has no decimals, and can be any length.

Example :- Integers :-

x = 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))

Output :-

<class 'int'>
<class 'int'>
<class 'int'>

You can also search for these topics, python int size, define the int python max and range, input the declaration of int and arguments using python, use of bytes in python int, python integer digit count, integer division method in python, equality methods in python int, exception and error handling with python integer, format the int using python.

Float

A float, also known as a "floating point number", is a number that contains one or more decimals and is positive or negative.

Example 1 :-Different types float values :-

x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))

Output :-

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

Scientific numbers with an "e" for the power of 10 can also be used as floats.

Example 2 :- Float values with scientific numbers

x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))

Output :-

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



You can also search for these topics, python float precision, python floating point, python float max, define the python accuracy, python float average, calculate the python float, python division precision, python empty in float, Example for float.

Complex

The imaginary part of complex numbers is represented by a "j" :

Example :- Complex floating values

x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))

Output :-

<class 'complex'>
<class 'complex'>
<class 'complex'>

You can also search for these topics, python complex programs, python number examples, python complex problems, python complex code, python complex argument, algorithms in complex using python, python built in complex, python complex is better then complicated, complex data in python.

Type Conversion

The int(), float(), and complex() functions allow you to convert from one type to another :

Example Convert from one type to another:

#convert from int to float:
x = float(1)
#convert from float to int:
y = int(2.8)
#convert from int to complex:
z = complex(x)
print(x)
print(y)
print(z)
print(type(x))
print(type(y))
print(type(z))

Output :-

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

Note : Complex numbers can't be converted into another type.



You can also search for these topics, python type conversion error and exeception, what is type conversion in python, how to do type conversion in python, what is type conversion how does python perform it, list the type conversion using python.

Random Number

The random() function for Python is not provided, but the Python module is built-in and can be used to make random numbers.

Example :- The random module is imported, displaying a random number from 1 to 9 :

import random
print(random.randrange(1, 10))

Output :-

6

You can also search for these topics, python generate random number, python random number between range, python program to generate random number, python 3 random number, random number without repeating using python, find random number between two number in python, why random numbers are always same in python.