Python Study Book 4th, chapter 13th while and for loops

Source: Internet
Author: User

" " Date: September 5-September 30 Requirements: 1. Summary of the book contents, finishing in the blog Park notes upload 2. After all the exercises in the course Note: "#" After the addition of the notes (42 pages per day, you can guarantee to read this book at the end of the month) "Key Notes" "chapter Exercises"-Heading 1, level two headings- Heading 2, Notes outline title, exercise title-Bold, 16px"

Summary of key notes

While statement, which provides a way to write a common loop

For statement, which iterates through the elements within a sequence object and runs a block of code on each element

A. While loop
The while loop is the most common iteration structure in the Python language. As long as the top test always evaluates to true, a block of statements is executed repeatedly.


Two. Break, continue, pass, and recycle else
Break: Jump out of the nearest loop (skip the entire loop statement)
Continue: Jumps to the beginning of the nearest loop (to the first line of the loop)
Pass: Do nothing, just empty placeholder
Loop Else BLOCK: Executes only if the loop is normally left

1. Continue
Examples are as follows:
  

while = x-1ifcontinueprint")8 6 4 2 0

2. Break
Examples are as follows:

while = input ('input your choose:')if'No  ':breakprint  (text) input your choose:oneinput your choose: your choose:inputyour choose:no

Three. For
The first line of the python for Loop defines an assignment target and the object you want to traverse. The first line is followed by the block of statements you want to repeat.
The For loop can traverse any one of the sequence objects.
To traverse a tuple:

 for inch (3,4), (5,6)): Print (A, B)1 23 45 6

Nesting for loops

>>> a = ['a','b','C']>>> B = ['b','D']>>> forIinchB: forJinchA:ifi = =J:Print(i +'found') BreakElse:Print(i +'Not found') Bfounddnot found

Four. Using the zip construction dictionary

>>> a = ['a','b','C']>>> B = [a]>>> Dict1 = {}>>> for(K,V)inchList (Zip (b)):d ict1[k] =v>>>dict1{'C': 3,'b': 2,'a': 1}

This chapter exercises:

What is the main functional difference between 1.while and for?
A: The while loop is a common loop statement, and the For loop is designed to iterate through the items in a sequence. For more efficient

2. What is the difference between break and continue?
Answer: Break jumps out of the current loop
Continue jumps back to the top of the current loop


3. When does the ELSE clause of a loop execute?
A: The ELSE clause of while and for is executed once from the loop. If there is a break statement, it immediately leaves the loop and skips the Else section


4. How to write a counter-based loop in Python?
For:

 while x: Print (x) x-=110987654321

5. How do I use range for the for loop?
For:

 for  in range (1,10,2):print(i)13579

Python Study Book 4th, chapter 13th while and for loops

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.