Python PART 3: Operators and Expressions in Python

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:

OperatorDescriptionExample
+Addition3 + 4
Subtraction3 – 4
*Multiplication3 * 4
/Division (float)3 / 4
//Division (integer quotient)3 // 4
%Modulus (remainder)3 % 4
**Exponentiation3 ** 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:

OperatorDescriptionExample
==Equal to3 == 4
!=Not equal to3 != 4
<Less than3 < 4
>Greater than3 > 4
<=Less than or equal to3 <= 4
>=Greater than or equal to3 >= 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:

OperatorDescriptionExample
andReturns True if both conditions are TrueTrue and False = False
orReturns True if at least one condition is TrueTrue or False = True
notReturns the opposite boolean value of the operandnot 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

Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *