First article on the path to Python Development (2)-First knowledge list and metadata, the path to python Development

Source: Internet
Author: User
Tags element groups

First article on the path to Python Development (2)-First knowledge list and metadata, the path to python Development

The list and element groups can be treated as normal "arrays", which can store any number of Python objects of any type. Like arrays, the list and element can be accessed through a numerical 0 index, lists and metadata groups can store different types of objects. There are several important differences between lists and metadata. List elements are included in ([]). The number and value of elements can be changed, while tuples are included in ({}) and cannot be changed. Tuples can be viewed as read-only lists.

I. Initial Knowledge list

1. The following is a normal list

>>> Lis = ['xiaoyuan ', 25]

In a sequence, it can contain other sequences, that is, the list can contain list tuples.

>>> Lis = ['xiaoyuan ', 25]
>>> Lis_jo = ['john', 30]
>>> Date = [lis, lis_jo]
>>> Date
[['Xiaoyuan', 25], ['john', 30]

Note: there is also a data structure named container in Python. A container is basically any object that contains other objects. Sequence type (such as list, tuples) and ing type (such as dictionary ). Each element in the sequence has its own number, and each element mapped has a name (also called a key ). It will be introduced later. It is not a sequence or a ing container type, such as a set, post-order introduction.

2. General sequence operations

All sequence types can perform certain operations, including index, sliceing, adding, and multiplying) and check whether each element belongs to the sequence member (to determine whether the element exists in the sequence ). The length of the computing sequence is also used to find the built-in functions of the maximum and minimum elements. In the future, there will be an important operation to explain and iterate ). The meaning of iteration to the sequence.

 

(1) All the elements in the index sequence are numbered. This number increases from 0:

>>> Lis = ['xiaoyuan ', 25]
>>> Lis_jo = ['john', 30]
>>> Date = [lis, lis_jo]
>>> Date
[['Xiaoyuan', 25], ['john', 30]
>>> Sta = 'hellow'
>>> Sta [0]
'H'
>>> Sta [-1]
'W'
>>> Date [0]
['Xiaoyuan', 25]

-1 indicates the last element.

 

(2) fragment

① We can use parts to operate on elements in a range.

>>> Tist = [1, 2, 3, 4, 5, 6]
>>> New = "abcdefg"
>>> Tist [1: 3]
[2, 3]
>>> New [0: 4]
'Abcd'

>>> Tist [2:] # From the second element to the end
[3, 4, 5, 6]

② What if we want to retrieve elements from the back? I believe everyone knows that it is to use '-'

>>> Tist = [1, 2, 3, 4, 5, 6]
>>>
>>> Tist [-3:-1]
[4, 5]
>>> Tist [-4:] # here is the last and last element.
[3, 4, 5, 6]

Exercise 1

We know that the domain name is www.xxxx.cn, which is divided into three parts. cn is China, xxxx indicates domain name, www indicates World Wide Web. What should we do if we want to retrieve the domain name?

1 >>> url = 'www .aaa.cn '2 >>> name = url [4: -3] 3 >>> name4 5 'aaa' 6 7 >>> print 'donaim name: <'+ name +'> '+ 'Type: '+ url [-2:] 8 donaim name: <aaa> type: cn
Exercise 1

③ Step size, that is, the element is obtained according to the length of step size. When step size is 1, the element is output.

>>> Tist = [1, 2, 4, 5, 6, 7, 8, 9, 10]

>>> Tist [0: 10: 1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> Tist []
[1, 3, 5, 7, 9]

If we want to extract one of the three elements from the beginning to the end, we only need to do this.

>>> Tist [: 3]
[1, 4, 7, 10]

 

(4) append element list. append

>>> Lis = [1, 2, 4, 5, 6]
>>> Lis. append (7)
>>> Lis
[1, 2, 3, 4, 5, 6, 7]

 

(5) delete the list element del list []

>>> Lis
[1, 2, 3, 4, 5, 6, 7]
>>> Del lis [0]
>>> Lis
[2, 3, 4, 5, 6, 7]

 

(6) List length: len ()

>>> Len (lis)
6

 

(7) Check whether an element contains: "5" in lis returns True (True) if it exists; otherwise, False (False)

>>> 5 in lis
True
>>> 1 in lis
False

 

(8) convert the list to a string and define the connection method "_". join (lis)

>>> Name_lis = ["a", "B", "c"]
>>> '_'. Join (name_lis)
'A _ B _c'

 

(9) sequence Addition

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

>>> 'Hangel' + 'World! '
'Hellworld! '

 

(10) multiplication *

>>> 'Hell' * 5
'Hellhellhellhelllell'
>>> [1] * 5
[1, 1, 1, 1, 1]

 

(11) The list function. Because the string cannot be modified as the list, you need to use the list function to convert the string into a list. (The list function applies to all types of sequences)

>>> Tup = 'asdfghj'

>>> List (tup)
['A', 's', 'D', 'F', 'G', 'h', 'J']

 

(12) element assignment

>>> Tup = list (tup)
>>> Tup
['A', 's', 'D', 'F', 'G', 'h', 'J']
>>> Tup [0] = 'A'
>>> Tup
['A', 's', 'D', 'F', 'G', 'h', 'J']

Exercise 2:

You need to write a welcome message, which must be formatted into a square box for output and changed based on the user input.

1 inpot = raw_input ("your name:") # Welcome Message 2 text = len (inpot) 3 scr = 17 + text # Set a length of 4 left = (scr) // 2 # Set the gap between spaces in front. 5 # The output content below can be changed according to the length of the input string. 6 print # output null. 7 print ''' * left +' -'* (scr) + '+ '8 print ''' * left +' | '+ ''' * scr +' | '9 print ''' * left + '|' + 'nice to meet you: '+ inpot +' | '10 print ''' * left + '|' + ''' * scr + '| '11 print ''' * left + '-' * (scr) +'

Exercise 2

 

Ii. First-recognized tuples

1. The following is a normal tuples.

>>> Name = ('A', 'B', 'C ')
>>> Name
('A', 'B', 'C ')

The basic operation of tuples is similar to that of the list. The only difference is that the list can be modified but cannot be modified.

When we check whether a can be normally executed in the name_tup tuples, but want to delete the tuples, an error is returned, indicating that the object cannot be deleted.

>>> Name_tup = ('A', 'B', 'C ')
>>> 'A' in name_tup
True
>>> Del name_tup [0]

Traceback (most recent call last ):
File "<pyshell #45>", line 1, in <module>
Del name_tup [0]
TypeError: 'tuple' object doesn' t support item deletion

Finally, we recommend that you use the Pycharm compiler.

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.