0% found this document useful (0 votes)
25 views23 pages

DAY 2 Operators

The document provides an overview of operators in Python, including arithmetic, assignment, comparison, logical, identity, and membership operators. Each type of operator is explained with syntax and examples, demonstrating how they are used to perform various operations on variables and values. The document emphasizes the importance of understanding these operators for effective programming in Python.

Uploaded by

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

DAY 2 Operators

The document provides an overview of operators in Python, including arithmetic, assignment, comparison, logical, identity, and membership operators. Each type of operator is explained with syntax and examples, demonstrating how they are used to perform various operations on variables and values. The document emphasizes the importance of understanding these operators for effective programming in Python.

Uploaded by

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

DAY 2

-Python Intern
Operators
 Operators are special symbols that perform
operations on variables and values. The
values that an operator acts on are called
operands.
 Here is an example:
 a = 5
 b = 6
 C = a + b
 11. Here, + is an operator that adds two

numbers: 5 and 6.
Types of operators
 Assignment Operators.
 Arithmetic Operators.
 Comparison (Relational) Operators.
 Logical Operators.
 Identity Operators.
 Membership Operators.
Python Arithmetic
Operators
 Arithmetic operators are used to perform
mathematical operations like addition,
subtraction, multiplication, etc.
 Syntax:

exp1 + exp2
exp1 - exp2
exp1 * exp2
exp1 ** exp2
exp1 / exp2
exp1 // exp2
exp1 % exp2
Operato Name Descriptions
r
+ Addition Operator It returns the addition of two
expressions.
- Subtraction It returns the subtraction of
Operator two expressions.
* Multiplication It returns the product of two
Operator expressions.
** Power Operator It returns the value of the
expression raised to the
given power i.e. it returns
the expression1 raised to the
power of expression2.
/ Division Operator
It returns the quotient of two
expressions.
// Floor Division It returns the integral part of
Operator the quotient of two
expressions.
% Modulus It returns the decimal part of
Operator the quotient i.e. remainder
Example
a=7
b=2

# addition
print ('Sum: ', a + b)

# subtraction
print ('Subtraction: ', a - b)

# multiplication
print ('Multiplication: ', a * b)

# division
print ('Division: ', a / b)
Example
a=7 #OUTPUT
b=2 Sum: 9
Subtraction: 5
# floor division Multiplication: 14
print ('Floor Division: ', a Division: 3.5
// b) Floor Division: 3
Modulo: 1
# modulo Power: 49
print ('Modulo: ', a % b)

# a to the power b
print ('Power: ', a ** b)
Assignment operators
 Assignment operators are used to assign
values to variables.
 Assignment operators in Python assign the

right-hand side values to the operand that is


present on the left-hand side.
 The assignment operators in Python is used

as the "=" symbol.


 We have different types of assignment

operators like +=, -=, \*=, %=, /=, //=, \*\


*=, &=, |=, ^=, >>=, <<=.

Example
x = 5 # Assigns the value 5 to variable x

x =5
x += 3 # Equivalent to x = x + 3; x is now 8
x =7
x -= 2 # Equivalent to x = x - 2; x is now 5
x =4
x *= 6 # Equivalent to x = x * 6; x is now 24
x = 10
x /= 2 # Equivalent to x = x / 2; x is now 5.0
x = 17
x %= 3 # Equivalent to x = x % 3; x is now 2
x =2
x **= 3 # Equivalent to x = x ** 3; x is now 8
Comparison Operators
 Comparison operators compare two
values/variables and return a Boolean
result: True or False.
 Here, the > comparison operator is used to
compare whether a is greater than b or not.

Operat Description Example


ors
== Is Equal To 3 == 5 gives us False
!= Not Equal To 3 != 5 gives us True

> Greater Than 3 > 5 gives us False


< Less Than 3 < 5 gives us True
>= Greater Than or 3 >= 5 give us False
Equal To
<= Less Than or Equal 3 <= 5 gives us True
To
Example 1: # OUTPUT
a=5 False
b=2 True
# equal to operator True
print(a == b) False
# not equal to operator True
print(a != b) False
# greater than operator
print(a > b)
# less than operator
print(a < b)
# greater than or equal to operator
print(a >= b)
# less than or equal to operator
print(a <= b)
Example 2 – use comparision
operators for string
 Equality (==) and Inequality (!=):
 Equality operator checks if two strings

have the same content, while the


inequality operator checks if they are
different.
#Example
str1 = "apple"
str2 = "orange"

print(str1 == str2) # Output: False


print(str1 != str2) # Output: True

str3 = "apple"
print(str1 == str3) # Output: True
print(str1 != str3) # Output: False
Logical Operators
 Logical operators are used to check whether
an expression is True or False. They are
used in decision-making.

Operators Description Example


and True only if both the operands are True a and b
or True if at least one of the operands is True a or b
not True if the operand is False and vice- not a
versa.
AND
True True True
False True False

True False False


False False False

OR
True True True
False True True

True False True


False False False
Example 1
# Example using and operator
x=5
y = 10
z = 15

result = (x < y) and (y < z)


# Output: True
# because both conditions are True
print(result)
Example 2
# Example using or operator
a=5
b = 10
c = 15

result = (a > b) or (b < c)


# Output: True
# because the second condition is True
print(result)
Example 3
# Example using not operator
value = True

result = not value


# Negates the boolean value of 'value'
print(result)
# Output: False
because the opposite of True is False
Python Identity Operators

 Identity operators are used to perform the comparison


operation on the objects i.e. these operators check whether
both operands refer to the same objects (with the same
memory location) or not.
 In Python, is and is not are used to check if two values are
located on the same part of the memory.
Oper Descriptions Exam
ator ple
is It returns True if both operands refer x is y
to the same objects; False, otherwise.
is It returns True if both operands do not x is
not refer to the same objects; False, not y
otherwise.
Example with Strings:
str1 = "hello"
str2 = "hello"
str3 = "world"
print(str1 is str2)
# Output: True, because both variables refer
to the same string object in memory
print(str1 is str3)
# Output: False, as str1 and str3 refer to
different string objects
print(str1 is not str3)
# Output: True, as str1 and str3 refer to
different string objects
Example with Numbers:
num1 = 10
num2 = 10
num3 = 20
print(num1 is num2)
# Output: True, because both variables refer
to the same integer object in memory
print(num1 is num3)
# Output: False, as num1 and num3 refer to
different integer objects
print(num1 is not num3)
# Output: True, as num1 and num3 refer to
different integer objects
Membership Operators
 In Python, in and not in are the membership
operators. They are used to test whether a value
or variable is found in a sequence (string, list,
tuple, set and dictionary)..
 These operator returns either True or False, if a
value/variable found in the list, its
returns True otherwise it returns False.
Operat Description Exampl
or e
in It returns True, if a 10 in
variable/value found in the list1
sequence.
not in It returns True, if a 10 not
variable/value does not found in list1
in the sequence.
Example with string
my_string = "Hello, World!"
Print('o' in my_string)
# Output: True
Print('o’ not in my_string)
# Output: False
Print('abc' not in my_string)
# Output: True
Print('abc' in my_string)
# Output: False
Example with list
my_list = [“hema”, “priya”, 3, 40, 5]

result1 = “hema” in my_list


Res=‘e’ in my_list
# Output: True(because hema exists in list)
result2 = 6 not in my_list
# Output: True(because 6 does not exist in
the list)

print(result1)
print(result2)

You might also like