Intro
From the book :
All objects have an inherent Boolean true or false value.
• Any nonzero number or nonempty object is true.
• Zero numbers, empty objects, and the special object None are considered false.
• Comparisons and equality tests are applied recursively to data structures.
• Comparisons and equality tests return True or False (custom versions of 1 and 0).
• Boolean and and or operators return a true or false operand object.
• Boolean operators stop evaluating (“short circuit”) as soon as a result is known.
Ex:
temp = input( "Enter a number between 1 to 10: " )
temp1 = int( temp )
print( temp1 )
if temp1 == 1 :
print ( "Number is 1" )
elif temp1 == 2 :
print ( "Number is 2" )
elif temp1 == 3 :
print ( "Number is 3" )
else :
print ( "Number is not 1,2 or 3." )
Output:
[amittal@hills chapter12]$ python3 ex1.py
Enter a number between 1 to 10: 2
2
Number is 2
Any object can be considered as a boolean object and can resolve to true or false as the below example shows.
Ex:
temp = input( "Enter an integer: " )
temp1 = int( temp )
print( temp1 )
if temp1 :
print ( "temp1 is not zero." )
else:
print ( "temp1 is zero." )
Output:
[amittal@hills chapter12]$ python3 ex2.py
Enter an integer: 0
0
temp1 is zero.
[amittal@hills chapter12]$ python3 ex2.py
Enter an integer: 1
1
temp1 is not zero.
[amittal@hills chapter12]$
Switch Statements
Python does not have the switch statement. The same functionality can be achieved by multiple else
if statements. An example in the "C++" language.
Ex:
#include <iostream>
using namespace std ;
/*
Run your program for the following inputs to make sure
it is working properly .
a
b
c
C
d
*/
int main()
{
char ch ;
cout << "Enter a character:" ;
cin >> ch ;
if ( ch == 'a' )
cout << "Small a." << endl ;
else if ( ch == 'b' )
cout << "Small b." << endl ;
else if ( ch == 'c' || ch == 'C' )
cout << "Small c or large C." << endl ;
else
cout << "Default." << endl ;
//To do write a switch statement that is logically equal to the
//above if statement
switch ( ch )
{
case 'a' :
cout << "Small a." << endl ;
break ;
case 'b' :
cout << "Small b." << endl ;
break ;
case 'C' :
case 'c' :
cout << "Small c or large C." << endl ;
break ;
default :
cout << "Default" << endl ;
break ;
}//
}
Output:
[amittal@hills chapter12]$ ./a.out
Enter a character:c
Small c or large C.
Small c or large C.
[amittal@hills chapter12]$
Let us understand what the above is saying . It is saying that if we have a character input then for
different characters 'a' , 'b' and 'c' we get different outputs. We can code the above using Python .
Ex:
ch = input( "Enter a character:" )
if ( ch == 'a' ) :
print("Small a.")
elif ( ch == 'b' ) :
print( "Small b." )
elif ( ch == 'c' ) :
print( "Small c " ) ;
else :
print ( "Default." ) ;
print( "---------------" )
dict1 = {}
dict1["a"] = "Small a."
dict1["b"] = "Small b."
dict1["c"] = "Small c."
print( dict1.get( ch, "Default" ) )
Output:
[amittal@hills chapter12]$ python3 switch.py
Enter a character:r
Default.
---------------
Default
[amittal@hills chapter12]$ python3 switch.py
Enter a character:a
Small a.
---------------
Small a.
[amittal@hills chapter12]$
Boolean Expressions
We can have boolean expressions such as:
b1 and b2
This is true if both b1 and b2 are true
b1 or b2
This is true if either b1 or b2 is true
not b1
This is true if b1 is false.
If b1 or b2 is an expression that return true or false then that value will be returned otherwise the object b1 or b2 will be returned.
>>> 3 < 5 and 1 < 4
True
>>> x1 = 0
>>> y1 = 5
>>> x1 or y1
5
>>> x1 and y1
0
>>>
Ex:
x1 = 0
y1 = 5
if x1 or y1 :
print( "True" )
else :
print( "False" )
if y1 and x1 :
print( "True" )
else :
print( "False" )
Output:
[amittal@hills chapter12]$ python3 expr1.py
True
False
[amittal@hills chapter12]$
Ternary Expressions
A = Y if X else Z
If X is true then A is assigned Y otherwise A is assigned Z .
>>> var1 = 4 if 1 < 2 else 5
>>> var1
4
>>> var1 = 4 if 1 < 0 else 5
>>> var1
5
The same effect can also be achieved by .
var1 = ( ( X and Y) or Z )
var1 = ( ( 1<2 and 4) or 5 )
>>> var1 = ( ( 1<2 and 4) or 5 )
>>> var1
4
If "1<2" is true then the "and" will produce 4 and that will be assigned to var1 . If "1<2" is false then the 5 will be assigned to var1.
This fails if the 4 is a 0 .
>>> var1 = ( ( 1<2 and 0 ) or 5 )
>>> var1
5