Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Operator in Python

The document provides an overview of various types of operators in Python, including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. Each operator type is described with its syntax and examples demonstrating their usage. The document serves as a foundational guide for understanding how operators function in Python programming.

Uploaded by

20kb1a3009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Operator in Python

The document provides an overview of various types of operators in Python, including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. Each operator type is described with its syntax and examples demonstrating their usage. The document serves as a foundational guide for understanding how operators function in Python programming.

Uploaded by

20kb1a3009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Python Programming

Topperworld.in

Operators
• The operator is a symbol that performs a specific operation between two
operands, according to one definition.
• Operators serve as the foundation upon which logic is constructed in a
program in a particular programming language.
• In every programming language, some operators perform several tasks.
Same as other languages, Python also has some operators, and these are
given below -

➢ Arithmetic operators
➢ Comparison operators
➢ Assignment Operators
➢ Logical Operators
➢ Bitwise Operators
➢ Membership Operators
➢ Identity Operators

❖ Arithmetic Operators in Python


Python Arithmetic operators are used to perform basic mathematical
operations like addition, subtraction, multiplication, and division.

Operator Description Syntax

+ Addition: adds two operands x+y

– Subtraction: subtracts two operands x–y

* Multiplication: multiplies two operands x*y

Division (float): divides the first operand by the


/ x/y
second

©Topperworld
Python Programming

Operator Description Syntax

Division (floor): divides the first operand by the


// x // y
second

Modulus: returns the remainder when the first


% x%y
operand is divided by the second

** Power: Returns first raised to power second x ** y

Example:
a=9
b=4
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Modulo of both number
mod = a % b
print(add)
print(sub)
print(mul)
print(mod)

©Topperworld
Python Programming

Output:

13
5
36
1
6561

❖ Comparison Operators in Python


In Python Comparison of Relational operators compares the values. It either
returns True or False according to the condition.

Operator Description Syntax

Greater than: True if the left operand is greater than the


> x>y
right

< Less than: True if the left operand is less than the right x<y

== Equal to: True if both operands are equal x == y

!= Not equal to – True if operands are not equal x != y

Greater than or equal to True if the left operand is


>= x >= y
greater than or equal to the right

Less than or equal to True if the left operand is less than


<= x <= y
or equal to the right

©Topperworld
Python Programming

Example:

# Examples of Relational Operators


a = 13
b = 33

# a > b is False
print(a > b)

# a < b is True
print(a < b)

# a == b is False
print(a == b)

# a != b is True
print(a != b)

# a >= b is False
print(a >= b)

Output:

False
True
False
True
False

©Topperworld
Python Programming

❖ Assignment Operators in Python


Python Assignment operators are used to assign values to the variables.

Operator Description Syntax

Assign the value of the right side of the


= x=y+z
expression to the left side operand

Add AND: Add right-side operand with left-


+= a+=b a=a+b
side operand and then assign to left operand

Subtract AND: Subtract right operand from left


-= a-=b a=a-b
operand and then assign to left operand

Multiply AND: Multiply right operand with left


*= a*=b a=a*b
operand and then assign to left operand

Divide AND: Divide left operand with right


/= a/=b a=a/b
operand and then assign to left operand

Modulus AND: Takes modulus using left and


%= right operands and assign the result to left a%=b a=a%b
operand

Divide(floor) AND: Divide left operand with


//= right operand and then assign the value(floor) a//=b a=a//b
to left operand

Exponent AND: Calculate exponent(raise


**= power) value using operands and assign value a**=b a=a**b
to left operand

Performs Bitwise AND on operands and assign


&= a&=b a=a&b
value to left operand

©Topperworld
Python Programming

Operator Description Syntax

Performs Bitwise OR on operands and assign


|= a|=b a=a|b
value to left operand

Performs Bitwise xOR on operands and assign


^= a^=b a=a^b
value to left operand

Performs Bitwise right shift on operands and


>>= a>>=b a=a>>b
assign value to left operand

Performs Bitwise left shift on operands and a <<= b a= a


<<=
assign value to left operand <<b

Example:

a = 10
# Assign value
b=a
print(b)
# Add and assign value
b += a
print(b)
# Subtract and assign value
b -= a
print(b)
# multiply and assign
b *= a
print(b)

©Topperworld
Python Programming

Output:

10
20
10
100

❖ Logical Operators in Python


Python Logical operators perform Logical AND, Logical OR, and Logical
NOT operations. It is used to combine conditional statements.

Operator Description Syntax

and Logical AND: True if both the operands are true x and y

or Logical OR: True if either of the operands is true x or y

not Logical NOT: True if the operand is false not x

Example:
# Examples of Logical Operator
a = True
b = False
# Print a and b is False
print(a and b)
# Print a or b is True
print(a or b)
# Print not a is False
print(not a)

©Topperworld
Python Programming

Output:

False
True
False

❖ Bitwise Operators in Python


Python Bitwise operators act on bits and perform bit-by-bit operations. These
are used to operate on binary numbers.

Operator Description Syntax

& Bitwise AND x&y

| Bitwise OR x|y

^ Bitwise XOR x^y

>> Bitwise right shift x>>

<< Bitwise left shift x<<

Example:
# Examples of Bitwise operators
a = 10
b=4
# Print bitwise AND operation
print(a & b)
# Print bitwise OR operation
print(a | b)

©Topperworld
Python Programming

Output:
0
14

❖ Membership Operators in Python

In Python, in and not in are the membership operators that are used to test
whether a value or variable is in a sequence.

in True if value is found in the sequence


Not in True if value is not found in the sequence

Example:

# Python program to illustrate


# not 'in' operator
x = 24
y = 20
list = [10, 20, 30, 40, 50]

if (x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")
if (y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")

©Topperworld
Python Programming

Output:

x is NOT present in given list


y is present in given list

❖ Identity Operators in Python


In Python, is and is not are the identity operators both are used to check if
two values are located on the same part of the memory. Two variables that
are equal do not imply that they are identical.

is True if the operands are identical

Is not True if the operands are not


identical

Example:

a = 10
b = 20
c=a

print(a is not b)
print(a is c)

Output:

True
True

©Topperworld

You might also like