0% found this document useful (0 votes)
0 views8 pages

Day 5 Python(Operators)

The document provides an overview of various types of operators in programming, including arithmetic, relational, logical, bitwise, and assignment operators. It explains the functionality of each operator with examples, detailing how they operate on values and variables. Additionally, it includes truth tables and sample code snippets to illustrate the use of logical operators in Python.

Uploaded by

sree priya palle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views8 pages

Day 5 Python(Operators)

The document provides an overview of various types of operators in programming, including arithmetic, relational, logical, bitwise, and assignment operators. It explains the functionality of each operator with examples, detailing how they operate on values and variables. Additionally, it includes truth tables and sample code snippets to illustrate the use of logical operators in Python.

Uploaded by

sree priya palle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

OPERATORS

Operators are used to perform operations on values and variables.


ARITHMETIC OPERATORS(BODMAS)
Addition (+): Adds two numbers.
result = 3 + 2 # Output: 5
Subtraction (-): Subtracts one number from another.
result = 5 - 2 # Output: 3
Multiplication (*): Multiplies two numbers.
result = 3 * 2 # Output: 6
Division (/): Divides one number by another, returning a float.
result = 6 / 2 # Output: 3.0
Floor Division (//): Divides one number by another, returning an integer.
result = 7 // 2 # Output: 3
Modulus (%): Returns the remainder of a division.
result = 7 % 2 # Output: 1
Exponentiation (**): Raises one number to the power of another.
result = 3 ** 2 # Output: 9
Relational Operators:
1. Equal to (==): Checks if two values are equal.
2. Not equal to (!=): Checks if two values are not equal.
3. Greater than (>): Checks if the left value is greater than the right value.
4. Less than (<): Checks if the left value is less than the right value.
5. Greater than or equal to (>=): Checks if the left value is greater than or
equal to the right value.
6. Less than or equal to (<=): Checks if the left value is less than or equal to
the right value.
Logical Operators:

Operator Description Syntax Example

Returns True if
both the
and x and y x>7 and x>10
operands are
true

Returns True if
or either of the x or y x<7 or x>15
operands is true

Returns True if
not(x>7 and x>
not the operand is not x
10)
false
Truth Table for Logical Operators in Python

AND Operator in Python

a = 10

b = 10

c = -10

if a > 0 and b > 0:

print("The numbers are greater than 0")

if a > 0 and b > 0 and c > 0:

print("The numbers are greater than 0")

else:
print("Atleast one number is not greater than 0")

Python OR Operator

a = 10
b = -10
c=0
if a > 0 or b > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
if b > 0 or c > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
Python NOT Operator

a = 10

if not a:
print("Boolean value of a is True")
if not (a % 3 == 0 or a % 5 == 0):
print("10 is not divisible by either 3 or 5")
else:
print("10 is divisible by either 3 or 5")

and: Returns True if both operands are True.


result = (5 > 3) and (8 > 5) # Returns True
or: Returns True if at least one of the operands is True.
result = (5 > 3) or (8 < 5) # Returns True
not: Returns True if the operand is False, and False if the operand is True.
result = not (5 > 3) # Returns False
BITWISE OPERATORS:

SYNTAX
OPERATOR NAME DESCRIPTION

Result bit 1, if
both operand x
Bitwise AND bits are 1; &
& Bitwise AND
operator otherwise y
results bit 0.

Result bit 1, if
any of the x
Bitwise OR operand bit is 1; |
| Bitwise OR
operator otherwise y
results bit 0.

Result bit 1, if
any of the
Bitwise XOR operand bit is 1 x^
^ Bitwise XOR but not both, y
Operator
otherwise
results bit 0.

Bitwise NOT Inverts


~ Bitwise NOT ~x
Operator individual bits.

The left operand’s


value is moved
Python Bitwise
toward right by the
Bitwise >> right
number of bits x>>
Right Shift shift
specified by the right
operand.

The left operand’s


value is moved
Python toward left by the
Bitwise x<<
Bitwise << number of bits
left shift
Left Shift specified by the right
operand.
ASSIGNMENT OPERATOR:

You might also like