[Python Basics (i)] Sequence operations basics

Source: Internet
Author: User

I. Data structure of Python

Container: Any object that contains other objects.

Include: sequences (such as lists, tuples), mappings (such as dictionaries), collections, and so on. Mainly sequences and mappings.

Second, sequence operation

Index, shard, add, multiply, check whether an element is a member of the sequence. Also, calculate the length of the sequence, looking for the largest, most lower element.

1. Index

Positive number: Starting from 0, left to right;

Negative number: From 1 onwards, to left

2. sharding

Two indexes as boundaries, the first index element is contained within a shard, and the second is not.

A= ' 123456789 '

Print A[2:5]

' 345 '

3. Sequence addition condition: the same type of sequence

4. Multiplication

5. Membership in operator

6. Length Len (), Max Max (), Min min ()

Third, List

1.list function: Create a list based on a string

2. Basic list Operations

Assign value
x=[1,1,1]
x[1]=2
Print X

#删除元素
name=[' x ', ' y ', ' Z ']
Del Name[2]
Print Name

#分片赋值
Name=list (' Perl ')
Name[2:]=list (' ar ')
Print Name

Name[2:]=list (' Ython ')
Print Name

numbers=[1,5]
numbers[1:1]=[2,3,4]
Print numbers

3. List method

#1. Append: Add new objects at the end of the list
lst=[1,2,3]
Lst.append (4)
Print LST

#2. Count counts the number of occurrences of an element
a=[' to ', ' to ', ' a ', ' B ', ' to ', ' C '].count (' to ')
Print a

#3. Extend add multiple values at the end of the list, which differs from the connection
a=[1,2,3]
b=[4,5,6]
A.extend (b)
Print a

#4. Index to find the first index position of a value
K=[' A ', ' B ', ' C ', ' d ']
Print K.index (' C ')

#5. Insert inserts an object into the list
ll=[1,2,3,4,5]
Ll.insert (3, ' four ')
Print LL

#6. Pop removes an element from the list, the default is the last
x=[1,2,3,4,5]
Print X.pop ()
X.pop (1)
Print X

#7. Remove removes a match for one of the values in the list
X=[' A ', ' B ', ' C ', ' B ', ' E ']
X.remove (' B ')
Print x #[' A ', ' C ', ' B ', ' E ']

#8. Reverse to store element orientation in the list
X=[' A ', ' B ', ' C ', ' B ', ' E ']
X.reverse ()
Print x #[' e ', ' B ', ' C ', ' B ', ' a ']

#9sort sort
x=[4,2,6,7,3,9,4,3]
X.sort ()
Print x #[2, 3, 3, 4, 4, 6, 7, 9]

#10 CMP
Print CMP (12,10) #1
Print CMP (10,12) #0
Print CMP (10,12) #-1

Four. Tuples: Immutable sequences

1. Tuple creation: Surrounded by parentheses

(1, 2, 3)

2. The tuple function converts a sequence to a tuple
K=tuple (' abc ')
Print k # (' A ', ' B ', ' C ')

[Python Basics (i)] Sequence operations basics

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.