Introduction to Programming Python Lab 6: Relational Operators and Boolean Variables 12 February PythonLab6 lecture slides.ppt Ping Brennan
Getting Started Create a new folder in your disk space with the name PythonLab6 Launch the Python Integrated Development Environment (IDLE) - begin with the Start icon in the lower left corner of the screen. If you are in a DCSIS lab, select the options in the order shown: Start -> All Programs -> Python 3.4 -> IDLE (Python GUI) A window with the title Python Shell should appear. This window is the Shell. 2
Getting Started (2) If you are in one of the ITS labs (MAL 109 or MAL 457), select the options in the order shown: Start -> All Programs -> Departmental Software -> Computer Science -> Python 3.4 -> IDLE (Python 3.4 GUI – 64 bit) A window with the title Python 3.4.4rc1 Shell should appear. This window is the Shell. In the Shell click on File. A drop down menu will appear. Click on New File. A window with the title Python 3.4.1:Untitled (DCSIS) or Untitled (ITS) should appear. This window is the Editor. 3
Getting Started (3) In the Editor, click on File, and then in the drop down menu click on Save As…. A window showing a list of folders should appear. –To search any folder on the list, double click on the folder. –Find the folder PythonLab6 and double click on it. –In the box File name at the bottom of the window type TruthTable.py, and then click on the button Save in the lower right corner of the window. The title of the Editor should change to show the location of the file TruthTable.py. 4
Objectives of the exercises set Understand the use of relational operators (, >=, ==, != ) to compare numbers. Understand the use of the Boolean type bool which has two values, False and True. 5 Table 1 Relational Operators PythonDescription < Less than <= Less than or equal > Greater than >= Greater than or equal == Equal != Not equal Table 2 Relational Operator Examples ExpressionValue 2 < 2False 2 <= 3True 5 > 3*2False 5 >= (3-1)True 5 == 6False 5 != 6True
Objectives of the exercises set (2) Understand the use of Boolean operators ( and, or, not ) to join Boolean expressions. –The and operator gives the result True only when both Boolean expressions are True. –The or operator gives the result True if at least one of the Boolean expressions is True. Boolean Truth Tables 6 ABA and B True False TrueFalse ABA or B True FalseTrue FalseTrue False A not A TrueFalse True
Program 1: Truth Table Problem statement Write a program that takes as input from the keyboard integer values for three variables a, b, c. Obtain from a, b, c the values of three corresponding variables p, q, r of type bool. To obtain these values use statements of the form. p = (a != 0) The right hand side of the above statement has the value True or False, depending on the value of a. Print out the value of the Boolean valued expression (p and q) or (not r) For example, if a = 3, b = -5, and c = 10, then The truth table for the above expression displays the value of the expression for each choice of values for p, q and r. It follows that truth table has eight rows. Add code to your program to write out all eight rows of the truth table. 7 Expression (a!=0)(b!=0)(c!=0)(p and q)r(not r)(p and q) or (not r) ValueTrue FalseTrue
Program 1: Truth Table (2) Problem statement (continued) It is not necessary to obtain any further input from the key board. Print out a header such as p q r (p and q) or (not r) and then focus on printing out each row correctly, for example, True True 8
Program 1: Truth Table (3) Problem solving - Convert the following pseudo code into a sequence of Python statements in your program. 1.Read in the first integer value and store it in the variable a. a = int(input("Enter the first integer")) 2.Read in the second integer value and store it in the variable b. Next, read in the third integer value and store it in the variable c. 3.Write a statement of the form below to obtain from a the value of the variable p of type bool. The Boolean expression (a != 0) yields True only when the value of a is not equal to 0. p = (a != 0) 4.Write a similar statement as above to obtain from b the value of the variable q of type bool. Next write a similar statement to obtain from c the value of the variable r of type bool. 5.Print out the value of the Boolean valued expression (p and q) or (not r) 9
Program 1: Truth Table (4) Problem solving (continued) 6.Create a variable named spaces and store seven spaces as follows: spaces = " " 7.Print out a header such as p q r (p and q) or (not r) 8.Print out the first row of the truth table for the Boolean valued expression (p and q) or (not r) as follows: p = True q = True r = True print(p, q, r, spaces, (p and q) or (not r)) 9.Write similar code as above to print out the remaining seven rows of the truth table by using a combination of values, False and True, for the variables p, q and r. 10
Program 1: Truth Table (5) Provide a comment at the beginning of the program to explain the purpose of the program together with your name and the date. Save the program to the file TruthTable.py and then run it. The truth table for the Boolean valued expression (p and q) or (not r) is as follows: 11 pqr(p and q)(not r)(p and q) or (not r) True FalseTrue FalseTrue FalseTrueFalse TrueFalse True FalseTrue False TrueFalse True False TrueFalse True
Program 2: Comparison Create a new Editor for a new file called Comparison.py Problem statement Write a program that takes as input from the keyboard integer values for three variables x, y, z. Construct a single Boolean expression that has the value True if exactly two of the variables x, y, z have the same value and that has the value False otherwise. Test your program using a range of inputs. Hint. Experiment with Boolean expressions which solve parts of the problem, and then join these Boolean expressions together using and or or as appropriate. For example the Boolean valued expression x == y has the value True if x has the same value as y, otherwise it has the value False. 12
Program 2: Comparison (2) Problem solving - Convert the following pseudo code into a sequence of Python statements in your program. 1.Read in the first integer and store it in the variable x x = int(input("Enter the first integer: ")) 2.Read in the second integer and store it in the variable y. 3.Read in the third integer and store it in the variable z. 4.Use the below Boolean expressions to solve parts of the problem: ((x == y) and (x != z)) ((x == z) and (y != z)) ((y == z) and (y != x)) Next use the Boolean operator or to join all three Boolean expressions into a single expression. Then print out the value of this Boolean valued expression. 13
Program 2: Comparison (3) Provide a comment at the beginning of the program to explain the purpose of the program together with your name and the date. Save the program to the file Comparison.py and then run it. For example, suppose x = 1 y = 1 z = 3 Then the Boolean expression (x == y) and (x != z) yields a True value 14
Appendix A Examples on using the Boolean operators: and, or, not Example 1 to show the use of the Boolean operator and A = (5 < 10) # yields True B = (10 > 3) # yields True result = (A and B) # yields True Example 2 to show the use of the Boolean operator or A = (5 < 10) # yields True B = (10 < 3) # yields False result = (A or B) # yields True Example 3 to show the use of the Boolean operator not A = (5 < 10) # yields True result = not A # yields False 15