[Tutor] div_t

Kent Johnson kent37 at tds.net
Thu Jan 12 12:06:25 CET 2006


Burge Kurt wrote:
>>divmod(...)
>>    divmod(x, y) -> (div, mod)
> 
>  
> What is the easiest way to use div and mod seperately as an attribute like:
>  
> if a = divmod(x,y) 
>  
> a.div or a.mod

The result of calling divmod() is a two-element tuple. You access the 
elements by indexing:

  >>> a=divmod(10, 3)
  >>> a[0]
3
  >>> a[1]
1

You can also use tuple assignment to assign the two values to two 
variables (you can use other names than div and mod):
  >>> div, mod = divmod(10, 3)
  >>> div
3
  >>> mod
1

BTW you can't put an assignment in an if statement in python so your 
original code might be written as
div, mod = divmod(x, y)
if div...

Kent



More information about the Tutor mailing list