Python traverses the serial number and value in the list (three methods), python serial number

Source: Internet
Author: User

Python traverses the serial number and value in the list (three methods), python serial number

Three methods are used to traverse the serial numbers and values in the list:

I recently learned the python language and felt that it has greatly improved my work efficiency. I wrote this blog on Valentine's Day. I will not talk about it here, but I will post it directly.

#! /Usr/bin/env python #-*-coding: UTF-8-*-if _ name _ = '_ main _': list = ['html ', 'js', 'css ', 'python'] # method 1 print method 1: 'For I in list: print ("Sn: % s value: % s "% (list. index (I) + 1, I) print '\ n traversal list Method 2:' # method 2 for I in range (len (list): print ("No: % s value: % s "% (I + 1, list [I]) # method 3 print '\ n traversal list method 3:' for I, val in enumerate (list): print ("No.: % s value: % s" % (I + 1, val )) # method 3 print '\ n traverse list method 3 (set the start position of traversal and only change the start sequence number):' for I, val in enumerate (list, 2 ): print ("Sn: % s value: % s" % (I + 1, val ))

The result after running the code is shown in:

Here we will introduce the enumerate () method and view it through the help () function. The query results are as follows:

The second parameter of the enumerate () function only changes the starting value of the sequence number and does not change other parameters.

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.