python lists, tuples, dictionaries (Mushishi "selenium3 automated testing combat - based on the Python language Note 3")

1. List []

  • Definition list
    lists = [1, 2, 'a', 5, 'kite']
  • Print List
    Print (Lists [0]) 
    output: [ . 1, 2, ' A ' ,. 5, ' Kite ' ]
  • Print a list of elements
    # Print list of the first element [0] 
    Print (Lists [0])
     # print list of three elements [2] 
    Print (Lists [2 ])
     # print the last element in the list [-1] 
    Print ( lists [-1])
  • Modify the elements in this list
    # Modify elements in the list is a B 
    Lists [2] = ' B ' 
    Print (Lists)
  • Adding new elements
    # Add to the end of the list elements 
    lists.append ( ' C ' )
     Print (Lists)
  • Delete the specified element
    # Delete the first element in the list 
    lists.pop (0)
     Print (Lists)

2. tuple ()

  • The definition of a tuple
    tup1 = ('a', 'b', 1, 2, 3)
  • View tuple
    # Print an element 
    Print (TUP1 [0])
     # printing specified element 
    Print (TUP1 [0: 2 ])
     # printing the second and subsequent start element 
    Print (TUP1 [. 1 :])
     # printing of four before the start of the element 
    Print (TUP1 [:. 3])
  • Connection tuple
    tup2 = ('kite', 5, 'c')
    tup3 = tup1 + tup2
    print(tup3)
  • Copy tuple
    tup4 = ("Hi!\n")
    print(tup4 * 3)

    Tuples difference list: the list is variable, may be added, modified, deleted elements thereof; the tuples are immutable and can not append, modify and delete elements therein. When the number of elements of uncertainty, it is recommended to use the list.

 

3. Dictionary {}

A plurality of sets of dictionary key pairs, each key of the key: value Composition

  • Dictionary definitions
    # Dictionary definitions, key not be the same, value may be the same 
    dicts = { " username " : " Kite " , " password " : 123456}
  • Print Dictionary
    # Print all key dictionary, keys () returns a list of dictionary key 
    Print (dicts.keys ())
     # print the dictionary of all value, values () Returns the value of the dictionary list 
    Print (dicts.values ())
     # items ( ) printed dictionary of all elements return in list form 
    Print (dicts.items ())
     # loop print dictionary key and value 
    for k, v in dicts.items ():
         Print ( " dicts Keys IS% r " % k)
         Print ( " % R & lt IS values dicts " % V)
  • Add key / value pairs
    dicts["age"] = 22
  • Deletes the specified key / value pairs
    dicts.pop("password")

Guess you like

Origin www.cnblogs.com/kite123/p/11418122.html