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
This exercise is part of the course
Intermediate Python
Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.
Boolean logic is the foundation of decision-making in Python programs. Learn about different comparison operators, how to combine them with Boolean operators, and how to use the Boolean outcomes in control structures. You'll also learn to filter data in pandas DataFrames using logic.
Exercise 1: Comparison OperatorsExercise 2: EqualityExercise 3: Greater and less thanExercise 4: Compare arraysExercise 5: Boolean OperatorsExercise 6: and, or, not (1)Exercise 7: and, or, not (2)Exercise 8: Boolean operators with NumPyExercise 9: if, elif, elseExercise 10: WarmupExercise 11: ifExercise 12: Add elseExercise 13: Customize further: elifExercise 14: Filtering pandas DataFramesExercise 15: Driving right (1)Exercise 16: Driving right (2)Exercise 17: Cars per capita (1)Exercise 18: Cars per capita (2)What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.