Loops: There are two python loops, for and while
The For loop indicates that an item variable is within the collection, and then continues the loop until the variable is not within a few, and then jumps out of the loop.
such as a set a=[1,2,3,4];
For I in A:
do sth;
A while loop is a conditional loop that executes a loop when the condition is met. Otherwise, it will not be executed.
Like what
a=[1,2,3,4]
i=0;
While I<len (a): #就是说i的值比a的长度值小.
do sth;
i++; #必须控制变量.
Key-value pairs:
Key-value pairs just like the Hashtable in C #, a key corresponds to a value.
To get a value, you can find the key.
There are two types of key-value pairs, dict and set types. Dict has keys and values, and set has only keys, and cannot be duplicated.
Declare a dict
A={"A": "A"; "B": "A"}, note that keys cannot be duplicated, but values can be duplicated. Because the key value pair is the key to find the value, if the key is repeated, you will find two values, but this situation is not allowed to occur.
Declare a set:a=set ([All-in-one]); Note that the data in the set must be a list,set is not ordered.
A few common methods: Dict
Assigning a value to a key a[key]=value;
Determines whether key is in a dict, returns TRUE or FALSE, or uses the Get method A.get (key), if none exists, returns the original value;
Yes, value is removed using A.pop (key): Note that key also does not exist.
Some methods of set: A.add (key); Add a key to a (if the key does not exist in the original collection, it is not valid).
A.remove (key) deletes the specified key;
Python Learning Diary-Looping and key-value pairs