Python Process Control If/for/while

Source: Internet
Author: User

There are no switch statements in Python

If statement

If condition:

Do STH

Elif Condition:

Do STH

Else

Do STH

The while statement has an optional ELSE clause

While condition:

Do STH

Else

Do STH

For loop

For I in range (1, 5): # that is sequence [1, 2, 3, 4]

Print I

Else

print ' The For loop was over '

Break statement

If you terminate from a for or while loop, any corresponding loop else blocks will not execute.

Try: Except statement handling exception

Import Sys

Try

s = raw_input (' Enter something--")

Except Eoferror:

print ' \nwhy did you do a EOF on me? '

Sys.exit () # Exit the program

Except

print ' \nsome error/exception occurred. '

# Here, we aren't exiting the program

print ' Done '

An exception can be thrown using the raise statement, which is equivalent to a throw in Java

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.