Python sequence operations

Source: Internet
Author: User

Our main discussion is the list and tuple of 2 sequences.

(1) Index

0 points to the first character, 1 points to the last character.

Example:

months = [     ' January ',     ' February ',      ' March ',     ' April ',     ' may ',     ' June ',      ' July ',     ' August ',     ' September ',      ' October ',     ' November ',     ' December ',]ending  = [' st ', ' nd ', ' rd '] + 17 * [' th '] \    + [' st ', ' nd ', ' rd '] + 7 * [' th '] \    + [' st ']year = raw_input ("Years:") Month = raw_input ("Month (1-12):") Day = raw_input ("Day (1-31):") month_number  = int (month) day_number = int (day) month_name = months[month_number - 1] ordinal = day + ending[day_number - 1]print month_name +  '   '  + ordinal +&nbSP; ', '  + year 


(2) sharding

A range of elements can be accessed through a shard operation, and The Shard is implemented by an index separated by 2 colons. The left side must be greater than the right and does not contain the right. You can specify a step by the third value.

Example:

Print "url example ' http://www.baidu.com '" url = raw_input (' Please enter the URL: ') domain = url[11:-4]print "domain name:" + Domain


(3) Sequence addition

>>>[1,2,3] + [4,5,6]

>>> ' Hello ' + ' world '

Note that lists and characters cannot be added.


(4) multiplication

>>> ' python ' * 5


(5) Membership

Use the in operator to detect whether a member is in a sequence.

Example:

Database = [[' Albert ', ' 1234 '], [' Dilbert ', ' 4242 '], [' Smith ', ' 7524 '], [' Jones ', ' 9843 '], [' Buranle ', ' 8888 '] Username = raw_input ("username:") pin = raw_input ("PIN Code:") if [Username,pin] in Database:print "Access granted"

(6) Length, Min and Max values

The built-in functions Len, Min, and Max.

This article from "Otherwise" blog, please be sure to keep this source http://buranle.blog.51cto.com/8870751/1641693

Python sequence operations

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.