Python if statement, Boolean operation

Source: Internet
Author: User

One:

If statement:

Function: To allow a program to selectively execute a statement or some statement based on a condition

Grammar:

If truth expression 1:

Statement Block 1 ...

Elif Truth-expression 2:

Statement Block 2 ....

............

Else

Statement block n .....

Description

1, the truth expression will be judged from top to bottom, if one is true then the statement block is executed, and then the execution of the IF statement is ended, if all truth expressions are false. The statement in the ELSE clause is executed.

The 2,ELIF clause can have 0, 1, or more

A 3,else clause can have 0 or more

Nesting of IF statements

The IF statement itself is a compound statement made up of multiple statements

The IF statement can be nested inside another statement as a statement

Attention:

    • 1. Use a colon (:) after each condition to indicate the next block of statements to be executed after the condition is met.

    • 2, using indentation to divide the statement block, the same indentation number of statements together to form a block of statements.

    • 3. There are no switch–case statements in Python.


such as: Enter a positive integer, determine whether the number is a radix or even, print the results.

n = Int (input ("Please enter a number:") if n% 2 = = 0:print (n, "is even") Else:print (N, "is odd") run result: [[email protected] data]#./te st.py Please enter a number: 44 is even [[email protected] data]#./test.py Please enter a number: 33 is odd [[email protected] data]#

Two: conditional expression:

Syntax: expression 1 if truth expression-else expression 2

Role:

If the Boolean value of the truth expression is true, the expression 1 is executed and a reference to the resulting object is returned, otherwise the expression 2 is executed and a reference to the object is returned.

Such as:

#商场促销, over 100 minus 20.

money = Int (input ("Enter commodity amount:") pay = money-20 if >= ("Requires payment:", Pay, "meta") [[email protected] dat a]#./test.py Please enter the product amount: 200 need to pay: 180 yuan [[email protected] data]#./test.py Please enter the product amount: 100 need to pay: 80 yuan [[email protected] data]#./t est.py Please enter the product amount: 80 need to pay: 80 yuan [[email protected] data]#

Three: Pass statement:

Role:

Usually used to fill syntax blanks

num = Int (input ("Enter a number 1-4:")) if 1 <= num <= 4:passelse:print ("wrong input") ###########[[email protected] data]#. /test.py Please enter a number 1-4:3[[email protected] data]#./test.py Please enter a number 1-4:7 input error [[email protected] data]#

---------------------------------------------------------------------------------------------

Boolean operations:

Operator:

not and OR

Boolean non-operation: not

Syntax: not X

Function: Boolean to x, such as bool (x) is true to return false, otherwise true


Boolean and Operation: and

Syntax: xand Y

Note: X, y represents an expression.

Function: Returns the false worthy object in priority, returns X when the Boolean value of X is Fals, otherwise returns Y.

Motioned: True and true # returns True

True and False #返回False

False and True #返回False

Fales and False #返回False

x = Int (Input ("Enter a month:")) if 1 <= x and x <= 12:print ("Valid month") Else:print ("Illegal month") ###[[email protect Ed] data]#./test.py Please enter a month: 1 valid months [[email protected] data]#./test.py Please enter a month: 8 valid months [[email protected] data]#./test.py Please enter a month: 1 March non-law

Boolean OR operator: or

Syntax: X or Y

Function: Returns the Truth object as a priority, returns X if X is true, otherwise returns y

Motioned:

True or True # True

True or False #True

Flase or True # True

Flase or False # False

x = Int (Input ("Enter a month:")) if x < 1 or x > 12:print ("Input error") ##[[email protected] data]#./test.py Please enter a month: 13 input error [[Emai L Protected] data]#


Python if statement, Boolean operation

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.