Curriculum
In this lesson, you will learn about numbers in python, some of its basic operations and how to use them in your python applications.
As you have learned about strings in the previous lessons, let’s move on to the next data type Numbers in python.
Numbers in python are used to store numeric values and they are immutable, i.e. when you change the numerical value of a variable, it will result in a new object. Python supports the following types:
int or Integers are the whole numbers, including negative numbers without a limitation of value.
Syntax:
a=57
You can straightaway declare the numerical value here, unlike in other languages where you need to define the type of numbers that you’re assigning.
Note:
Similar to int, float accommodates the positive and negative integers in their decimal form.
Syntax:
a=57.00
Complex numbers are numbers with real and imaginary parts. Consider the equation x+jy or x+yj, here ‘x’ is the real part and ‘yj’ is the imaginary part. Therefore, when you perform a mathematical operation on complex numbers, the real parts get added separately and the imaginary part gets added separately.
Syntax:
a=57+75j
Now let us perform some mathematical operations on the number data types.
Input:
a=int(input("Enter the value of a:" )) b=int(input("Enter the value of b:" )) print("Adding both values :", a+b) print("Subtracting both values :", a-b) print("Multiplying both values :", a*b) print("Dividing both values :", a/b) print("Mod of two values:", a%b) c=float(input("Enter the value of ‘c’:")) print("Value of c is :",c) d=complex(input("Enter the value of d:")) e= complex(input("Enter the value of e:")) print("Adding two complex numbers : ", d+e)
Output:
Enter the value of a:57 Enter the value of b:75 Adding both values : 132 Subtracting both values : -18 Multiplying both values : 4275 Dividing both values : 0.76 Mod of two values: 57 Enter the value of ‘c’:77 Value of c is : 77.0 Enter the value of d:67+57j Enter the value of e:57+67j Adding two complex numbers :(124+124j)
Learn more about python programming and other programming languages at DeveloperPublish.com