The main difference between a list and a tuple is that the list is enclosed in parentheses ([]) and their elements and sizes can be changed, while tuples are in parentheses () and cannot be updated. Tuples can be thought of as read-only lists.
Values stored in a list can be accessed using the slice operator ([] and [:]) with the index starting at 0, at the beginning of the list and ending with-1. The plus sign (+) symbol lists the join operator, and the asterisk (*) repeats the operation.
A python dictionary is a hash table type. They are made up of key-value pairs like associative arrays or hashes in Perl. The dictionary key can be almost any Python type, but it is usually a number or a string. The value can be any Python object.
The dictionary is composed of curly braces ({}), which can be assigned a value and accessed with square brackets ([]).
Sometimes, you might need to perform a conversion between built-in types. Conversions between types, simply use the class masterpiece as a function.
There are several built-in features that convert from one data type to another. These functions return a new object that represents the converted value.
Data type conversions:
function |
Description |
int (x [, Base]) |
Converts x to an integer. The cardinality is specified as base, if X is a string. |
Long (x [, Base]) |
Converts x to a long integer. The cardinality is specified as base, if X is a string. |
Float (x) |
Converts an X to a floating-point number. |
Complex (real [, Imag]) |
Creates a complex number. |
STR (x) |
Convert object x to a string representation. |
REPR (x) |
Object x is converted to an expression string. |
eval (str) |
Computes a string and returns an object. |
Tuple (s) |
Converts s to a tuple. |
List (s) |
Convert s to a list. |
Set (s) |
Converts s to a collection. |
Dict (d) |
Create a dictionary. D must be the (key, value) tuple sequence. |
Frozenset (s) |
Converts s to frozen set. |
Chr (x) |
Integers are converted to one character. |
UNICHR (x) |
The integer is converted to a Unicode character. |
Ord (x) |
Converts a single character to an integer value. |
Hex (x) |
Converts an integer to a hexadecimal string. |
Oct (x) |
Converts an integer into a eight-based string. |
Python Learning note 02 python data type 2