Types of Numbers and Calculations in Python
In this Python tutorial, you will learn how to work with numbers in Python. You will have a complete understanding of numbers at the end of this session.
There are three types of numbers in Python:
integers, floating-point numbers, and Complex.
An integer type is a number that is a whole number. It could be both positive and negative. There is no decimal in it.
How to Learn Python for Free? »
Consider the following scenario:
a=15 #int b= 107 #int c= -659 #int
Float Type- It has decimal and can be either positive or negative.
Consider the following scenario:
a= 9.20 #float b=15.0 #float c= -147.23 #float
Complex numbers are those that have the form a+bJ, where J is the square root.
a= 16+8j # complex b= 9j #complex c= -4j #complex
You may check the type of any number by typing-
print(type(a))
and it will yield the number’s type. For instance, if you want to check the type of the number of a=5, you can do it as follows.
a=50 print(type(a)) <class 'int'>
Type Conversion- You may easily convert a number from one type to another, such as from an int to a float, a float to a complex, and so on.
Check it out. How to convert a specific integer type-
a= 16 #int b=72.9 #float c= 72+5j #complex
From int to float-
x= float(a)
From float to int-
y= int(b)
From float to complex-
z= complex(b)
It’s as simple as that; you can change from one type to another with only one line of code.
Complex numbers cannot be converted to any other type.
Simple Python Calculations- Now that you’ve mastered the different sorts of numbers and how to convert them, it’s time to practice some basic python calculations.
So, are you all set? Let’s begin with some basic math.
Best Books For Deep Learning »
Simply type as you enter on the calculator for simple addition, subtraction, multiplication, and division. Isn’t it simple? Let’s take a look at how to do it-
To practice with us, copy and paste this code into your Python environment
15+15 #addition 30
10-10 #substraction 0
6*6 #multiplication 36
15/15 #division 1.0 # float type
5//5 #division 1 #int type
For the square root of number-
5**5 3125
For remainder/modulus–
20%3 2
BIDMAS Rule-If you want to execute a longer calculation, you must follow the BIDMAS Rule. Let’s look at an example to see how it impacts the entire calculation.
5+5*3 20
Because * has a higher priority than + according to the BIDMAS Rule, it returns 20 as a result.
If you want to execute addition first, put it in a bracket, since in the BIDMAS Rule, a bracket takes precedence over * Let’s look at an example where the outcome is changed just by using a bracket ().
(5+5)*3 30
Numbers can be stored in variables and then used to conduct calculations on them. Let’s take a look at how you can do it-
age=35 #store number in variable "age" age #print "age" 35
perform addition on “age”
age+5 40
“age+5” just adds 5 to your age variable’s value of 35, but it has no effect on the value of the variable “age.” If you type in “age,” you’ll get a result of 35.
age 35 #print 35 again
If you want to change the value of “age,” all you have to do is write-
age= age+5 #add 5 in variable "age", not direct. age #print age 40
There’s another technique to change the value of the variable “age.” Take a peek at it as well
Data Visualization with R-Scatter plots »
age+=5 #add 5 in variable "age", not direct. age #print age 40
You can conduct various computations on a variable with just a few lines of code, just like you can with addition. Let’s take a closer look
age=25 age-=5 #subtraction age 20
age/=2 #division age 15.0
Sure now have a solid grasp of numbers in Python. Now it’s time to get down to business with some high-level calculations.
Salary=50000 bills=20000 rent=1000 food=5000 savings= Salary -bills-rent-food savings 24000
Hurray, Happy Learning,
Okay, start learning something new in the upcoming tutorial.