Page Contents (hide)
Selections are made in Python as in Java using the if
statement. Python has a boolean class to represent logical values and a complete set of logical operators.
Python provides a boolean class which defines the two values True
and False
for use with logical expressions. Python will implicitly convert other types to a boolean when necessary. The basic rules are
True
while a value of zero, both integer and floating-point, is False
.
True
if the object is nonempty and False
when they are empty.
The built-in function bool( x )
can be used to convert value x to a boolean using these standard rules.
![]() | Note:
An empty literal string ( |
Logical expressions are constructed using logical and boolean operators as they are in Java and most other langauges. The result of a logical expression is a boolean value of either True
or False
.
The relational operators available in Python include
< > <= >= == != <>
where <>
and !=
are equivalent. As in Java, parentheses can be used to override the order of precedence.
The boolean operators in Python are represented as reserved words instead of symbols. They are used to create compound logical operators.
or and not
The logical operators can be used with any of the built-in types. In general, the comparisons are performed as follows
Logical operators in Python are applied to objects and not to the values within a variable.
In Java, the operators were applied to literal and variable values. This meant that the comparison of two reference variables determined whether they were aliases. For example, consider the following Java code segment
in which the two reference variables are compared for equality. In Java, the ==
operator compares the values stored in the two variables name1
and name2
and not the strings to which those variables refer.
If the ==
operator is applied to two string variables in Python, the strings themselves are compared and not the variable references. This is the case for all Python variables since all variables are object references.
When applied to a non-primitive object, Python performs the logical operation on each data field of the object. If the result from each of those comparisons is true, the final result is true, otherwise, it is false.
To compare references themselves, that is the addresses stored in the variables, Python provides the is
operator. For example, in the code segment
result
is set to False
since the two variables point to two different objects. If we modify this segment
result
is now set to True
since the two variables refere to the same object and are thus aliases.
A null reference in Python is represented by the special object called None
. It can be used in logical expressions to test references or assigned to variables to remove references.
if
StatementPython provides the if
statement as the sole selection statement. While it works the same as in Java, the Python sytnax is somewhat different.
Consider the following example of a simple if statement in Python
Note that Python does not use parentheses to enclose the condition. The if
statement is a compound statement, however, which means it requires a statement block following the condition even if there is only a single statement to be executed. The format of the simple if
statement is shown below
if <condition>: <statement-block>
The following program provides a complete example using a Python if statement
The if
statement can contain an else part to be executed when the logical condition is false. It too is a compound statement which is indicated by the required colon following the else
statement
The format of the if-else
structure is shown below
if <condition> : <statement-block-1> else: <statement-block-2>
The following program illustrates the use of nested if
statements.
Python does not provide a switch type statement. Instead, all multiway branchings are encoded using a special if-elif-else structure.
The else
part can be omitted if there is no default part to the multiway branch. The if
, elif
and else
words should be aligned to the same indentation level as shown in the format description below
if <condition-1> : <statement-block-1> elif <condition-2> : <statement-block-2> : : elif <condition-N> : <statement-block-N> else: <statement-block-N+1>
Evaluating a single string or comparing two strings are common operations in many programs. In Java, these operations were performed using methods of the String class. In Python, they can be performed using ordinary operators.
The normal equivalency operator is also used in Python to evaluate strings.
In Java, this was done using either the equal()
or compareTo()
method of the String class as illustrated below
Likewise, the lexicographical order of two strings is determined using the appropriate logical operator instead of the compareTo()
operator as is done in Java
To determine if a string contains a substring of characters, Python provides the in
operator. In the following example,
a value of True
is returned if the string name1
contains the given substring and False
otherwise. In Java, this is done using the contains()
method of the String class
There is also a not version of the in
operator that can be used to determine if a string dose not contain a substring