Python operators

Created By: chatGPT

Python provides a variety of operators that allow you to perform operations on variables and values. The most common types of operators include arithmetic, comparison, logical, and assignment operators. Below, we will explore each type with examples.
Arithmetic Operators are used to perform basic mathematical operations. The common arithmetic operators include +, -, *, /, //, %, and **.
a = 10
b = 3

# Addition
addition = a + b  # 13

# Subtraction
subtraction = a - b  # 7

# Multiplication
multiplication = a * b  # 30

# Division
division = a / b  # 3.3333...

# Floor Division
floor_division = a // b  # 3

# Modulus
modulus = a % b  # 1

# Exponentiation
exponentiation = a ** b  # 1000
Comparison Operators are used to compare two values. They return True or False based on the condition. The common comparison operators are ==, !=, >, <, >=, and <=.
x = 5
y = 10

# Equal to
is_equal = (x == y)  # False

# Not equal to
is_not_equal = (x != y)  # True

# Greater than
greater_than = (x > y)  # False

# Less than
less_than = (x < y)  # True

# Greater than or equal to
greater_than_equal = (x >= y)  # False

# Less than or equal to
less_than_equal = (x <= y)  # True
Logical Operators are used to combine conditional statements. The common logical operators include and, or, and not.
a = True
b = False

# Logical AND
result_and = a and b  # False

# Logical OR
result_or = a or b  # True

# Logical NOT
result_not = not a  # False
Assignment Operators are used to assign values to variables. The basic assignment operator is =. There are also compound assignment operators such as +=, -=, *=, etc.
num = 10

# Add and assign
num += 5  # 15

# Subtract and assign
num -= 3  # 12

# Multiply and assign
num *= 2  # 24

# Divide and assign
num /= 4  # 6.0
Bitwise Operators perform bit-level operations on integers. They include & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift).
a = 5  # 0101 in binary
b = 3  # 0011 in binary

# Bitwise AND
bitwise_and = a & b  # 1 (0001)

# Bitwise OR
bitwise_or = a | b  # 7 (0111)

# Bitwise XOR
bitwise_xor = a ^ b  # 6 (0110)

# Bitwise NOT
bitwise_not = ~a  # -6 (inverts bits)

# Left Shift
left_shift = a << 1  # 10 (1010)

# Right Shift
right_shift = a >> 1  # 2 (0010)
Introduction And SetupVariablesData TypesIntFloatStrBoolListTupleSetDictMethodsString MethodsList MethodsDict MethodsSet MethodsControl FlowIf StatementElif StatementElse StatementFor LoopWhile LoopBreak StatementContinue StatementFunctionsDef StatementReturn StatementLambda FunctionsScopeParametersArgumentsDefault ParametersVariable Length ArgumentsOperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsAssignment OperatorsIdentity OperatorsMembership OperatorsData StructuresListsTuplesSetsDictionariesCommentsSingle Line CommentsMulti Line CommentsSyntaxIndentationDocstringsEvent HandlingError HandlingTry StatementExcept StatementFinally StatementRaise StatementAssert StatementContext ManagersWith Statement