# Difference between revisions of "Type conversion functions"

m |
|||

Line 2: | Line 2: | ||

==Type Conversion in [[Python]]== | ==Type Conversion in [[Python]]== | ||

− | *<tt>'''str'''(x)</tt> returns a [[string]] version of x. Almost all datatypes support this function, and you can use it on your own [[class]] | + | *<tt>'''str'''(x)</tt> returns a [[string]] version of x. Almost all datatypes support this function, and you can use it on your own [[class (Python)|classes]] by implementing the <tt>__str__</tt> function. |

*<tt>'''int'''(x)</tt> 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. | *<tt>'''int'''(x)</tt> 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. | ||

*<tt>'''float'''(x)</tt> converts x into a [[floating point number]]. Strings that don't represent a valid floating point number will result in an error. | *<tt>'''float'''(x)</tt> converts x into a [[floating point number]]. Strings that don't represent a valid floating point number will result in an error. | ||

− | *<tt>'''list'''(x)</tt> converts any [[python sequence types| | + | |

+ | ====[[sequence (Python)|Sequence]] Conversion==== | ||

+ | *<tt>'''list'''(x)</tt> converts any [[python sequence types|sequence]] x into a [[list]]. | ||

+ | *<tt>'''tuple'''(x)</tt> converts any [[python sequence types|sequence]] x into a [[tuple]]. | ||

+ | |||

[[Category:Introduction to Programming]] | [[Category:Introduction to Programming]] | ||

+ | [[Category:Python Functions]] |

## Latest revision as of 20:10, 19 April 2011

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

## Type Conversion in Python

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**(x)`__str__`function.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.**int**(x)converts x into a floating point number. Strings that don't represent a valid floating point number will result in an error.**float**(x)