Public Group

# [python] the % operator

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

## 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 on other sites
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 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 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 on other sites
Quote:
 Original post by mldaalderThe % 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 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 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 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 on other sites
Quote:
 Original post by RDragon1Because 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 on other sites
i just had to typ the first line press enter then the next line and then enter etc
not everthing right away.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634092
• Total Posts
3015444
×