[Python Learning]--sequence (1)--a preliminary understanding of the sequence in Python

Source: Internet
Author: User

The sequence we're talking about in Python includes :
1. String (a sequence consisting of multiple characters)
2. List
3. Tuples

# What are the similarities and differences between lists and tuples? list=[1,2,3]print  type (list) tuple= (all in a)print  type (tuple)  #< the same points >    lists and tuples are containers that can contain any type of element (even a sequence)   list and elements also contain the order of the Elements #  < The difference >     list is mutable, and tuples are immutable,    so usually, when you have a queue of the same type with indeterminate length, use a    tuple with a list of the number of known elements 

#< Assignment of strings >string='sequence'#Assignment of < list >alist=[1,2,3,4]#assignment for < tuples >Seq1= ('Hello',' World', 9,'a') SEQ2=('Hi','123')

The operators for all sequences are (high-to-low priority):
1. Member relationship operator (in, not in)

Print '  World ' inch seq1true Print ' Hello '  not inch Seq1false

2. Connection operator (+)

print seq1+seq2 ('hello'World '  a'hi'123')

3. Repeat operator (*)

PrintAlist[1, 2, 3, 4, 1, 2, 3, 4]PrintSeq1('Hello',' World', 9,'a','Hello',' World', 9,'a','Hello',' World', 9,'a')

4. Slice operator ([], [:], [::])

PrintstringsequencePrintSTRING[0]#Subscript starting from 0sPrintString[1:3]#[n:m] denotes n=<index<m rangeeqPrintSTRING[1::2]#[N:m:x] represents the N=<index<m range and the number of steps is xEunePrintSTRING[-1]#-1 indicates the last element, and so onCPrintSTRING[::-1]#so you can use 1 as the step number to achieve reverse outputEcneuqesPrintalist[1, 2, 3, 4]PrintAlist[0]1PrintAlist[2:] [3, 4]PrintAlist[:3][1, 2, 3]PrintAlist[1:3][2, 3]Printseq1 ('Hello',' World', 9,'a')PrintSeq1[1:3](' World', 9)Print(' This',' is','python') [2]python#an index that is out of bounds causes an exceptionPrintAlist[4]indexerror:list Index out of range

[Python Learning]--sequence (1)--a preliminary understanding of the sequence in Python

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.