Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Division
#1
Hi,
I have recently started to look at Python. I found a tutorial pdf document written by Brian Heinold called A Practical Introduction to
Python Programming.
It is interesting and I am enjoying reading it. At the end of the first section there are 9 exercises to have a go at, I have figured out 8 of them but can not get one of them to come up with the answer suggested. Below is the question,
Write a program that computes and prints the result of
512 − 282
47 · 48 + 5
. It is roughly .1017.
Reply
#2
Hello, and good luck with your Python programming.

Hint for the exercise: you can use parentheses in Python numeric expressions, for example
>>> (612 - 182) / 7
61.42857142857143
>>> 
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Thanks for the reply.
I tried this way but still didn't get the right answer.

num1=512-282
num2=47.48+5
print(num1/num2)
Reply
#4
What result do you get? I get 4.382621951219512, which is correct.

Maybe you made a mistake entering the equation. I think you meant to enter:
num1=512-282
num2=47-48+5
print(num1/num2)
When I run that I get 57.5
Or use parenthesis in place of the assignment.
num1=512-282 becomes (512-282)
num2=47-48+5 becomes (47-48+5)
num1 / num2 becomes (512-282) / (47-48+5)
Reply
#5
What Gribouillis said: Use brackets, then Python will complete what is in the brackets first, before division.

As I see it you have 4 numbers at the start.

num1 = 512
num2  = 282
num3 = 47.48
num4 = 5

result = (num1 - num2) / (num3 + num4)
print(result)
I got what deanhystad got, so I can't be too far wrong! Big Grin

Output:
4.382621951219512
Using a Python module called random you can generate random integers. So make 4 random numbers and repeat the above until it is clear to you.!

from random import randint

# randint(start, stop) is inclusive, it can return the first number or the last number
num1 = randint(0, 999)
num2  = randint(0, 999)
num3 = randint(0, 999)
num4 = randint(0, 999)

# have a look at your numbers
print(num1, num2, num3, num4)
result = (num1 - num2) / (num3 + num4)
print(result)
To help you on your Python way there are many, many websites with info on all aspects. I like https://realpython.com, they have stuff for raw beginners and much more complex stuff, but there really is so much info on Python out there, identify your problem, then search, for example: Python module random
Mallard likes this post
Reply
#6
Some folks seem to be interpreting the dot in the denominator as a decimal point instead of a dot operator. That signifies multiplication in this context. In python, you'll have to use an asterisk for this type of multiplication.

>>> (512-282)/ (47*48+5)
0.1017249004865104
Gribouillis likes this post
Reply
#7
Thanks to all who took the time to read and reply.
It is, as bowlofred replied, the dot in the denominator should be a multiplication sign to give the answer that the tutorial suggest.
This is a misprint in the pdf document that I am following.
I'm now looking at Chapter 2, For Loops so I could well be back again Wink
Reply
#8
(Dec-16-2025, 11:28 AM)Mallard Wrote: This is a misprint in the pdf document that I am following.
It's not a misprint. Mathematicians often use the dot to denote multiplication. It's one of the many differences between the mathematical notations and the syntax of programming languages.
« We can solve any problem by introducing an extra level of indirection »
Reply
#9
If you write it without parentheses, Python will calculate it incorrectly.

result = (512 - 282) / (47 * 48 + 5)
print(result)

Output:
0.10176.

Conclusion;
If you were missing parentheses, Python was likely evaluating only part of the denominator, which leads to the wrong result.
Reply
#10
(Dec-16-2025, 05:00 PM)Gribouillis Wrote:
(Dec-16-2025, 11:28 AM)Mallard Wrote: This is a misprint in the pdf document that I am following.
It's not a misprint. Mathematicians often use the dot to denote multiplication. It's one of the many differences between the mathematical notations and the syntax of programming languages.

Thanks for the clarification.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Why was floor division used here? tmv 2 115 Jan-31-2026, 02:00 AM
Last Post: Pedroski55
  division error nathanael 2 776 Dec-12-2025, 11:55 PM
Last Post: deanhystad
  Division questions Dionysis 5 3,605 Feb-14-2023, 02:02 PM
Last Post: Dionysis
  Division by zero and value of argument Lawu 5 10,682 Jul-01-2022, 02:28 PM
Last Post: Lawu
  Division calcuation with answers to 1decimal place. sik 3 3,557 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE
  Floor division return value Chirumer 8 7,826 Nov-26-2020, 02:34 PM
Last Post: DeaD_EyE
  Integer division plozaq 2 3,321 Sep-28-2020, 05:49 PM
Last Post: plozaq
  Overcoming ZeroDivisionError: division by zero Error dgrunwal 8 9,358 Jun-12-2020, 01:52 PM
Last Post: dgrunwal
  Division of an integer into sub-numbers Richard_SS 4 4,895 Jun-14-2019, 11:47 AM
Last Post: DeaD_EyE
  Logic of using floor division and modulus for a different variable at different time SB_J 2 3,932 Nov-01-2018, 07:25 PM
Last Post: SB_J

Forum Jump:

User Panel Messages

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