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. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, 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.