Every single code project that you will touch in your career will contain control flow statements such as while and if. A control flow statement requires Boolean logic to determine the program’s execution.
Basics of Boolean Logic
The basics of Boolean logic are simple.
- 1) The expression
x and yis
True, if both
- 2) The expression
x or yis
True, if at least one of the two variables is
- 3) The expression
True, if x is
- 4) The expression
a and b or cis the same as
(a and b) or cmeaning that “and” evaluates before “or”.
These four rules are enough to solve the following puzzle about Boolean operators: Can you solve it?
Puzzle Boolean Logic
I scraped this puzzle’s data from Reddit’s most influential users:
# Influential reddit users # in million karmas way_fairer = 2.7 StickleyMan = 2.3 _vargas_ = 2.3 smeeee = 1.3 a = way_fairer > StickleyMan b = StickleyMan < smeeee and a c = _vargas_ >= StickleyMan c = a and b or c and smeeee > 1.2 d = not ((a and b) or c) if c and a: print(d) else: print(not d)
Puzzle: What is the output of this code snippet?
A good puzzle trains one aspect of your coding skills. This puzzle improves an important skill: understanding basic logical operators.
Are you a master coder? You can check out the solution to this puzzle here:
Test your skills now!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.