The Student Room Group

divide by a number and get remainder

What sign do I use to divide two numbers in python and get the remainder of the two?
% (modulus) I think
Original post by Ethan9133
% (modulus) I think

yea thanks, do you know if there is a way to get the answer to the division at the same time and not just the remainder?
Original post by Biology123ABC
yea thanks, do you know if there is a way to get the answer to the division at the same time and not just the remainder?


In Visual Basic you can use the backslash (\) to do integer division (dividing the numbers but ignoring the remainder). For Python I think you have to do int(a/b) (the int bit can be used to convert anything to an integer) - I don't know of a shortcut like VB has.
Do you only need to print the results to the console or are you storing the divisions in variables to use in later calculations?
Python uses / for dividing i.e. print(6/2) prints 3
% outputs the remainder i.e. print(7%2) prints 1
you can also use variables in place of the numbers
you can also store the answer to the calculation in a variable too
(edited 6 years ago)
Reply 5
Original post by Biology123ABC
yea thanks, do you know if there is a way to get the answer to the division at the same time and not just the remainder?


Come on dude...

/
(edited 6 years ago)
Original post by Biology123ABC
yea thanks, do you know if there is a way to get the answer to the division at the same time and not just the remainder?

no way i know but // returns the maximum number of times the thing can be divided

so 1//3 gives 0 for example
Original post by Speckle
Come on dude...

/


wat that's just good ol regular divide % gives the remainder after diving as many times as possible
Reply 7
If you're looking to output the remainder and the division then you would have to write two separate lines of code to print out the two answers. One for using '/' and the other using '%'. I don't know if there is an operation that prints both together.

Quick Reply