In this tutorial, we will different types of numbers in Python, how to define them, and how to use them, with examples.
There are two basic types of numbers in Python. They are
- Floating-point Numbers
An Integer is a positive or negative whole number without any fraction. For example: 25, -96, 0, 125, 33321 are some of the integers.
To define an integer, we can assign the integer value to a variable using assignment operator, as shown in the following example.
n = 15
We can also use Python builtin function int() to define an integer.
n = int(15)
A Floating-point number is a positive or negative whole number with decimal point. For example: 3.14, -1.96, 0.88, 12.5, 0.0033321 are some of the floating-point numbers.
To define a floating-point number, we can assign the float value to a variable using assignment operator, as shown in the following example.
n = 3.14
We can also use Python builtin function float() to define an integer.
n = float(3.14)
Arithmetic Operations on Numbers
We can perform arithmetic operations on numbers using arithmetic operators.
In the following example, we have taken two numbers and did addition of these two numbers.
a = 6 b = 9 result = a + b print('Result =', result)
Result = 15
Similarly, we can do other operations like subtraction, multiplication, division, etc.
In this Python Tutorial, we learned about different types of numbers in Python, how to define each of them, and use in our programs, with examples.