[Tutor] Does casting exist in Python?

Kent Johnson kent37 at tds.net
Wed Feb 22 20:22:17 CET 2006


Edgar Antonio Rodriguez Velazco wrote:
> Does it?

No, because variables are untyped in Python there is no need for 
casting. In Python values have types, variables are just names. You just 
use the value and if it has the correct type it will work.

There are some functions that transform values from one type to another. 
These are not really casts, they create new values of a different type. 
Some examples are int(), str(), float(). For example:

  >>> i=3
  >>> type(i)
<type 'int'>
  >>> f=float(i)
  >>> f
3.0
  >>> type(f)
<type 'float'>

Kent



More information about the Tutor mailing list