What is the output of this code snippet?
x = 55 / 11
The majority of people solve this puzzle correctly. Even so, the first goal of this puzzle is to introduce the concept of variables. Python evaluates the result of the expression on the right side of the equation and stores it in the variable
x. After defining the variable, you can access it at any point in the program code.
The second goal of this puzzle is to show an interesting twist in the code: Python division always returns a floating point number. Thus, variable
x stores the float value
5.0. The print function outputs the result as a float and not as an integer value 5. This is the source of most errors in the code. People focus too much on what they mean (semantics) and too little on how they say it (syntax). But computers are not good yet at interpreting the meaning of people. We must talk to them in their language.
Are you a master coder?
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.