Python 0 Basic Primer Three---(For loop + list)

Source: Internet
Author: User
Tags python list
Python's for Loop and C, C + + languages such as the structure of the For loop is still very different, but it is very easy to get started, the Python list is similar to the C language of the array, but compared to the function of the array is more powerful, the joke is said to hit the array of hormones.

For loop

(1) Syntax structure for the FOR loop:
for element in variable (list, etc.)


(2) The For loop is used in conjunction with the range () function
The range () function has the following syntax: Range ([Start],stop[,step=1])
In general, use the for loop with the range () function.
The first parameter is the starting number, the second is the ending number (not including the ending number), the last parameter is the step, the default is 1, and the starting position defaults to 0.

2. List: A hormone-hit array
(1) The definition of the list and the output of the element
List of flags: [,,,]
List definition: List name =[element 1, Element 2, Element 3 ...]

(2) List related actions
A. Adding elements to the list

1. Use the Append () function to add elements to the list

2. Use the Extend () function to add elements to the list

3. Use the Insert () function to add elements to the list

B. Removing elements from a list

1. Remove the element with the remove () function, and the parameter is the element to be deleted, not the index

Use the pop () function to remove elements from the list by default, by deleting the last element, or by filling in a parameter to delete the element at the specified position.

Delete element with Del statement, del list name [index of specified element]

The above is a list of some simple elements of the operation, and then if there will be a timely supplement.
Attach the source code of the operation:

#For循环的联系print ("======for Cycle Practice ======") str1= "for loop exercise" for I in Str1:print (i,end= "") print ("\ n") print ("======range ()" Used with the For Loop ====== ") #一般情况下将For循环与range () function with the syntax of the #range () function is: Range ([start],stop[,step=1]) #第一个参数是起始数字, and the second is the ending number ( Does not include the ending number) #最后一个参数是步长, default is 1, the starting position defaults to 0for m in range (5): Print (m,end= "") print ("\ n") for n in range (1,10,2): Print (n,end = "") print ("\ n") #列表的练习: An array of hormones # A list of flags: [,,,]print ("------Output the entire list------") list=["Little Turtle", "Little Pudding", "Stray"]print (list) Print ("------elements in the output list------") list=["Little Turtle", "Little Pudding", "lost"]for I in Range (3): #一定要注意列表中的元素从0开始 print (list[i],end= ") print ( "\ n") print ("------Create a list of mixed type elements------") mixture=["Yao Xi", 1,2]for I in range (3): Print (mixture[i],end= "") print ("\ n") # Add elements to the list, add elements at the end of the element List.append ("Yao Xi") Print (list, "\ n") #获取列表的长度print ("The number of elements in the list is:%d"%len (list), "\ n") # Add more than one element at a time to the end of the list, using extend, the parameter is a list listadd=["Guoxiangyao", "Xiaoshuai"]list.extend (listadd) for I in range (6): Print (list [i],end= "] Print (" \ n ") #使用insert (,) function to insert data # where the first element is the position of the element to be inserted # The second argument is the element to be inserted List.insert (0," littleTurtle ") for I in range (7): Print (list[i],end=" ") print (" \ n ") #从列表中获得, delete an element List.remove (" Little Turtle ") #删除元素是指定元素 instead of index for I in range (5): Print (list[i],end= "") print ("\ n") #使用索引删除元素del list[0]for I in range (4): Print (list[i],end= "") print ("\ n") #删除列 The last element of the table, using the pop () function list.pop () for I in Range (3): Print (list[i],end= ") print (" \ n ") #也可以使用在pop () to add an index to the element, which pops up the element list at the specified position. Pop (0) for I in Range (2): Print (list[i],end= "")

The above is the Python 0 basic Primer Three---(For loop + list) content, more relevant content please follow topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.