III Select structure (this chapter mainly talks about the various forms of judgment statements in python. After reading this chapter, you will remove many doubts you have encountered and be more interested in python)
1. Condition description
1. Relational operation
python's relational operators are:
< (less than) > (greater than) > = (greater than or equal) < = (less than or equal) = = (equal to)= (not equal to)
i,j,k = 1,2,3 print(i,j,k) 1 2 3 print(i>j) False print(i+j == k) True
2. Logical operation
python's logical operators are:
And or not
print(not 1) False print(not "BB") False print(12 + (not True)) 12
Here are three examples:
(1) Judge whether year is a leap year
(year % 4 == 0 and year % 100 != 0) or year % 400 == 0
(2) Determine whether ch is a lowercase letter
ch >= 'a' and ch <= 'z'
(3) Judge whether m can be divided by n
m % n == 0 or m - m // n * n == 0
(4) Judge ch is neither a letter nor a number
not ((ch >= 'A' and ch <= 'Z') or (ch >= 'a' and ch <= 'z') or (ch >= '0' and ch <= '9'))
Note: (discrete mathematics is good, these are easy to understand)
(1)a and b: when a is False, the result is False regardless of the value of b;
(2)a or b: when a is True, the result is True regardless of the value of b
3. Test operation
Expressed in a few pieces of code
print(5 in (102,121,5,13)) True print(3 not in (20,11,23,3)) False a = 10 b = 10 print(a is b) True print(a is not b) False
2. Implementation of selection structure
1. Single branch selection structure
Expression: if expression:
Statement block
#Input two integers a and b, output the small value first, and then output the large value a = input("input a:") b = input("input b:") if a > b: a,b = b,a print(a,b)
2. Double branch selection structure
Expression: if expression:
Statement block 1
else:
Statement block 2
""" y = { 1 x<=0; 1/2 * x x>0 } """ x = input("Please enter x:") if x <= 0: y = 1 else: y = 1/2 * x
3. Multi branch selection structure
Expression: if expression 1:
Statement block 1
elif expression 2:
Statement block 2
...
else:
Statement block n
#Enter a student's score and score it in the scoring range according to the score student_c = input("Please enter the student's grade:") if student_c >= 90: print("A") elif 90 > student_c >= 80: print("B") elif 80 > student_c >= 70: print("C") else: print("You too low Bang")
4. Select nesting of structure
expression:
if expression 1:
if expression 2:
Statement block 1
else:
Statement block 2
#-------------------------------------------------------------------------------------------------------------------------
if expression 1:
if expression 2:
Statement block 1
else:
Statement block 2
#Enter three numbers and find the largest one x = input("input x:") y = input("input y:") z = input("input z:") max = x if z > y: if z > x: max = z else: if y > x: max = y print("Maximum:"+max)
In this chapter, it's good to understand how to use if elif else. There are so many contents. Here are two examples, which are very classic. Try to make them yourself. If you can't, you can program for Baidu:
(1) Enter an integer to determine whether it is the number of daffodils. (narcissus number refers to such 3-digit integers: the sum of the example square of each number is equal to the number itself. Example: 153 = 1 * * 3 + 5 * * 3 + 3 * * 3)
(2) Input a time (hour: minute: Second) and output the time after 10 minutes and 30 seconds.