Write a Program to Demonstrate Different Number Data Types in Python

Write a program to demonstrate different number data types in Python.

 
There are three types of numbers in python integer, floating point and complex numbers
 
 
 
Write a Program to Demonstrate Different Number Data Types in Python

 
 
 
Let create six variables for each number (integer, float, complex) and try to add them and display them
 
Code
==================
x = 10
y = 20
print(“addition of two integer numbers are “ , x + y)
 
float_number1 = 34.777
float_number2 = 78.99
print(“addition of two floating point numbers are “ , float_number1 + float_number2)
 
complex1 = 2+9j
complex2 = 3+4j
print(“addition of two complex numbers are “ , complex1+ complex2)
 
 
Now try yourself to subtract, multiply and divide these numbers and let me know in the comment section if you find any problem.
 
 

Write a program to demonstrate different number data types in Python (All number types)

Create a generic function to add, subtract, multiply and divide all the three number types. Then create six variables, one pair of each variable for integer, float and complex number. Then ask the user to enter these numbers and then at the end call the function to add, subtract, multiply and divide these pairs of numbers.

 
Code 2
==================

def numbers_demo(a, b, format):
    print(f"Addition of two {format} types {a} + {b} is {a+b} ")
    print(f"Subtraction of two {format} types {a} - {b} is {a-b} ")
    print(f"Product of two {format} types {a} * {b} is {a*b} ")
    try:
        print(f"Division of two {format} types {a} / {b} is {a / b}\n")
    except ZeroDivisionError:
        print(f"Division by Zero {a} / {b} is Not Possible ")
    return 0


var1_int = int(input("Enter First Integer : "))
var2_int = int(input("Enter Second Integer : "))

var1_float = float(input("Enter First Floating Point Number : "))
var2_float = float(input("Enter Second Floating Point Number : "))

var1_complex = complex(input("Enter first Complex Number : "))
var2_complex = complex(input("Enter second Complex Number : "))

numbers_demo(var1_int, var2_int, "int")
numbers_demo(var1_float, var2_float, "float")
numbers_demo(var1_complex, var2_complex, "complex")
 
 
OUTPUT 1:
====================
Enter First Integer : 50
Enter Second Integer : 10
Enter First Floating Point Number : 3.99
Enter Second Floating Point Number : 2.5
Enter first Complex Number : 3+4j
Enter second Complex Number : 2+5j

Addition of two int types 50 + 10 is 60
Subtraction of two int types 50 - 10 is 40
Product of two int types 50 * 10 is 500
Division of two int types 50 / 10 is 5.0

Addition of two float types 3.99 + 2.5 is 6.49
Subtraction of two float types 3.99 - 2.5 is 1.4900000000000002
Product of two float types 3.99 * 2.5 is 9.975000000000001
Division of two float types 3.99 / 2.5 is 1.596

Addition of two complex types (3+4j) + (2+5j) is (5+9j)
Subtraction of two complex types (3+4j) - (2+5j) is (1-1j)
Product of two complex types (3+4j) * (2+5j) is (-14+23j)
Division of two complex types (3+4j) / (2+5j) is (0.896551724137931-0.24137931034482757j)
 
 
OUTPUT 2:
====================
Enter First Integer : 3
Enter Second Integer : 0
Enter First Floating Point Number : 4.5
Enter Second Floating Point Number : 0.0
Enter first Complex Number : 3+4j
Enter second Complex Number : 0

Addition of two int types 3 + 0 is 3
Subtraction of two int types 3 - 0 is 3
Product of two int types 3 * 0 is 0
Division by Zero 3 / 0 is Not Possible
 
Addition of two float types 4.5 + 0.0 is 4.5
Subtraction of two float types 4.5 - 0.0 is 4.5
Product of two float types 4.5 * 0.0 is 0.0
Division by Zero 4.5 / 0.0 is Not Possible
 
Addition of two complex types (3+4j) + 0j is (3+4j)
Subtraction of two complex types (3+4j) - 0j is (3+4j)
Product of two complex types (3+4j) * 0j is 0j
Division by Zero (3+4j) / 0j is Not Possible 
 

 
Conclusion : Try the program by yourself and comment down below if any queries / suggestions.


 

Post a Comment

0 Comments