Detailed explanation of the sequence of basic python tutorials and basic python tutorials

Source: Internet
Author: User
Tags python list

Detailed explanation of the sequence of basic python tutorials and basic python tutorials

Sequence

Sequence is a set of ordered elements.

(Strictly speaking, it is a collection of objects, but we have not introduced the concept of "object", so we will talk about elements for the moment)

A sequence can contain one or more elements, or contain no elements.

The basic data types we mentioned earlier can all be used as sequence elements. The element can also be another sequence and other objects we will introduce later.

There are two sequences: tuple (value table; also translated as tuples) and list (table)
Copy codeThe Code is as follows:
>>> S1 = (2, 1.3, 'love', 5.6, 9, 12, False) # s1 is a tuple

>>> S2 = [True, 5, 'smile '] # s2 is a list

>>> Print s1, type (s1)

>>> Print s2, type (s2)

The main difference between tuple and list is that, once created, each element of tuple cannot be changed, and each element of list can be changed.

Elements of a sequence as another sequence
Copy codeThe Code is as follows:
>>> S3 = [1, [3, 4, 5]

Empty Sequence
Copy codeThe Code is as follows:
>>> S4 = []

 
Element reference

The subscript of the sequence element starts from 0:
Copy codeThe Code is as follows:
>>> Print s1 [0]

>>> Print s2 [2]

>>> Print s3 [1] [2]

Since the list element can be changed, you can assign a value to an element in the list:
Copy codeThe Code is as follows:
>>> S2 [1] = 3.0

>>> Print s2

If you perform this operation on tuple, an error is returned.

Therefore, we can see that the reference of the sequence is implemented through s [<int>], and int Is the subscript.

Other reference methods

Range reference: basic style [lower limit: Upper Limit: Step Size]
Copy codeThe Code is as follows:
>>> Print s1 [: 5] # From start to subscript 4 (Elements of subscript 5 are not included)

>>> Print s1 [2:] # From subscript 2 to the end

>>> Print s1 [] # From subscript 0 to subscript 4 (subscript 5 is not included), take an element every 2 (subscript is 0, 2, 4)

>>> Print s1 [2: 0:-1] # From subscript 2 to subscript 1

As you can see from the above, if the upper limit is specified during range reference, this upper limit is not included in itself.

Tail element reference
Copy codeThe Code is as follows:
>>> Print s1 [-1] # the last element of the sequence

>>> Print s1 [-3] # The last and third elements of the sequence

Similarly, if s1 [0:-1], the last element will not be referenced (again, excluding the upper limit element itself)

String is a tuples

A string is a special element, so you can perform operations on tuples.
Copy codeThe Code is as follows:
>>> Str = 'abcdef'

>>> Print str [2: 4]

 
Summary

The tuple element is unchangeable, And the list element is variable.

Reference of a sequence s [2], s []

A string is a tuple.


The first chapter of a basic python tutorial first describes how to install the fifth and sixth articles in python.

Terminal means the terminal. Because Python is cross-platform, you can understand it in terms of terminals.

In Windows, Start Menu --- run ----- CMD press Enter. This is the Dos interface, that is, the Terminal of Windows.

"The method for running python in Terminal is to input the program name and press the enter key ."

You have compiled a Python program named demo1, so you only need to enter demo1 and press Enter,
Then the program runs.

There is already a set. I need to perform sequence operations on the elements in it. How to implement this? Python 27

Simple: Use the python list built-in method sort
For example, a = [1, 3, 2]
Then run:
A. sort ()
Print
A = [1, 2, 3]
Or use the python built-in method sorted.
Sorted can specify a comparison method. For more information, see references.
Reference: wiki.python.org/moin/HowTo/Sorting

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.