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:
pythonresult = value1 == value2value1andvalue2are the values you want to compare.resultwill beTrueif the values are equal, andFalseif they are not.
Examples and Exercises:
Equal Comparison of Numbers:
pythona = 10 b = 12 c = 12 print(a == b) # Output: False print(b == c) # Output: TrueUsing Equal Operator in an IF Statement:
pythona = 10 if a % 2 == 0: print(a, "is an even number.")- Output:
10 is an even number.
- Output:
Equal Operator with Strings:
- You can also compare text.
pythona = "python" b = "python" if a == b: print("a and b have the same value.")- Output:
a and b have the same value.
Equal Operator in a While Loop:
- You can use the equal operator to control loops.
pythona = 20 while int(a / 6) == 3: print("hello") a += 1Output:
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!