Python 3 – Numbers
Python is a very powerful programming language that is used to build several different kinds of software applications. One of the most significant features of Python is its capability to handle numbers efficiently. In this article, we will discuss how to work with numbers in Python 3, including integer, floating-point, and complex numbers.
Python 3 Number data types:
There are three different number data types available in Python: Integers, Floating-Point, and Complex numbers.
- Integers: The integer represents whole numbers. These numbers can be either positive or negative or zero.
# Python code to show integer data type a = 10 b = -10 c = 0 print("The type of variable a is:", type(a)) print("The type of variable b is:", type(b)) print("The type of variable c is:", type(c))
Output:
“`bash
The type of variable a is: <class 'int'>
The type of variable b is: <class 'int'>
The type of variable c is: <class 'int'>
“`
- Floating-Point: The floating-point number represents real numbers. These numbers contain a decimal point.
# Python code to show floating-point data type a = 10.5 b = -10.5 c = 0.0 print("The type of variable a is:", type(a)) print("The type of variable b is:", type(b)) print("The type of variable b is:", type(c))
Output:
“`bash
The type of variable a is: <class 'float'>
The type of variable b is: <class 'float'>
The type of variable c is: <class 'float'>
“`
- Complex numbers: A complex number is a combination of a real number and an imaginary number.
# Python code to show complex data type a = 3 + 5j b = 1j c = complex(3, 5) print("The type of variable a is:", type(a)) print("The type of variable b is:", type(b)) print("The type of variable c is:", type(c))
Output:
“`bash
The type of variable a is: <class 'complex'>
The type of variable b is: <class 'complex'>
The type of variable c is: <class 'complex'>
“`
Python 3 Arithmetic Operators:
Python 3 provides several arithmetic operators to perform operations on numbers. Here are some of the essential arithmetic operators:
- Addition (+): It is used to add two numbers.
# Python code to add two numbers a = 10 b = 20 print("The sum of a and b is:", a + b)
Output:
“`bash
The sum of a and b is: 30
“`
- Subtraction (-): It is used to subtract one number from another.
# Python code to subtract two numbers a = 40 b = 20 print("The difference between a and b is:", a - b)
Output:
“`bash
The difference between a and b is: 20
“`
- Multiplication (*): It is used to multiply two numbers.
# Python code to multiply two numbers a = 5 b = 5 print("The product of a and b is:", a * b)
Output:
“`bash
The product of a and b is: 25
“`
- Division (/): It is used to divide one number by another.
# Python code for division a = 25 b = 5 print("The quotient of a and b is:", a / b)
Output:
“`bash
The quotient of a and b is: 5.0
“`
- Modulus (%): It returns the remainder when one number is divided by another.
# Python code for modulus a = 20 b = 3 print("The remainder when a is divided by b is:", a % b)
Output:
“`bash
The remainder when a is divided by b is: 2
“`
- Floor Division (//): It returns the integer part of the result of dividing two numbers.
# Python code for floor division a = 25 b = 6 print("The integer part of a divided by b is:", a // b)
Output:
“`bash
The integer part of a divided by b is: 4
“`
- Exponentiation (**): It is used to calculate the exponential power of the number.
# Python code for exponentiation a = 2 b = 5 print("The value of a to the power of b is:", a ** b)
Output:
“`bash
The value of a to the power of b is: 32
“`
Conclusion:
In conclusion, Python 3 has a powerful number handling capability that allows developers to perform various mathematical operations efficiently. We have covered three different number data types available in Python, namely integers, floating-point, and complex numbers, and how to perform arithmetic operations in Python using different operators. By using Python’s number handling capabilities, developers can build efficient and powerful applications with ease.