Introduction to Programming Python Lab 6: Relational Operators and Boolean Variables 12 February 2016 1 PythonLab6 lecture slides.ppt Ping Brennan

Slides:



Advertisements
Similar presentations
Introduction to Programming Java Lab 1: My First Program 11 January JavaLab1.ppt Ping Brennan
Advertisements

Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming
Windows Basics An Introduction to the Windows Operating System.
 Use the Left and Right arrow keys or the Page Up and Page Down keys to move between the pages. You can also click on the pages to move forward.  To.
WebDFS Budget Amendment and Personnel Processing.
Recitation 1 Programming for Engineers in Python.
Fundamentals of Python: From First Programs Through Data Structures
Mastering Your Word Processing Skills
Fundamentals of Python: First Programs
1 iSee Player Tutorial Using the Forest Biomass Accumulation Model as an Example ( Tutorial Developed by: (
30S Applied Math Mr. Knight – Killarney School Slide 1 Unit: Linear Programming Lesson 3: Solving Systems Using Graphmatica Solving Systems Using Graphmatica.
COPYRIGHT © 2008 – APEX SOFTWARE LTD. ALL RIGHTS RESERVED Human Resources COPYRIGHT © 2008 – APEX SOFTWARE LTD. ALL RIGHTS RESERVED Filter Functions Keyboard.
Introduction to Digital Works. The Digital Works Window.
CPSC 171 Introduction to Computer Science Boolean Logic, Gates, & Circuits.
 We are going to learn about programming in general…How to think logically and problem solve. The programming language we will use is Python. This is.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Learning How to Make a PowerPoint Presentation A tutorial for novice users.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
Lesson 01: Introduction to Database Software. At the end of this lesson, students should be able to: State the usage of database software. Start a database.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 1: My First Program 8 January PythonLab1 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 5: Strings and Output 05 February PythonLab5 lecture slides.ppt Ping Brennan
Introduction to Programming
Introduction to Programming Python Lab 2: Variables 15 January PythonLab2 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Computer Science Up Down Controls, Decisions and Random Numbers.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Introduction to Programming
Introduction to Programming
Development Environment
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
CSCI N207 Data Analysis Using Spreadsheet
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Hardware is… Software is…
Presentation transcript:

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