Python Foundation 6-Process Control if condition statement

Source: Internet
Author: User

A python conditional statement determines which block of code is executed by judging the execution result of one or more conditional statements.
The IF statement in Python is used for control program execution in the basic form:
If judging condition:
Execute statement ...
Else
Execute statement ...

#coding =utf8
A = 2
If a > 0:
print ' A greater than 0 '
Else
print ' A is less than 0 '

The judgment condition of the IF statement can be expressed by > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than or equal).
When judging the condition for multiple values Yes, you can use if: Elif Extended form:
If judgment condition 1:
Execute Statement 1 ...
Elif Judgment Condition 2:
Execute Statement 2 ...
Elif Judgment Condition 3:
Execute Statement 3 ...
Else
Execute Statement 4 ...

#coding =utf8
name = ' C5 '

If name = = ' B6 ':
print ' This is B6 '
elif name = = ' A7 ':
print ' This is A7 '
elif name = = ' Q5 ' or name = = ' C5 ':
print ' This is Q5 '
Else
print ' This was nothing '

Note: Since Python does not support switch statements, multiple conditions can only be determined by using elif, or (or) may be used to determine if multiple conditions need to be judged at the same time, indicating that two conditions are successful when a condition is established; when using and (with), Indicates that only two conditions have been established and the conditions have been successfully

Python Foundation 6-Process Control if condition statement

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.