[Diary the Python -2] List - tuples - -if-for dictionaries

Today, learning the lists, tuples, dictionaries knowledge, really trivial. I should learn it several times, and just started a passionate, learned a method could not wait to try, now at peace. Well, summarize.

1. List

Python in square brackets ([]) to indicate the list by commas to separate its elements. Name [index] to access a list of elements of the list.

Index may be negative, as the index specified as -1, allowing Python returns the last element of a list. In case you can not clear the length of the list, access the last element.

 

1.1 add an element to the list:

. 1 Lis is = []
 2  
. 3 Lis.title ()     # so that the list of capital first letter of each element 
. 4  
. 5 Lis.append ( ' Apple ' )     # Add the 'apple' element to the end of the list 
. 6  
. 7 Lis.insert (0 , ' Apple ' )     # index list Lis 0 position, the additive element 'apple'

 

Among these, often have to wait until after the program is running, the user wants to know what data is stored in the program. To control the user may first create an empty list, values ​​stored for the user to be input, then each new user-supplied values ​​appended to the list (append).

1.2 removed from the list of elements method:

= Lis [] 

del [0] Lis     # delete list Lis a value of the index 

pop_imp = Lis.pop ()     # value of the last element of a pop-up list, stored by pop_imp 

pop_imp = Lis.pop (0)     # pop-up list value of the first element, by pop_imp storage 

Lis.append ( ' Apple ' ) 
Lis.remove ( ' Apple ' )     # remove an element, the list

 

If you remove an element from the list, and is no longer used in any way, on the use of del ()

If you can continue to use it after you delete elements, we use pop ()

 

1.3 Organization list (sorted)

  a. sort () method permanently modify the sort order of the list of elements (the first letter of the comparison). May also be simply () method of transmission parameters according to the alphabetical list elements are arranged in reverse order, to sort reverse = True.

  B. function sorted () to display elements (temporarily) in a specific order, without affecting their original order in the list. May also be () passing arguments to functions sorted reverse = True.

  c. Reverse list elements reverse (). reverse the order permanently modify the list of elements, but the call again reverse can be restored.

  d. len () rapid access list length (number of elements contained in the list).

Lis.sort (Reverse = True)     # parameter non-essential 
Lis.sorted (Reverse = True)     # temporary adjustment 
Lis.reverse () 
Lis.len ()

 

tips: can effectively avoid cross-border problems with a negative index list, unless the list is empty.


 

2. traversal cycle

Here briefly for loop iterations, to generate a list of numbers and the like.

for CAR in cars:
     Print (CAR)     # Note retracted and colon 

for I in Range (0,3)     # can be seen as an initial value 0, 3 to generate a required number of 

even_numbers = List (Range (2,11 , 2))     # print even-numbered 1-10, Range () can also be specified in steps

 

We show here how to square the first 10 integers added to a list:

squares = []

for val in range(1,11):
    square = val**2    #乘方符号**
    squares.append(square)
print(squares)

 

While parsing the list for the cycle and create new elements merged into one line of code, and automatically append the new element. The following example uses a list comprehension to create a square number list:

square = [value**2 for value in range(1,11)]
print(squares)

 


 

3. Slice

Can be used for loop through the list of sections, so that the role of the control loop.

The default value is a list of the slice head-to-tail

LIS [0: 2]     # of the first element to the third 

LIS [ 2:]     # returns all elements from the third element to the end of the list 

LIS [ -3:]     # returns a list of the last three elements 

LIS [: . 3 ]     # before returning the list of three elements 

Lis_1 = Lis [:] copy #Lis list to Lis_1

 


 

4. tuple

Attempts to modify the operating tuple is prohibited, Python pointed out that not modify the value of a tuple elements. You may be modified value tuples of variables.

Tuple using arc parentheses () package together a series of elements, index format: name of the tuple [Index]

dimensions = (200,50)

dimensions = (400,100)

for dim in dimensions:
    print(dim)

 


 

5. Dictionary

In fact, the key is the dictionary - value pairs of the form { "key": "value"}. Itself can contain a list of elements of the dictionary. Can also be surrounded by it, a bit like a C language structure of a storage.

= favorite_languages {
     ' Jen ' : ' Python ' ,
     ' Sarah ' : ' C ' ,
     ' Edward ' : ' Ruby ' ,
     ' Phil ' : ' Python ' , 
} 

# del favorite_language [ 'Jen'] key to remove a 

for k, v in dic.items ()     # traversing dictionary 
       
for name in favorite_language.key ():
    print(name.title)

 

Explicit use key () allows the code easier to understand, if removed, the output unchanged. Similarly, the method value () as well.

 


 

6. if structure

I did not have to say, the programming language, the most basic of a flow control statements.

Python is a case-sensitive language, even if the same piece of string, the case will be different not considered equal.

 

Check the condition 6.1

  a logic symbol:. and, or

  . B check that: in, not in

if user not in users:
    print('false')

 

The basic flow of 6.2 if statement

if conditinal_test:
    do something
elif conditional_test:
    do another thing
else:
    do something else

 

Which can be nested, nested, or other flow control statements, as appropriate.

 


 

summary:

These are trivial things learned today, prudent, and go on tomorrow, probably will end this week python based learning, to make a project.

 

Guess you like

Origin www.cnblogs.com/vilogy/p/12273324.html