-
1. Introduction to Python
-
2. Python Basics
-
3. Working with Data Structures
-
4. Functions and Modules
-
5. Object-Oriented Programming (OOP)
-
6. File Handling
-
7. Error and Exception Handling
-
8. Python for Data Analysis
-
9. Advanced Topics in Python
-
10. Working with APIs
-
11. Python for Automation
-
12. Capstone Projects
- 13. Final Assessment and Quizzes
2.3.1 Arithmetic, comparison, and logical operators
Python supports a variety of operators that allow you to perform different types of operations on data. These operators are categorized into three main types: Arithmetic Operators, Comparison Operators, and Logical Operators. Here’s an overview of each type:
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations. These are the basic operations we perform in mathematics, such as addition, subtraction, multiplication, etc.
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 5 - 3 = 2 |
* | Multiplication | 5 * 3 = 15 |
/ | Division | 5 / 3 = 1.666 |
// | Floor Division (integer division) | 5 // 3 = 1 |
% | Modulus (remainder of division) | 5 % 3 = 2 |
** | Exponentiation (power) | 5 ** 3 = 125 |
Examples:
a = 5 b = 3 print(a + b) # 8 (Addition) print(a - b) # 2 (Subtraction) print(a * b) # 15 (Multiplication) print(a / b) # 1.666 (Division) print(a // b) # 1 (Floor Division) print(a % b) # 2 (Modulus) print(a ** b) # 125 (Exponentiation)
2. Comparison Operators
Comparison operators are used to compare two values or expressions. They return a Boolean value (True or False) based on the condition.
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 3 (False) |
!= | Not equal to | 5 != 3 (True) |
> | Greater than | 5 > 3 (True) |
< | Less than | 5 < 3 (False) |
>= | Greater than or equal to | 5 >= 3 (True) |
<= | Less than or equal to | 5 <= 3 (False) |
Examples:
a = 5 b = 3 print(a == b) # False (Equal comparison) print(a != b) # True (Not equal comparison) print(a > b) # True (Greater than comparison) print(a < b) # False (Less than comparison) print(a >= b) # True (Greater than or equal to comparison) print(a <= b) # False (Less than or equal to comparison)
3. Logical Operators
Logical operators are used to combine multiple conditions or expressions, allowing you to perform logical tests.
Operator | Description | Example |
---|---|---|
and | Returns True if both conditions are True | (5 > 3) and (8 > 6) (True) |
or | Returns True if at least one condition is True | (5 > 3) or (2 > 6) (True) |
not | Reverses the result of the condition (negation) | not (5 > 3) (False) |
Examples:
a = 5 b = 3 # 'and' checks if both conditions are True print(a > b and b < 4) # True (both conditions are true) # 'or' checks if at least one condition is True print(a > b or b > 4) # True (one condition is true) # 'not' negates the condition print(not(a > b)) # False (negation of 'a > b')
- and: Returns True only if both conditions are True. If either condition is False, it returns False.
- or: Returns True if at least one condition is True. It only returns False if both conditions are False.
- not: Reverses the Boolean value of the condition. If the condition is True, not makes it False, and vice versa.
Summary of Operators
- Arithmetic Operators are used for performing basic mathematical calculations like addition, subtraction, multiplication, etc.
- Comparison Operators are used to compare two values and return a Boolean result (True or False).
- Logical Operators are used to combine multiple Boolean expressions and evaluate them to True or False based on the given conditions.
These operators are crucial in programming, especially when dealing with decision-making, calculations, and logical flow in Python programs.
Commenting is not enabled on this course.