Go to: Na-Rae Han's home page  

Python 3 Notes

        [ HOME | LING 1330/2330 ]

Data Types and Conversion

<< Previous Note           Next Note >>
On this page: type(), str(), int(), float(), list(), tuple(), and set()

Data Types in Python, tuple, set

Python has many built-in data types. You have seen: int (integer), float (floating-point number), str (string), list (list), and dict (dictionary). They all have distinct representations:
 
>>> 'hello'    # str
'hello' 
>>> 256        # int
256 
>>> 1.99       # float
1.99 
>>> [1,2,3,4]  # list
[1, 2, 3, 4] 
>>> {'a':'apple', 'b':'banana', 'c':'carrot'}    # dict
{'a': 'apple', 'c': 'carrot', 'b': 'banana'} 
Let me introduce here another important data type, which is called tuple. A tuple is just like a list except it is fixed (i.e., immutable). It is marked with a pair of parentheses ( ), with each item separated by a comma ,. In fact, parentheses are not necessary but commas are, as seen in the bottom example.
 
>>> ('gold', 'silver', 'bronze')    # tuple
('gold', 'silver', 'bronze') 
>>> 1, 2, 3                  # () are optional
(1, 2, 3) 
Last but not least, set is a very important and handy data type. A set is enclosed in curly braces { }, and it is both orderless and duplicate-less.
 
>>> {'Ross', 'Joey', 'Chandeler'}           # set in { }
{'Joy', 'Chandeler', 'Ross'} 
>>> {'Ross', 'Joey', 'Chandeler', 'Ross', 'Joey'}      # Duplicates are ignored
{'Joy', 'Chandeler', 'Ross'} 

Displaying object type with type()

If you are ever unsure of the type of the particular object, you can use the type() function:
 
>>> type('hello')
<type 'str'> 
>>> type(256)
<type 'int'> 
>>> type('256')
<type 'str'> 

Converting Between Types

Many Python functions are sensitive to the type of data. For example, you cannot concatenate a string with an integer:
 
>>> age = 21
>>> sign = 'You must be ' + age + '-years-old to enter this bar'

Traceback (most recent call last):
  File "<pyshell#71>", line 1, in <module>
    sign = 'You must be ' + age + '-years-old to enter this bar'
TypeError: cannot concatenate 'str' and 'int' objects  
Therefore, you will often find yourself needing to convert one data type to another. Luckily, conversion functions are easy to remember: the type names double up as a conversion function. Thus, str() is the function that converts an integer, a list, etc. to a string, and list() is the function that converts something into the list type. For the example above, you would need the str() conversion function:
 
>>> age = 21
>>> sign = 'You must be ' + str(age) + '-years-old to enter this bar'
>>> sign
'You must be 21-years-old to enter this bar'  

Conversion Functions

Below is a table of the conversion functions in Python and their examples.
Function Converting what to what Example

int()

string, floating point → integer
>>> int('2014') 
2014 
>>> int(3.141592)
3 

float()

string, integer → floating point number
>>> float('1.99') 
1.99 
>>> float(5)
5.0 

str()

integer, float, list, tuple, dictionary → string
>>> str(3.141592) 
'3.141592' 
>>> str([1,2,3,4])
'[1, 2, 3, 4]' 

list()

string, tuple, set, dictionary → list
>>> list('Mary')      # list of characters in 'Mary' 
['M', 'a', 'r', 'y'] 
>>> list((1,2,3,4))   # (1,2,3,4) is a tuple
[1, 2, 3, 4] 
>>> list({1, 2, 3})   # {1, 2, 3} is a set
[1, 2, 3] 

tuple()

string, list, set → tuple
>>> tuple('Mary')
('M', 'a', 'r', 'y') 
>>> tuple([1,2,3,4])   # [ ] for list, ( ) for tuple
(1, 2, 3, 4) 

set()

string, list, tuple → set
>>> set('alabama')            # unique character set from a string
{'b', 'm', 'l', 'a'} 
>>> set([1, 2, 3, 3, 3, 2])   # handy for removing duplicates from list
{1, 2, 3}