[Tutor] Basic terminology

2005-02-15 Thread python
Hi,

I'm reading a Python book right now (Learning Python, a great book!), and there
are few terms that come are brought up a few times but without any explanation.

So what are:
- remainders (in the context of remainders-of-division modulus for numbers)
- modulus (in the same context; I have also seen it in different context, like
3D graphics programs to perform certain types of calculations).


Thanks
Bernard
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Basic terminology

2005-02-15 Thread Bill Mill
A remainder is what's left over after a division:

10/3 = 3 remainder 1
12/5 = 2 remainder 2
27/3 = 9 remainder 0

and the modulus operator (which is % in python) gives you that remainder:

10%3 = 1
12%5 = 2
27%3 = 0

See http://mathworld.wolfram.com/Remainder.html and
http://mathworld.wolfram.com/Modulus.html for more formal
explanations. In particular, it explains some deeper meanings of the
word modulus. Once you get into group theory, it can start to mean
some related but different things.

Peace
Bill Mill
bill.mill at gmail.com


On Tue, 15 Feb 2005 16:16:44 -0500, [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote:
 Hi,
 
 I'm reading a Python book right now (Learning Python, a great book!), and 
 there
 are few terms that come are brought up a few times but without any 
 explanation.
 
 So what are:
 - remainders (in the context of remainders-of-division modulus for numbers)
 - modulus (in the same context; I have also seen it in different context, 
 like
 3D graphics programs to perform certain types of calculations).
 
 Thanks
 Bernard
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Basic terminology

2005-02-15 Thread Danny Yoo


 A remainder is what's left over after a division:

 10/3 = 3 remainder 1
 12/5 = 2 remainder 2
 27/3 = 9 remainder 0

 and the modulus operator (which is % in python) gives you that remainder:

 10%3 = 1
 12%5 = 2
 27%3 = 0


Hi Bernard,

Another familiar example of modulo is checking to see if a number is even
or odd:

###
def isEven(n):
Check to see if n is divisible by 2.
return n % 2 == 0
###

The remainder of 'n' divided by 2, that is, n modulo 2, is zero if 'n'
is even.


Another cute thing about modulo is that we can use it to evenly distribute
things.  Let's make this more concrete, or at least a little sweeter.
Imagine that it's Halloween, and we want to evenly distribute some candy
out to some trick-or-treaters:

###
people = ['fred', 'wilma', 'barney', 'betty']
candy = ['almond joy', 'take 5', 'cadbury', 'twizzlers',
 'reeses', 'york', 'jolly rancher', 'nestle crunch']
###


There are two ways we can pass candy to the folks: we can give each people
two candies each:

###
 for (i, c) in enumerate(candy):
... print people[i / (len(candy) / len(people))], gets, c
...
fred gets almond joy
fred gets take 5
wilma gets cadbury
wilma gets twizzlers
barney gets reeses
barney gets york
betty gets jolly rancher
betty gets nestle crunch
###

That is, since there are eight pieces of candy, and only four people, we
can give each person two pieces of candy each.  We use simple division:

(i / (len(candy) / len(people))
== i / 2

to figure out, given the index number of the candy, which person can seize
the sugary treat.  So the first two pieces belong to fred, the second two
pieces belong to wilma, and so on.


Or we can do something that looks more round-robin:

###
 for (i, c) in enumerate(candy):
... print people[i % len(people)], gets, c
...
fred gets almond joy
wilma gets take 5
barney gets cadbury
betty gets twizzlers
fred gets reeses
wilma gets york
barney gets jolly rancher
betty gets nestle crunch
###

And here, we use the modulo operator to figure out which person each candy
belongs to.  In this case, we see the calculation involves:

i % len(people)

So there's this intimate relationship between division and modulo that
corresponds to two main ways we pass things out to people.


Does this make sense?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Basic terminology

2005-02-15 Thread Bill Mill
On Tue, 15 Feb 2005 14:26:52 -0800 (PST), Da
 
 Hi Bernard,
 
 Another familiar example of modulo is checking to see if a number is even
 or odd:
 
snip lots of good stuff from danny

Since Danny got it started with the examples, I'll give another
canonical example of the use of the modulus operator. Imagine that
we're trying to figure out what number the hour hand of a clock will
be pointing at x hours from now. If it's one o'clock right now, in 5
hours, the hour hand will be pointing at the six, and in 10 hours, it
will be pointing at the 11.

However, where will it be pointing in 16 hours? Well, in 12 hours it
will be at the one, then four more hours later it will be pointing at
the five. This can be represented as:

1 + (16 % 12) = 1 + 4 = 5

In general, the hour at some point in the future will be:

(start time) + (hours in the future % 12)

Peace
Bill Mill
bill.mill at gmail.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Basic terminology

2005-02-15 Thread Bernard Lebel
Well, thanks everyone who answered, much clearer now.
Bernard

Max Noel wrote:
In a slightly more generic fashion (everybody started dropping 
examples), the goal of an integer (euclidian) division (say, a / b) is 
to express an integer as such:

a = b * quotient + remainder
Where all the numbers used are integers, and 0 = remainder  b.
When you perform integer division in Python, a / b (a // b in 2.4+) 
gives you the quotient, and a % b gives you the remainder.

See the other posts for examples of use :p
-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
Look at you hacker... A pathetic creature of meat and bone, panting and 
sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor