Python Operators: A
Comprehensive Overview
Python operators are symbols that perform operations on values
and variables. This presentation provides a clear understanding of
different operators in Python. We'll cover arithmetic, comparison,
logical, and other essential types. Get ready to unlock the power of
Python's operators!
by Kushan Musku
Arithmetic Operators
+: Addition
-: Subtraction
*: Multiplication
/: Division
//: Floor Division
%: Modulus
**: Exponentiation
Arithmetic operators perform mathematical calculations. They return numerical results based on the input values.
Understand how to use these for basic and complex computations.
Comparison Operators
Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Comparison operators compare two values. The result is a Boolean
value: True or False. These are crucial for conditional statements
and decision-making.
Logical Operators
and: Returns True if both statements are true.
or: Returns True if at least one statement is true.
not: Reverses the result; returns False if the statement is true.
Logical operators combine conditional statements. They are used to make complex decisions in your code. They
always return a boolean result: True or False.
Bitwise Operators
&: Bitwise AND
|: Bitwise OR
^: Bitwise XOR
~: Bitwise NOT
<<: Left Shift
>>: Right Shift
Bitwise operators perform operations on bits. They are used for low-level programming and manipulation. Bitwise
operations can be highly efficient.
Assignment Operators
=: Assign value
+=: Add and assign
-=: Subtract and assign
*=: Multiply and assign
/=: Divide and assign
//=: Floor divide and assign
%=: Modulus and assign
**=: Exponentiate and assign
Assignment operators assign values to variables. Combined assignment operators provide a shorthand way to
update variables. This makes your code cleaner and more concise.
Membership Operators
in: Returns True if a sequence with the specified value is present
in the object.
not in: Returns True if a sequence with the specified value is not
present in the object.
Membership operators check if a value exists in a sequence.
Common use cases include lists, strings, and tuples. They simplify
checking for membership in collections.
Identity Operators
is: Returns True if both variables are the same object.
is not: Returns True if both variables are not the same object.
Identity operators compare the memory locations of two objects. They check if the variables refer to the same
object. This differs from the equality operator (==), which compares values.