Python: Looping

Source: Internet
Author: User

Loop statement: While loop, for loop

Example 1: The and of all numbers of 1-100

         n = + = 1 while count <= N: = sum + count + = 1 print ( c8> '1 to%d sum:%d' % (n,sum))

Output Result:

1 to 100 of the sum of: 5050

Example 2:for cycle

A = ['what','where',' How','  Why' " in range (len (a)):          Print(I,a[i])

Output Result:

0What 1 where2 how 3 why

Example 3:

 forLetterinch 'Runoob':        ifLetter = ='b':                 Break        #When the letter B appears, exit the loop!         Print('The current letter is:', letter) var= 10 whilevar >0:Print('the current variable value is:', Var) var= Var-1ifvar = = 5#Value is 5 o'clock, exit loop         BreakPrint('Goodbye!')

Output Result:

9876Goodbye!

Example 4:

 for inch (0,10,3)   # Press 3 to increase        from 0 to 10 Print (i)

The output is:

0369

Example 5: Finding odd even numbers

# Ask 100 inside odd number:  for  in range:    if i% 2 = = 1        :print(i)# ask for an even number within 100:  for  in range:    if i% 2 = = 0:        print(i)

Python: Looping

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.