Python list (list) and meta-Group (tuple) Learning notes

Source: Internet
Author: User
Tags python list

Sequence

Index: Index is the key value of the PHP array. Starting from 0.
Fragmentation: PHP's slice, if number[3:9] Note that the intercepted value starts at the 4th index.
Step size: The stepping length of each operation of the fragment. Can not be 0, to be a negative number, that is, from the back forward.

Sequence addition: Connecting operations using the + number for sequence
Two sequences of the same type can be added.
Multiplying a sequence with a number x will generate a new sequence, and the original sequence will be repeated x times.

None is a python built-in value, and its exact meaning is "nothing" and some language is null.
Membership: Use in to check whether a string exists in a list or string.
Len can check the length of a list or string. PHP uses Str_len and count to check.

List function, which creates lists based on strings. such as list (' Hello ')
Note: You cannot assign values to elements that do not exist in a list location

The Append method is used to append a new object at the end of the list.
The Count method counts the number of times an element appears in the list, such as X.count (1)
The Extend method can append multiple values from another sequence at the end of the list.
The index method finds the indexed position of the first occurrence of a value from the list.
The Insert method is used to insert an object into the list.
The Pop method removes the data from the list, and the default is the last one.
The Remove method removes the first occurrence of a value in the list. Pop is Operation Key,remove is operation value
Reverse the elements in the list backwards.
The sort method sorts the elements in the list.
The sort method does not return a list after it is sorted. So to get a sorted list, you first assign x to Y, and then you sort the Y.
The sort advanced sort provides two parameters, the first of which is that you must specify a function to use in the sort process, and the second is the bool value in reverse order

META Group

Tuples, like lists, are also a sequence. The only difference is that tuples cannot be modified.
Tuples are most often enclosed in parentheses. For example (1,2,3), the sequence is enclosed by square brackets.
If you want to implement a tuple that includes only one value, you must also add a comma, such as (23,)
Tuple function: Takes a sequence as a parameter and converts it to a tuple.
Whether a tuple is fragmented or a tuple. Just like a list fragment or a list.

Cases

List

  code is as follows copy code

>> > 3>5 and 3<8
False
>>> not 3>5
True
>>> 3>=3
True
>>> 3 >=3 or 3<1
True
>>> pystr= "python"
>>> iscool= "is cool!"
>>> pystr+iscool
' Pythonis cool! '
>>> Iscool[1:3]
' s '
>>> iscool*2
' is Cool!is cool! '
>>> '% ' *9
'%%%%%%%%% '
>>> alist=[1,5,9,4]
>>> alist
[1, 5, 9, 4]
>& Gt;> alist[3:]
[4]
>>> Alist[1:3]
[5, 9]
>>> alist[1]
5

Tuples (read-only cannot write):

The code is as follows Copy Code

>>> atuple= (' Safdad ', 23141.3214243, ' Xxff ')
>>> Atuple
(' Safdad ', 23141.3214243, ' Xxff ')
>>> Atuple[2:4]
(' Xxff ',)
>>> Atuple[1:3]
(23141.3214243, ' Xxff ')
>>> atuple[2]=99
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: ' Tuple ' object does not support item assignment
>>>

Related Article

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.