Python03 if, while, for

Source: Internet
Author: User



    • If

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823215821168-1032966523.png "/>

Satisfies if the condition on print (' Bingo '), does not satisfy then skips;

Meet the conditions of the elif ..., not satisfied on the skip;

If the IF and elif conditions are not met, execute the code under else.

The code in the figure should note that int (input) here, for want to write a number, the use of int to escape to the number format, otherwise the default input is a string, the execution of the code will be error.


650) this.width=650; "Src=" http://imglf1.nosdn.127.net/img/ t0ryaxfmk2ztofjnz0w1sjjqa1dqr3vfztn6bwrvdukvs1q3nelgl2jfrxdkm1jwmewwee93pt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>
Satisfy else's execution results

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823215957371-1886522511.png "/>

Meet the Elif execution results

650) this.width=650; "Src=" http://imglf0.nosdn.127.net/img/ t0ryaxfmk2ztofjnz0w1sjjqa1dqtmjid3bcsxhebjhdwjyrdmv3m3pdbhhuckvuwja2cvn3pt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>
Satisfies the If execution result

    • While

1. Infinite loops

650) this.width=650; "Src=" http://imglf1.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rswp2ymg0ndnwathfk0hmcdnyvvhtudg2wctjvjnqv2h3pt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>

When always true, which is always true, the Count +=1 (equivalent to count = Count + 1) is executed, and each additional 1 is assigned to count and printed out.

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823220049089-697312642.png "/>
Just for a moment the number is added to 490964, here can only force stop the loop, otherwise it will continue to be added.

2.break

650) this.width=650; "Src=" http://imglf1.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rqwz1a3j6dgmzczv1wulsslzgdgrwcxzkdk9ltwqyn2xnpt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>

If condition is set to print, then break will jump out while loop;

Otherwise it will always be the while loop

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823220232933-1678441346.png "/>

3. Set the number of attempts to limit

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823220349855-1937904388.png "/>

We count by a variable count, with an initial value of 0.

Once, the Count +=1 is executed once, and when Count equals 3 o'clock (the limit is 3), the while loop is ended by a break.

4.while Condition Modification

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823220501371-1005428898.png "/>
Delete the If Count = = 3 in Heading 3, which is defined directly in the while, executes the while statement when Count<3, which reduces and optimizes the code and executes the same result.

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823220637839-981835534.png "/>

5.while and else, selective exit loops

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823220716168-1271469910.png "/>

When the condition of while is true, run the sub-code under the while;

If the while condition is not true, run the code at the bottom else.

If break does not execute any of the following code, including else.

At the same time the code also set up a selective exit statement, if Count ==3, use Y and N to choose whether to continue the loop, as long as the input is not n, continue the loop, continue to count 0, so that while the loop condition continues to be able to continue the loop.

If n is selected, because count is already equal to 3, and the while condition is not met, the following else code is executed.

650) this.width=650; "Src=" http://imglf0.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rr2xrd2p1t1dbahazq0tnz3barutxyitjuhqyb21zz01bpt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>

You can see that after selecting Y, you can continue to guess, and select N to execute the following else code;

    • For loop

1. Circular Digital Range

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823220751449-1898177653.png "/>
650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823220818902-1674260047.png "/>

For loop, the value of the current loop is assigned to I once per loop.

Range is the meaning of the scope, according to 10 in parentheses, is 0 to 9, starting from 0, altogether 10 numbers.

I is a name, in will assign a value of 0 to 9 loops to I

Print prints the contents of each poll until the polling ends.

2.for Alternative while

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823220947730-727353392.png "/>

For loops up to 3 times, if the for loop completes, the else code is executed;

If you do not complete the for loop, then break, the else code is not executed below.

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823221015402-787029938.png "/>

3. Step size

650) this.width=650; "Src=" http://imglf0.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rrlkwttvuyitesm41tgxsuvzrtnlyszdmtvfyy29luk1npt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>

The 0,10 represents a range of numbers from 0 to 10, and then loops over the range;

2 represents the step size, which is the loop once every 2 digits.
650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823221131418-669793302.png "/>
The execution result is once every 2 cycles, no loop to 10, because the last number does not loop, that is, the actual number range is to 9, does not meet the step 2 cycle, so eventually only loop to 8.

Breakpoint Test for 4.pycharm

650) this.width=650; "Src=" "/>
You can see the right side of the line number, and the left side of the code is empty by default, without any markup.

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823221213902-1872082038.png "/>
Click on the right side of each row by using the left mouse button, and a red dot will appear when clicked.

650) this.width=650; "Src=" http://imglf1.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rr2crq05lwkpvvvc2b0xuoulungrywm9jk2hrwwxmwuj3pt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>
Locate the debug icon in the toolbar and click (Debug is also found in the right mouse button of the current file)

650) this.width=650; "Src=" http://imglf1.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rtuffnfe0c014rkdia1v5rllpmwd1wlhswkfluexqsmpnpt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>
Click Debug, the place where the breakpoint is marked, the color will change, you can see in addition to 121 lines, the other rows are red, 121 lines are different colors, is the current code card in 121 lines, the line code has not been executed.

650) this.width=650; "Src=" http://imglf1.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rqnb6cnbvwc9ay1pkqkp2vw1yrwpxb3l5nitvevdzr25bpt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>
After clicking Debug, the window will pop up below the Pycharm tool

Note the circled part of the figure, one is the arrow, the other is debugger, we are currently in the debugger window

Click the arrow to execute the current line of code (121 lines)

650) this.width=650; "Src=" http://imglf1.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rrjnwa04rtgjvzmoxwitlbwxasurcy0vpz1e1z0pbaefnpt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>

Click on the arrow can see 121 lines, the code to the right of a number of information, i:0 represents the current I equals 0 meaning.

Then the 122th line of code changes color again, indicating that the current code card is in the second line, not yet executed.

So the debug feature is very powerful and can be used to see how our code executes every step of the way, in order to determine whether or not our code executes correctly; Debug is a very useful troubleshooting feature.

650) this.width=650; "Src=" http://imglf1.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rset5dfloy1hzu25crexavdvusfrxsll2edbfn2xuazrrpt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>
From the debugger window below the Pycharm, you can see some information after the first step, and you can see I={INT}0, which indicates that the current I equals 0,i is in int format.

650) this.width=650; "Src=" http://imglf0.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rtulkb0w2exb4zc9zoxvfu0vnbwtqvzarl1pesljqt0j3pt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>
Switch to the console window and then continue to click on the arrows to see the results of each step of the code execution, so you can determine if each step of the code is correct.

5.continue

650) this.width=650; "Src=" http://imglf0.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rslu5atzfcgc4enjttxvmuuhhzzvwyzy5ru1xnmp4tnnbpt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>

The continue is used to jump out of the loop and execute the next loop, meaning that the following code will no longer execute after the continue is executed, and the next loop is executed according to the loop.
650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823221333605-708951806.png "/>
When setting the IF condition <3, I and test are printed once, so when I equals 3 o'clock, if condition is not satisfied, then the continue in else will be executed, and the next loop will not print the value of I and will not print test.

6. Nesting loops

650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823221412777-2048163656.png "/>
650) this.width=650; "Src=" http://images2017.cnblogs.com/blog/1220935/201708/ 1220935-20170823221510574-625205787.png "/>

There will be 10 small loops in each cycle.

10*10, it's been circulating 100 times.

650) this.width=650; "Src=" http://imglf0.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rt3lpl1vqbktktwtwvhr1q01qdmtasdjcrgpbbdv4tw53pt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>

Set the condition of the inner loop stop cycle, and stop the loop when it is greater than or equal to 3 o'clock.
650) this.width=650; "Width=" 149 "height=" 269 "src=" http://imglf1.nosdn.127.net/img/ t0ryaxfmk2ztofnvbm1tczrsve0rszjqmlvjbe14r1jjcm1wc29kv1dxcctly3g4qvnlnwdbpt0.png?=imageview&thumbnail=500x0 &quality=96&stripmeta=0&type=jpg%7cwatermark&type=2 "/>


Python03 if, while, for

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.