Assign long integer (was: [Tutor] Calculating a math formula
and finding errors)
Jeff Shannon
jeff@ccvcorp.com
Wed Jan 15 19:40:02 2003
Jens Kubieziel wrote:
>On Mon, Jan 13, 2003 at 03:22:07AM -0800, Danny Yoo wrote:
>
>
>>def factorial(z):
>> assert z >= 0, "Invalid input: z must be nonnegative."
>> if z == 1 or z == 0:
>> return 1
>> else:
>> return z*factorial(z-1)
>>
>>
>
>I still try to improve above mentioned function and want to extend this
>for long integers. I read that I can do this by adding an "l". But how do
>I do this with variables?
>If I'd write zL it is like a new variable. So must be another way.
>
>
Actually, because Python will automatically convert to long integers if
there's integer overflow, you don't need to do anything.
>>> factorial(3)
6
>>> factorial(10)
3628800
>>> factorial(15)
1307674368000L
>>>
As you can see, the final result there is a number followed by an L.
That means that the result is a long int, despite the fact that nothing
in the function specifies a need to use long ints -- I've used exactly
the same function posted here. Python has automagically handled the
overflow by switching to longs, without us needing to do anything.
Jeff Shannon
Technician/Programmer
Credit International