Number (number)
As the name implies, numeric types are used to store numeric values. It should be remembered that they taste a bit like Java strings. If the value of numeric data type changes, memory space will be reallocated.
Del statement can be used to delete references of some digital objects: delvar 1 [,var2 [,var3 [..., varn]]].
Python supports three different numeric types:
1.integer (int)-usually called integer or integer, positive integer or negative integer without decimal point. Python3 integer has no limit on its size and can be used as a Long type, so Python3 does not have Python2' s Long type.
2.float)-Float consists of an integer part and a decimal part, and can also be expressed by scientific notation (2.5e2 = 2.5 x 102 = 250).
3. Complex number ((complex number))-Complex number consists of real part and imaginary part, which can be represented by a+bj or complex number (a, b). The real part A and imaginary part B of a complex number are both floating-point types.
Digital type conversion
1.int(x) converts x into an integer.
2.float(x) converts x into a floating point number.
3.complex(x) converts X into a complex number, where the real part is X and the imaginary part is 0.
4.complex(x, y) converts X and Y into complex numbers, where the real part is X and the imaginary part is Y..x and Y are numerical expressions.
Additional explanation
Like other languages, numeric types support various common operations, but python is richer in operations than most other common languages. In addition, there are many rich ways to provide more efficient development.
String (string)
Create string
You can use single quotation marks, double quotation marks, three single quotation marks and three double quotation marks to create a string, in which three quotation marks can define a multi-line string, which is somewhat similar to the back quotation marks in ES6.
Python does not support single character types, and single characters are also used as strings in Python.
Access the value in the string
Like ES, you can use square brackets to take screenshots of strings, as shown in the following example:
val_str='yelloxing '
print(val_str[0]) #y
print(val_str[ 1:3]) #el
print(val_str[:3]) #yel
print(val_str[:5]) #yello
String operator
In addition to the brackets explained above, there are some other string operations. See the documentation for more information.
tostring
Temp= "My name is %s and I'm %d years old!" % ("Heart Leaf", 7)
Print('['+temp+']') #[ My name is Xinye, and I'm 7 years old! ]
As shown above, strings support formatting. Of course, besides the %s and %d used above, there are others, depending on the documentation. Does it feel a bit like C language?
Additional explanation
All strings are Unicode strings (for python3), and there are many useful methods, really a combination of ES and C.
List (list)
Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its position or index, the first index is 0, the second index is 1, and so on.
Python has six sequences of built-in types (list, tuple, string, Unicode string, buffer object and xrange object).
Lists are actually similar to arrays, and some specific operations are very similar to strings (similar to the relationship between arrays and strings in ES).
Ordinary operation
The following examples show some common operations:
val_arr=['Made ',' in ',' China']
del val_arr[ 1]
Print(val_arr) #[' Manufacturing',' China']
Print (len(val_arr)) #2
val _ new arr = val _ arr+[':information ']
Print(val_newarr) #[' manufacturing',' China',': information'].
val_arr=val_arr*2
Print(val_arr) #[' Manufacturing',' China',' Manufacturing',' China'].
print(' in ' in in val _ arr)# False
print('Made' in val_arr) #True
For lines in val_newarr:
Print(row, end="-") Made in China-:Information-
Print(val_newarr[- 1]) #: information
Print(val_newarr[ 1:]) #[' China',': information']
Let's look at another useful example:
Number of columns =3
Number of rows =2
list _ 2d =[[0 for col in range(cols)]for row in range(rows)]
print(list_2d) #[[0,0,0],[0,0,0]]
Nested list
Using nested lists means creating other lists in the list, for example:
Loop_arr=['yelloxing ',' heart lobe']
Result_arr=[loop_arr,' same level']
Print(result_arr) #[['yelloxing',' heartbeat'],' sibling']
The nesting of lists is very flexible. By the way, as mentioned above, there are many ways to provide efficient development.
Tuple (tuple)
Tuples are similar to lists, except that the elements of tuples cannot be modified. Tuples are in brackets and lists are in square brackets.
build
When a tuple contains only one element, you need to add a comma after the element, otherwise parentheses will be used as operators.
tup 1 = ('Google ',' Runoob ', 1997,2000);
tup2 = ( 1,2,3,4,5);
tup3 = "a "," b "," c "," d ";
print(tup 1) #('Google ',' Runoob ', 1997,2000)
print(tup2) #( 1,2,3,4,5)
print(tup3) #('a ',' b ',' c ',' d ')
Basic operation
Similar to the operation of the list, there are several special places here:
1.del You can delete a tuple, but you can't delete an entry of a tuple.
2. Can't modify, maybe tuples will be faster, feel, and have no actual test.
3. Because tuples cannot be modified, although there are some methods, there is no method related to modification.
Set (set)
Think back to the set in mathematics. Are the operations of combination, intersection, difference and complement suddenly recalled? The collection here also has these methods.
Similar to Java collections, unordered collections of elements are not repeated (unlike lists and tuples, collections are unordered and cannot be indexed by numbers).
More specific instructions will be given separately if necessary.
Dictionary (dictionary)
A dictionary is another variable container model, which can store any kind of objects.
Each key value (key => value) pair of the dictionary is separated by a colon (:), and each pair is separated by a comma (,). The whole dictionary is enclosed in curly braces ({}). The key must be unique, but the value is not.
Similar to JSON in ES, the operation is very similar, but there are also great differences. There are many built-in methods, but the details are the same. Read the document.
Delete dictionary elements
Del can be used to delete entries or dictionaries, and clear () can be used to empty dictionaries (for example, there is a field dict, which is dict.clear ()).