0

I have just now started learning python from Learn Python 3 The Hard Way by Zed Shaw. In exercise 3 of the book, there was a problem to get the value of 100 - 25 * 3 % 4. The solution to this problem is already mentioned in the archives, in which the order preference is given to * and %(from left to right). I made a problem on my own to get the value of 100 - 25 % 3 + 4. The answer in the output is 103.

I just wrote: print ("the value of", 100 - 25 % 3 + 4), which gave the output value 103.

If the % is given the preference 25 % 3 will give 3/4. Then how the answer is coming 103. Do I need to mention any float command or something?

I would like to know how can I use these operations. Is there any pre-defined rule to solve these kinds of problems?

blhsing
  • 91,368
  • 6
  • 71
  • 106
  • I would also like to know some good online sources for learning python. – Atul Anurag Sharma Apr 14 '19 at 15:15
  • The book you are reading is a good source. I also recommend you the O'Reilly Python Cookbook and O'Reilly Learning Python. Don't be afraid to read the Official Docs of Python. I know it's boring when we are in learning process, but it's good for you to be use to read the Docs. – Stévillis Apr 14 '19 at 15:26
  • Can I use them parallelly? or first I should complete the shaw book? @Stévillis – Atul Anurag Sharma Apr 14 '19 at 15:28
  • I have started learning python a few days back @Stévillis. – Atul Anurag Sharma Apr 14 '19 at 15:30
  • Choose one that you like most. If you are not understanding what the Author are trying to explain, it's better to choose another source. You don't need to read all of them! But it will fix more your understanding if you do so. Each author takes a different way to explain, they show different examples. It helps me a lot because when I have some problem, I remember when I saw the same problem or similar on one of the books I've read. Then I go there, and I read the specific part of the book again and I can solve my problem. – Stévillis Apr 14 '19 at 15:32

3 Answers3

1

Actually, the % operator gives you the REMAINDER of the operation.
Therefore, 25 % 3 returns 1, because 25 / 3 = 8 and the remainder of this operation is 1.
This way, your operation 100 - 25 % 3 + 4 is the same as 100 - 1 + 4 = 103

Stévillis
  • 491
  • 1
  • 6
  • 16
0

The % operator is used to find the remainder of a quotient. So 25 % 3 = 1 not 3/4.

Mike
  • 706
  • 7
  • 16
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
0

From the docs you can find the full python operation order. Then, % operator is called the modulus operator and return the remainder of the integuer division:

11 % 2 == 1

You may want to take a look at divmod

Netwave
  • 40,134
  • 6
  • 50
  • 93