The "Uncool" Python types
-
int:
- Integer values (-2^32 , 2^32)
-
long:
- Any integer larger then a plain int.
-
float:
- All floating point values.
-
str:
- Strings and characters are represented as the same type.
The "Cool" Python Types
- tuple:
- Immutable groups of elements.
Elements can have different types
Coordinates: p1 = (0, 1)
Used to return more then one value: return (name, address)
-
list:
- Mutable "lists" of elements.
Elements can have different types
Similar to arrays or linked lists, with similar uses
-
dict:
- Dictionaries are mutable and keyed datastructures. They store and retrieve elements based on hashed keys (of type str).
Elements can be of different types.
Typing
Python features a "dynamic" typing model, where types are allocated on the fly and can be changed mid program. From one line to the next a variable "x" could be an integer, a string, and a list. Not all at the same time of course, but x could be set to 3 different values, and Python would accept it as completely valid. This flexible and powerful type system is useful because it is generally very intuitive, and doesn't really intrude when you're writing code. You rarely have to go back and define variables, nor do you have to define lots of temporary variables for different types of data. Let's look at a simple example or two.
Type Specific Functions
Here I'd like to illustrate a bunch of type specific operations and functions as a reference. A lot of these functions you'll need often, so it seems only fair to introduce them early on. On to the examples: