if ( Conditional expression ):
Sentence block
# The conditional expression can be any expression , As long as the result is not 0 The idea that True, Otherwise False
# Sentence block : It could be a statement , It can also be multiple statements
# Enter two integers to store and a and b in , bring a The data stored in is less than b Data stored in .
# Use branch structure
a = int(input(' Please enter a:'))
b = int(input(' Please enter b:'))
print(' Before processing ')
print('a={},b={}'.format(a, b))
if a > b:
a, b = b, a # In exchange for a,b A variable's value
print(' After processing ')
print('a={},b={}'.format(a, b))
## Known coordinate points (x,y), Judge its quadrant
x = float(input(' Please enter x Coordinate value :'))
y = float(input(' Please enter y Coordinate value :'))
if x == 0 and y == 0:
print(' The point is at the origin ')
elif x == 0:
print(' The point is at y On the shaft ')
elif y == 0:
print(' The point is at x On the shaft ')
elif x > 0 and y > 0:
print(' First quadrant ')
elif x < 0 and y > 0:
print(' Beta Quadrant ')
elif x < 0 and y < 0:
print(' The third quadrant ')
elif x < 0 and y > 0:
print(' Quadrant four ')
# Determine if a year is a leap year
# The condition for judging leap years is : The year can be 4 Divide but not be 100 to be divisible by , Or can be 400 to be divisible by .
# Method 1: Use a multi branch structure
y = int(input(' Please enter the year :'))
if y % 4 == 0 and y % 100 != 0:
print('{} Year is a leap year '.format(y))
elif y % 400 == 0:
print('{} Year is a leap year '.format(y))
else:
print('{} Year is not a leap year '.format(y))
# Method 2: With the help of logical operators
y = int(input(' Please enter the year :'))
if (y % 4 == 0 and y % 100 != 0) or (y % 400) == 0:
print('{} Year is a leap year '.format(y))
else:
print('{} Year is not a leap year '.format(y))
if (y % 4 == 0 and y % 100 != 0) or (y % 400) == 0:
if (y % 4 == 0 and y % 100 ) or (y % 400) == 0:
if (not(y % 4) and y % 100 ) or (y % 400) == 0:
The above three conditional expressions all have the same effect , But the first one is more Simple and easy to understand
author | pythonic Biological m
In the development process , W