Python3 Basic Data Type _ 2, python3 Data Type _ 2
1 #! /Usr/bin/python3 2 3 # The following set, the dict method py2 cannot run 4 5 # create a set, use braces {} or the set () function to create 6 # Note: to create an empty set, you must use set () instead of {}, because {} is used to create an empty dictionary 7 num1 = {1, 2, 3, 4, 5, 6} 8 # print the set, automatic deduplication 9 print ('num1: ', num1) 10 11 if (9 in num1): 12 print ('9 in num1') 13 else: 14 print ('9 not in num1') 15 16 num2 = {1, 22, 33,4, 55} 17 18 print ('num2: ', num2) 19 # Check num1 type 20 print ("type num1:", type (num1) 21 print ('num1-num2 difference set: ', num1-num2) 22 print ('num1 | num2 Union: ', num1 | num2) 23 print ('num1 & num2 intersection:', num1 & num2) 24 print ('num1 ^ num2 does not exist at the same time element: ', num1 ^ num2) 25 26 print (' * 60) 27 # The dictionary is identified, it is an unordered key: value Pair set 28 # key must use an unchangeable type 29 # In the same dictionary, key (key) it must be the only 30 31 dict1 = {'http': 'www .cnblogs.com ', 'sub202020 ': 'P \/8006988.html '} 32 dict1 ['com'] = "sub2020" 33 print ("dict1:", dict1) 34 print ("dict1 type :", type (dict1) 35 # print dictionary key 36 print (dict1.keys () 37 # print dictionary value 38 print (dict1.values ()) 39 40 # create an empty dictionary 41 dict2 = {} 42 dict2 ['com '] = "sub2020" 43 dict2 ['1'] = "a" 44 dict2 ["2"] = "B" 45 # After several assigned values, print dictionary 46 print ("dict2:", dict2) 47 48 # constructor dict () the dictionary 49 dict3 = dict ([('A', 1), ('B', 2), ('C ', 3)]) 50 dict4 = dict (a = 1, B = 2, c = 3) 51 dict5 = {x: x ** 2 for x in (2, 3, 4 )} 52 print ("dict3:", dict3) 53 print ("dict4:", dict4) 54 print ("dict5:", dict5)
Output
Num1: {1, 2, 3, 4, 5, 6} 9 not in num1num2: {1, 33, 4, 22, 55} type num1: <class 'set'> num1-num2 difference set: {2, 3, 5, 6} num1 | num2 Union: {1, 2, 3, 4, 5, 6, 33, 22, 55} num1 & num2 intersection: {1, 4} num1 ^ elements of num2 that do not exist simultaneously: {33, 2, 3, 22, 55, 5, 6 }************************************** * ********************** dict1: {'http': 'www .cnblogs.com ', 'sub2020': 'P \/8006988.html', 'com': 'sub202020'} dict1 type: <class 'dict '> dict_keys (['HTTP', 'sub2020', 'com']) dict_values (['www .cnblogs.com ', 'P \/8006988.html ', 'sub202020']) dict2: {'com ': 'sub2020', '1': 'A', '2':' B '} dict3: {'A ': 1, 'B': 2, 'C': 3} dict4: {'A': 1, 'B': 2, 'C': 3} dict5: {2: 4, 3: 9, 4: 16} *** Repl Closed ***