Python branch structure (no switch structure)

Source: Internet
Author: User

#在if语句嵌套中, you need to pay special attention to the pairing of else and if. The Python compilation system handles this problem in the same principle that else is always paired with the not-so-paired if pair that is closest to him in the same syntax hierarchy
#例如:

X=10y=85if x>20 or X<-10:if y<100 or y>x:print ("good") Else:print ("Bad") Num=int (Input ("Please enter a number:")) if num%2 ==0:if Num%3==0:print ("The number you enter can be divisible by 2 and 3") Else:print ("The number you enter can be divisible by 2, but not divisible by 3") Else:if Num%3==0:print ("The number you enter can be divisible by 3, But not divisible by 2 ") Else:print (" The number you entered cannot be divisible by 2 and 3 ")

#4.3.2 Truth test;
#python与cC + + is different in how truth and logic operations are handled. In Python:
#任何非0数字和非空对象都未真.
#数字0, empty objects (such as empty list [], empty Dictionary {}), none are false;
#比较和相当测试返回true (True) or False (false).
#逻辑运算and和or, the true or False object that participates in the operation is returned.
#回顾: Logical operation: Not x:x is not true or FALSE, False is true;
#x and Y: Double true is true; X or y:x two false;
#1. Compare and equal tests, always return TRUE or false.
Print (2<5)

Print (2>5)

Print (2==5)
#2 not operation, the not operation returns TRUE or FALSE. For example:
Print (not True,not False)

Print (not 0,not 1,not 2)

Print (not ' abc ', not [1,2],not{' a ': ') #非空对象为真;

Print (not ', not[],not{}) #空的对象为假

The and and or operators in #3. And and Or,python always return the object that participates in the operation, not true and false. Python always calculates the and operation in a left-to-right order.
#在找到第一个计算为假的对象, the object is returned, and the calculation ends even if there are objects on the right that need to be evaluated. This method of calculation is called short-circuit calculation.
Print (2 and 0)

Print ([] and 2)

Print (2 and {})

Print ([]and{})

Print (2 and 5) #如果参与运算的对象都为真, returns the last object that is true.
Print (5 and 2)
#or运算同样执行短路计算, when the first true object is found, the object is returned, and the calculation ends.
Print (0 or 2)

Print (2 or [])

Print (False or 5)

Print ([]or{})
Print ({}or[])
Print (False or 5)
#3.3 If...elif Ternary expression
X=5
y=100
If X>y:
A=x
Else
A=y

Print (a)
#该if语句, the larger value in X, Y is assigned to a, and the statement can be simplified to the following if...els ternary expression.
#a =x if x>y else y
#python还支持从列表中挑选对象, its basic format is as follows:

A=[X,Y][F]

#f为假时, assign x to a, otherwise assign Y to a. Fake before, really after
A=5
b=10
C=[A,B][A>B]
Print (c)

Python branch structure (no switch structure)

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.