All data values in Python are objects, and each object, or value, has a type. An object that can be altered is known as a mutable object, while one that cannot be altered is an immutable object.
The built-in type(obj) accepts any object as its argument and returns the type object that is the type of obj. Built-in function isinstance(obj, type) returns true if object obj has type type (or any subclass thereof); otherwise, it returns False.
Python has built-in types for fundamental data types such as numbers, strings, tuples, lists, and dictionaries. You can also create user-defined types, known as classes.
Sequences
A sequence is an ordered container of items, indexed by nonnegative integers. Python provides built-in sequence types known as strings (plain and Unicode), tuples, and lists. All sequences are iterable: whenever I say that you can use an iterable, you can, in particular, use a sequence.
'This is a literal string'
"This is another string"
"""An even bigger string that spans three lines"""
A variant of a string literal is a raw string, where an r or R immediately precedes the leading quote. In raw strings, escape sequences are not interpreted, but are literally copied into the string, including backslashes and newline characters.
Unicode string literals have the same syntax as other string literals, with a u or U immediately before the leading quote.
Raw Unicode string literals start with ur, not ru.
Multiple string literals can be adjacent, with optional whitespace in between. The compiler concatenates such adjacent string literals into a single string object. If any literal in the concatenation is Unicode, the whole result is Unicode. Writing a long string literal in this way lets you present it readably across multiple physical lines and gives you an opportunity to insert comments about parts of the string. For example:
marypop = ('supercalifragilistic' # Open paren -> logical line continues
'expialidocious') # Indentation ignored in continuation
The string assigned to marypop is a single word of 34 characters.
Tuples
The items of a tuple are arbitrary objects and may be of different types.
To create a tuple of one item (often known as a singleton), add a comma to the end of the expression.
To denote an empty tuple, use an empty pair of parentheses.
You can also call the built-in type tuple to create a tuple. When x is iterable, tuple(x) returns a tuple whose items are the same as the items in x. For example:
tuple('wow') builds a tuple equal to: ('w', 'o', 'w')
Lists
A list is a mutable ordered sequence of items. The items of a list are arbitrary objects and may be of different types. To specify a list, use a series of expressions separated by commas (,) and within brackets ([]).
You can also call the built-in type list to create a list. When x is iterable, list(x) returns a tuple whose items are the same as the items in x. For example:
Sets
====
Python 2.4 introduces two built-in set types, set and frozenset, to represent arbitrarily ordered collections of unique items. These types are equivalent to classes Set and ImmutableSet found in standard library module sets, which also exists in Python 2.3.
Items in a set may be of different types, but they must be hashable.
Instances of type set are mutable, and therefore not hashable; instances of type frozenset are immutable and hashable. Therefore you can't have a set whose items are sets, but you can have a set (or frozenset) whose items are frozensets.
To create a set, call the built-in type set with no argument (this means an empty set) or one argument that is iterable.
Dictionaries
A mapping is an arbitrary collection of objects indexed by nearly arbitrary values called keys. Mappings are mutable and, unlike sequences, are not ordered.
You can also call the built-in type dict to create a dictionary;
dict(x=42, y=3.14, z=7)
You can also create a dictionary by calling dict.fromkeys. The first argument is an iterable whose items become the keys of the dictionary; the second argument is the value that corresponds to each key (all keys initially have the same corresponding value). If you omit the second argument, the value corresponding to each key is None. For example:
dict.fromkeys('hello', 2) # same as {'h':2, 'e':2, 'l':2, 'o':2}
dict.fromkeys([1, 2, 3]) # same as {1:None, 2:None, 3:None}
None
The built-in None denotes a null object.
Callables
In Python, callable types are those whose instances support the function call operation.
Functions are callable.
Types are also callable, as we already saw for the dict, list, and tuple built-in types.
Other callables are methods, which are functions bound to class attributes and instances of classes that supply a special method named _ _call_ _.
Boolean Values
Every data value in Python can be taken as a truth value: true or false. You can call bool(x) with any x as the argument.
But good Python style always write if x:, never if bool(x):, if x==True:, if bool(x)==True, and so on.
Any nonzero number or nonempty container (e.g., string, tuple, list, set, or dictionary) is true.
0 (of any numeric type), None, and empty containers are false.
Built-in type bool is a subclass of int. The only two values of type bool are True and False,