Skip to main content
Practice

operators

---
id: operators
title: Symbols for Performing Operations, Operators
description: Key Python Operators with Examples
tags:
- operator
- literals
- Python
- programming
sidebar_position: 17
isPublic: false
---

# Symbols for Performing Operations, Operators

In programming, an `Operator` refers to a **symbol** or **keyword** used to perform a specific operation.

<br />

## Key Python Operators

Operators are symbols that perform mathematical or logical operations. In Python, the following operators are mainly used:

<br />

### Arithmetic Operators

Operators like `+`(addition), `-`(subtraction), `*`(multiplication), `/`(division), `**`(exponentiation), `%`(modulus), `//`(floor division) are used to perform arithmetic operations between numbers.

```python title="Example of Using Arithmetic Operators"
multiply = 10 * 5 # 50

division = 10 / 2 # 5.0, Division in Python returns a float

integer_division = 10 // 3 # 3, Returns the integer part of the division result

remainder = 10 % 3 # 1

power = 2 ** 3 # 8

Assignment Operators

Assigns values to variables, or assigns the result of operations to variables.

  • =: Assign the value on the right to the variable on the left (e.g., x = 1 assigns 1 to the variable x)

  • +=: Add the value on the right to the variable on the left, then assign the result to the left variable (e.g., x += 1 is equivalent to x = x + 1)

  • -=: Subtract the value on the right from the variable on the left, then assign the result to the left variable (e.g., x -= 1 is equivalent to x = x - 1)

In Python, = does not mean "equal to" mathematically, but rather assigns the value on the right to the left.

The operator for "equal to" is ==.


Example of Using Assignment Operators
x = 10
y = 20

x += 10 # Equivalent to x = x + 10

print(x) # 20

Comparison Operators

Compare values to see if they are equal, greater, or less.

  • ==(equal to), !=(not equal)

  • >(left is greater), <(right is greater)

  • >=(left is greater or equal), <=(right is greater or equal)


Example of Using Comparison Operators
x = 10
y = 20

# == : equal to
print(x == y) # False

# != : not equal to
print(x != y) # True

# > : left is greater
print(x > y) # False

# <= : right is greater or equal
print(x <= y) # True

Logical Operators

Perform logical operations within a program.

  • and: The result is true only if both the left and right conditions are true.

  • or: The result is true if at least one of the left or right conditions is true.

  • not: Reverses the result of the condition.

Example of Using Logical Operators
x = 10
y = 20

# and: Result is true only if both conditions are true
print(x > 5 and y > 15)
# Both conditions are true, so the result is True

# or: Result is true if at least one condition is true
print(x < 5 or y > 15)
# The condition y > 15 is true, so the result is True

# x > 5 is true, but the not operator reverses the result to False
print(not x > 5)

Coding Practice

In programs, the multiplication symbol is represented by an asterisk (*).

Store the result of multiplying 10 and 5 in the multiply variable of the blank space and print the result.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.