The basic usage of the for Loop and while loop in Python, pythonwhile

Source: Internet
Author: User
Tags iterable

The basic usage of the for Loop and while loop in Python, pythonwhile

While loop:

while expression:  suite_to_repeat

While condition:
Statement Block

No parentheses!

>>> x 1.2 >>> while x < 2:  print(x)  x += 0.2    1.2 1.4 1.5999999999999999 1.7999999999999998 1.9999999999999998 >>> 

Frequently used:

while True:  ....  if ... :   break  .... 
For Loop:
for iter_var in iterable:  suite_to_repeat

For cyclically accesses all the elements of the iterable object, and iter_var is set to the current element of the iterated object. A for loop can be used for sequence types, including strings, lists, and tuples. It can also be used for iterator types. The iterator object has a next () method, and the next entry is returned after the call, after all entries are iterated, The iterator raises a StopIteration exception to tell the program that the loop ends. The for statement calls next () internally and captures exceptions.

For something in XXXX:
Statement Block

That is, execute some statement blocks for each element in XXXX. XXXX can be a list, Dictionary, tuples, iterator, and so on.

>>> for x in range(0,10):  print(x*x)    0 1 4 9 16 25 36 49 64 81 

 
This is the for... else... statement
It is executed only when there is no break, or if you do not have a break, it will execute

>>> for n in range(99,81,-1):  root = sqrt(n)  if root == int(root):   print (n)   break else:  print ("I didn't fint it")    I didn't fint it 

However, you should try to use the list derivation because it is more convenient and clear.

>>> [x*x for x in range(1,5)] [1, 4, 9, 16] >>> [x**2 for x in range(1,10) if x % 2 ==0] [4, 16, 36, 64] >>> [(x,y) for x in range(1,3) for y in range(4,6)] [(1, 4), (1, 5), (2, 4), (2, 5)] 

Break, continue, pass:
The break statement ends the current loop and jumps to the next statement. The continue statement terminates the current loop and tries the next iteration. The pass statement indicates that nothing is done.
Else in the Loop:
Else can also be used in the loop, which is different from C/C ++. The else statement is executed only after the loop is completed, that is, the break statement skips the else block.

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.