[Tutor] Stupid Pythony Newbie Question: Basic Operators;

Luke Paireepinart rabidpoobear at gmail.com
Sun Mar 16 12:33:12 CET 2008

Michael Lim wrote:
> Sorry guys, this is a newbie question... can someone please help me with what the following 2 basic operators do in like simple english:
Well, it depends on the situation you use the operation in.
I'll give you some examples.
> 1.
> ^
This is a bitwise exclusive-or.  If you don't know what that's used for, 
you probably don't need it.
Basically it goes bit-by-bit and performs an exclusive-or on each bit pair.
For example:
2 ^ 4 is 6,  because
0010  (2)
0100  (4)
0110  (6)
I won't go into more detail about xor, that should be enough to go on if 
you know what an exlusive-or is supposed to do.
> 2.
> %
This performs a few functions.
Firstly, just as a normal arithmetic operator, it's called the "mod" or 
"modulus" operator.
It gives you the remainder after an integer division.
you know that 6 can go into 19 three whole times, but there will be some 
left over.  Mod gives you the leftover.
 >>> 19 / 6
 >>> 19 % 6

Because when you divide 19 by 6 you get 3 and 1/6th.

Another thing % is used for is string substitution.
print "Hello, %s!" % "world"
is a basic substitution.
Then there are more complex, multi-valued substitutions:
import math
print "pi: %1.6f, cos(0): %.0f" % (math.pi, math.cos(0))
Note that the collection of values on the right needs to be a tuple (the 
comma denotes a tuple)
Then there's dictionary string substitution:
students = {'number' : 2, 'names' : ['joe','bob'] }
print "There are %(number)i students in your class.  The students are 
%(names)s." % students

There may be other uses for these operators.
You really should give us a specific case where you don't understand, 
cause then we could explain it better.

By the way, don't put [TUTOR]  in the subject.  The mailing list 
automatically adds [Tutor] so if you manually add it, it just makes your 
subject harder to read because there are then two tags.
Hope that helps,

More information about the Tutor mailing list