Equality
To check if two Python values, or variables, are equal you can use ==
. To check for inequality, you need !=
. As a refresher, have a look at the following examples that all result in True
. Feel free to try them out.
2 == (1 + 1)
"intermediate" != "python"
True != False
"Python" != "python"
When you write these comparisons in a script, you will need to wrap a print()
function around them to see the output.
This is a part of the course
“Intermediate Python”
Exercise instructions
- Write code to see if
True
equalsFalse
. - Write Python code to check if
-5 * 15
is not equal to75
. - Ask Python whether the strings
"pyscript"
and"PyScript"
are equal. - What happens if you compare booleans and integers? Write code to see if
True
and1
are equal.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Comparison of booleans
# Comparison of integers
# Comparison of strings
# Compare a boolean with an integer