Logo

CHiPSET

A Technical Community

Python Operators | If-Else Statements | AIML Ep 3

Learn Python operators and conditional statements with simple examples. Understand arithmetic, comparison, logical operators, and if-else statements in Python for AI and Machine Learning.

ChakradharMay 19, 202630 min
#operators #ml
Python Operators | If-Else Statements | AIML Ep 3

Operators in Python

Operators are symbols used to perform operations on variables and values. They help us do calculations, comparisons, and logical operations in a program.

Example:

Image

Image

image showing example

Image

Image

types of operators in python

image source: Geeks for Geeks

Types of Operators in Python

Python has:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Ternary Operator
  • Identity Operators
  • Membership Operators
  • Arithmetic operators:

    Image

    Image

    image showing arithmetic operators

    Now let us look at an example for a better understanding

    Image

    Image

    image showing example usage of arithmetic operators

    2. Relational (Comparison) Operators

    Used to compare values.

    Image

    Image

    types are illustrated in image

    Image

    Image

    example of usage is illustrated in above image

    Logical operators

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

    The precedence of Logical Operators in Python is as follows:

  • 42Logical not
  • 43logical and
  • 44logical or
  • Image

    Image

    the above image shows an example usage in python

    Bitwise Operators

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

    Bitwise Operators in Python are as follows:

  • 51Bitwise NOT
  • 52Bitwise Shift
  • 53Bitwise AND
  • 54Bitwise XOR
  • 55Bitwise OR
  • Image

    Image

    image illustrating bitwise operators

    Bitwise AND (&)

  • The Rule: The output switch is ON (1) only if BOTH input switches are ON (1).
  • Look at the image:
  • Input A: 1 0 1 0
  • Input B: 1 1 0 0
  • Only the very first column has a 1 in both rows. So, the output is 1 0 0 0.
  • 2. Bitwise OR (|)

  • The Rule: The output switch is ON (1) if AT LEAST ONE of the input switches is ON (1). It’s very generous!
  • Look at the image:
  • Anywhere you see a 1 in either Input A or Input B, the output gets a 1.
  • Only the last column is 0 0, so it stays 0. The output is 1 1 1 0.
  • 3. Bitwise XOR (^)

  • The Rule: "Exclusive OR." The output switch is ON (1) only if the inputs are DIFFERENT from each other (one is 1, the other is 0). If they are the same, it outputs 0.
  • Look at the image:
  • Column 1 is 1 and 1 (same $\rightarrow$ 0).
  • Column 2 is 0 and 1 (different $\rightarrow$ 1).
  • Column 3 is 1 and 0 (different $\rightarrow$ 1).
  • Column 4 is 0 and 0 (same $\rightarrow$ 0).
  • Result: 0 1 1 0.
  • 4. Bitwise NOT (~)

  • The Rule: The "Inverter." It doesn't compare two numbers; it just looks at one number and flips everything.
  • Look at the image:
  • Every 0 becomes a 1, and every 1 becomes a 0.
  • 0 1 0 1 completely reverses to become 1 0 1 0.
  • 5. Left Shift (<<) and Right Shift (>>)

  • The Rule: Imagine your binary digits are sitting on a conveyor belt. Shifting moves them all to the left or right, knocking some off the edge and filling the empty spaces with 0s.
  • Left Shift (<<): Moves bits to the left. As seen in the image, 01100 shifted left pushes the numbers over, sliding a new 0 onto the right side. Fun fact: Shifting left by 1 position mathematically multiplies the number by 2!
  • Right Shift (>>): Moves bits to the right. As seen in the image, 0110 shifted right pushes the numbers to the right, sliding a new 0 onto the left side. Fun fact: Shifting right by 1 position divides the number by 2!
  • Image

    Image

    the above image illustrates an example usage in bitwise

    Assignment Operators

    Assignment operators are used to assign values to the variables. This operator is used to assign the value of the right side of the expression to the left side operand.

    Image

    Image

    types of assignment operators

    Image

    Image

    image showing example

    Identity Operators

    "is" and "is not" are the identity operators and 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

    Image

    Image

    Image shows example showing demonstration

    Before understanding last two lets have a look on conditional statements in python

    Conditional statements

    Conditional statements are how we make our code smart. They let your program make decisions based on certain criteria, executing different blocks of code depending on whether a condition is true or false.

    Think of it like deciding what to wear: if it's raining, you grab an umbrella; else if (elif) it's cold, you wear a jacket; otherwise (else), you just wear a t-shirt.

    The Three Core Keywords

    Python uses three main keywords to handle logic: if, elif, and else.

    Here is how they look in action:

    Image

    Image

    3 Rules of Python Conditional Syntax

    There are a few syntax rules that often trip people up when they're first starting out. Keep these in mind to avoid errors:

  • The Colon (:): Every if, elif, and else line must end with a colon. This tells Python, "Hey, look at the next line for what to do."
  • Indentation Matters: Python doesn't use curly braces {} to group code like other languages do. It uses spaces. Everything indented under an if statement belongs to that decision block. Usually, this is 4 spaces (or one press of the Tab key).
  • The Double Equals (==): When you want to check if two things are equal, you must use ==. A single = is only used to assign a value to a variable.
  • To create a condition, you'll use comparison operators to evaluate your data. These always return either True or False:

    1. Short-Hand if (The One-Liner)

    When you have only one action to take after an if statement, you can skip the line break and write it all on a single line.

    Image

    Image

    2. If-Else vs. If-Elif-Else

    This is the core of structural logic. Think of it as a path that splits.

  • if-else (Two Paths): Used when there are only two possible outcomes (Yes or No).
  • Image

    Image

    if-elif-else (Multiple Paths): Used when you have a chain of conditions. Python checks them top to bottom. The moment it finds one that is True, it runs that code and skips the rest.

    below is demonstration in code

    Image

    Image

    5. Match-Case (Python's Modern Switch)

    Introduced in Python 3.10, match-case is a much cleaner way to write long if-elif-elif-else chains when you are just checking the value of a single variable against multiple options.

    below is demonstration of it

    Image

    Image

    Now that you understood conditional statements let us look at ternary operators and membership operators

    Ternary operators:

    Image

    Image

    image showing ternary operators

    Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5. 

    It simply allows testing a condition in a single line replacing the multiline if-else, making the code compact.

    Syntax : [on_true] if [expression] else [on_false]

    Image

    Image

    Membership Operators

    "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

    Image

    Image

    example showing membership operatrs

    Homework

    Try these programs on your own to improve your understanding of operators and conditional statements in Python.

    Operators Practice

  • 160Write a program to take two numbers as input and perform:
  • Addition
  • Subtraction
  • Multiplication
  • Division
  • 165Write a program to find:
  • Remainder using %
  • Power using **
  • 168Create a program to compare two numbers using:
  • >
  • <
  • ==
  • !=
  • 173Write a program using logical operators:
  • and
  • or
  • not
  • 177Write a program using assignment operators:
  • +=, -=, *=

  • 179Write a program to check whether a value exists in a list using:
  • innot in

    Conditional Statements Practice

  • 182Write a program to check whether a number is:
  • Positive
  • Negative
  • Zero
  • 186Write a program to check whether a person is eligible to vote.
  • 187Write a program to find the greatest of two numbers.
  • 188Write a program to check whether a number is even or odd.
  • 189Write a program to display grades:
  • Marks above 90 → A Grade
  • Marks above 75 → B Grade
  • Marks above 50 → C Grade
  • Else → Fail
  • Conclusion

    In this episode, we learned about:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Membership and Identity Operators
  • Conditional Statements using:
  • if
  • elif
  • else
  • These concepts are very important because they help programs:

  • Perform calculations
  • Compare values
  • Make decisions
  • Execute different actions based on conditions
  • Operators and conditional statements are the backbone of programming logic and are widely used in Artificial Intelligence and Machine Learning applications.

    Keep practicing regularly, because coding skill comes only by writing programs and debugging your own mistakes 😭

    Stay tuned for the next episode to continue learning Python for AI and Machine Learning.

    Source:

    This article uses ai generated images for better understanding

    this article uses some references from geeks for geeks

    for previous episode click here

    Python Fundamentals: Variables, Data Types and Input/Output | CHiPSET Tech Articles | CHIPSET SRM University Ramapuram

    Updated for 2026 with beginner-friendly Python examples.

    Tags:#operators #ml

    Written by Chakradhar on May 19, 2026