Basic Operators in Python
When it comes to programming in Python, one of the most important concepts to understand is the use of operators. Operators are used to perform various operations on variables, values, and other elements in a program. In this article, we will discuss the basic operators in Python, their syntax, and examples of their usage.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. The basic arithmetic operators in Python are as follows:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Exponentiation (**)
- Floor Division (//)
Example:
a = 10 b = 5 # Addition print(a + b) # Output: 15 # Subtraction print(a - b) # Output: 5 # Multiplication print(a * b) # Output: 50 # Division print(a / b) # Output: 2.0 # Modulus print(a % b) # Output: 0 # Exponentiation print(a ** b) # Output: 100000 # Floor Division print(a // b) # Output: 2
Comparison Operators
Comparison operators are used to compare two values or variables. The result of a comparison operator is either True or False. The basic comparison operators in Python are as follows:
- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
Example:
a = 10 b = 5 # Equal to print(a == b) # Output: False # Not equal to print(a != b) # Output: True # Greater than print(a > b) # Output: True # Less than print(a < b) # Output: False # Greater than or equal to print(a >= b) # Output: True # Less than or equal to print(a <= b) # Output: False
Logical Operators
Logical operators are used to combine multiple conditions or values and evaluate them as a single expression. The basic logical operators in Python are as follows:
- And (and)
- Or (or)
- Not (not)
Example:
a = True b = False # And print(a and b) # Output: False # Or print(a or b) # Output: True # Not print(not a) # Output: False
Assignment Operators
Assignment operators are used to assign values to variables. The basic assignment operators in Python are as follows:
- Assignment (=)
- Addition assignment (+=)
- Subtraction assignment (-=)
- Multiplication assignment (*=)
- Division assignment (/=)
- Modulus assignment (%=)
- Exponentiation assignment (**=)
- Floor division assignment (//=)
Example:
a = 10 b = 5 # Assignment c = a print(c) # Output: 10 # Addition assignment a += b print(a) # Output: 15 # Subtraction assignment a -= b print(a) # Output: 10 # Multiplication assignment a *= b print(a) # Output: 50 # Division assignment a /= b print(a) # Output: 10.0 # Modulus assignment a %= b print(a) # Output: 0 # Exponentiation assignment a **= b print(a) # Output: 0 # Floor division assignment a //= b print(a) # Output: 0
Conclusion
In this article, we discussed the basic operators in Python, including arithmetic, comparison, logical, and assignment operators. Understanding these operators is essential for writing effective Python programs. By using operators in your programs, you can perform various operations, manipulate data, and control the flow of your program.