Python_ Practical Introductory Article _10

Source: Internet
Author: User
Tags for in range

Tag: Generate is use Ali element break BSP Font = =

For loops in Python

1.For Loop statement

    A 1.Python for loop can traverse any sequence type, such as a list or a string. The following code should be able to understand, sequentially outputting sequence elements.

 name = [ " CDX   ", "  red   ", "  molly   " ]  for  i in   name:  print   (i)  >>>"  CDX  Span style= "COLOR: #800000" > " "  red   "  " molly   " 

    2.for Else statement (that is, after the loop is finished, the content of else is executed)

name = ["CDX","Red","Molly"] forIinchName:Print(i)Else:    Print("hahaha")>>>"CDX","Red","Molly" "hahaha"

    The break statement in the 3.for Loop and the Countinue statement (when the condition is reached, encounters a break that jumps directly out of the loop body, encounters a countinue and returns to the execution loop)

#break语句
name = ["CDX"] forIinchName:ifi = ="CDX": Print("redmolly") Break Print("Does this sentence still carry on? ")Else: Print("no looping data!")Print("complete the Loop!")>>>redmolly complete the loop!

#countinue语句
For i in "CDX":
if i = = c:
Countinue
Print (i)

>>> "DX"


2. A powerful and convenient range function that is often used in conjunction with a For loop!

1.range function:

The range function automatically generates a sequence of numbers, for example, Range (1,5,2) that generates a sequence of 1 and 3. Of these, 1, 5 indicates the generation of 1,2,3,4, why not 1,2,3,4,5, because the range in parentheses is left closed to the right open interval. As for 2, it is the step length, the previous article also talked about the steps, no longer repeat.

2. With for use

 for  in range (5,9):    print(i)>>>          5        6        7        8        

Note that the step size or the interval can be negative, too.

For I in range ( -10, -100, -30):    print (i) >>>-10   -40   -70

3.pass statements

    Look at the name to know, empty, do nothing, is used to occupy the seat. See example.

 for  letter in   " redmolly   " :  if  letter = =  " l   " :    pass  print  ( "  current letter:  "   print  ( " good bye!   " )  >>>redmoy good bye!  

4. Advanced usage in traversal

1. Built-in enumerate function to traverse, can return the sequence index value plus corresponding sequence value, use see below you understand.

Enumerate function Format:

 for inch Enumerate (sequence):p rocess (index, item)

Enumerate function Traversal instance:

sequence = [[j] , A, V, A, a, a, a, a, a, "for in Enumerate (sequence):  print(i , j)
>>>0
123445
56

2.for Loop nesting use (Do you understand?) Is that the outer loop executes once, the inner loop executes all, and then goes into the outer loop for the second time, and the inner loop is all executed. The following program is classic, outer loop control line, Inner Loop control column).

It is estimated that some students will ask the program in the end = "" What is meant, Python output function print output after the end of the Add line character, print a sentence will be line break. If you pass an empty string at the end With end = "" ("" "in the middle of the space), the print function does not add a newline character at the end of the string, but instead adds an empty string (the following is a space in the program).

 for  in range (1,6):   for in range (1, i+1):       print("  *", end="   )print('\ r ' )>>>***************

PS (Bo Master Cold today, continue tomorrow more, will be these knowledge points to summarize and string up to review, refueling more complete Python stack, Bo Master is a computer professional, there are other professional want to exchange words can be private bloggers)

        

        

Python_ Practical Introductory Article _10

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.