Arithmetic operators are fundamental components of any programming language, and Python is no exception. These operators enable us to perform mathematical operations, such as addition, subtraction, multiplication, and division, in Python programs. In this video, we will explore the different arithmetic operators available in Python and how to use them.
The basic arithmetic operators in Python are the addition, subtraction, multiplication, and division operators represented by the symbols +, -, *, and /, respectively. These operators are used to perform basic mathematical operations and can be applied to integers, floating-point numbers, and complex numbers.
In addition to these operators, Python also provides a floor division operator (//) and a modulus operator (%). The floor division operator returns the integer quotient of the division operation, discarding any remainder, while the modulus operator returns the remainder of the division operation.
Finally, we have the power operator (**), which is used to raise a number to a certain power. This operator takes two operands, the base and the exponent, and returns the result of the base raised to the power of the exponent.
Table of Contents
Introduction to Arithmetic Operators in Python
Arithmetic operators are used in Python to perform mathematical operations on numerical values. Python supports a variety of arithmetic operators, including addition, subtraction, multiplication, division, and more. Let’s explore each of these operators and show you how to use them in your Python programs.
Addition (+) Operator
The addition operator is used to add two values. For example:
a, b = 4, 5 c = a + b print(c)
Subtraction (-) Operator
The subtraction operator is used to subtract one value from another. For example:
a, b = 6, 4 c = a – b print(c)
Multiplication (*) Operator
The multiplication operator is used to multiply two values. For example:
a, b = 7, 8 c = a * b print(c)
Division (/) Operator
The division operator is used to divide one value by another. For example:
a, b = 22, 7 c = a / b print(c)
Floor Division (//) Operator
The floor division operator is used to get the result of the division as an integer value. For example:
c = 22 // 7 print(c)
Modulus (%) Operator
The modulus operator is used to get the remainder of a division. For example:
c = 22 % 7 print(c)
Power (**) Operator
The power operator is used to raise a number to a power. For example:
c = 2 ** 3 print(c)
Underscore (_) in Python
Underscore is not an operator or a function in Python. It is a variable that stores the last printed value. For example:
a, b = 10, 20 print(a + b)
print(b + _)
Round() Function in Python
The round() function is used to round off a decimal value. If the number at the decimal place is greater than or equal to 5, then the number is rounded up; else, it is rounded down. For example:
print(round(17.2)) print(round(17.7)) print(round(17.5)) print(round(12.5))
Output: 17 18 18 12
Arithmetic operators are an essential part of Python programming. With these operators, you can perform mathematical operations on numerical values and manipulate data in your programs.