Python PART 3: OPERATORS AND EXPRESSIONS IN Python
Please Subscribe Youtube| Like Facebook | Follow Twitter
Operators and Expressions in Python
In this article, we’ll cover the most commonly used operators and expressions in Python, along with their syntax, examples, and outputs.
Python is a powerful programming language that provides a range of operators and expressions to perform various operations on data. These operators and expressions help in performing arithmetic, comparison, and logical operations on values.
Arithmetic Operators
Python provides various arithmetic operators that perform mathematical calculations on numeric values. The following table shows the arithmetic operators available in Python:
Operator | Description | Example |
---|---|---|
+ | Addition | 3 + 4 |
– | Subtraction | 3 – 4 |
* | Multiplication | 3 * 4 |
/ | Division (float) | 3 / 4 |
// | Division (integer quotient) | 3 // 4 |
% | Modulus (remainder) | 3 % 4 |
** | Exponentiation | 3 ** 4 |
Here is an example code that demonstrates the use of arithmetic operators in Python:
# Arithmetic operators in Python
a = 10
b = 3
# Addition
print("a + b = ", a + b)
# Subtraction
print("a - b = ", a - b)
# Multiplication
print("a * b = ", a * b)
# Division (float)
print("a / b = ", a / b)
# Division (integer quotient)
print("a // b = ", a // b)
# Modulus (remainder)
print("a % b = ", a % b)
# Exponentiation
print("a ** b = ", a ** b)
Output
a + b = 13 a - b = 7 a * b = 30 a / b = 3.3333333333333335 a // b = 3 a % b = 1 a ** b = 1000
Comparison Operators
Comparison operators are used to compare two values and return a boolean value (True or False). The following table shows the comparison operators available in Python:
Operator | Description | Example |
---|---|---|
== | Equal to | 3 == 4 |
!= | Not equal to | 3 != 4 |
< | Less than | 3 < 4 |
> | Greater than | 3 > 4 |
<= | Less than or equal to | 3 <= 4 |
>= | Greater than or equal to | 3 >= 4 |
Here is an example code that demonstrates the use of comparison operators in Python:
# Comparison operators in Python
a = 10
b = 3
# Equal to
print("a == b is ", a == b)
# Not equal to
print("a != b is ", a != b)
# Less than
print("a < b is ", a < b)
# Greater than
print("a > b is ", a > b)
# Less than or equal to
print("a <= b is ", a <= b)
# Greater than or equal to
print("a >= b is ", a >= b)
Output
a == b is False a != b is True a < b is False a > b is True a <= b is False a >= b is True
Logical Operators
Logical operators are used to perform logical operations on Boolean values. The following table shows the logical operators available in Python:
Operator | Description | Example |
---|---|---|
and | Returns True if both conditions are True | True and False = False |
or | Returns True if at least one condition is True | True or False = True |
not | Returns the opposite boolean value of the operand | not True = False |
Here is an example code that demonstrates the use of logical operators in Python:
# Logical operators in Python
a = True
b = False
# and operator
print("a and b is ", a and b)
# or operator
print("a or b is ", a or b)
# not operator
print("not a is ", not a)
Output
a and b is False a or b is True not a is False
Conclusion
In conclusion, operators and expressions are fundamental concepts in Python programming. Operators are special symbols that perform operations on one or more operands, while expressions are combinations of values, variables, and operators that produce a result.
Python Beginner Tutorial Series
- Python Part 1: Setup and Introduction
- Python Part 2: Understanding Basic Data Types And Variables In Python
- Python PART 3: OPERATORS AND EXPRESSIONS IN Python
- PYTHON PART 4: CONTROL FLOW STATEMENTS IN PYTHON
- Python PART 5: FUNCTIONS IN Python
- Python Part 6: Arrays and Lists In Python
- Python Part 7: String Manipulation In Python
- Python Part 8: Object-Oriented Programming In Python (Classes and Objects)
- Python Part 9: Object-oriented Programming In Python (OOP Pillars)
- Python Part 10: Exception Handling in Python