Reference: http://www.runoob.com/python3/python3-data-type.html
Standard data type
There are six standard data types in Python3: Number (numeric) string (string) list (list) Tuple (tuple) Sets (set) Dictionary (dictionary) numbers and strings are not much to say, but note:
1, the backslash can be used to escape, using R allows the backslash to not be escaped. 2. Strings can be joined together with the + operator, repeated with the * operator. 3. Strings in Python are indexed in two ways, starting from left to right with 0, starting from right to left with-1. 4, the string in Python can not be changed.
list (listing)
The list is the most frequently used data type in Python.
The list can complete the data structure implementation of most collection classes. The types of elements in a list can be different, it supports numbers, and strings can even contain lists (so-called nesting).
The list is a comma-separated list of elements written between brackets ([]).
As with strings, lists can also be indexed and intercepted, and the list is truncated to return a new list of required elements.
The syntax format for the list interception is as follows:
Variable [header subscript: tail subscript]
The index value starts with 0, and 1 is the starting position from the end.
The plus sign (+) is a list concatenation operator, and an asterisk (*) is a repeat operation. accessing values in a list
Use the subscript index to access the values in the list, but you can also use the square brackets to intercept the characters.
Update list You can modify or update a list's data items, or you can use the Append () method to add list items.
Delete list elements you can use the DEL statement to delete the elements of a list.
python list script operator
The list of + and * operators are similar to strings. The + number is used to combine the list, and the * is used to repeat the list.
As shown below:
| Python Expression |
Results |
Description |
| Len ([1, 2, 3]) |
3 |
Length |
| [1, 2, 3] + [4, 5, 6] |
[1, 2, 3, 4, 5, 6] |
Combination |
| [' hi! '] * 4 |
[' hi! ', ' hi! ', ' hi! ', ' hi! '] |
Repeat |
| 3 in [1, 2, 3] |
True |
Whether an element exists in the list |
| For x in [1, 2, 3]: print (x, end= "") |
1 2 3 |
Iterations |
python list interception and stitching
The Python list intercepts and the string operation type, as follows: L = [' Google ', ' Runoob ', ' Taobao ']
Operation:
| Python Expression |
Results |
Description |
| L[2] |
' Taobao ' |
Reading a third element |
| L[-2] |
' Runoob ' |
Read the penultimate element from the right-hand side: count from the right |
| L[1:] |
[' Runoob ', ' Taobao '] |
Outputs all elements after the start of the second element |
Nested lists
Use nested lists to create other lists in the list, for example: >>> a = [' A ', ' B ', ' C ']
>>> n = [1, 2, 3]
>>> x = [A, n]
>>> x [[' A ', ' B ', ' C '], [1, 2, 3]]
>>> x [0] [' A ', ' B ', ' C ']
>>> x [0] [1] ' B ' python list function & method
Python contains the following functions:
| Serial Number |
function |
| 1 |
Len (list)
Number of list elements |
| 2 |
Max (list)
Returns the maximum value of a list element |
| 3 |
MIN (list)
Returns the minimum value of a list element |
| 4 |
List (seq)
Convert a tuple to a list |
Python includes the following methods:
| ordinal |
method |
| 1 |
list.append (obj)
at the end of list End Add new Object |
| 2 |
list.count (obj)
count the number of times an element appears in the list |
| 3 |
list.extend (seq)
Appends multiple values from another sequence at the end of the list (extending the original list with the new list) |
| 4 |
List.index (obj)
Find the index position of the first occurrence of a value from the list |
| 5 |
list.insert (index, obj)
Inserts an object in the list |
| 6 |
List.pop (obj= LIST[-1])
removes an element from the list (the default last element), and returns the value of the element |
| 7 |
list.remove (obj)
Remove first of a value in the list Matches |
| 8 |
list.reverse ()
Reverse list elements |
| 9 |
List.sort ([func])
Sort the original list |
| ten |
list.clear ()
Empty list |
| one |
list.copy ()
Copy list |
Tuple (META Group)
Tuples (tuple) are similar to lists, except that the elements of a tuple cannot be modified. The tuples are written in parentheses (()), and the elements are separated by commas. The element types in the tuple can also be different.
Create an empty tuple
Tup1 = ();
When you include only one element in a tuple, you need to add a comma after the element, otherwise the parentheses will be used as an operator.
Tuples are similar to strings, the subscript index starts at 0 and can be intercepted, combined, and so on.
Access tuples can use subscript indexes to access values in tuples
Modifying tuples
The element values in the tuple are not allowed to be modified, but we can combine the tuples into the following example:
#!/usr/bin/python3
tup1 = (34.56);
tup2 = (' abc ', ' xyz ')
# The following is illegal to modify the tuple element operation.
# Tup1[0] =
# Create a new tuple
Tup3 = Tup1 + tup2;
Print (TUP3)
The above example outputs the result:
(34.56, ' abc ', ' XYZ '
Delete tuplesThe element values in the tuple are not allowed to be deleted, but we can use the DEL statement to delete the entire tuple.
Tuple Operators
As with strings, the + and * numbers can be used between tuples. This means that they can be combined and copied, and a new tuple will be generated after the operation.
| Python Expression |
Results |
Description |
| Len ((1, 2, 3)) |
3 |
Calculate the number of elements |
| (1, 2, 3) + (4, 5, 6) |
(1, 2, 3, 4, 5, 6) |
Connection |
| (' hi! ',) * 4 |
(' hi! ', ' hi! ', ' hi! ', ' hi! ') |
Copy |
| 3 in (1, 2, 3) |
True |
Whether an element exists |
| For x in (1, 2, 3): Print x, |
1 2 3 |
Iterations |
Meta-Group index, intercepting
Because the tuple is also a sequence, we can access the element at the specified position in the tuple, or we can intercept a section of the index, as follows:
META Group:
L = (' Google ', ' Taobao ', ' Runoob ')
| Python Expression |
Results |
Description |
| L[2] |
' runoob! ' |
Reading a third element |
| L[-2] |
' Taobao ' |
Reverse read; Read the penultimate element |
| L[1:] |
(' Taobao ', ' runoob! ') |
Intercepts elements from all elements after the second start. |
Tuple built-in functions
The Python tuple contains the following built-in functions
| Ordinal |
method and Description |
instance |
| 1 |
len (tuple)
Compute the number of tuple elements. |
>>> tuple1 = (' Google ', ' Runoob ', ' Taobao ') >>> len (tuple1) 3 >>> |
| 2 |
max (tuple)
Returns the maximum number of elements in a tuple. |
>>> tuple2 = (' 5 ', ' 4 ', ' 8 ') >>> max (tuple2) ' 8 ' &G t;>> |
| 3 |
min (tuple)
Returns the element minimum value in a tuple. |
>>> tuple2 = (' 5 ', ' 4 ', ' 8 ') >>> min (tuple2) ' 4 ' &G t;>> |
| 4 |
tuple (seq)
Converts a list to a tuple. |
>>> list1= [' Google ', ' Taobao ', ' Runoob ', ' Baidu '] >> > tuple1=tuple (list1) >>> tuple1 (' Google ', ' Taobao ', ' Runoob ', ' Baidu ') |
Set (set)
A collection (set) is a sequence of unordered, distinct elements.
The basic function is to conduct member relationship testing and delete duplicate elements.
You can use the curly braces {} or the set () function to create a collection, and note that creating an empty collection must be set () instead of {} because {} is used to create an empty dictionary.
To create a format:
Parame = {value01,value02,...}
or
set (value)
Python3 Dictionary
A dictionary is another variable container model and can store any type of object.
Each key value (Key=>value) pair of the dictionary is separated by a colon (:), separated by commas (,), and the entire dictionary is enclosed in curly braces ({}) as follows:
D = {key1:value1, key2:value2}
The key must be unique, but the value does not have to be.
Value can take any data type, but the key must be immutable, such as a string, a number, or a tuple.
A simple example of a dictionary:
Dict = {' Alice ': ' 2341 ', ' Beth ': ' 9102 ', ' Cecil ': ' 3258 '}
You can also create a dictionary like this:
Dict1 = {' abc ': 456};
Dict2 = {' abc ': 123, 98.6:37};
accessing values in a dictionary
Put the corresponding key into the familiar square bracket, the following example:
#!/usr/bin/python3
dict = {' name ': ' Runoob ', ' age ': 7, ' Class ': '
a ' (' dict[' name ']: ', dict[' name '] C4/>print ("dict[' age ']:", dict[' age ')
The above example outputs the result:
dict[' Name ']: runoob
dict[' age ': 7
Modify a dictionary
The way to add new content to a dictionary is to add new key/value pairs, modify or delete existing key/value pairs as follows:
#!/usr/bin/python3
dict = {' Name ': ' Runoob ', ' age ': 7, ' Class ': ' The ' of ' the ' of ', ' and ' ' (' age
') = 8; # Update Age
dict[' School ' = "Rookie tutorial" # Add info
print ("dict[' age ']:", dict[' age ')
print ("dict[' School ')" : ", dict[' School '])
The above example outputs the result:
Dict[' age ': 8
dict[' School ']: rookie Tutorial
Delete a dictionary element
Can delete a single element can also empty the dictionary, empty only one operation.
Display deletes a dictionary with the DEL command, as follows:
#!/usr/bin/python3
dict = {' name ': ' Runoob ', ' age ': 7, ' Class ': ' ' the ' name ' of ' the ', ' ' The ' name ' of '
# ' delete key ' # '
dict.c Lear () # Delete dictionary
del dict # Delete dictionary
print ("dict[' age ']:", dict[' age ')
print ("dict[' School ']": ", dict[' School '])
However, this throws an exception because the dictionary does not exist after the Del operation:
Traceback (most recent):
File "test.py", line 9, in <module>
print ("dict[' age '):", dict[' age ' )
typeerror: ' Type ' object is not subscriptable
characteristics of dictionary keys
The dictionary value can be any Python object, either a standard object or a user-defined one, but the key is not available.
Two important points to remember:
1 does not allow the same key to appear two times. When created, if the same key is assigned two times, the latter value is remembered, as in the following example:
#!/usr/bin/python3
dict = {' name ': ' Runoob ', ' age ': 7, ' name ': ' Little Rookie '}
print ("dict[' name ']:", dict[' name ')
The above example outputs the result:
dict[' Name ']: little Rookie
2 The key must be immutable, so you can use numbers, strings, or tuples to act as a list, and the following examples:
#!/usr/bin/python3
dict = {[' name ']: ' Runoob ', ' Age ': 7}
print (' dict[' name ': ', dict[' name '])
The above example outputs the result:
Traceback (most recent):
File "test.py", line 3, in <module>
dict = {[' Name ']: ' Runoob ', ' Age ': 7}
typeerror:unhashable Type: ' List '
Dictionary built-in Functions & methods
The Python dictionary contains the following built-in functions:
1
| ordinal |
function and description |
instance |
len (dict)
calculates the number of dictionary elements, that is, the total number of keys. |
>>> = {' Name ': ' Dict ', ' age ': 7, Runoob < span> |