Sequence Overview
Python contains 6 built-in sequences that focus on two of the most common types: lists and tuples.
The main difference between a list and a tuple is that the list can be modified and tuples cannot. This means that if you want to add elements according to the requirements, then the list can be better, and for some reason, it is more appropriate to use tuples when the sequence cannot be modified.
The sequence works well when working with a set of values. You can use a sequence to represent a person's information in a database---11th is the name, and the 2nd element is age. Write a list based on the above content.
>>> hu=[' Huhu ', 25]
Also, a sequence can contain other sequences, so a list of information for building the following people is also available, and the list is your database:
>>> huhu = [' Huhu ',25]>>> Chongshi = [' Chongshi ',32]>>> database = [huhu,chongshi]>> > database[[' huhu ', [' Chongshi ', 32]]
General sequence Operations
All sequence types can perform certain operations. These actions include: index (Indexing), Shard (sliceing), add (adding), multiply (multiplying), and check whether an element belongs to a member of a sequence (membership).
Index
All elements in the sequence are numbered-----increment from 0. These elements can be accessed by number separately, as follows:
>>> greeting = ' Chongshi ' >>> greeting[0] ' C ' >>> greeting[2] ' o '
When using a negative index, Python starts counting from the right, that is, the last 1 elements. The position number of the last 1 elements is-1 (not-0, because that will coincide with the 1th element):
>>> greeting = ' Chongshi ' >>> greeting[-1] ' i ' >>> greeting[-2] ' h '
Of course, we can also use the cited in another way, the effect of the two methods is the same:
>>> ' Chongshi ' [0] ' C ' >>> ' Chongshi ' [-1] ' I '
If a function call returns a sequence, it is possible to index the returned result directly. For example, if you are only interested in the 4th number of years that a user enters, you can do the following:
>>> fourth = Raw_input (' year: ') [3]year:2013>>> fourth ' 3 '
Tip: Try tapping the code, which will help you understand. Don't bother.
Example index:
#根据给定的年月日以数字形式打印出日期months = [ ' January ', ' February ', ' March ', ' April ', ' may ', ' June ', ' July ', ' August ', ' September ', ' October ', ' November ', ' December ' ]# With a number of 1-31 as the end of the list endings = [' 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) #记得要将月份和天数减1 to get the correct index month_name = months[month_number-1]ordinal = day + endings[day_number-1]print month_ Name + ', ' + ordinal + ', ' + year-----------------------------------------------input:>>> year:2013month (1-12): 4d Ay (1-31) 14 output: April, 14th, 2013
Sharding
Similar to using an index to access a single element, you can use a shard operation to visit an element within a Cong range. Shards are implemented by two indexes separated by a colon:
>>> tag = ' <a href= ' http://www.python.org ' >python Web site</a> ' >>> tag[9:30] # Take 9th to 30th characters between ' http://www.python.org ' >>> tag[32:-4]
If you ask for 10 numbers, the last three numbers:
>>> numbers = [0,1,2,3,4,5,6,7,8,9]>>> numbers[7:-1] # from 7th Count to the first count [7, 8] #显然这样不可行 >> > Numbers[7:10] #从第7个数到第10个数 [7, 8, 9] #这样可行, index 10 points to the 11th element. >>> numbers[7:] # can be empty last index resolved [7, 8, 9]>>> Numbers[:3] [0, 1, 2]>>> numbers[:][0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Shard Example:
# http://fnng.cnblogs.com URL in the form of url = raw_input (' Please enter the URL: ') domain = url[11:-4]print "domain name:" + Domain------------------------------------------
Enter:>>> Please enter the url:http://fnng.cngblogs.com output: Domain name:.cngblogs
Larger step size:
>>> numbers = [0,1,2,3,4,5,6,7,8,9]>>> numbers[0:10:1] #求0到10之间的数, step is 1[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> Numbers[0:10:2] #步长为2 [0, 2, 4, 6, 8]>>> Numbers[0:10:3] #步长为3 [0, 3, 6, 9]
sequence addition
You can sequence a connection operation by using the plus sign:
>>> ' Hello. ' + ' world! ' hello.world! ' >>> [+] + ' world! ' Traceback (most recent): File "<pyshell#20>", line 1, in <module> [ ] + ' world! ' Typeerror:can only concatenate list (not "str") to List
As with the error message, lists and strings cannot be joined together, although they are sequences. In simple terms, two sequences of the same type are required for the connection operation.
Multiplication
>>> ' python ' * 5 ' python python python python python ' >>> [25] * 10[25, 25, 25, 25, 25, 25, 25, 25, 25, 25]
If you want to create a list that takes up 10 elements, but does not include any useful content, you can use the Nome
>>> sequence = [None] * 10>>> sequence[none, none, none, none, none, none, none, none, none, none]
Example sequence (string) multiplication:
# Print a sentence with the correct width in the Center box # Note that the integer division operator (//) can only be used in Python 2.2 and later versions, in previous versions, only with normal division (/) sentence = Raw_input ("sentence:") Screen_width = 80text_width = Len (sentence) Box_width = text_width + 6left_margin = (screen_width-box_width)//2printprin T ' * left_margin + ' + ' + '-' * (box_width-2) + ' + ' print ' * left_margin + ' | ' + ' * text_width + ' | ' print ' * left_margin + ' | ' + sentence + ' | ' print ' * left_margin + ' | ' + ' * text_width + ' | ' print ' * left_margin + ' + ' + '-' * (box_width-2) + ' + '
---------------------------------------------------------
Input:>>> sentence:haha! This is my box output: +------------------------+ | | | haha! This is my box | | | +------------------------+
Member qualifications
In order to check whether a value is in a sequence, you can use the in operator. This operator is a little different from what has been discussed previously (for example, the +, * operator). This operator checks that a condition is true or False (True or false).
An example of the in operator:
>>> permissions = ' rw ' >>> ' W ' in permissionstrue>>> ' y ' in permissionsfalse>>> users = [' Zhangsan ', ' Lisi ', ' Wangwu ']>>> raw_input (' Enter your user name: ') in Usersenter your user name:lisitrue> >> subject = ' $$$ Get rich now!!! $$$ ' >>> ' $$$ ' in Subjecttrue
length, minimum and maximum values
The built-in functions Len, Min, and Max are useful. The Len function returns the number of elements contained in the sequence. The Min function and the Max function return the largest and smallest elements of the sequence, respectively.
>>> numbers = [100,34,678]>>> len (numbers) 3>>> max (numbers) 678>>> min (numbers) 34 >>> Max (2,3) 3>>> min (9,3,2,5) 2
Python Basic Learning Notes (iii)