Jump to content
  • Advertisement
Sign in to follow this  
jordi_0071

[python] the % operator

This topic is 4897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

where is the % operator for in python? i made this code:
50 % 1000

output: 50

50 % 100

output: 50


but why does it give the same output? and where is the ** for?

Share this post


Link to post
Share on other sites
Advertisement
Because 50 % 1000 and 50 % 100 both evaluate to 50. Try it on a calculator. 50 is not bigger than 1000 or 100, so dividing 50 by 1000 (or 100) will be "0 remainder 50" in both cases. % is the 'modulus' operator, in case you were asking its name.

Share this post


Link to post
Share on other sites
I'm pretty sure that's the modulo operator. that is the result is the remainder of 50 / 1000. That is: 50 divides into 1000 zero times with 50 left over. Check 34 % 3 and if it is the "mod" operator you'll get a result of 1.

Share this post


Link to post
Share on other sites
% is modulus (division with remainder).

** is to the power of.

So 2**4 = 16.



There is an use of % with strings in Python.
If you were to run this in Python:
print "This is a number: %d" % 1
it would output:
"This is a number: 1"

The % types are the same as with printf and such things (I think).

Share this post


Link to post
Share on other sites
Quote:
Original post by mldaalder
The % types are the same as with printf and such things (I think).


With the difference that in python, "%s" can print anything, using their string representation.

Share this post


Link to post
Share on other sites
i've a other question. if i typ this:


print "Stop!"
s = raw_input("What is your name? ")
print "You may pass,", s



it only prints Stop!

why doestn it show what is your name, etc?

Share this post


Link to post
Share on other sites
Works for me... Are you direcly typing that in the interpreter, where each statement is interpreted one after the other as you type them, or in a function, or in a script?

Share this post


Link to post
Share on other sites
i open the python Shell and typ

print "Stop!"
s = raw_input("What is your name? ")
print "You may pass,", s

and then press enter, but it only shows Stop!

Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
Because 50 % 1000 and 50 % 100 both evaluate to 50. Try it on a calculator.


Your calculator's % key does modulus? O_O

Anyway. If you are pasting in a whole script and then pressing enter at the end, it may well ignore the subsequent lines. But what I think is happening in your case is that you type the print line (and it works), then you type the raw_input line, and then right away the Python command prompt is expecting your raw_input, so the third line of the script, 'print "You may pass,", s', gets assigned. :)

Try saving the script to a Python source file (.py extension) and running that instead (at the command line, "python myfile.py").

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!