Completed
-
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 Basic Operators
Operators are essential components in Python that allow you to perform operations on variables and values. Python supports a variety of operators, and they can be classified into different categories, such as arithmetic, comparison, logical, and assignment operators. This section covers the basic operators that you will frequently use in Python programming.
1. Arithmetic Operators
These operators are used to perform mathematical operations.
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 |
Example:
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)
- +: Adds two numbers.
- -: Subtracts the second number from the first.
- *: Multiplies two numbers.
- /: Divides the first number by the second, returning a float.
- //: Performs integer (floor) division, discarding the fractional part.
- %: Returns the remainder after division (modulo).
- **: Raises the first number to the power of the second.
2. Comparison Operators
Comparison operators are used to compare two values. These operators return a Boolean value (True or False).
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) |
Example:
a = 5 b = 3 print(a == b) # False (not equal) print(a != b) # True (not equal) print(a > b) # True (a is greater than b) print(a < b) # False (a is not less than b) print(a >= b) # True (a is greater than or equal to b) print(a <= b) # False (a is not less than or equal to b)
- ==: Checks if two values are equal.
- !=: Checks if two values are not equal.
- >: Checks if the left value is greater than the right value.
- <: Checks if the left value is less than the right value.
- >=: Checks if the left value is greater than or equal to the right value.
- <=: Checks if the left value is less than or equal to the right value.
3. Logical Operators
Logical operators are used to combine conditional statements and evaluate the conditions.
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 a condition (negation) | not (5 > 3) (False) |
Example:
a = 5 b = 3 print(a > b and b < 4) # True (both conditions are true) print(a > b or b > 4) # True (one condition is true) print(not(a > b)) # False (negation)
- and: Returns True only if both conditions are True.
- or: Returns True if at least one condition is True.
- not: Reverses the truth value of the condition.
4. Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assigns a value to a variable | a = 5 |
+= | Adds and assigns the result | a += 3 (a = a + 3) |
-= | Subtracts and assigns the result | a -= 2 (a = a - 2) |
*= | Multiplies and assigns the result | a *= 2 (a = a * 2) |
/= | Divides and assigns the result | a /= 2 (a = a / 2) |
//= | Floor divides and assigns the result | a //= 2 (a = a // 2) |
%= | Takes the modulus and assigns the result | a %= 2 (a = a % 2) |
**= | Exponentiates and assigns the result | a **= 2 (a = a ** 2) |
Example:
a = 10 a += 5 # a = a + 5 -> a = 15 a -= 3 # a = a - 3 -> a = 12 a *= 2 # a = a * 2 -> a = 24 print(a)
- =: Assigns a value to a variable.
- +=: Adds the value and assigns the result to the variable.
- -=: Subtracts the value and assigns the result to the variable.
- *=: Multiplies the value and assigns the result to the variable.
- /=: Divides the value and assigns the result to the variable.
- //=: Floor divides the value and assigns the result to the variable.
- %=: Takes the modulus and assigns the result to the variable.
- **=: Raises the value to the power and assigns the result to the variable.
5. Membership Operators
These operators are used to check if a value or variable is found in a sequence (like a list, tuple, or string).
Operator | Description | Example |
---|---|---|
in | Returns True if a value is found in a sequence | "a" in "apple" (True) |
not in | Returns True if a value is not found in a sequence | "b" not in "apple" (True) |
Example:
fruits = ["apple", "banana", "cherry"] print("banana" in fruits) # True print("grape" not in fruits) # True
- in: Returns True if the value is found in the sequence.
- not in: Returns True if the value is not found in the sequence.
6. Identity Operators
These operators are used to compare the memory locations of two objects.
Operator | Description | Example |
---|---|---|
is | Returns True if two variables point to the same object | a is b |
is not | Returns True if two variables do not point to the same object | a is not b |
Example:
a = [1, 2, 3] b = [1, 2, 3] c = a print(a is b) # False (a and b are different objects) print(a is c) # True (a and c point to the same object)
- is: Returns True if both variables point to the same object.
- is not: Returns True if both variables do not point to the same object.
Summary:
- Arithmetic operators are used for mathematical calculations.
- Comparison operators are used to compare two values.
- Logical operators combine multiple conditions to evaluate expressions.
- Assignment operators are used to assign values to variables, and can also be used with arithmetic operations.
- Membership operators check if a value is present or absent from a sequence.
- Identity operators check if two variables point to the same memory location or object.
These operators are essential tools for performing tasks, making decisions, and manipulating data in Python.
Commenting is not enabled on this course.