Python Learning Diary-Looping and key-value pairs

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.