Python3 Basic Data Type Self-record, python3 Data Type
String (String)
Strings in Python are enclosed by single quotation marks (') or double quotation marks ("), and special characters are escaped using a backslash.
>>> str = "kevin">>> str'kevin'>>> str[0]'k'>>> str[0:-1]'kevi'>>> print(str + '111')kevin111>>> print(str *2)kevinkevin
1. The backslash can be used for escape, and r can be used to prevent the backslash from being escaped.
2. Strings can be connected together using the + operator, which is repeated using the * operator.
3. There are two indexing methods for strings in Python: 0 from left to right and-1 from right to left.
4. the strings in Python cannot be changed.
List is the most frequently used data type in Python.
The list can implement the data structure of most collection classes. The types of elements in the list can be different. It supports numbers, strings, and even lists (so-called nesting ).
A list is a list of elements written between square brackets ([]) and separated by commas.
Like a string, the list can also be indexed and intercepted. After the list is intercepted, a new list containing the required elements is returned.
Example:
>>> list=["ab",1,1.2,'kevin']>>> list2=[True,0.23]>>> print(list)['ab', 1, 1.2, 'kevin']>>> print(list2)[True, 0.23]>>> print(list[1])1>>> print(list[1:])[1, 1.2, 'kevin']>>> print(list*3)['ab', 1, 1.2, 'kevin', 'ab', 1, 1.2, 'kevin', 'ab', 1, 1.2, 'kevin']>>> print(list+list2)['ab', 1, 1.2, 'kevin', True, 0.23]
# The list elements can be changed >>> list [1] = 'aaaaaaaaaaa' >>> list ['AB', 'aaaaaaaaaaa', 1.2, 'kevin '] >>> list. pop () 'kevin '>>> list. append (2222) >>> list ['AB', 'aaaaaaaaaaa', 1.2, 2222]
Summary:
1. List is written between square brackets, and elements are separated by commas.
2. Like strings, list can be indexed and sliced.
3. List can be spliced using the + operator.
4. The elements in the List can be changed.
========================================================== ========================================================== ==============================================
The tuple is similar to the list, except that the elements of the tuples cannot be modified. Tuples are written in parentheses () and separated by commas.
Tuples are similar to strings. They can be indexed and the subscript index starts from 0.-1 is the position starting from the end. It can also be intercepted.
In fact, we can regard a string as a special tuples.
>>> Tuple = (0.3, "qwe", 'kevin ', True,) >>> tuple2 = (2, 3) >>> print (tuple) (1, 2, 'qw', 'kevin ', True, 0.3) >>> print (tuple [2]) qwe # Here are some special >>> a = (1) >>> type (a) <class 'int' >>> B = (1,) >>> type (B) <class 'tuple' >>>> c = () >>> type (c) <class 'tuple'> # the element of the tuples cannot be modified> B [0] = 22 Traceback (most recent call last): File "<pyshell #12>", line 1, in <module> B [0] = 22 TypeError: 'tuple' object does not support item assignment> print (tuple [0]) (1,) >>> print (tuple [0: 3]) (1, 2, 'qw') >>> print (tuple * 3) (1, 2, 'qw', 'kevin ', True, 0.3, 1, 2, 'qw', 'kevin ', True, 0.3, 1, 2, 'qw', 'kevin', True, 0.3)> print (tuple + tuple2) (1, 2, 'Qi', 'kevin ', True, 0.3, 2, 3)
Summary:
String, list, And tuple belong to sequence ).
1. Like a string, the elements of the tuples cannot be modified.
2. tuples can also be indexed and sliced in the same way.
3. Note the special syntax rules for constructing tuples containing 0 or 1 element.
4. tuples can also be spliced using the + operator.
========================================================== ========================================================== ==============================================
A set is a sequence of unordered, non-repeating elements.
The basic function is to test the member relationship and delete duplicate elements.
You can use braces {} or the set () function to create a set. Note: To create an empty set, you must use set () instead of {}, because {} is used to create an empty dictionary.
# Unordered and non-repeated >>> student = {'Tom ', 'Tom', 'Jack ', 'kevin'} >>>> student {'kevin ', 'Tom ', 'jack' }>>> a = set ("AB") >>> B = set ('bc') >>> print (a, B) {'B ', 'A'} {'B', 'C' }>>> print (a-B) {'A' }>> print (a | B) {'B', 'A', 'C'} # Only a and B can be used, but not a & B >>> print (a & B) {'B '}
========================================================== ========================================================== ==============================================
A dictionary is another useful built-in data type in Python.
A list is a combination of ordered objects, and a dictionary is a collection of unordered objects. The difference between the two is that the elements in the dictionary are accessed by keys, rather than by offset.
A dictionary is a ing type, which is identified by "{}". It is an unordered key: value Pair set.
The key must be of an unchangeable type.
In the same dictionary, the key must be unique.
>>> dict = {}>>> dict['name']='kevin'>>> dict['age']=18>>> dict{'name': 'kevin', 'age': 18}>>> dict2={'one':"1","two":"2"}>>> dict2{'one': '1', 'two': '2'}>>> dict['name']'kevin'>>> dict.keys()dict_keys(['name', 'age'])>>> dict.values()dict_values(['kevin', 18])
Summary:
In addition, the dictionary type also has some built-in functions, such as clear (), keys (), and values.
1. a dictionary is a ing type. Its elements are key-value pairs.
2. The dictionary keywords must be of an unchangeable type and cannot be repeated.
3. Create an empty dictionary {}.
2 int (x [, base]) converts x to an integer 4 float (x) and converts x to a floating point number 6 complex (real [, imag]) create a plural 8 str (x) to convert object x to string 10 repr (x) to convert object x to expression string 12 eval (str) to calculate a valid Python expression in the string, returns an object 14 tuple (s) to convert the sequence s into a single tuples 16 list (s) to convert the sequence s into a list 18 set (s) convert to a variable set 20 dict (d) to create a dictionary. D must be a sequence (key, value) tuples. 22 frozenset (s) to an unchangeable set 24 chr (x) to convert an integer to a character 26 unichr (x) to an integer converted to a Unicode Character 28 ord (x) convert a character to its integer value 30 hex (x) to an integer into a hexadecimal string 32 oct (x) to convert an integer into an octal string