Python Learning Notes (Ⅱ)--loops/selections/functions

Source: Internet
Author: User
Tags builtin

I. Cyclic structure

In Python, there are two operations for the For loop and while loop, with no do......while statements.

1.for Loop:

Unlike the common notation for loops in other languages such as for (int i=0;i<10;i++), the for loop in Python is more similar to the foreach structure:

# #举例: Traverse all letters that output a single word
For letter in'Python':
  Print(' current letter:'+ letters ')

Contrast:

string " Python " ; foreach (charin  str) {    Console.WriteLine (" current Letter:"+C) ;       }

We sometimes involve operations or judgments on an array, a list, or a dictionary index or subscript, and in Python we provide a for-I in range (start index, end index) operation:

 #   #举例: Outputs the first 3 letters of a list  list = [ " c#  " , Span style= "COLOR: #800000" > " java  " ,  " python   ", "  c   ", "  c++   " "  for  I in  Range (0,2) #      #注意索引下表是从0开始, this notation is equivalent to the for (int i=0;i<=2;i++)  print  (List[i]) 

In Python3, we can even make range start with a specified number and specify different increments (even negative numbers, sometimes referred to as ' stride '):

For example: For i in range (0, 3) : print(i ), this notation is equivalent to the for (int i=0;i<=10;i=i+3)

< Span class= "Hl-number" >2.while loop:

The basic syntax is as follows:

 while Judging Condition:    Loop statement

When judging the condition as a perpetual condition, while is an infinite loop, it is more common to handle real-time requests or other looping operations:

 while 1==1:    ##循环语句

Use Crtl+c to jump directly out of the loop

3. Use break with continue for loop control:

 var = while  var > 0:  print  ( '   "  , var) var  = Var-1 if  var = = 5:  break
      #   #break直接跳出整个循环  print  ("  good bye!   ") 
var = ten while                   var > 0:                 = var-1   if var = = 5:             #  continue to skip this Loop's remaining code, jump directly into the next loop      continue   print (' current variable value:')  , var)print ("good bye! ")

Python Learning Notes (Ⅱ)--loops/selections/functions

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.