Arithmetic and Logical Operators in Python

Introduction

Python is a high-level, general-purpose programming language that allows us to perform various operations. One of the important aspects of programming is dealing with numbers and logical values. Python has a set of built-in arithmetic and logical operators that help us to perform various calculations and comparisons.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numerical data types, such as integers and floating-point numbers. Python provides the following arithmetic operators:

OperatorDescription
+Addition
Subtraction
*Multiplication
/Division
%Modulus
**Exponentiation
//Floor division

Addition

Addition operator + is used to add two values. Here is an example:

				
					a = 2
b = 3
c = a + b
print(c)    # Output: 5
				
			

Subtraction

Subtraction operator - is used to subtract two values. Here is an example:

				
					a = 2
b = 3
c = a - b
print(c)    # Output: -1
				
			

Multiplication

Multiplication operator * is used to multiply two values. Here is an example:

				
					a = 2
b = 3
c = a * b
print(c)    # Output: 6
				
			

Division

Division operator / is used to divide two values. Here is an example:

				
					a = 6
b = 3
c = a / b
print(c)    # Output: 2.0

				
			

Modulus

Modulus operator % is used to find the remainder of the division operation. Here is an example:

				
					a = 7
b = 3
c = a % b
print(c)    # Output: 1

				
			

Exponentiation

Exponentiation operator ** is used to raise a number to the power of another number. Here is an example:

				
					a = 2
b = 3
c = a ** b
print(c)    # Output: 8

				
			

Floor Division

Floor division operator // is used to perform integer division and return the quotient without the remainder. Here is an example:

				
					a = 7
b = 3
c = a // b
print(c)    # Output: 2

				
			

Logical Operators

Logical operators are used to perform logical operations on boolean values, such as True and False. Python provides the following logical operators:

OperatorDescription
andLogical AND
orLogical OR
notLogical NOT

Logical AND

Logical AND operator and is used to check if both the operands are True. Here is an example:

				
					a = 5
b = 7
c = 10
print(a < b and b < c)  # Output: True
print(a < b and b > c)  # Output: False

				
			

Logical OR

Logical OR operator or is used to check if at least one of the operands is True. Here is an example:

				
					a = 5
b = 7
c = 10
print(a < b or b > c)   # Output: True
print(a > b or b > c)   # Output: False

				
			

Logical NOT

Logical NOT operator not is used to invert the logical value

				
					x = 5
y = 3

print(not x == y) # Output: True

				
			

Conclusion

In this article, we covered the basics of arithmetic and logical operators in Python. These operators are essential in programming and are used in various situations to perform arithmetic and logical operations. It is important to have a good understanding of these operators to be able to write efficient and effective code. With this knowledge, you can start experimenting with these operators and write your own Python programs.

Scroll to Top