Difference between revisions of "Type conversion functions"

m
Line 1: Line 1:
'''Type conversion functions''' are used in programming to convert data to primitive [[datatypes]].
+
'''Type conversion functions''' are used in programming to convert data to primitive [[datatype]]s.
  
 
==Type Conversion in [[Python]]==
 
==Type Conversion in [[Python]]==

Revision as of 00:33, 11 March 2011

Type conversion functions are used in programming to convert data to primitive datatypes.

Type Conversion in Python

  • str(x) returns a string version of x. Almost all datatypes support this function, and you can use it on your own classes by implementing the __str__ function.
  • int(x) converts x into an integer. Floats will be rounded down to the nearest integer, and strings that don't represent a valid integer will result in an error.
  • float(x) converts x into a floating point number. Strings that don't represent a valid floating point number will result in an error.
  • list(x) converts any iterable x into a list.