Modular arithmetic the modulus operator模塊化算術模運算符 <<
Previous Next >> Checking for equality and comparators in general檢查是否相等以及一般的比較器
Conditionals有條件的
When a computer (or a program) needs to decide something, it checks whether some condition is satisfied, which is where the term conditional comes from. Conditionals are a fancy way of saying “if statements”. If Michele was born in New York, she has an American passport. That statement is a conditional (if statement) that in this case is true. In Python this works the same way:
if age > 17:
print("can see a rated R movie")
elif age < 17 and age > 12:
print("can see a rated PG-13 movie")
else:
print("can only see rated PG movies")
When the program gets to the if statement, it will check the value of the variable called age against all of the conditions, in order, and will print something to the screen accordingly. Note that elif is a portmanteau of “else” and “if”. So if the variable age holds the value 15, the statement "can see a rated PG-13 movie" will be printed to the screen.
Note how the statement elif age < 17 and age > 12 has the statement and - you can use or and not in the same way. Understanding a bit about logic and how it works, or being able to rationally think about logic will help you get the conditions right - oh, and a lot of practice.
當計算機(或程序)需要做出決定時,它會檢查是否滿足某些條件,這就是條件一詞的來源。條件語句是說“ if語句”的一種奇特的方式。如果米歇爾(Michele)出生於紐約,則她擁有美國護照。該語句是在這種情況下為真的條件(如果語句)。在Python中,這是相同的方式:
if age > 17:
print("can see a rated R movie")
elif age < 17 and age > 12:
print("can see a rated PG-13 movie")
else:
print("can only see rated PG movies")
當程序到達該if語句時,它將按age所有條件依次檢查被調用的變量的值,並將相應的內容打印到屏幕上。請注意,這elif是“ else”和“ if”的組合。因此,如果變量age保留值15,則該語句"can see a rated PG-13 movie"將被打印到屏幕上。
請注意該語句如何elif age < 17 and age > 12具有該語句and-您可以使用or和not以相同的方式。了解一些有關邏輯及其原理的知識,或者能夠理性地思考邏輯,將幫助您找到合適的條件-哦,還有很多練習。
有關條件的鏈接: