Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Reference ManualContentsIndex
Previous:5.9 ComparisonsUp:5. ExpressionsNext:5.11 Lambdas

 
5.10 Boolean operations

 

Boolean operations have the lowest priority of all Python operations:

expression ::= or_test |lambda_form
or_test ::= and_test |or_test "or"and_test
and_test ::= not_test |and_test "and"not_test
not_test ::= comparison | "not"not_test
lambda_form ::= "lambda" [parameter_list]:expression
Download entire grammar as text.

In the context of Boolean operations, and also when expressions areused by control flow statements, the following values are interpretedas false:None, numeric zero of all types, empty sequences(strings, tuples and lists), and empty mappings (dictionaries). Allother values are interpreted as true.

The operatornot yields1 if its argument is false,0 otherwise. 

The expressionx andy first evaluatesx; ifx is false, its value is returned; otherwise,y isevaluated and the resulting value is returned. 

The expressionx ory first evaluatesx; ifx is true, its value is returned; otherwise,y isevaluated and the resulting value is returned. 

(Note that neitherand noror restrict the valueand type they return to0 and1, but rather return thelast evaluated argument.This is sometimes useful, e.g., ifs is a string that should bereplaced by a default value if it is empty, the expressions or 'foo' yields the desired value. Becausenot has toinvent a value anyway, it does not bother to return a value of thesame type as its argument, so e.g.,not 'foo' yields0,not''.)


Previous PageUp One LevelNext PagePython Reference ManualContentsIndex
Previous:5.9 ComparisonsUp:5. ExpressionsNext:5.11 Lambdas
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp