This question already has answers here:
What is the result of % in Python?
(20 answers)
Closed 9 years ago.
I'm working on the exercises in Learning Python the Hard Way and I am wondering what the % function does. I am getting the wrong answer when i count my eggs and hoping understanding this will help me understand what I'm doing wrong.
I can't really tell why your code is broken because you haven't shown anybody what your code is. Please post samples and links next time.
Python % is used in two places, one is mathematical (the modulo operator), and the other has to do with formatting text. I'm going to assume "counting eggs" means the math way.
The modulo operator in X % Y means "Divide X by Y and give me the remainder." So:
10 % 2 == 0
10 % 3 == 1
10 % 11 == 10
That is the the modulo operator
Related
This question already has answers here:
Recursive function in simple english [duplicate]
(6 answers)
Closed 11 months ago.
I was making some exercise to train myself and the exercise asked me to do a program that calculates fractals, very simple, i've done in about 1-2 minutes and it work, but looking at his solution it return x multiplicated by the function itself? how does this run? I know maybe it's a stupid question but i think it might be useful.
def fract(x):
if x == 0:
return 1
return x * fract(x - 1)
print(fract(int(input())))
Here's a walk through of whats going on.
First, you call fract(int(input())). The input method gets a response from the user and parses that to an int. and then calls fract on that int.
Say we enter 3. So our print statement evaluates to fract(3).
fract(3) returns 3 * fract(2)
fract(2) is called and that returns 2 * fract(1)
fract(1) is called and that returns 1
So putting it altogether and substituting function calls for what they return we get fract(3) returns 3 * (2 * (1)).
This question already has answers here:
Simplify Chained Comparison
(2 answers)
Closed 2 years ago.
I am learning Python and trying to understand the following if statement. May I know what does it mean?
if 2 < a < b:
do something.
Thank you!
An if statement is a check of a condition and if this condition is true it will execute the code after the : .
In this case the Code prior to your line should have two variables a and b which both contain numbers.
If a is greater value than 2 and b is an even greater value than a, for example a is 3 and b is 4, or a is 10 and b is 200, the following Code will be executed. „Do something“ is just a place holder for the code that should be executed.
The best for you would be to search and read about if and if else statements for python to find some real life examples.
This question already has answers here:
Ordering of string representations of integers [duplicate]
(6 answers)
Closed 2 years ago.
Here's my code, super new to python. Struggling to understand why if I use < it always thinks is less than even though it will print a higher number. If I use greater than it works just fine. What am I missing? Here's my code, super new to python. Struggling to understand why if I use < it always thinks is less than even though it will print a higher number. If I use greater than it works just fine. What am I missing?
import time
t=time.localtime()
msttime=time.strftime("%H",t)
if(msttime < '2'):
print(msttime)
else:
print("This calculation believes msttime is greater than 2")
This code will give you the expected result:
import time
t = time.localtime()
msttime = time.strftime("%H", t)
if (int(msttime) < 2):
print(msttime)
else:
print("This calculation believes msttime is greater than 2")
The reason is that "18" < "2" lexographically, but 18 > 2 numerically. This is because the lexographical comparison has no regard for the second digit. Since 1 is before 2, the comparison ends there. In the numerical comparison, all digits are accounted for.
This question already has answers here:
What is the result of % in Python?
(20 answers)
Closed 3 years ago.
I have just now started learning python from Learn Python 3 The Hard Way by Zed Shaw. In exercise 3 of the book, there was a problem to get the value of 100 - 25 * 3 % 4. The solution to this problem is already mentioned in the archives, in which the order preference is given to * and %(from left to right).
I made a problem on my own to get the value of 100 - 25 % 3 + 4. The answer in the output is 103.
I just wrote: print ("the value of", 100 - 25 % 3 + 4), which gave the output value 103.
If the % is given the preference 25 % 3 will give 3/4. Then how the answer is coming 103. Do I need to mention any float command or something?
I would like to know how can I use these operations. Is there any pre-defined rule to solve these kinds of problems?
Actually, the % operator gives you the REMAINDER of the operation.
Therefore, 25 % 3 returns 1, because 25 / 3 = 8 and the remainder of this operation is 1.
This way, your operation 100 - 25 % 3 + 4 is the same as 100 - 1 + 4 = 103
The % operator is used to find the remainder of a quotient. So 25 % 3 = 1 not 3/4.
From the docs you can find the full python operation order.
Then, % operator is called the modulus operator and return the remainder of the integuer division:
11 % 2 == 1
You may want to take a look at divmod
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python - '>>' operator
What does the >> operator means in Python?
ie:
x = x + str(n%2)
n >> 1
Thank you
n >> 1 shifts n right 1 bit. This is the same as dividing by 2.
More generally, n >> m shifts n right m bits, giving a division by 2^m.
See also:
Shifting operations in Python's documentation
Logical shift on Wikipedia
Just for completeness, note that there's a completely different usage which is changing the stream that print uses by default:
print >> sys.stderr, message
For more information, please have a look at this related question.
That's the right shift operator.
It's the bitshift operator:
x >> n
x shifted right by n bits.