How to Implement element variables in the Python list

Source: Internet
Author: User

When using the Python list, we need to learn many key points. Next we will look at how to use the Python list. The Python list is a set of values of any type, which are combined in a certain order. The value of the List is called Elements ).

Each element is identified as an index, and the first index is 0. The elements in the list can be of any type or even list type. That is to say, the list can be nested drops. The elements in the list are enclosed in brackets and separated by commas. For example:

 
 
  1. 1 a = [1,2,3,4]2 b = [1.0,2.0,3,4,"5"]3 c = [1,2.0,"3",[1,2,3,4]] 

The element in the Python list can also be a variable, but changing the value of the variable does not affect the value of the element in the list. For example:

 
 
  1. # Coding: UTF-8
  2. E = 1
  3. F = 2
  4. G = 3
  5. H = [e, f, g, 4]
  6. Print h # result = [1, 2, 4]
  7. Print type (h) # <type 'LIST'>
  8. E = 5
  9. F = 6
  10. G = 7
  11. Print h # result = [1, 2, 4] instead of [5, 6, 7, 4]

For a list containing only integers, Python also provides several other methods to create a list:

(1) range (n, m) function range returns an integer list. The list starts from the first n parameter of the function and ends with the last m parameter, but does not contain the last m parameter, the difference between two adjacent numbers is 1.
Example:

 
 
  1. a = range(1,4)   
  2. print a #result = [1,2,3] 

(2) The range (n) function range generates a list starting from 0. The most given parameter n ends, but does not contain the given parameter n.
Example:

 
 
  1. a=range(8)  
  2. print a #result = [0,1,2,3,4,5,6,7] 

(3) The range (n, m, k) function generates a list of mathematical ascending arithmetic numbers. The start value is n, the end value is m, and the step size is k.
Example:

 
 
  1. a = range(1,20,4)  
  2. print a #result = [1,5,9,13,17] 

There is also a special list that does not contain any elements, which is called an empty list. The empty list is expressed as []. The Python list can be assigned a value or passed to the function as a parameter.
 

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.