Python for loops and arrays

Source: Internet
Author: User
Tags time 0

For-Target in expression:

Loop body

Example 1:

' songcuiting '  for inch a:print (i,end=',')

Example 2:

member = ['song','cui','ting' ] for in Member:print (Each,len (member))

Range Syntax:

Range ([Start,] end[,step=1])

[] Enclose the non-required parameters, the start parameter represents the start number (not fill the time 0 start), the step parameter represents the end number-1. Step indicates every few numbers. Default is 1

Example:

List (range (10))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

List (range (0,5))

[0, 1, 2, 3, 4]

List (range (2,9))

[2, 3, 4, 5, 6, 7, 8]

List (range (1,10,2))

[1, 3, 5, 7, 9]

 for  in range:    if i%2 ! =0:        print (1  , i)        continue    2    print (2, I

Please answer the output of the For loop above

 for  in range:    2    Print (i)

Doubt: I will not be assigned a value of i + = 2.

The current explanation is that I was re-assigned to the range (10) at the time of the For loop, or that I was not affected by the range (ten) for loop.

member = [' song ', ' Cui ', ' ting ', ' Girl ']

Member1 = Member [1:3] # #创建member1 The end result of an array containing 1 to 3 (not containing 3) is member1=[' cui ', ' ting ']

Member [1:]

Member [: 3]

Member [:]

Ways to add elements to a list: Append, Extend, insert

Member.append (' haha ') #只能逐个在数组尾部添加元素

Member.extend (' haha ')

Member.extend ([' Nihao ', ' Xiexie ']) #可以再数组尾部添加元素及数组

Member.insert (1, ' haha ') #在数组的第二个位子上插入haha

Remove an element from the list: Remove (), Del, Pop ()

Member.remove (' haha ') # #删除列表中的haha

Del member [1] # #删除列表中的第一个元素

Del member # #删除列表

Name = Member.pop () # #删除最后一个元素 return value for this element. When the Python array is created, it is created as a stack, and the last entry is dropped first.

Python for loops and arrays

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.