The sequence of basic Python tutorials notes general operations

Source: Internet
Author: User

    • Index

All elements in the sequence are numbered-incrementing from 0. When using a negative index, Python starts counting from the right, that is, the last element, and the last element's position number is-1. In addition, the string is a sequence of characters, and the string literal can be used directly in the index. If a function call returns a sequence, it is possible to index the returned result directly. Such as

#Prints the date in digital form on a given datemonths = ['January','February','March',' May','June','August','September',          'October','November','December']#list with a number of 1-31 as the endendings = ['St','nd','Rd'] + 17*['th'] + ['St','nd','Rd'] + 7*['th'] + ['St']year= Raw_input ('Year :') Month= Raw_input ('month (1-12):') Day= Raw_input ('Day (1-31):') Month_number=Int (month) Day_number=Int (day)#remember to subtract months and days by 1 to get the correct indexMonth_number = Months[month_number-1]ordinal= Day + endings[day_number-1]PrintMonth_number +' '+ ordinal +', '+ year

Results

year:1985
Month (1-12): 2
Day (1-31): 5
February 5th, 1985

    • Sharding

A shard operation can access a range of elements, which are implemented by two indexes separated by a colon, the first index is the number of the first element that needs to be extracted, and the final index is the number of the first element remaining after the Shard, such as

>>> numbers = [1,2,3,4,5,6,7,8,9]
>>> Numbers[3:6]
[4, 5, 6]

If the portion of the Shard includes the element at the end of the sequence, simply empty the last index, and the same applies to the elements that begin the sequence, such as

>>> numbers = [1,2,3,4,5,6,7,8,9]
>>> Numbers[7:]
[8, 9]
>>> Numbers[:3]
[1, 2, 3]
In a normal shard, the step size is implicitly set to 1. For a positive step, Python extracts the element to the right from the head of the sequence until the last element, and for a negative step, extracts the element to the left from the end of the sequence until the first element, such as

>>> numbers = [1,2,3,4,5,6,7,8,9]
>>> Numbers[0:3:1]
[1, 2, 3]

>>> Numbers[3:0:-1]
[4, 3, 2]

    • Add

The plus sign can be used for sequential connection operations, and the sequence must be of the same type as

>>> [4, 3, 2]+[8, 9]
[4, 3, 2, 8, 9]
>>> ' Hello ' + ' world '
' Hello World '
>>> [8, 9] + ' world '

Traceback (most recent):
File "<pyshell#13>", line 1, in <module>
[8, 9] + ' world '
Typeerror:can only concatenate list (not "str") to List

    • Multiply

Multiplying the number x by a sequence creates a new sequence in which the original sequence is repeated x times, as

>>> 5 * ' HL '
' Hlhlhlhlhl '

[]--empty list, there's nothing there

None-no elements are placed inside

>>> seq = 10*[none]
>>> seq
[None, None, none, none, none, none, none, none, none, none]

#print a sentence in the Center "box" with the correct widthSentence = Raw_input ("sentence:") Screen_width= 80Text_width=Len (sentence) Box_width= Text_width + 6Left_margin= (screen_width-box_width)//2PrintPrint ' '* Left_margin +'+'+'-'* (box_width-4) +'+'Print ' '* Left_margin +'| '+' '* Text_width +' |'Print ' '* Left_margin +'| '+ sentence +' |'Print ' '* Left_margin +'| '+' '* Text_width +' |'Print ' '* Left_margin +'+'+'-'* (box_width-4) +'+'Print

Results

Sentence:he ' s a very naughty boy!

+--------------------------+
| |
| He ' s a very naughty boy! |
| |
+--------------------------+

    • Member qualifications

Use the In operator to check whether a value is in the sequence, that there is a return of true, that there is no return false, such as

>>> numbers = [1,2,3,4,5,6,7,8,9]
>>> 8 in numbers
True
>>> in Numbers
False

    • Length, minimum and maximum values

Len-Returns the number of elements contained in the sequence

Min--Returns the smallest element in the sequence

Max--Returns the largest element in a sequence

Such as

>>> numbers = [1,2,3,4,5,6,7,8,9]
>>> Len (Numbers)
9
>>> min (Numbers)
1
>>> Max (Numbers)
9

The sequence of basic Python tutorials notes general operations

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.