Python Forum
*Ultra beginner math question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
*Ultra beginner math question
#1
My second day with book, Atom & Powershell. Using the Python calculator...

Why does--
25+30/6
30.0

Whereas--
55/6
9.1666666666

?

Mark
Reply
#2
Due to operator precedence, 25 + 30/6 is the same as 25 + (30/6) which is not (25 + 30)/6. It means that the / operator binds terms more tightly than the + operator. In the documentation, there is a table of operator precedence in increasing priority. You see that / is below + in the table, which means that it binds more tightly.

In case of doubts, you can always add parentheses in the expression to remove apparent ambiguity.
Reply
#3
And I had just read 'PE(M&D)(A&S) this morning. But when discerning the current problem my mind 'absolutely' knew, no questions asked, that I would 'of course' add the two numbers then divide. Big lesson--

Thanks,

Mark
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,793 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  beginner math MarioM 13 4,635 Dec-02-2020, 12:10 PM
Last Post: MarioM
  Beginner Python Homework Question (Calculate Gross Pay) matchamochi7 4 5,676 Nov-02-2018, 01:06 PM
Last Post: buran
  Beginner's String Operation question docboy 6 3,481 Sep-30-2018, 05:49 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020