Data structure without hash table
Slow lookup Data structures
number
The number types are divided into the following categories:
int represents an integer number, and float represents a floating point number, which is a decimal number. bool represents the boolean type, which contains two types of True and False. The last type is a complex number, which contains real and imaginary parts. The unit of imaginary number is represented by j.
We can assign a number to a variable:
Enter the variable name after the >>> symbol, and the value of the variable can be displayed on the screen, like >>> a.
In this case, the value of variable a is 2.
About variables
The first character must be a letter in the alphabet or an underscore _.
The other parts of the Variable consist of letters, numbers, and underscores.
Variable are case sensitive.
Variable names should not use Python reserved words.
To explain the firstand second one, for example, abc can be a variable name, _abc can also be a variable name, studnt_id can also be a variable name, but 2021_weeks cannot be a variable name, because a variable name cannot start with a number.
To explain the third point: time and Time and TIME are three different variables, because variable names are case sensitive.
To explain the fourth item, if the variable name is the same as the reserved word in python, then this python reserved word is invalid, which is equivalent to modifying python itself. This is possible, but it is strongly not recommended. Python reserved words can be viewed using the following methods:
About '='
In programming languages, = means assignment, not equality in mathematics.
For example, a=3 means to assign 3 to a. b=a means to assign a to b. a=a+1 means to assign a+1 to a, that is, to increase a by 1.
The demonstration program is as follows:
string
The string can use single quotation marks ('...') or double quotation marks ("...") to indicate, a multi-line string uses three single quotation marks ("'...''') or three Multiple quotation marks ("""...""") to indicate.
Among them, \n means newline.
String index
The string is composed of characters, and the character at the corresponding position can be extracted by the string index. It is important that the string index starts from 0.
For convenience, we use the string '123456' (note: the string in the quotation marks is not a number) to illustrate
We assign the string to a variable, and then index the variable. When the Index is exceeded the length of the string(array out of bounds), python will report an error.
One advantage of using python is that python will report an array out-of-bounds error.
This is not reminded in the C language, C++ and Java. The array beyond the boundary may cause serious consequences, may modify the memory that is not intended to be modified, or leak important data to the hacker.
Index value can be negative
Let's still use string '123456' as an example to try what happens with negative indexes:
We found that when a negative number is used as an index value, it will index from the end of the string forward.
When the index exceeds the length of the string, an error will still be reported.
Indexes are used in python strings, lists, tuples, and sets. The rules for indexing in python are the same.
String slice
String slicing is very important. When we want to extract the key information of a string, slicing can help us extract part of it:
The string slice is like this: string name** [ start position : end position ]**
When the start position is empty, it means to start cutting from the beginning of the string:
When the end position is empty, it means to keep to the end of the string:
Python's slicing principle is left-closed and right-opened, which means that the start position is included in the slicing result and the end position is not included in the splicing result.
An example is:
The elements on the left edge of the slice will be kept, and the elements on the right edge of the slice will be discarded.
Slices are used in python strings, lists, tuples, and sets. The rules for slicing in python are the same.
list
Lists are one of the most commonly used data types in python.
We can creat an empty list in the following two ways.
The slice and index of the list are exactly the same as the string, so I won’t repeat them here. I will give a few examples:
range(...)
The range(...) function is one of the most commonly used construction methods in python.
The range function will generate a sequence of numbers:
Range will generate a range type variable. If you force this range type variable into a list, then you can see the numbers contained in this range.
If you give only one parameter to the range function, then range will generate an integer sequence starting from 0 to this parameter minus 1.
This parameter must be an integer, otherwise an error will be reported.
Range can also enter multiple parameters. The first parameter is the start point of the range(included), the second parameter is the end point of the range(not included), and the third parameter is the step length of the range (optional parameter)
The step length can be a negative number, then the sequence is a decreasing sequence, notice that the starting point must be greater than the ending point, otherwise the range function will return an empty list.
Forced type conversion
**Forced type conversion ****** can convert a data structure of one type into a data structure of another type.
For example, list(...) can convert the data type in parentheses to list.
The type(...) function can return the type of a variable.
Let's continue to explain the list:
append
The most important method in the list is append, This is a way to add variables to the end of the list.
We can add any element to the end of the list, including numbers, strings, and even add a list, so that this list is nested into another list, as shown above.
pop
pop is one of the classic operations of the stack, which means to remove and output the last element from the stack
We can observe the pop operation from the above example. The pop operation of python also supports popping the element with the corresponding index. For example, pop(8) means popping the element with the index 8. The 8 here does not refer to the 8, but the 9th element (the index is 8)
tuple
Tuples are almost the same as lists, except that the contents of tuples cannot be changed, so tuples do not have methods such as append or pop that can change its elements.
Tuples are rarely used in python, because although the contents of tuples are immutable, variable names can be reassigned, which also destroys the immutable properties of tuples:
Through the above program, you can change the content referred to by the variable name that originally pointed to the tuple.
This makes tuples meaningless in python.
Unlike in C++:
The C++ const attribute makes an element completely fixed, and there is no way to change this variable.
Statistics
Start time of this page: December 19, 2021
Completion time of this page: December 30, 2021
Last updated