8.1. if statement

The if statement for Python will seem amazingly familiar; it is made up of three main components: the keyword itself, an expression which is tested for its truth value, and a code suite to execute if the expression evaluates to non-zero or true. The syntax for an if statement:

						if
						expression:
						expr_true_suite
					

The suite of the if clause, expr_true_suite, will be executed only if the above conditional expression results in a Boolean true value. Otherwise, execution resumes at the next statement following the suite.

8.1.1. Multiple Conditional Expressions

The Boolean operators and, or, and not can be used to provide multiple conditional expressions or perform negation of expressions in the same if statement.

							if not warn and (system_load >= 10):
    print "WARNING: losing resources"
    warn = warn + 1

8.1.2. Single Statement Suites

If the suite of an if clause consists only of a single line, it may go on the same line as the header statement:

							if (make_hard_copy == 1): send_data_to_printer()

Single line if statements such as the above are valid syntax-wise; however, although it may be convenient, it may make your code more difficult to read, so I recommend you indent the suite on the next line. Another good reason is that if you must add another line to the suite, you have to move that line down to the next anyway.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset