What is the output of this code snippet?
x = 50*2 + (60-20)/4
The Python interpreter is a powerful tool. In this puzzle, it acts as a simple calculator. It takes a basic mathematical expression and calculates the result.
The syntax of maths expressions is straightforward: use the operators +, -, * and / exactly as you have learned them in school. The Python interpreter will handle basic rules such as multiplication before addition for you.
Note that a common mistake here is that people write the result as an integer rather than as a float. This can lead to difficult bugs in the code in a dynamically typed language such as Python. A dynamically typed language assigns the type of a variable at runtime and not at compile time like Java.
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.