Python Numbers

Python consists of built-in support for storing and processing numeric data which is the Python Numbers. It is required to work with the numbers in every Python application. This article has listed the various types of Python Numbers.

What is a Numeric data type?

The numeric data type can store numeric values. So, they are considered immutable data types, which refer to the value of several data types that can vary and hence, impact the newly allocated object.

Types of Numeric Data Types:

a. int

b. Float

c.Complex

What is Python Int Type?

Python int denotes a whole number, which will include the negative numbers but it doesn’t mean the fractions. In Python, it doesn’t have any limit on the integer value.

Example 1– Creating int and checking type in Python

# Creating an integer
my_integer = 42

# Checking the type
type_of_my_integer = type(my_integer)

# Output
print("My Integer:", my_integer)
print("Type of My Integer:", type_of_my_integer)

Output


My Integer: 42
Type of My Integer: <class 'int'>

Example 2: Arithmetic Operations on Int Type In Python


 # Creating integer variables
a = 10
b = 5

# Addition
sum_result = a + b

# Subtraction
difference_result = a - b

# Multiplication
product_result = a * b

# Division
division_result = a / b  # Result will be a float in Python 3.x

# Floor Division (result is the quotient without the remainder)
floor_division_result = a // b

# Modulus (result is the remainder after division)
modulus_result = a % b

# Exponentiation
exponentiation_result = a ** b

# Output
print("a + b =", sum_result)
print("a - b =", difference_result)
print("a * b =", product_result)
print("a / b =", division_result)
print("a // b =", floor_division_result)
print("a % b =", modulus_result)
print("a ** b =", exponentiation_result)

Output

a + b = 15
a - b = 5
a * b = 50
a / b = 2.0
a // b = 2
a % b = 0
a ** b = 100000

What is Python Float Type?

Python Float number is referred to as the real number which consists of a floating-point representation. So, it will be denoted by a decimal number. Hence, this character e or E will be followed by positive or negative integers and will indicate a scientific notation. Examples of numbers that will be identified as floats are 0.5 and  -7.823457.

However, it can be formed directly by typing the number with a decimal number. Extra zeros that are present on the number’s end will be neglected automatically.

Example 1: Creating Float and Checking Type in Python

# Creating a float
my_float = 3.14

# Checking the type
type_of_my_float = type(my_float)

# Output
print("My Float:", my_float)
print("Type of My Float:", type_of_my_float)

Output

My Float: 3.14
Type of My Float: <class 'float'>

Thus, it is understood that float is created by dividing these two integers. A float will be formed through the execution of the two floats.

Example 2- Arithmetic operations on the Float type in Python

a = 5.5
b = 3.2
 
# Addition
c = a + b
print("Addition:", c)
 
# Subtraction
c = a-b
print("Subtraction:", c)
 
# Division
c = a/b
print("Division:", c)
 
# Multiplication
c = a*b
print("Multiplication:", c)

Output

My Float: 3.14
Type of My Float: <class 'float'>

What is Python Complex Type?

A Complex number consists of a number that has real and imaginary parts. Such as 2+3j is considered a complex number, in which 2 is the real component and 3 will be multiplied by j and will be identified as an imaginary part.

Example 1- Creating Complex and checking types in Python

# Creating a complex number
my_complex = 2 + 3j

# Checking the type
type_of_my_complex = type(my_complex)

# Output
print("My Complex:", my_complex)
print("Type of My Complex:", type_of_my_complex)

Output

My Complex: (2+3j)
Type of My Complex: <class 'complex'>

Example 2 – Arithmetic operations on the complex type

# Creating complex numbers
complex_num_1 = 2 + 3j
complex_num_2 = 1 - 1j

# Addition
sum_result = complex_num_1 + complex_num_2

# Subtraction
difference_result = complex_num_1 - complex_num_2

# Multiplication
product_result = complex_num_1 * complex_num_2

# Division
division_result = complex_num_1 / complex_num_2

# Output
print("Complex Number 1:", complex_num_1)
print("Complex Number 2:", complex_num_2)

print("\nAddition:", sum_result)
print("Subtraction:", difference_result)
print("Multiplication:", product_result)
print("Division:", division_result)

Output

Complex Number 1: (2+3j)
Complex Number 2: (1-1j)

Addition: (3+2j)
Subtraction: (1+4j)
Multiplication: (5+1j)
Division: (1+2j)

What is Type Conversion in Python?

In Python, it is possible to turn one number into another with the help of two methods.

Using Arithmetic Operations:

Operations such as addition and subtraction are used to convert the type of number automatically. One of the operands will be float. Then, this method won’t be performed on complex numbers.

Example 1 – Type Conversion using the arithmetic operations.

a = 1.6
b = 5
 
c = a + b
 
print(c)

Output

6.6

Using the built-in functions

Example 1- Type conversion using the Built-in functions in Python

# Creating variables of different types
integer_value = 42
float_value = 3.14
string_value = "123"

# Using built-in functions for type conversion
converted_integer = int(float_value)
converted_float = float(string_value)
converted_string = str(integer_value)

# Output
print("Original Integer Value:", integer_value)
print("Converted Float Value:", converted_integer)
print("Type of Converted Integer:", type(converted_integer))

print("\nOriginal Float Value:", float_value)
print("Converted String Value:", converted_float)
print("Type of Converted Float:", type(converted_float))

print("\nOriginal String Value:", string_value)
print("Converted Integer Value:", converted_string)
print("Type of Converted String:", type(converted_string))

Output

Original Integer Value: 42
Converted Float Value: 3
Type of Converted Integer: <class 'int'>

Original Float Value: 3.14
Converted String Value: 123.0
Type of Converted Float: <class 'float'>

Original String Value: 123
Converted Integer Value: 42
Type of Converted String: <class 'str'>

Random Numbers in Python

Python will create a random module for producing a pseudo-random number. Hence, this module can make random numbers, and choose a random element from a sequence in Python.

Example 1- To create random value in Python

import random

# Generating a random integer between a specified range
random_integer = random.randint(1, 100)

# Generating a random float between 0 and 1
random_float = random.random()

# Generating a random value from a list
my_list = ["apple", "banana", "cherry", "date"]
random_value_from_list = random.choice(my_list)

# Output
print("Random Integer:", random_integer)
print("Random Float:", random_float)
print("Random Value from List:", random_value_from_list)

Output

Random Integer: 73
Random Float: 0.864826264695416
Random Value from List: cherry

Conclusion

To conclude, Python provides straightforward and versatile support for numbers, offering built-in data types like integers, floats, and complex numbers. You can easily perform arithmetic operations, type conversions, and generate random values using simple and intuitive syntax.

Python Numbers- FAQs

Q1. What are Python numbers?

Ans. It denotes the numeric data types in Python

Q2.What is int () in Python?

Ans. Python int () function will turn the specified value into the integer value.

Q3. How do you declare numbers in Python?

Ans. Python doesn’t have any command to declare a variable.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment