Looping statements in the 20:python

Source: Internet
Author: User

20.1 whileStatement

Problem Description:

please 5!.

Tips:

ask for factorial of 5, i.e. 5*4*3*2*1

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/9C/5C/wKiom1lvUNHC8dTIAABy6JPa2gg856.png-wh_500x0-wm_ 3-wmp_4-s_1210640152.png "title=" Untitled. png "alt=" wkiom1lvunhc8dtiaaby6jpa2gg856.png-wh_50 "/>

I always feel not intuitive, I think the final print is the shape of: 5 ! = 5*4*3*2*1 =

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/9C/5C/wKioL1lvUO6DFuClAACuGokjfcE367.png-wh_500x0-wm_ 3-wmp_4-s_333708214.png "title=" Untitled. png "alt=" wkiol1lvuo6dfuclaacugokjfce367.png-wh_50 "/>

20.1.1 Printhow to get rid of line wrapping

How do I get rid of line breaks?

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/9C/5C/wKioL1lvUQrgomW4AACLDwsZRrE289.png-wh_500x0-wm_ 3-wmp_4-s_2281444994.png "title=" Untitled. png "alt=" wkiol1lvuqrgomw4aacldwszrre289.png-wh_50 "/>

remove the newline, but the middle clip has a space, how to remove the space?

20.1.2 PrintHow to remove spaces

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/9C/5C/wKiom1lvUS2jzZi1AACgTe-VeEg688.png-wh_500x0-wm_ 3-wmp_4-s_2581131767.png "title=" Untitled. png "alt=" wkiom1lvus2jzzi1aacgte-veeg688.png-wh_50 "/>

Personal Exercise: How to implement a number from the keyboard input (need to judge more than 0 ) to calculate the factorial of it.

20.2 forStatement

Problem Description:

write the program to put the string ' I love python ' print out as a single character.

The effect is as follows:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/9C/5C/wKioL1lvUUrQE6lEAAAH2uCUZqI726.png-wh_500x0-wm_ 3-wmp_4-s_1503980171.png "title=" Untitled. png "alt=" wkiol1lvuurqe6leaaah2ucuzqi726.png-wh_50 "/>

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/9C/5C/wKiom1lvUWDCZM8sAAEXRJMHqXc227.png-wh_500x0-wm_ 3-wmp_4-s_3741617949.png "title=" Untitled. png "alt=" wkiom1lvuwdczm8saaexrjmhqxc227.png-wh_50 "/>

where the I of line 4 represents the loop variable,for,in, followed by the colon are fixed format, and ' I love Python ' is a string.

line 5, print I, a comma means no wrapping, and the loop variable i automatically accumulates.

Personal Exercise: How to achieve the delay effect

20.2.1 forLooping Format

pythonfor loops can traverse any sequence of items, such as a list or a string.

Grammar:

for The syntax format for the loop is as follows:

for I in sequence Span style= "font-size:13px;font-family: ' Courier New '; color: #666600; border:none 1px;padding:0;" >:

Statements (s)

Flow chart:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/9C/5C/wKiom1lvUZDyvhQjAADO6BbNX4k411.png-wh_500x0-wm_ 3-wmp_4-s_2610503591.png "title=" Untitled. png "alt=" wkiom1lvuzdyvhqjaado6bbnx4k411.png-wh_50 "/>

20.3 Rangefunction

The Range function is often used in conjunction with a looping statement.

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/9C/5C/wKiom1lvUa_QvHJ4AAAXELawgeg354.png-wh_500x0-wm_ 3-wmp_4-s_4215724813.png "title=" Untitled. png "alt=" wkiom1lvua_qvhj4aaaxelawgeg354.png-wh_50 "/>

Problem Description:

we want to realize 1+2+3+4+5+ ... +100 and, how is Python code implemented?

Tips:

we know 1+2+3+4+5+ ... +100=5050

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/9C/5C/wKioL1lvUcaDyL9iAAB4zLwUfII691.png-wh_500x0-wm_ 3-wmp_4-s_2176519339.png "title=" Untitled. png "alt=" wkiol1lvucadyl9iaab4zlwufii691.png-wh_50 "/>

Range ( n,m ) function does not include the right boundary. So, there is no addition .

4950+100=5050.

Section 4 Line, s=0 , if not, the compilation will error, indicating 6 Line s not defined. "Variables must be defined before they are used"

individual try to remove line 4

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/9C/5C/wKiom1lvUduyO0hDAABBQRy-0XE584.png-wh_500x0-wm_ 3-wmp_4-s_2621126952.png "title=" Untitled. png "alt=" wkiom1lvuduyo0hdaabbqry-0xe584.png-wh_50 "/>

20.3.1 Rangefunction Considerations

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/9C/5C/wKioL1lvUfaAWm8WAABN_NRyfLU078.png-wh_500x0-wm_ 3-wmp_4-s_3389185087.png "title=" Untitled. png "alt=" wkiol1lvufaawm8waabn_nryflu078.png-wh_50 "/>

20.4 Enumeratefunction

The for statement implements two loop variables

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/9C/5C/wKioL1lvUhCiaEnhAAAloW1P_B0883.png-wh_500x0-wm_ 3-wmp_4-s_1209554409.png "title=" Untitled. png "alt=" wkiol1lvuhciaenhaaalow1p_b0883.png-wh_50 "/>

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/9C/5C/wKioL1lvUiXBSl_DAAFFKUmJ75o635.png-wh_500x0-wm_ 3-wmp_4-s_1851452118.png "title=" Untitled. png "alt=" wkiol1lvuixbsl_daaffkumj75o635.png-wh_50 "/>

The list will be explained in more detail later.

Personal Practice: Print 1-9 Triangular Array :

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/9C/5C/wKioL1lvUkHgcdB9AAAZbJstmd4996.png-wh_500x0-wm_ 3-wmp_4-s_1531750607.png "title=" Untitled. png "alt=" wkiol1lvukhgcdb9aaazbjstmd4996.png-wh_50 "/>


This article is from the "13088633" blog, please be sure to keep this source http://13098633.blog.51cto.com/13088633/1949093

Looping statements in the 20:python

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.