Beginner python essay--Control and looping statements

Source: Internet
Author: User

Python is similar to Java's control and looping statements with if, while, and for statements

If statement

If condition:

An expression

Elif Conditions:

An expression

Else

An expression

While statement

While judging condition:

EXECUTE statement

While judging condition:

EXECUTE statement

Else

EXECUTE statement

The While...else statement is primarily used to determine if a block of code is running, for example if the while condition is false, and the Else statement is executed if the while condition is true;

For statement

Just like Java, you don't have to specify the I type and the initialization of I

For <variable> in <sequence>: <statements>else: <statements> # Simple instance for I in range:    print (i)

Describe the range () function, which is often used

Range (Start,stop,step)

Start is the starting position, stop is the end, step is step, default is 1

>>>list (Range (0, 5)) [0, 5, ten,, 25]>>> list (range (0,, 2)) [0, 2, 4, 6, 8]>>> list ( Range (0, -10,-1)) [0,-1,-2, -3, -4, -5,-6,-7,-8, -9]>>> list (range (1, 0)) []>>>[x * x for x in range (1,10,2)] [1,9,25,49,81]

Using break and continue to control flow in loops

Break is encountered in the loop, the statement can exit the loop in advance

Encounter continue in the loop, skip the current cycle and start the next cycle directly

 

Beginner python essay--Control and looping statements

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.