In this article, we will talk about numbers in Python. Numbers in every programming language plays very important role because they are useful for mathematical functions. We can use numbers to build programs that requires calculation like, calculator, students mark reminder etc.
Inter and Float
You’ve got two main types of numbers, and that is integer whole numbers, and floating-point numbers or numbers with a decimal place. For example, if we do ‘age = 35’, this is an integer, and if we do ‘PI = 3.14159’, this is a float. Notice that the variable name itself is largely irrelevant in this explanation, what matters is the value. Here, we’ve got a whole number, a number that doesn’t have a decimal place, and here we’ve got a number with a decimal place, so this is a float.
Mathematics works just as normal, so we can have a variable called ‘maths_operation’ and make it equal to 1 + 3 * 4 / 2 – 2, making use of all the major mathematical symbols. And just as in normal math, the PEMDAS or BODMAS rules are followed, so multiplication is evaluated first, then division, then addition and then subtraction.
So here we would have 12 divided by 2 is 6, 1 plus 6 is 7, minus 2 is 5, and you can verify that by printing it out. When you run this numbers in python example, we’ll see 5.0 gets printed out there.
This is an important part of mathematics in Python, because whenever you do division, you always get a float, even if the result is essentially something point zero, which is basically a whole number, you always get back a float. So, if we do something like ‘float division’, and we’d say 12 divided by 3, then we print it out. And I’m just going to delete this code here for simplicity. So, if we do something like this, you’ll see that we get 4.0 back.
Numbers in Python | Integer Division
If you want to get rid of the floating point or the decimal place, you can do integer division. So here I’m going to create a new variable called ‘integer division’. You can call it whatever you want though. Remember, these are just names. And I’m going to do 12 // 3. And what this does is it performs the division, and then removes everything after the decimal place. So, this can come in handy at times.
So, if we run that, you’ll see that you get 4.0 first from this first ‘print’ statement, and then just 4 for the second one. Notice that if we change this to 8 / 3, the floating-point division is 2.6, but the integer division is 2. This here does not round the numbers up and down; it just removes everything after the decimal place. So, this is something to keep in mind as you do integer division later.
Read More | Best Programming languages to learn in 2022
Python Comments
Anywhere in Python you can also use the ‘#’ symbol to write a comment. So here we have something called an integer, and here we have a float. And what happens is when Python goes through this code, it ignores everything that comes after the ‘#’ symbol.
Conclusion
This is all about numbers in Python. Python is the world’s most used language today because to its simplicity. We can use Python is very big industries like, automation, networking, machine learning, AI (Artificial Intelligence). So, learning python can change your life.
Leave a Reply