Hey you're here for your third lesson! So far so good :D
Watch the video for the detailed tutorial:
Now, if you are done watching the video, let's try solving some problems. We'll start with the problem I gave you guys in the video:
Q1.
Evaluate the variable 'result'.
Answer:
result = 12 - (17 + 45) * 34 / 6 * 9
Now, before we evaluate this, I want to show you the precedence order of arithmetic operators in Python. The order will go from higher to lower. The operators holding higher precedence are evaluated first and the operators holding lower precedence are next.
Based on this, we can start evaluating result as:
result = 12 - (17 + 45) * 34 / 6 * 9
= 12 - 62 * 34 / 6 * 9
Now, we are dealing with * and / -- two operators with same precedence. In this case, we evaluate the expression from left to right (start reading the expression from left to right and evaluate whichever operator you encounter first).
result = 12 - 2108 / 6 * 9
= 12 - 351.333333 * 9
= 12 - 3162.0
= - 3150.0
Now that you know about precedence and associativity, evaluating expressions should be simple. Let's do one more question.
Q2.
Evaluate the variable 'result'.
Answer:
result = 45 % 5 + 12 ** 3 - 1
% -- this is the modulus operator in Python. It returns the remainder obtained when you divide the number in front of it by the number behind it. In this case, the remainder when you divide 45 by 5, i.e., 0.
result = 45 % 5 + 12 ** 3 - 1
= 45 % 5 + 1728 - 1
= 0 + 1728 - 1
= 1727
So, now you know how to deal with numbers in Python. Next time, we'll dive into strings. Until then, feel free to leave any doubts you may have in the comment section below or drop an email and I'll be sure to get back to you.
Cya!