Python if statement

The reason why a computer can do many automated tasks is because it can make conditional judgments on its own.
output result

  1. Note:  Indentation rules for Python code . Code with the same indentation is considered a code block, and the above 3 or 4 lines of print statements constitute a code block (but not the print on line 5). If the if statement evaluates to True, the block of code will be executed.
  2. For indentation, please strictly follow the habit of Python: 4 spaces, do not use Tab, and do not mix Tab and spaces , otherwise it is easy to cause grammatical errors caused by indentation.
  3. Note: An if statement is followed by an expression and then a : to indicate the start of a block of code.
  4. If you are typing code in a Python interactive environment , pay special attention to indentation , and you need to type an extra line of carriage return to exit the indentation:

 


When the if statement evaluates the expression to be True, the block of code contained in the if is executed:

Using the if...else... statement, we can execute the if code block or the else code block respectively according to the value of the conditional expression being True or False.
Note: there is a ":" after the else
To avoid if...else... in nested structures, we can use if... multiple elif...else... structures and write all the rules at once :
Special Note: This series of conditional judgments will be judged from top to bottom. If a judgment is True, the corresponding code block will be executed, and the subsequent conditional judgments will be ignored and will not be executed .
 
For more details, please see  http://note.youdao.com/noteshare?id=ddd78caf8d8d195e4c66d24a4fb5f862&sub=EDA7F9C87FF7454DA80DA9F046EE7975

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023534&siteId=291194637