Basic data type
- number
- Character string
- List list
- Tuple tuple
- Dictionary dict
- Boolean bool
Lists & Tuples
List: ordered, elements can be modified
Tuple:
- Writing format: the first level element of a tuple cannot be modified, added or deleted. Generally, when writing a meta lease, it is recommended to add ','
- Index: v = tu[0]
- Slice: v = tu[0:2]
- Can be looped by for, and can iterate over the object: for item in tu
- Conversion: STR < -- > List < -- > tuple
Tips: elements of nested lists in tuples can be modified
Dictionaries
dict:
info = { 1:'asdf', "k1":'wqwr', True:"123", (11,22):123 "kk3":(11.22) }
- List cannot be the Key of dictionary, tuple can
- List and dictionary cannot be the Key of Dictionary (True for 1, False for 0, repeat)
- The dictionary is out of order
- Dictionary supports del deletion
- for loop not while loop
Dictionary function
- . fromkeys creates a dictionary based on the sequence and specifies a uniform value
dict.fromkeys(["k1",123,"999"],123)
- . get takes the dictionary value. When the key does not exist, an error will not be reported. Get will return None or the specified value
-
. setdefault setting value. If it exists, do not set it to obtain the value corresponding to the current key. If it does not exist, set the value to obtain the value corresponding to the current key
- . update updates the values in the dictionary
Practice
Dictionary search:
#!/usr/bin/env python # -*- coding=utf-8 -*- dic = { u"Botany": {u"Herb": [u"Lotus", u"Leaf", u"Grass", u"Chrysanthemum", u"orchid", u"Jasmine", u"gourd"], u"Woody plants": [u"Wintersweet",u"Plum blossom",u"Magnolia",u"Jasmine",u"Flowering plum",u"poplar",u"Apple"], u"Aquatic plant": [u"Lotus",u"reed",u"Typha",u"Zizania",u"Scirpus Tabernaemontani",u"Reed bamboo",u"Calamus",u"Pu Wei"] }, u"Animal": {u"poultry": [u"kitten",u"puppy",u"Piglet",u"snake",u"Little bird",u"fish",u"Tiger",u"Lion"], u"Amphibian": [u"Rain frog",u"Tree frog",u"Toad",u"Giant salamander",u"Salamander",u"C.",u"Vermus",u"Fish newt"], u"Mammals": [u"A sloth Sloth",u"zebra",u"wolf",u"tiger",u"Mouse",u"Elk",u"Monkey",u"Lynx",u"Pangolin"] } } li = [] bi = [] go = True while go: for i,v in enumerate(dic,1): print i,v li.append(v) scan_1 = input(">>>") while go: for i,v in enumerate(dic[li[scan_1 - 1]],1): print i,v bi.append(v) scan_2 = raw_input(">>>") if scan_2 == "b": break elif scan_2 == "q": go = False break elif scan_2 == "1" or scan_2 == "2" or scan_2 == "3": while go: for i, v in enumerate(dic[li[scan_1 - 1]][bi[int(scan_2) - 1]], 1): print i, v scan_3 = raw_input(">>>>>") scan_3 = str(scan_3) scan_3 = scan_3.lower() if scan_3 == "b": break elif scan_3 == "q": go = False break else: print(u"Wrong input, please input again") else: print(u"Wrong input, please input again")