Python data type, python

Source: Internet
Author: User

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
  1. >>> (0, 'hahaha', (4j, 'y '))
  2. (0, 'hahaha', (4j, 'y '))
  3. >>> T = (1, 3, 'B ')
  4. >>> T [2]
  5. 'B'
  6. >>> T [3]
  7. Traceback (most recent call last ):
  8. File "#41>", line 1, in <module> </module>
  9. T [3]
  10. IndexError: tuple index out of range
  11. >>> T [-1]
  12. 'B'
  13. >>> T [0:-1]
  14. (1, 3)
  15. >>> T * 2
  16. (1, 3, 'B', 1, 3, 'B ')
  17. >>> For x in t:
  18. Print x,
  19. 1 3 B
  20. >>> 'B' in t
  21. True
  22. >>> Q = t + (3, 'abc '))
  23. >>> Q
  24. (1, 3, 'B', 3, 'abc ')
  25. >>> For x in (2, (3, 'A ')):
  26. Print x
  27. 2
  28. (3, 'A ')
  29. >>> Len (q)
  30. 5
  31. >>> Len (2, (3, 'abc ')))
  32. 2
  33. >>> (1, 2, 3) [1]
  34. 2
  35. >>> Q [1] = 'D'
  36. Traceback (most recent call last ):
  37. File "#57>", line 1, in <module> </module>
  38. Q [1] = 'D'
  39. TypeError: 'tuple' object does not support item assignment
  40. >>> A = ('B', 'C', q)
  41. >>> 1 in
  42. False
  43. >>> Q in
  44. True
  45. >>>
  46. ('B', 'C', (1, 3, 'B', 3, 'abc '))
  47. >>> Q = 'D'
  48. >>>
  49. ('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
  1. >>> L = ['A', 'B', 'C']
  2. >>> Len (l)
  3. 3
  4. >>> L + ['D']
  5. ['A', 'B', 'C', 'D']
  6. >>> L * 2
  7. ['A', 'B', 'C', 'A', 'B', 'B']
  8. >>> For x in l:
  9. Print x,
  10. A B c

2) index and partition, assign values (assign values to a single element, assign values to a partition)

Python code
  1. >>> L = ['abc', 'def ', 'ghi', 123]
  2. >>> L [2]
  3. 'Ghi'
  4. >>> L [-3]
  5. 'Def'
  6. >>> L [: 3]
  7. ['Abc', 'def ', 'ghi']
  8. >>> L [1] = 'hahaha'
  9. >>> L
  10. ['Abc', 'hahaha', 'ghi', 123]
  11. >>> L [1:] = ['apple', 'Banana ']
  12. >>> L
  13. ['Abc', 'apple', 'bana']
  14. >>> L [2] = [123,345,456]
  15. >>> L
  16. ['Abc', 'apple ', [123,345,456]
  17. >>> L [1:] = [1, 123,234,345,456,567]
  18. >>> L
  19. ['AB', 123,234,345,456,567]

 

3) add, sort, and delete operations

Python code
  1. >>> L = ['abc', 'def ', 'ghi', 123]
  2. >>> L. append (456)
  3. >>> L
  4. ['Abc', 'def ', 'ghi', 123,456]
  5. >>> L. sort ()
  6. >>> L
  7. [123,456, 'abc', 'def', 'ghi']
  8. >>> Del l [0]
  9. >>> L
  10. [456, 'abc', 'def', 'ghi']
  11. >>> Del l [2:]
  12. >>> L
  13. [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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.