In this tutorial, we’ll learn what Python comparison operators are and how to properly use them. We’ll go through proper practical examples in order to better understand how comparison operators work in Python.
Outline
- Introduction: Python Comparison Operators
- 6 Python Comparison Operators (Multiple Examples)
- Password Length Validation Using Python Comparison Operators
- Conclusion
Introduction: Python Comparison Operators
As the name suggests, these operators are used to compare values. Boolean(True/False) is returned using these operators. In Python, we’ve multiple comparison operators. Let’s now understand each and every one of them using practical Python code examples.
6 Python Comparison Operators (Multiple Examples)
These 6 operators are listed below:
- Equal (==)
- Not Equal (!=)
- Greater than (>)
- Less then (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
Equal (==)
This operator return true if both the values are same. See below code:
a=5 b=6 print(a == b)
Output
False
False is returned as the values are different. Let’s now give same value to both of them and then see the output. See below code:
a=10 b=10 print(a == b)
Output
True
We can use it inside Python if statement to use it as a condition. See below:
age=18 if age == 18: print('You are an adult now') else: print('Try again')
Output
You are an adult now
Not Equal (!=)
This operator return true if both the values are different. See below example:
val1='Zee' val2='Shan' print(val1 != val2)
Output
True
True is returned as both the variables have different values. If we pass same value to both of them then false will be returned. See below:
val1='Zeeshan' val2='Zeeshan' print(val1 != val2)
Output
False
Greater Than (>)
This operator return true if the value of left/first operand is greater than the right one. See below:
aa=30 bb=20 print(aa > bb)
Output
True
False will be returned if the value of first operand it less as compared to the second one. See below example:
aa=10 bb=20 print(aa > bb)
Output
False
Providing both the operands with same value will also result in false as value of first operand will not be greater than the second one. See below code:
aa=10 bb=10 print(aa > bb)
Output
False
Less Than (<)
True is returned using this operator if the value of first operand is less than the second one. See below:
a=5 b=8 print(a < b)
Output
True
If we pass greater value to the left operand and less to the second one then it’ll return false. See below:
a=55 b=40 print(a < b)
Output
False
If both the operands are assigned with same value then false will be returned as value of first operand will not be less than the second one. See below code:
a=10 b=10 print(a < b)
Output
False
Greater Than Or Equal To (>=)
This operator return true if the value of first operand is greater than or equal to the value of second operand. See below:
a=5 b=10 c=5 print(a >= c) print(b >= c)
Output
True True
We can see that true is returned in both cases. False will be returned if the value of left operand is lesser than the right one. See below:
a=2 b=5 print(a >= b)
Output
False
Less Than Or Equal To (<=)
True is returned by this operator if the value of left/first operand is less than or equal to the right one. False will be returned if the value of left operand is greater than the right one. See below examples:
val1=8 val2=5 val3=8 val4=10 print(val1 <= val3) print(val2 <= val3) print(val4 <= val3)
Output
True True False
Password Length Validation Using Python Comparison Operators
passwordVal=input('Enter your password:') if len(passwordVal) == 0: print('Password cannot be empty') elif len(passwordVal) < 8: print('Password should be atleast 8 characters long') elif len(passwordVal) > 20: print('Password should be maximum 15 characters long') else: print('try again')
Test 1 (Empty password)
Enter your password: Password cannot be empty
Test 2 (Less Than The Minimum Limit)
zee Password should be atleast 8 characters long
Test 3 (More Than The Maximum Limit)
jdjdjbfbujmsnhudj7483hnwhb Password should be maximum 15 characters long
Test 4 (Good Length)
zeeshan123 Password is Valid
We’ve also used logical operator in this example code. Click here if you want to learn Python logical operators in detail.
So this is how we can use Python comparison operators. Feel free to share this Python comparison operators tutorial with your developer buddies.
Don’t hesitate to ask if you still have any queries regarding Python comparison operators. We’ll be more than happy to answer all.
Conclusion
To conclude this tutorial, hope you now have an in-depth knowledge of how Python comparison operators work. Your feedback will be well appreciated.
Do visit our other posts on Python programming. Thank you for reading this article.