20190804-Python basis of Chapter II lists and tuples

Container, Python support the basic concept of a data structure (container may basically comprise to another object.)

The two main containers are: the sequence (such as lists and tuples) and maps (e.g., dictionary)

Ps: the difference between the list and tuples, lists can be modified, tuples can not.

The general sequence of operations of: indexing, slicing, addition, multiplication, membership check. Other built-in functions to determine the length of the sequence, sequence and to identify the maximum and minimum elements.

1. Index

1  # will specify the year, month date, print date 
2 months = [
 . 3      ' January ' ,
 . 4      ' February ' ,
 . 5      ' March ' ,
 . 6      ' April ' ,
 . 7      ' On May ' ,
 . 8      ' June ' ,
 . 9      ' july ' ,
 10      ' August ' ,
 . 11      ' September ' ,
 12 is      'October' ,
 13 is      ' November ' ,
 14      ' December ' 
15  ]
 16  
. 17  # a list containing 1-31 corresponding to the end 
18 is Endings = [ ' ST ' , ' Nd ' , ' RD ' ] +. 17 * [ ' TH ' ] \
 . 19          + [ ' ST ' , ' Nd ' , ' RD ' ] +. 7 * [ ' TH ']\
20         + [ ' ST ' ]
 21 is  
22 is year = INPUT ( ' Year: ' )
 23 is month The INPUT = ( ' Month (1-12): ' )
 24 Day = INPUT ( ' Day (1-21): ' )
 25  
26 is MONTH_NUMBER = int (month the)
 27 DAY_NUMBER = int (day)
 28  
29  # Do not forget the number of months and days minus 1, so as to get the correct index 
30 MONTH_NAME = months [MONTH_NUMBER-1 ]
 31 is ORDINAL = day + Endings [ . 1-DAY_NUMBER ]
 32  
33 is  Print(MONTH_NAME + '  ' + ORDINAL + ' , ' + year)
 34 is  
35  # test endings list prints content 
36  Print ([ ' ST ' , ' Nd ' , ' RD ' ] +. 17 * [ ' TH ' ] \
 37 [          + [ ' ST ' , ' Nd ' , ' RD ' ] +. 7 * [ ' TH ']\
38         + ['st'])

2. Slice

1  # extracts the domain name from the url https://www.google.com 
2 url = INPUT ( ' INPUT your url: ' )
 . 3 Domain = url [12 is: -4 ]
 . 4  Print ( ' the Domain name: ' + Domain)
 . 5  results:
 6 the iNPUT your url: HTTPS: // www.google.com 
 7  Domain name:. Google
 8 due to input directly enter url will jump to open the link, so the space enter output, but introduces an extra space, the end result leads to one more " . " 

number [3: 6: 1], default step 1, may not be written.

The first index included, the second index is not included.

When the step is positive, moves from the start point to the end point; is negative, from the starting point to the end point, and a first index must be greater than the second index.

3 sequences are added

 1 print([1,2,3] + [7,8,9,10])
 2 结果:
 3 [1, 2, 3, 7, 8, 9, 10]
 4 
 5 print([1,2,3,4] + 'qwrrt')
 6 结果:
 7 Traceback (most recent call last):
 8   File "D:/Python/PycharmProjects/untitled1/venv/Robots_learning.py", line 818, in <module>
 9     print([1,2,3,4] + 'qwrrt')
10 TypeError: can only concatenate list (not "str") to list

Sequence using splice adder, however, generally can not be spliced ​​sequences of unlike type.

4. Multiplication

The sequence number when multiplied by x, x sequence will be repeated this time to create a new sequence:

. 1  Print ( ' the Python ' *. 5 )
 2  Results:
 . 3 the Python the Python the Python the Python the Python

None not mean anything in Python; initialization.

1  # print a sentence in the center of the screen and the width of the right block 
2 sentence = INPUT ( ' Plz Enter your sentence: ' )
 . 3  
. 4 SCREEN_WIDTH = 80
 . 5 text_width = len (sentence)
 . 6 box_width = + 12 is text_width
 . 7 left_margin = ( SCREEN_WIDTH - box_width) // 2
 . 8  
. 9  Print ()
 10  Print ( '  ' * left_margin + ' + ' + ' - ' * (box_width-2) + ' + ' )
 . 11  Print (' '* left_margin + '|' + ' '*(box_width-2) + '|')
12 print(' '* left_margin + '|' + ' '*(((box_width - text_width)//2)-1) + sentence +' '*(((box_width - text_width)//2)-1)+'|')
13 print(' '* left_margin + '|' + ' '*(box_width-2) + '|')
14 print(' '* left_margin + '+' + '-'*(box_width-2) + '+')
15 print()
16 
17 结果:
18 Plz enter your sentence:You'er beautiful!
19 
20                          +---------------------------+
21                          |                           |
22                          |     You'er beautiful!     |
23                          |                           |
24                          +---------------------------+

5. Membership detection

Checking whether a value is contained in the sequence, using the operator in, returns the result True, False, Boolean operations.

1  # Check username and PIN code 
2 Database = [
 . 3      [ ' Albert ' , ' 12345 ' ],
 . 4      [ ' Dilbert ' , ' 32564 ' ],
 . 5      [ ' Smith ' , ' 43 456 ' ]
 . 6  ]
 . 7  
. 8 username = INPUT ( ' the User the Name: ' )
 . 9 PIN INPUT = ( ' the PIN code: ')
10 
11 # 检查用户名和PIN码
12 if [username, pin] in database:
13     print('Access granted!')
14 else:
15     print('You don\'t have access to the database!')
16 
17 结果:
18 User Name:Elon
19 PIN code:1232345
20 You don't have access to the database!

 

Guess you like

Origin www.cnblogs.com/ElonJiang/p/11299293.html