Python development-for-loop operation examples

Source: Internet
Author: User
This article mainly introduces the for loop operation in python development, and analyzes in detail the specific usage skills of for loop in Python in the form of examples, which has some reference value, for more information, see the examples in this article. We will share this with you for your reference. The details are as follows:

Below are some of my learning records for your reference:

# Basic for loop statement test_list = [2, "Jone", 7, 'hongten ', 'hanyuan', 'good ', "Tom"] # print the length of the list print (len (test_list) # traverse the list for I in test_list: print (I) test_str = "hello, i'm hongten "print ('print string: '+ test_str) # print a string ('traverse A string') for I in test_str: print (I) test_tuple = [("a", 1), ("B", 2), ("c", 3), ("d", 4)] print (test_tuple) # print ('traverse A tuples ') for (I, j) in test_tuple: print (I, j) test_dict = {'name': 'hongten ', 'age': '20', 'Gender ': 'M', 'Sports': 'soccer, table tennis, swimming '} # dictionary iterator for key in test_dict: print (key + ':' + test_dict [key]) L1 = [1, 3, 5, 7] L2 = [2, 4, 6, 8] # Use zip to merge the two lists print (zip (L1, l2) for (I, j) in zip (L1, L2): print (I, j) print ('##################################### ##################') l3 = L2 [:] L3.remove (8) print ('l1, L3 list: ') print (L1) print (L3) for (I, j) in zip (L1, l3): print (I, j) # It can be seen that when the length is different, test_keys = ['name', 'age', 'gender' is ignored ', 'weight', 'higint'] test_values = ['hongten ', '20', 'M', '55 ', '000000'] # Use zip to construct a dictionary print ('keys: 'In the dictionary) print (test_keys) print ('value:' corresponding to the key in the dictionary) print (test_values) print ('after dictionary construction ') test_dic = dict (zip (test_keys, test_values) for key in test_dic: print (key +': '+ test_dic [key])

Running effect:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license () "for more information. >>> ================================== RESTART ======== ========================================>> 92jone?hongtenhanyuangoodtom print string: hello, I'm hongten traversing a string hello, I'm hongten [('A', 1), ('B', 2), ('C', 3 ), ('D', 4)] traverse a tuples ('A', 1) ('B', 2) ('C', 3) ('D', 4) gender: Mage: 20 name: hongtensports: football, table tennis, swimming [(1, 2), (3, 4), (5, 6), (7, 8)] (1, 2) (3, 4) (5, 6) (7, 8) ######################################## ############## L1, the L3 list is: keys in the [1, 3, 5, 7] [2, 4, 6] (1, 2) (3, 4) (5, 6) dictionary: ['name', 'age', 'gender', 'weight ', 'higint'] value corresponding to the key in the dictionary: ['hongten', '20 ', 'M', '55', '000000'] after constructing the dictionary, gender: Mage: 20 name: Hongtenweight: 55 hight: 170 >>>

I hope this article will help you with Python programming.

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.