Python math question

RogerBacon

King
Joined
Nov 16, 2003
Messages
649
If I have some integers:

int1 = 10
int2 = 3

and I do:

int3 = int1 / int2
will I get 3 as my answer?

Likewise, if I do:

int3 = int1 * 1.25
will I get 12 as my answer?

Sorry to sound so lazy but I'm at work right now and I can't test it myself.

Roger Bacon
 
if I'm not mistaken, you'll get the full decimal answer, so 10/3 would result in 3.333... which you would then have to round yourself using the round function.
 
10/3 will give you 3 as an asnwer. 10/3.0 will give you 3.33333. 10//3.0 will give you 3.0

Note: the 10/3 will change sometime so that it returns 3.33333. When that happens you should be able to use the // operator.
 
RogerBacon said:
If I have some integers:

int1 = 10
int2 = 3

and I do:

int3 = int1 / int2
will I get 3 as my answer?

int3 will be equal to 3

RogerBacon said:
Likewise, if I do:

int3 = int1 * 1.25
will I get 12 as my answer?

Sorry to sound so lazy but I'm at work right now and I can't test it myself.

Roger Bacon

int3 will be 12.5 on this one....

Req
 
Thank you all again.

So, if I understand this correctly...

Writing a number without a decimal point tells Python it is an int. 3
Writing a number with a dcimal point tells the number it is a real (or float). 3.0
Multiplying two ints gives an int
Multiplying an int and a real gives a real.
Is all of that correct?

What does the // operator do?

Roger Bacon
 
// is called floor division. It always truncates fractional remainders down to their floor, regardless of type. It basically does the same thing as / but can do the same with floating-point integers.

Also, In a future Python release, / divison will be changed to return a true division result. So 1/2 will be .5, but 1//2 will still be 0.
 
vbraun said:
10/3 will give you 3 as an asnwer. 10/3.0 will give you 3.33333. 10//3.0 will give you 3.0
Damn, and here I was thinking the number of decimals in the number was the number of decimal places that would be stored/calculated. That means I can remove some of my "1.00"s!
 
Back
Top Bottom