New course! Every coder should learn Generative AI!
Try a free lesson+ 5
[Solved]How to calculate % in python ?
Not the %(20%6=2) function.
5 Answers
+ 5
You need to do that normally.
10 + (10.0 * (2.0/100.0))
=> 10 + 0.2
=> 10.2
+ 7
What exactly are you asking then? Give an example...
+ 5
% is modulus or remainder operator
When 6%3 the answer is 0 because the remainder is 0
23 % 10 evaluates to 3 (because 23/10 is 2 with a remainder of 3)
50 % 50 evaluates to 0 (50/50 is 1 with a remainder of 0)
9 % 100 evaluates to 9 (9/100 is 0 with a remainder of 9)
In x/y results consists of an integer part and a fraction part. If you multiply the fraction part with the divisor, you get the remainder. And x = Integer party + Remainder (i.e. Fraction party). In this case Integer part is 0, and the remainder is 3.
+ 4
Aronya, may be you can share us the code to get a clear impression what you mean?
Regular division and modulo division can be done in one step with divmod()
res = divmod(10, 2) returns a tuple (1, 4). So 1 is the result from 10//6 and 4 is the remainder from 10%6.
+ 2
you can calculate it as usual.
by multiply it by the percentagees and divide it by 100.
x*percent/100