DEV Community

Cover image for PYTHON OPERATORS – Day 4
augustineowino357-design
augustineowino357-design

Posted on • Edited on

PYTHON OPERATORS – Day 4

Python Operators

In the previous lesson, we learned about Input and Output operations in Python. Today, we will learn about Operators, which are used to perform operations on variables and values.

Operators are important in programming because they help perform calculations, comparisons, logical decisions, and data manipulation.


What are Operators?

Operators are special symbols used to perform operations on operands (values or variables).

Example

x = 10
y = 5

print(x + y)
Enter fullscreen mode Exit fullscreen mode

Output

plaintext
15
Enter fullscreen mode Exit fullscreen mode

In this example:

  • "+" is the operator
  • "x" and "y" are operands

Types of Operators in Python

Python provides several types of operators:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Identity Operators
  6. Membership Operators
  7. Bitwise Operators

1. Arithmetic Operators

Arithmetic Operators are used for mathematical calculations.

Operator| Meaning| Example
"+"| Addition| "x + y"
"-"| Subtraction| "x - y"
""| Multiplication| "x * y"
"/"| Division| "x / y"
"%"| Modulus| "x % y"
"
"| Exponent| "x * y"
"//"| Floor Division| "x // y"

Example

python
x = 20
y = 3

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
print(x ** y)
print(x // y)
Enter fullscreen mode Exit fullscreen mode

Output

plaintext
23
17
60
6.666666666666667
2
8000
6
Enter fullscreen mode Exit fullscreen mode

2.Assignment Operators

Assignment Operators are used to assign values to variables.

Operator| Example
"="| "x = 5"
"+="| "x += 3"
"-="| "x -= 3"
"*="| "x *= 3"
"/="| "x /= 3"

Example

python
x = 10

x += 5

print(x)
Enter fullscreen mode Exit fullscreen mode

Output

plaintext
15
Enter fullscreen mode Exit fullscreen mode

3. Comparison Operators

Comparison Operators compare two values and return either "True" or "False".

Operator| Meaning
"=="| Equal to
"!="| Not equal to
">"| Greater than
"<"| Less than
">="| Greater than or equal to
"<="| Less than or equal to

Example

python
x = 10
y = 20

print(x == y)
print(x != y)
print(x < y)

Enter fullscreen mode Exit fullscreen mode

Output

False
True
True
Enter fullscreen mode Exit fullscreen mode

4. Logical Operators

Logical Operators are used to combine conditions.

Operator| Meaning
"and"| Returns True if both conditions are True
"or"| Returns True if at least one condition is True
"not"| Reverses the result

Example

python
x = 5

print(x > 2 and x < 10)
print(x > 10 or x < 10)
print(not(x > 2))
Enter fullscreen mode Exit fullscreen mode

Output

plaintext
True
True
False
Enter fullscreen mode Exit fullscreen mode

5. Identity Operators
Identity Operators compare memory locations of objects.

Operator| Meaning
"is"| Returns True if both variables are the same object
"is not"| Returns True if variables are not the same object

Example

python
x = [1, 2, 3]
y = x

print(x is y)
Enter fullscreen mode Exit fullscreen mode

Output

plaintext
True
Enter fullscreen mode Exit fullscreen mode

6. Membership Operators

Membership Operators check whether a value exists in a sequence.

Operator| Meaning
"in"| Returns True if value exists
"not in"| Returns True if value does not exist

Example

python
languages = ["Python", "Java", "C++"]

print("Python" in languages)
print("PHP" not in languages)
Enter fullscreen mode Exit fullscreen mode

Output

plaintext
plaintext
True
True
Enter fullscreen mode Exit fullscreen mode

7. Bitwise Operators

Bitwise Operators perform operations on binary numbers.

Operator| Meaning
"&"| AND
|
"^"| XOR
"~"| NOT
"<<"| Left Shift
">>"| Right Shift

Example

python
x = 5
y = 3

print(x & y)
Enter fullscreen mode Exit fullscreen mode

Output

plaintext
1
Enter fullscreen mode Exit fullscreen mode

Operator Precedence

Python follows a specific order when evaluating expressions.

Example

python
result = 5 + 2 * 3

print(result)
Enter fullscreen mode Exit fullscreen mode

Output

11
Enter fullscreen mode Exit fullscreen mode

Multiplication happens before addition because it has higher precedence.


Why Operators are Important

Operators help programmers:

  • Perform calculations
  • Compare values
  • Make decisions
  • Build conditions
  • Manipulate data
  • Create dynamic programs

Without operators, programming logic would not work effectively.


Conclusion

Operators are essential building blocks in Python programming. They allow programs to perform mathematical calculations, comparisons, logical decisions, and many other operations.

Understanding operators is important because they are used in almost every Python program.

Practice using different operators to become more comfortable with Python expressions and logic.

Python #PythonForBeginners #CodingJourney #LearnPython #Programming

Top comments (0)