day 08 summary (built-in method)

First, list the type of built-in method

  1. Role: Description multiple values, such as hobbies.

  2. Definition: a plurality of any type can have the value of [], the elements separated by commas.

    hobby_list = ['play', 'swimming', 'dancing']
    my_friend = ['tbb','bill','tom']
  3. Built-in methods: Priority to master, need to know.

    3.1 Priority grasp

    1. By index value (positive value + antiporter value), may be taken to storage.

      my_friend = ['tbb','bill','tom']
      my_friend[0] = 'TBB'
      print(my_friend[0])

      TBB

    2. slice

      my_friend = ['tbb','bill','tom']
      print(my_friend[0::2])

      ['tbb', 'tom']

    3. length

      my_friend = ['tbb','bill','tom']
      print(len(my_friend))

      3

    4. Members and not in operation in

      my_friend = ['tbb','bill','tom']
      print('Tbb' in my_friend)
      print('frank' in my_friend)
      False
      True
    5. Added value

      my_friend = ['tbb','bill','tom']
      my_friend.append('frank')
      print(my_friend)

      ['tbb','bill','tom','frank']

    6. delete

      my_friend = ['tbb','bill','tom']
      del my_friend[1]
      print(my_friend)

      ['tbb','tom']

    7. cycle

      my_friend = ['tbb','bill','tom']
      for name in my_friend:
          print(name)
      
      tbb
      bill
      tom
      

    3.2 need to know

    1. insert () object into the specified position of the specified list.

      my_friend = ['tbb','bill','tom']
      my_friend.insert(1,'frank')
      print(my_friend)
      

      ['tbb', 'frank', 'bill', 'tom']

      2.pop () default to delete the last element

      my_friend = ['tbb','bill','tom']
      print(my_friend.pop(0))
      print(my_friend)
      
      tbb
      ['bill', 'tom']
      
      1. remove () remove the first occurrence of a value in the list
      my_friend = ['tbb','bill','tom','frank']
      my_friend.remove('frank')
      print(my_friend)
      

      ['tbb', 'bill', 'tom']

      1. count () Count Occurrences
      my_friend = ['tbb','bill','tom','frank']
      print(my_friend.count('frank'))
      

      1

      1. index () Find index
      my_friend = ['tbb','bill','tom','frank']
      print(my_friend.index('tom'))
      

      3

      1. clear () Clears
      my_friend = ['tbb','bill','tom','frank']
      my_friend.clear()
      print(my_friend)
      

      []

      1. copy () Copy
      my_friend = ['tbb','bill','tom','frank']
      print(my_friend.copy())
      

      ['tbb', 'bill', 'tom', 'frank']

      1. extend () a plurality of value added at the end of disposable another sequence list
      my_friend = ['tbb','bill','tom','frank']
      my_friend2 = ['byebye']
      my_friend.extend(my_friend2)
      print(my_friend)
      

      ['tbb', 'bill', 'tom', 'frank', 'byebye']

      1. reverse () Reverse
      my_friend = ['tbb','bill','tom','frank']
      my_friend.reverse()
      print(my_friend)
      

      ['frank', 'tom', 'bill', 'tbb']

      1. sort () to sort the list of original
      my_friend = ['tbb','bill','tom','frank']
      my_friend.sort()
      print(my_friend)
      

      ['bill', 'frank', 'tbb', 'tom']

  4. Stored value or a plurality of values: a plurality of values

  5. Ordered or disordered: Ordered

    hobby_list = ['read', 'run', 'girl']
    print(id(hobby_list))
    hobby_list[2] = ''
    print(id(hobby_list))  
    
    2352832078472
    2352832078472
    
  6. Variable or immutable: Variable Data Type

Second, the tuple type built-in method (Learn)

Tuples are immutable list, i.e., value tuples can not be changed, and therefore generally used only for only the tuple memory requirements are not taken. Thus tuples may also be substituted out list, so compared to the list of tuples used rarely. Tuple is a list of advantages compared to: modify the value list, the list structure will change, but only need to store tuples, in some extent, so the list require more memory. But now the industry is not a memory problem, so the industry group generally does not use Spring.

1. Role

Description multiple values, such as hobbies.

2. Definitions

In () may have a plurality of values ​​of any type, a comma-separated elements

3. built-in method

  1. 1 index values
name_tuple = ('tbb', 'bill', 'tom', 'frank')
print(name_tuple[2])

tom

  1. 2 slices (care regardless of the end, step)
name_tuple = ('tbb', 'bill', 'tom', 'frank')
print(name_tuple[0:3:2])

('tbb', 'tom')

  1. 3 length
name_tuple = ('tbb', 'bill', 'tom', 'frank')
print(len(name_tuple))

4

  1. 4 members of the operation
name_tuple = ('tbb', 'bill', 'tom', 'frank')
print('tom' in name_tuple)

True

  1. Cycle 5
name_tuple = ('tbb', 'bill', 'tom', 'frank')
for name in name_tuple:
    print(name)
tbb
bill
tom
frank
  1. 6count()
name_tuple = ('tbb', 'bill', 'tom', 'frank')
print(name_tuple.count('bill'))

1

  1. 7index()
name_tuple = ('tbb', 'bill', 'tom', 'frank')
print(name_tuple.index('tbb'))

0

4. The stored value or a plurality of values

A value

5. orderly or disorderly

Ordered

6. The variable or invariable

Immutable data type

7. The difference between tuples and lists of

  • Variable is a list of reasons: the memory address corresponding to the index value can be changed.
  • Tuples can not become the reason is: the index of the corresponding memory address values ​​can not change, or conversely, as long as the memory address corresponding to the index value has not changed, then the tuple is never changed.

Third, the dictionary type built-in method

1. Role

A plurality of stored values, but each has a value of a corresponding key, key values ​​described functions. It is used for different states when the stored value indicates, for example, the value stored there name, age, height, weight, hobbies.

2. Definitions

A plurality of elements separated by commas within {}, each element is key: value form, value can be any data type, but generally should be a string type key, but the key must be immutable.

3. built-in methods: Priority to master, need to know

3.1 Priority grasp

  1. Access key press value: deposit may be desirable

    dic = {'a': 1, 'b': 2}
    
    print(dic['a'])
    

    1

  2. Length len

    dic = {'a': 1, 'b': 2}
    
    print(len(dic))
    

    2

  3. Members and not in operation in

    dic = {'a': 1, 'b': 2}
    
    print('a' in dic)
    print(1 in dic)
    
    True
    False
    
  4. del Delete

    dic = {'a': 1, 'b': 2}
    del dic['a']
    print(dic)
    

    {'b': 2}

    dic = {'a': 1, 'b': 2}
    dic.pop('a')
    print(dic)
    

    {'b': 2}

  5. Keys Key (), the value of values ​​(), on the key-value items () can return a list traversal (key, value) tuples array.

    dic = {'a': 1, 'b': 2}
    print(dic.keys())
    print(dic.values())
    print(dic.items())
    
    dict_keys(['a', 'b'])
    dict_values([1, 2])
    dict_items([('a', 1), ('b', 2)])
    
  6. cycle

    dic = {'a': 1, 'b': 2}
    for k, v in dic.items():  
        print(k, v)
    
    a 1
    b 2
    

3.2 need to know

  1. get () Returns the value of the specified key, if the value is not in the dictionary returns the default value.

    dic = {'a': 1, 'b': 2}
    print(dic.get('a'))
    print(dic.get('c'))
    
    1
    None
    
  2. update () dict2 parameters of the dictionary key / value (key / value) updates the dictionary in dict

    dic = {'a': 1, 'b': 2}
    
    dic1 = {'a': 1, 'b': 2}
    dic2 = {'c': 3}
    dic1.update(dic2)
    print(dic1)
    

    {'a': 1, 'b': 2, 'c': 3}

  3. fromkeys () is used to create a new dictionary, the sequence elements do seq dictionary key, value corresponding to all the keys to the dictionary initial value.

    dic = dict.fromkeys(['name', 'age', 'sex'], None)
    print(dic)
    

    {'name': None, 'age': None, 'sex': None}

  4. setdefault (): Similar to get (), if the key does not exist in the dictionary, it will add key and value to the default value

    dic = {'a': 1, 'b': 2}
    print(dic.setdefault('a',3))
    print(dic)
    print(dic.setdefault('c',3))
    print(dic)
    
    1
    {'a': 1, 'b': 2}
    3
    {'a': 1, 'b': 2, 'c': 3}
    

4. The stored value or a plurality of values

A plurality of values, the value may be a plurality of types, key must be immutable type, generally should be immutable types string type

5. orderly or disorderly

Disorderly

6. The variable or invariable

Variable data types

dic = {'a': 1, 'b': 2}
print(id(dic))
dic['a'] = 3
print(id(dic))
2875759947640
2875759947640

Fourth, the set of built-in type method (set)

Collection can be understood as a collection of learning Python is a collection of students; students can learn Linux is a collection.

  • Features: 1 to 2 weight scrambled

1. Role

A lot of stuff storage elements, container data type

2. Definitions

{} The plurality of elements spaced apart by a comma, each element must be immutable.

s = {1, 2, 1, 'a'}  

print(s)

{1, 2, 'a'}

3. built-in methods: Priority to master, need to know

3.1 Priority grasp

  1. Length len

    s = {1, 2, 'a'}
    
    print(len(s))
    

    3

  2. Members and not in operation in

    s = {1, 2, 'a'}
    
    print(1 in s)
    

    True

  3. | Union

    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    
    print(pythoners|linuxers)
    print(pythoners.union(linuxers))
    
    {'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'}
    {'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'}
    
  4. & Intersection

    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    
    print(pythoners&linuxers)
    print(pythoners.intersection(linuxers))
    
    nick
    nick
    
  5. - difference set

    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    print(pythoners-linuxers)
    print(pythoners.difference(linuxers))
    
    {'tank', 'jason', 'sean'}
    {'tank', 'jason', 'sean'}
    
  6. ^ Symmetric difference

    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    
    print(pythoners^linuxers)
    print(pythoners.symmetric_difference(linuxers))
    
  7. ==

    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    javers = {'nick', 'egon', 'kevin'}
    
    print(pythoners==linuxers)
    print(javers==linuxers)
    
    False
    True
    
  8. Parent Set:>,> =

    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    javaers = {'jason', 'nick'}
    
    print(pythoners>linuxers)
    print(pythoners>=linuxers)
    print(pythoners>=javaers)
    print(pythoners.issuperset(javaers))
    
    False
    False
    True
    True
    
  9. Subsets: <, <=

    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    javaers = {'jason', 'nick'}
    
    print(pythoners<linuxers)
    print(pythoners<=linuxers)
    print(javaers.issubset(javaers))
    
    False
    False
    True
    

3.2 need to know

  1. add()

    s = {1, 2, 'a'}
    s.add(3)
    
    print(s)
    

    {1, 2, 3, 'a'}

  2. .remove()

    s = {1, 2, 'a'}
    s.remove(1)
    
    print(s)
    

    {2, 'a'}

  3. difference_update () for removing the two sets of elements are present

    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    pythoners.difference_update(linuxers)
    
    print(pythoners)
    

    {'tank', 'jason', 'sean'}

  4. discard () for removing the specified collection element.

    # set之discard()
    s = {1, 2, 'a'}
    # s.remove(3)  # 报错
    s.discard(3)
    
    print(s)
    

    {1, 2, 'a'}

  5. isdisjoint () for determining whether the two sets contain the same elements, if not returns True, otherwise return False.

    pythoners = {'jason', 'nick', 'tank', 'sean'}
    linuxers = {'nick', 'egon', 'kevin'}
    pythoners.isdisjoint(linuxers)
    
    print(pythoners.isdisjoint(linuxers))
    

    False

4. The stored value or a plurality of values

A plurality of values, and is immutable.

5. orderly or disorderly

Disorderly

6. The variable or invariable

Variable data types

Guess you like

Origin www.cnblogs.com/mgytz/p/11305005.html