Example of a For loop operation developed by Python

Source: Internet
Author: User
The examples in this article describe the for loop operation of Python development. Share to everyone for your reference, as follows:

Here are some of my study records for your reference:

#基本的for循环语句test_list = [2, "Jone", 3,6,7, ' Hongten ', ' Hanyuan ', ' good ', "Tom"] #打印列表的长度print (len (test_list)) #遍历列表for I in Test_list:print (i) test_str = "Hello,i ' m hongten" print (' Print string: ' + test_str ') #遍历一个字符串print (' Traverse a string ') for I in TEST_STR:PR  int (i) test_tuple = [("A", 1), ("B", 2), ("C", 3), ("D", 4)]print (test_tuple) #遍历一个元组print (' Traversal of a tuple ') for (i,j) in Test_tuple:  Print (i,j) test_dict = {' name ': ' Hongten ', ' age ': ' A ', ' gender ': ' M ', ' Sports ': ' Football, table tennis, swim '} #字典迭代器for key in Test_dict: Print (key + ': ' + test_dict[key]) L1 = [1,3,5,7]L2 = [2,4,6,8] #使用zip将两个列表合并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) #可以看出来当长度不一的时候, redundant ignored Test_keys = [' name ', ' age ', ' gender ', ' Weight ', ' hight ']test_values = [' Hongten ', ' a ', ' M ', ' a ', ' [+] '] #使用zip来构造一个字典print (' Keys in the Dictionary: ') print (Test_keys) Print (' Key in dictionary ' corresponds to value: ') print (test_values) print (' Construct dictionary ') Test_dic = dict (Zip (test_keys,test_values))) for key in Test_dic:print (key + ': ' + Test_dic[key]) 

Operating effect:

Python 2.7.9 (default, Dec, 12:24:55) [MSC v.1500 + bit (Intel)] on Win32type "copyright", "credits" or "license ( ) "For more information.>>> ================================ RESTART ================================> >> 92jone367hongtenhanyuangoodtom Print string: Hello,i ' m hongten traverse a string hello,i ' m hongten[(' a ', 1), (' B ', 2), (' C ', 3), ('  d ', 4)] traverse a tuple (' a ', 1) (' B ', 2) (' C ', ' 3 ') (' d ', ' 4 ') Gender:mage:20name:hongtensports: Football, table tennis, swimming [(1, 2), (3, 4), "(5, 6)] (7,  2) (3, 4) (5, 6) (7, 8) ###################################################### #L1, the L3 list is: [1, 3, 5, 7][2, 4, 6] (1, 2) (3, 4) (5, 6) Dictionary of keys:[' name ', ' age ', ' gender ', ' weight ', ' hight ' dictionary key corresponding to the value:[' Hongten ', ' a ', ' M ', ' 55 ', ' 170 '] construct the dictionary after gender: Mage:20name:hongtenweight:55hight:170>>>

I hope this article is helpful for Python program design.

  • 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.