Python data type, python
The Data Types of the tuples, lists, and dictionaries in python are very python (there python is a adjective) data structures. These structures are optimized enough, so if they are used well, they will be of great benefit in some areas.
Tuples
In my opinion, just like the java array, the tuples in python have the following features:
- An ordered set of any object. This article does not cover the same type of array;
- Read by offset;
- Once generated, it cannot be changed;
- Fixed Length, supporting nesting
For example:
Python code
- >>> (0, 'hahaha', (4j, 'y '))
- (0, 'hahaha', (4j, 'y '))
- >>> T = (1, 3, 'B ')
- >>> T [2]
- 'B'
- >>> T [3]
- Traceback (most recent call last ):
- File "#41>", line 1, in <module> </module>
- T [3]
- IndexError: tuple index out of range
- >>> T [-1]
- 'B'
- >>> T [0:-1]
- (1, 3)
- >>> T * 2
- (1, 3, 'B', 1, 3, 'B ')
- >>> For x in t:
- Print x,
- 1 3 B
- >>> 'B' in t
- True
- >>> Q = t + (3, 'abc '))
- >>> Q
- (1, 3, 'B', 3, 'abc ')
- >>> For x in (2, (3, 'A ')):
- Print x
- 2
- (3, 'A ')
- >>> Len (q)
- 5
- >>> Len (2, (3, 'abc ')))
- 2
- >>> (1, 2, 3) [1]
- 2
- >>> Q [1] = 'D'
- Traceback (most recent call last ):
- File "#57>", line 1, in <module> </module>
- Q [1] = 'D'
- TypeError: 'tuple' object does not support item assignment
- >>> A = ('B', 'C', q)
- >>> 1 in
- False
- >>> Q in
- True
- >>>
- ('B', 'C', (1, 3, 'B', 3, 'abc '))
- >>> Q = 'D'
- >>>
- ('B', 'C', (1, 3, 'B', 3, 'abc '))
The above example is enough to illustrate the majority. The most important thing when using tuples is "Once generated, it will not be changed ".
List
The list is like a collection in java. It has more features than tuples and is more flexible. Its character is summarized as follows:
- An ordered set of any object;
- You can use offset access. Note that the elements in the list are variable, which is different from the tuples;
- Variable Length, supports nesting;
- There are also some object reference mechanisms similar to java
These features make the list widely used in practical applications. The following are some examples.
1) The first is basic usage.
Python code
- >>> L = ['A', 'B', 'C']
- >>> Len (l)
- 3
- >>> L + ['D']
- ['A', 'B', 'C', 'D']
- >>> L * 2
- ['A', 'B', 'C', 'A', 'B', 'B']
- >>> For x in l:
- Print x,
- A B c
2) index and partition, assign values (assign values to a single element, assign values to a partition)
Python code
- >>> L = ['abc', 'def ', 'ghi', 123]
- >>> L [2]
- 'Ghi'
- >>> L [-3]
- 'Def'
- >>> L [: 3]
- ['Abc', 'def ', 'ghi']
- >>> L [1] = 'hahaha'
- >>> L
- ['Abc', 'hahaha', 'ghi', 123]
- >>> L [1:] = ['apple', 'Banana ']
- >>> L
- ['Abc', 'apple', 'bana']
- >>> L [2] = [123,345,456]
- >>> L
- ['Abc', 'apple ', [123,345,456]
- >>> L [1:] = [1, 123,234,345,456,567]
- >>> L
- ['AB', 123,234,345,456,567]
3) add, sort, and delete operations
Python code
- >>> L = ['abc', 'def ', 'ghi', 123]
- >>> L. append (456)
- >>> L
- ['Abc', 'def ', 'ghi', 123,456]
- >>> L. sort ()
- >>> L
- [123,456, 'abc', 'def', 'ghi']
- >>> Del l [0]
- >>> L
- [456, 'abc', 'def', 'ghi']
- >>> Del l [2:]
- >>> L
- [456, 'abc']
4) some interesting usage (from the Forum id-coffee dancer)
Remove the spaces at the beginning and end of each element in the list:
Fixed python code () Elements
Python Data Type
CS is a reference.
The difference between reference and pointer should be clear: the former is the memory alias, and the latter is the storage variable of the memory address.
CS is a reference. To return the C language, you must use pointer variables instead of pointer variables.
The pointer should accept id (CS ).
* The pointer accepts cs.
Appendix: actual operation of the ID function
>>> Cd
'Abcdef'
>>> Id (cd)
35255712
Beginner in python Data Type
This has nothing to do with the xlrd module. The xlrd module should not have the get_Excel_value_S function, which should be defined by you and the returned data type is float, if you do not want to use data conversion, there are two solutions.
1. Write a function that returns an integer.
2. Confirm the table corresponding to this array and obtain this value through the cell in xlrd, such as xxx = table. cell (0, 0 ). value. Two zeros indicate the number of rows and the number of columns respectively.