Applications of whlie and for in Python loop statements

Source: Internet
Author: User

Python loop statements are a skill that many programmers must master. Although it is a bit old, it still needs to be consolidated. Dict simulates switch Selection in a more elegant way, aggregates lambda functions, and further implements more complex logical branch statements. For how to use lambda functions, go to the next chapter.

Python loop statements

Python loop statements support two types of loop statements: while LOOP and for loop. do-while loop in C # is not supported. The while loop of Python is basically the same as that of C #. Here we compare the differences between the for loop in the two languages.

To put it simply, the for statement in the Python loop statement is equivalent to the foreach statement in C #. It is often used to traverse data from the collection Object list, str, tuple, and so on. For example:

 
 
  1. for i in [1,2,3,4,5]:  
  2. print i 


This is basically the same as the foreach syntax in C #. below is the corresponding code in C:

 
 
  1. IEnumerable<int> numbers = Enumerable.Range(0, 5);  
  2. foreach( int i in numbers)  
  3. Console.WriteLine(i);  

How to implement a for loop similar to for (int I = 0; I <10; I ++) in C? The answer is to use a range or xrange object. See the following code:

 
 
  1. # Range (10) can also be replaced by xrange (10 ).
  2. For I in range (10 ):
  3. Print I # is equivalent to the following C # statement
  4. # For (int I = 0; I <10; I ++)
  5. # Console. WriteLine (I );

The built-in function range ([I,] j [, stride]) creates an integer list with the content k (I <= k <j ). The first parameter I and the third parameter stride are optional. The default values are 0 and 1, respectively. The built-in function xrange ([I,] j [, stride]) is similar to range, but xrange returns an unchangeable XRangeType object. This is an iterator, that is, the provided value is calculated only when that number is used. When the j value is large, xrange can more effectively use the memory.

  • How to bind a C ++ program to Python
  • Main features of Python objects
  • Summary of Python experience
  • Python scripts solve difficulties in Game Development
  • Adaptation and processing of related files in Python

The while and for loops in Python loop statements support the break and continue statements. The break statement is used to stop the loop immediately. The continue statement is used to directly enter the next loop (ignore the remaining statements of the current loop ). The break and continue statements have the same usage in C # And Python and are only used for the current loop where the statement is located. If you need to exit a multi-loop, you should use an exception because the goto statement is not provided in Python.

Finally, the loop in Python also supports the else statement, which only runs for and while Loops After the loop is completed normally), or runs immediately when the loop condition is not met (only the while LOOP ), or, if the iteration sequence is empty, execute immediately (for loop only ). If the Python loop statement exits using the break statement, the else statement is ignored.

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.