Operators in Python

# Operators in Python - Basics of Python - Software Development

 Table of contents Introduction Arithmetic Operators Comparison Operators Logical Operators ## Introduction

In Python, operators are symbols that perform various operations on variables or values. They allow you to manipulate data and perform calculations. Understanding operators is essential for writing effective and efficient Python code. In this article, we will explore different types of operators in Python, along with examples and explanations.

## Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations. Here are the commonly used arithmetic operators in Python:

• Subtraction (-): Subtracts the second operand from the first.
• Multiplication (*): Multiplies two operands.
• Division (/): Divides the first operand by the second (result is always a float).
• Floor Division (//): Divides the first operand by the second and returns the largest integer less than or equal to the quotient.
• Modulus (%): Returns the remainder when the first operand is divided by the second.
• Exponentiation (**): Raises the first operand to the power of the second.

Let's see some examples:

x = 10

y = 3

print(x + y)  # Output: 13

# Subtraction

print(x - y)  # Output: 7

# Multiplication

print(x * y)  # Output: 30

# Division

print(x / y)  # Output: 3.3333333333333335

# Floor Division

print(x // y)  # Output: 3

# Modulus

print(x % y)  # Output: 1

# Exponentiation

print(x ** y)  # Output: 1000

## Comparison Operators

Comparison operators are used to compare two values. They return a Boolean value (True or False) based on the comparison result. Here are the comparison operators in Python:

• Equal to (==): Checks if the operands are equal.
• Not equal to (!=): Checks if the operands are not equal.
• Greater than (>): Checks if the left operand is greater than the right.
• Less than (<): Checks if the left operand is less than the right.
• Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right.
• Less than or equal to (<=): Checks if the left operand is less than or equal to the right.

Let's see some examples:

x = 5

y = 10

# Equal to

print(x == y)  # Output: False

# Not equal to

print(x != y)  # Output: True

# Greater than

print(x > y)  # Output: False

# Less than

print(x < y)  # Output: True

# Greater than or equal to

print(x >= y)  # Output: False

# Less than or equal to

print(x <= y)  # Output: True

## Logical Operators

Logical operators are used to combine multiple conditions and perform logical operations. They return a Boolean value based on the evaluation result. Here are the logical operators in Python:

• Logical AND (and): Returns True if both operands are True.
• Logical OR (or): Returns True if at least one of the operands is True.
• Logical NOT (not): Reverses the logical state of the operand.

Let's see some examples:

x = 5

y = 10

z = 3

# Logical AND

print(x < y and y < z)  # Output: False

# Logical OR

print(x < y or y < z)  # Output: True

# Logical NOT

print(not x < y)  #

The document Operators in Python | Basics of Python - Software Development is a part of the Software Development Course Basics of Python.
All you need of Software Development at this link: Software Development

## Basics of Python

49 videos|38 docs|18 tests

## Basics of Python

49 videos|38 docs|18 tests Explore Courses for Software Development exam Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

;