Python Learning (Five) list, Ganso, for circulation

1. List: list

Python is a list of the most common types of data, it can appear as a comma-separated values ​​in square brackets.

Data items list need not have the same type

Create a list, as long as the comma-separated data items using different brackets can be. As follows:

list1 = ['Google', 'Runoob', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]

Like string indices, list indices start at 0. The list can be intercepted, combinations and the like.

Public function

#for loop nest 
user = [ 'John Doe', 'John Doe', 'Wang Wu', 'Lee III' ] 
for I in User: 
    # the first cycle, i = seating 
    for ELE in I: 
        Print ( ELE)  # exercises: for loop and implemented by digital computer: user = [ 'John Doe', 'John Doe', 'Wang Wu', 'Lee III']  # 1 John Doe 2 0 Zhang Wang Wu Li 3 three  users = [ 'John Doe', 'John Doe', 'Wang Wu', 'Lee III' ]  # mode. 1  users_len = len (Users) for I in Range (users_len): Print (I, Users [I]) # = counteat embodiment 2 0 for I in Users: Print (counteat, I) =. 1 + counteat

Unique features: .append add elements

# 1, the last element added .append list () 
users = [] 
the while True: 
    name = INPUT ( 'Enter your name:' ) 
    users.append (name) # add an element to a list of users 
    Print (users)  # Example # input user name and password system  users = []  for I in Range (0,3 ): name = iNPUT ( 'enter a username and password' ) users.append (name) # add an element to a list of users Print (users) # when the login user name and password verification username = input ( 'username' ) = INPUT password ( 'password' ) for in Item users: Result = item.split ( ',' ) = user Result [0] PSW = Result [. 1 ] IF User PSW == == username and password: Print ( 'the OK' ) BREAK the else : Print ( 'No')
# 2, the insertion element .insert (sequence of the content) at the specified index 
List = [1,2,3,4,5,6, " Hello " , " World " ] # index definition list in the list from the start of counting 0 
list.insert (1,666) # specified index added 
Print (Lite) 

# 3, remove elements: .remove / .pop / del 
list.remove ( " the Hello " ) 
list.pop () # default to delete the last 
del List [7 ] # delete the first seven data 

# 4, empty: .clear () 
list.clear ()

 

 2, Ganso: tuple

Python is similar to a list of tuples, except that the elements of the tuple can not be modified.

Tuples use parentheses, square brackets list.

Tuple create very simple, only need to add elements in parentheses and separated by commas can be.

= TUP1 ( ' the Google ' , ' Runoob ' , 1997, 2000 ) 
tup2 = (. 1, 2,. 3,. 4,. 5 ) 
tup3 = " A " , " B " , " C " , " D "  # does not require parentheses also can 
type (tup3) 
< class ' tuple ' >

Empty tuples

tup1 = ()

When tuple contains only one element, the element needs to be added after the comma, or brackets will be used as the operator:

= TUP1 (50 ) 
type (TUP1) # without comma, type Integer 
< class  ' int ' > 
TUP1 = (50 ,) 
type (TUP1) # comma, a tuple of type 
< class  ' tuple ' >

 

3, for circulation

 

# Exercises: integer addition implement a calculator (two numbers) 
'' ' as count = input (' Enter content: ') a user input: 9 or 5 + 5 + 5 + 9 or 9 (including the blank) , 
and then divided to calculate the final conversion result obtained integer. '' ' 
COUNT = INPUT ( " Please enter Content: ' ) 

# line of thinking (for single digit sum) 
COUNT = count.strip () # remove both sides of the blank 
V1 = int (COUNT [0]) 
V2 = int ( COUNT [-1 ]) 
V3 = V1 + V2
 # ideas two (for single digit sum) 
count_len = len (COUNT) 
index = 0 
Total = 0
 the while True: 
    in Flag = COUNT [index] # listing
    IF flag.isdigit (): # judgment is not a digital 
        Total + = int (In Flag) 
    index + =. 1
     IF index == count_len:
         BREAK 
Print (Total)
 # idea three 
Result = count.split ( ' + ' ) # a + dividing 
Print (Result) 
V1 = int (Result [0]) 
V2 = int (Result [-1 ]) 
V3 = V1 + V2
 Print (V3)

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/ZBHH/p/12483550.html