Python learning notes-several data types, python learning notes
1. list:
A built-in data type in Python is list: list, which is represented by brackets. List is an ordered set. You can add and delete elements at any time, and the element types do not have to be the same. List can be accessed through subscript, range from 0 to len (name)-1 (len () function can get the list length ).
1.1 insert (position, element ):You can insert an element to a specified position by subscript, and the subsequent elements will be moved one by one in order.
1.2 add append (element ):Insert the element to the end of the list.
1.3 Delete pop (element ):Deletes an element at the specified position. If the parameter is null, the elements at the end of the list are deleted by default.
1.4 replace:There is no function to replace. You only need to assign values to the elements to be replaced by subscript.
2. tuple:
Tuples (represented by parentheses () are very similar to the list, but the tuples cannot be changed once initialized, making the code more secure. Tuples can also be accessed through subscripts, but no operation functions such as insertion or deletion are provided.
2.1When defining tuples with only one number, you cannot directly define them. You need to add a comma after the number, because parentheses can also represent parentheses in the mathematical formula, which will lead the compiler to mistakenly think it is a number rather than a tuple.
>>> t = (1,)>>> t(1,)
2.2 "mutable" tuples:When the element of the list is a list, the list element can be changed. In fact, the elements of the tuples have not changed. The tuples always point to those elements during initialization and are not changed to other elements.
>>> t = ('a', 'b', ['A', 'B'])>>> t[2][0] = 'X'>>> t[2][1] = 'Y'>>> t('a', 'b', ['X', 'Y'])
3. dictionary dict:
The full name of dict is dictionary (represented by braces {}). It is stored with key-value (key-value), and the search speed is extremely fast. When searching, the dictionary calculates the memory location of the value through the key (a key can only correspond to one value, and the previous value will be washed out if you repeat a key), and does not need to be traversed one by one, therefore, the search speed is fast.
>>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}>>> d['Michael']95
3.1 determine whether a key exists:
Method 1: key in dict returns a Boolean value.
>> 'Thomas' in dFalse
Method 2: If the get (key) does not exist, None is returned.
3.2 delete pop (key ):You can delete a key-Value Pair Based on the key and return the value.
Dictionary has a lot of operational functions, not record one by one, visible in http://www.jb51.net/article/67106.htm
4. set:
Set is similar to dict. It cannot be placed into a variable object, but set does not store value. To create a set, you must provide a list as the input set:
>>> s = set([1, 2, 3])>>> sset([1, 2, 3])
4.1 add (key ):You can add the same key again, but it will not be affected.
>>> s.add(4)>>> sset([1, 2, 3, 4])>>> s.add(4)>>> sset([1, 2, 3, 4])
4.2 Delete remove(key)。
4.3 intersection and Union operations. (&, |)
>>> s1 = set([1, 2, 3])>>> s2 = set([2, 3, 4])>>> s1 & s2set([2, 3])>>> s1 | s2set([1, 2, 3, 4])