Equal

What Is the Equal Operator?

  • The equal operator == in Python is used to check if two values are the same.

How to Use It:

  • The basic way to use the equal operator is:

    python
    result = value1 == value2
    
    • value1 and value2 are the values you want to compare.
    • result will be True if the values are equal, and False if they are not.

Examples and Exercises:

  1. Equal Comparison of Numbers:

    python
    a = 10
    b = 12
    c = 12
    print(a == b)  # Output: False
    print(b == c)  # Output: True
    
  2. Using Equal Operator in an IF Statement:

    python
    a = 10
    if a % 2 == 0:
        print(a, "is an even number.")
    
    • Output: 10 is an even number.
  3. Equal Operator with Strings:

    • You can also compare text.
    python
    a = "python"
    b = "python"
    if a == b:
        print("a and b have the same value.")
    
    • Output: a and b have the same value.
  4. Equal Operator in a While Loop:

    • You can use the equal operator to control loops.
    python
    a = 20
    while int(a / 6) == 3:
        print("hello")
        a += 1
    
    • Output:

       
      hello
      hello
      hello
      hello
      

Summary:

  • The equal operator == in Python is used to check if two values are the same. It works with numbers, text, and more. Try out the examples to see how it works!