Into the world of Python (v) data type 3. Sequence Type-Ganso (tuple)

Source: Internet
Author: User

What is a sequence
    • Lists, tuples, and strings are sequences.

    • The two main features of a sequence are index operators and slice operators

      • The index operator allows us to take a value from the sequence

      • The slice operator allows us to get a slice of the sequence, which is part of the sequence

    • The index can also be negative, and the position is calculated from the end of the sequence.

      • Therefore, Shoplist[-1] represents the last element of the sequence and Shoplist[-2] The penultimate item of the crawl

    • The slice operator is a sequence name followed by a square bracket, with a pair of optional digits in the square brackets, separated by a colon

      • Note that the colon is mandatory and the number is optional

      • The first number in the slice operator represents the start position, and the second number indicates where the slice ends.

      • If you do not specify the first number, Python starts with the number of the first position

      • If you do not specify a second number, Python stops at the end of the sequence

      • Returns the sequence from the start position to the end of the slice, that is, the end position is excluded from the slice

Basic operation of the sequence
Len () To find the sequence length Str= "ABCDE"
5, Len (str)

+ Connect 2 sequences str1= "ABCDE"
Str2= "Fdeg"
STR1+STR2, "Abcdefdeg"

* Repeating sequence elements str1= "ABCDE"
Str1*5, "ABCDEABCDEABCDEABCDEABCDE"

Inch Determines whether the element is in the sequence str1= "ABCDE"
' C ' in str1-True

Max () Returns the maximum value str1= "12345"
Max (STR1)->5

Min () Returns the maximum value str1= "12345"
Min (str1)->1

CMP (TULP1,TULP2) Determine if 2 sequences are the same str1= "ABCDE"
Str2= "Fdeg"
CMP (STR1,STR2)
Compare sequence length and number of elements and element content
For x in (1, 2, 3): Print x, 1 2 3 Iteration
Meta-group

Tuples and lists are very similar, except that tuples and strings are immutable, that is, tuples cannot be modified

Tuples are delimited by parentheses, comma-separated items

Tuples are typically used to enable statements or user-defined functions to safely take a set of values

Tuples can store several types of values, strings, numbers, lists, tuples

userinfo= ("Milo", "Male") user= ("1", userinfo)
Indexes and slices for tuples

The tuple's index and slice operations are similar to strings in that the tuple is stored in units as cells, and strings are in alphabetical units

Such as:

userinfo= ("Milo", "male") userinfo[1], 30userinfo[1:3], ("Male")
Creation of tuples
Empty tuple: empty= (); single element tuple: single= (1,) general form: userinfo= ("Milo", "male")
No enclosing delimiter

Any set of multiple objects, separated by commas, do not write the recognition symbol, the list in parentheses, the tuple in parentheses, etc., by default is a tuple, in the following short example illustrates:

#!/usr/bin/pythonprint ' abc ', -4.24E93, 18+6.6j, ' xyz '; x, y = 1, 2;print "Value of X, y:", x, y;

When the above code is executed, the following results are produced:

abc-4.24e+93 (18+6.6j) Xyzvalue of X, Y:1 2
List Conversion Narimoto Group

Tuple (SEQ) list converted to tuples

Into the world of Python (v) data type 3. Sequence Type-Ganso (tuple)

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.