This question already has answers here:
What is the result of % in Python?
(20 answers)
The % function in Python [duplicate]
(2 answers)
Closed 9 years ago.
What does the percentage
%
function do in python in a line such as
x % 2
In this context it is the modulus, or remainder in the math sense.
So 7 % 2 == 1 because when you divide 7 by 2, you get a remainder of 1.
Similarly, if you wanted the fact that 2 goes into 7 three times, 7 // 2 == 3
The context is important, because % can also be used for old-style string formatting.
In that context, '%s to %s' % ('A', 'Z') would return a string 'A to Z'
However, don't use % for string formatting in python today. Use str.format.
This is called the modulus operator, it gives you the remainder after division:
>>> 5 % 2
1
>>> 44 % 3
2
>>>
Related
This question already has answers here:
How to calculate a mod b in Python?
(7 answers)
Closed 1 year ago.
I want to make the number begin from zero if it is more than twenty-five. For example,
x = 24 + 10 must be 8. How can I do that?
You can use the modulus operator, or %. a % b finds the remainder when you divide a by b. If you want to keep the number at 25 or lower, you should take the mod with 26.
def add_and_wrap(n1, n2, wrap_at):
return (n1 + n2) % (wrap_at + 1)
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Python 3 integer division [duplicate]
(1 answer)
Closed 2 years ago.
Why isn't q equal to b as the formula for modulo is: a % b = a - (a/b)*b ?
x = (int(time.time())*100)
q = x % 360
b = x - (x/360)*360
print(x)
print(q)
print(b)
This question already has answers here:
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
Closed 3 years ago.
This is the entire code. What does 'and not' mean in the code. I understand it to mean that only a number that will equal to 0 when the number modulus 2 is carried out.
That is if 10 is entered by the user, 2,4,6,8 will be sum to get 20.
the_max = int(input("Enter the upper limit:"))
the_sum = 0
extra = 0
for number in range(1,the_max):
if number%2 and not number%3:
the_sum = the_sum + number
else:
extra = extra + 1 # Line 1
print(the_sum) # Line 2
print(extra) # Line 3
It means that the number is not a multiple of 2 but a multiple of 3;
the if statement has two conditions:
number % 2
since the % returns the remainder of a a number divided by 2, it will return 0 on a multiple of 2 and will result in rejection of if condition.
and not number % 3
This means that we need both condition to be good. But with this one the not operand reverses it.
So this time any number % 3 in which number is a multiple of 3 will result in 0 and will be reversed to 1;
You're parsing it wrong. The correct interpretation is
if (number % 2 != 0) and (number % 3 == 0):
This code is taking shortcuts by omitting the explicit comparison to zero, since 0 evaluates to False in a boolean expression like this one, whereas any other integer value evaluates to True.
The second clause, thus, is given a not to flip its polarity. not (number % 3) is equivalent to (number % 3 == 0).
This question already has answers here:
Python modulo on floats [duplicate]
(3 answers)
Closed 5 years ago.
I'm trying to check in Python if a math.log(x, base) has decimal or not:
import math
# math.log(8, 2) = 3
print math.log(8)
print math.log(2)
print math.log(8) / math.log(2)
print 2.07944154168 % 0.69314718056
print math.log(8) % math.log(2)
Output is:
2.07944154168
0.69314718056
3.0
0.0
0.69314718056
Why does fourth print row return zero but fifth does not?
This will probably be closed for being a duplicate, but just so one can see how this plays out:
>>> import math
>>> math.log(8)
2.0794415416798357
>>> math.log(2)
0.6931471805599453
Now let's say you need to compute math.log(8) % math.log(2). You need to compute the remainder after dividing math.log(2) into math.log(8). Let's see, does it go in 3 times?
0.6931471805599453
+ 0.6931471805599453
+ 0.6931471805599453
--------------------
2.0794415416798359
Woah! We overshot the value of 2.0794415416798357 which means it actually goes in TWO times:
0.6931471805599453
+ 0.6931471805599453
--------------------
1.3862943611198906
Okay so what is the remainder?
2.0794415416798359
- 1.3862943611198906
--------------------
0.6931471805599453
So TL;DR your remainder is close to math.log(2) because of rounding errors. It does not go in exactly three times. It goes in two times with just about math.log(2) left over.
Yes when you print the quotient it says 3.0 but again, this is all rounding error in floating point, which is not unique to Python.
This is what I get using python 3
>>> print (math.log(8))
2.0794415416798357
>>> print (math.log(2))
0.6931471805599453
>>> print (math.log(8) / math.log(2))
3.0
>>> print (2.07944154168 % 0.69314718056)
0.0
>>> print (math.log(8) % math.log(2))
0.6931471805599452
>>> print (2.0794415416798357 % 0.6931471805599453)
0.6931471805599452
It looks like in your example (python 2 ?), the precision of math.log is not enough.
This question already has answers here:
How to display a float with two decimal places?
(13 answers)
Closed 7 years ago.
I have to convert this value to 23.69 or 23.70 not like that talll number 23.69098712446352
a=552.0
b=23.3
c=a/b
print(c)
str.format the output:
print("{:.2f}".format(c))
Or round:
round(c, 2)
Output:
In [9]: print(c)
23.6909871245
In [10]: print("{:.2f}".format(c))
23.69
In [11]: print(round(c, 2))
23.69
Use round function -
c = round(a/b , 2)
Two ways:
round(a.b, 2)
Or just for display use this:
>>> "%.2f" % 3.14159
'3.14'
>>> print("%.2f" % a)