Python ''control flow'', python Control Flow

Source: Internet
Author: User

Python ''control flow'', python Control Flow
I. if statement

Format:

i1 = 3if i1 > 4:    print('yes you are right')elif 0 < i1 < 4:    print('im dont konw')else:    print('no you are wrong')

Note the (:) symbol after if, elif, and else. We use it to tell Python to follow a statement block.

Ii. while statement
Number = 23 running = Truewhile running: guess = int (input ('enter an integer: ') if guess = number: print ('Congratulations, you guessed it ') running = False elif guess <number: print ('No, it is a little lower than that ') else: print ('No, it is a little higher than that') else: print ('the while loop is over') print ('done ')

Output result:

Enter an integer: 22
No, it is a little lower than that
Enter an integer: 24
No, it is a little higher than that
Enter an integer: 23
Congratulations, you guessed it
The while loop is over
Done

Note: In Python2.x, raw_input is used, and in Python3.x, input is used. And it must be followed by a colon.

3. for Loop

Format: for... in

For I in range (): print (I) else: print ('the loop is over') Result: C: \ Python36 \ python.exe C: /Users/CAI Rui/7. py1234the loop is overProcess finished with exit code 0

Note: range (1, 5) Only outputs 1-4 but not 5. Also, the else part is optional. If else is included, it is always executed once after the for loop ends, unless a break statement is encountered.

The for loop is recursive within this range, which is equivalent to assigning each number (or object) in the sequence to I, one at a time, and then executing the program block with each I value.

 

Programming is fun

When the work is done

If you wanna make your work also fun:

Use Python

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.