Python comparison operators

Created By: chatGPT

In Python, comparison operators are used to evaluate the relationship between two values. The most common comparison operators include equal to, not equal to, greater than, less than, greater than or equal to, and less than or equal to. Each operator returns a boolean value: True or False.
a = 5
b = 10

# Equal to
result = (a == b)  # False

# Not equal to
result = (a != b)  # True

# Greater than
result = (a > b)   # False

# Less than
result = (a < b)   # True

# Greater than or equal to
result = (a >= b)  # False

# Less than or equal to
result = (a <= b)  # True
You can use these operators in conditional statements to control the flow of your program based on the evaluation of expressions. For example, you can check if one number is greater than another and execute certain code if this condition is satisfied.
if a < b:
    print('a is less than b')
else:
    print('a is greater than or equal to b')
You can also combine comparison operators with logical operators like and, or, and not for more complex conditions. This allows you to evaluate multiple expressions at once, enhancing the logic of your application.
if a < b and a > 0:
    print('a is between 0 and b')

if a < b or a > 10:
    print('either a is less than b or greater than 10')
Overall, comparison operators are fundamental in controlling the decision-making process in your code and are widely used in loops, conditions, and many other structures.
# Using comparison operators in a loop
for i in range(1, 11):
    if i % 2 == 0:
        print(f'{i} is even')
    else:
        print(f'{i} is odd')
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