Introduction to Programming

Slides:



Advertisements
Similar presentations
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Advertisements

1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
JavaScript, Third Edition
Introduction to C Programming
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Introduction to Python
 Pearson Education, Inc. All rights reserved Formatted Output.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
2440: 211 Interactive Web Programming Expressions & Operators.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Input, Output, and Processing
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
13 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
22 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
19 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Introduction to Programming
28 Formatted Output.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Definition of the Programming Language CPRL
Introduction to Computer Systems
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Java Variables and Types
Documentation Need to have documentation in all programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
TMF1414 Introduction to Programming
The Selection Structure
Introduction to Programming
OUTPUT STATEMENTS GC 201.
Introduction to Programming
Logical Operators & Truth Tables.
Data Types, Identifiers, and Expressions
And now for something completely different . . .
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter 8 JavaScript: Control Statements, Part 2
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Python Primer 1: Types and Operators
The Data Element.
Introduction to Programming
Introduction to Programming
Introduction to Programming
The Data Element.
Introduction to Programming
Introduction to C Programming
Presentation transcript:

Introduction to Programming Department of Computer Science and Information Systems Steve Maybank sjmaybank@dcs.bbk.ac.uk Spring 2018 Week 6: Relational Operators and Boolean Variables 16 February 2018 Birkbeck College, U. London

Revision: Strings String literals: "Hello", "World!" Length: len("Hello") # value 5 Convert a number to a string: str(5) # value "5" str(34.2) # value "34.2" String concatenation: "H"+"W" # value "HW" 16 February 2018 PFE Section 2.4.4

Revision: String Indexing The characters in a string are indexed left to right, beginning with the index 0 Individual characters are obtained using […], e.g. "Hello" [1] # value "e" "Hello" [4] # value "o" Negative indices can be used, counting from the right, -1, -2, ... "Hello" [-1] # value "o" "Hello" [-3] # value "l" Valid indices for "Hello" -5, -4, -3, -2, -1, 0, 1, 2, 3, 4 16 February 2018 PFE Section 2.4.4

Revision: Escape Sequences \" : include the character double quote in a string, e.g. "a\"b" is the string with three characters a, double quote (") and b \n : new line, e.g. print("*\n*") \\: include the character backslash in a string, e.g. "a\\b" is the string with three characters a, backslash (\) and b. len("a\"b") = len("*\n*") = len("a\\b") = 3 16 February 2018 PFE Section 2.4

Revision: Format Specifiers %5d place an integer right justified in a field of 5 characters %7.2f place a floating point number with two digits after the decimal point right justified in a field of 7 characters. The decimal point and the – sign, if present, count as characters %9s place a string right justified in a field of 9 characters print("%5d" % 56) # three spaces then 56 print("%8.2f" % -586.189) # one space then -586.19 print("%8s" % "Hello") # three spaces then "Hello" 16 February 2018 PFE Section 2.5.3

Relational Operators Python Math Notation Description >  >  Greater than  >=  ≥  Greater than or equal  <  Less than  <=  ≤  Less than or equal  ==  =  Equal  !=  ≠  Not equal 16 February 2018 PFE Section 3.2

Examples of Relational Operators  Expression  Value Comment   3 <= 4  True  3 is less than 4; <= test for “less than or equal”  3 =< 4  Error  Use <= for “less than or equal”, not =<  3 > 4  False  > is the opposite of <=  4 < 4  For True, the left-hand side must be strictly smaller than the right hand side  4 <= 4  The two sides are equal  3 == 5-2  == tests for equality  3 != 5-1  != tests for inequality. It is true that 3 is not 5-1  3 = 6/2  Use == to test for equality  1.0/3.0 == 0.333333333  The values are close but they are not exactly equal  "10" > 5  A string cannot be compared with a number 16 February 2018 PFE Section 3.2

Relational Operators and Strings name1 = "John" name2 = "John" name3 = "Smith" name1 == name2 # True name1 == name3 # False name1 != name3 # True 16 February 2018 PFE Section 3.2

Ordering of Single Characters All uppercase letters come before lowercase letters Numbers come before letters The space character comes before all printable characters Empty string comes before all non-empty characters Example "" < " " < "0" < "1" < "9" < "A" < "B" < "Z" < "a" < "b" < "z" 16 February 2018 PFE Section 3.2

Examples of Lexicographic Ordering Given strings s1, s2 find the longest string s such that s1 = s+u1 s2 = s+u2 s1  s2  u1  u2  comparison order   "catch" "cart"  "tch" "rt" "t" > "r"  s1 > s2   "coal" "coat"  "l" "t"  "l" < "t"  s1 < s2   "tone" "ton"  "e" "" "e" > ''"  s1 > s2  "pit" "pith"  "h"  "" < "h"  "pitch" "pitch" ""  "" == "" s1 == s2  16 February 2018 PFE Section 3.2

Summary of Lexicographic Ordering Given strings s1, s2, find the longest string s such that s1 = s+u1 s2 = s+u2 If u1 = u2 = "", then s1 = s2 If u1 = "" and u2 ≠ "", then s1 < s2 If u1 ≠ "" and u2 = "", then s1 > s2 If u1 ≠ "" and u2 ≠ "", then if u1[0] < u2[0] then s1 < s2 if u1[0] > u2[0] then s1 > s2 16 February 2018 PFE Section 3.2

Boolean Variables Variables of type bool have the value True or the value False, e.g. failed = False passed = True True and False are special values, not numbers or strings. True and False are reserved words 16 February 2018 PFE Section 3.7

Boolean Operators A Boolean operator takes one or more Boolean values as input and produces a Boolean value as output. Example: and input: two Boolean values True, True output: True flag = True and True # The Boolean variable flag has the value True 16 February 2018 PFE Section 3.7

Truth Tables A B A and B True True False A B A or B True True False A not A   True  False 16 February 2018 PFE Section 3.7

Boolean Operator Examples  Expression Value  Comment   0 < 200 and 200 < 100 False  Only the first condition is true   0 < 200 or 200 < 100 True  The first condition is true  0 < 200 or 100 < 200 True The or is not exclusive or  0<x and x<100 or x == -1 (0<x and x<100) or x== -1  The and operator has a higher precedence than the or operator  not (0 < 200)  False  0 < 200 is true, therefore its negation is false  frozen == True  frozen There is no need to compare a Boolean variable with True   frozen == False  not frozen It is clearer to use not than to compare with False  16 February 2018 PFE Section 3.7

Boolean Operator Examples 0 > x or x < 100 and x > 50 (0 > x or x < 100) and x > 50 (True or True) and False = True and False = False 0 > x or (x < 100 and x > 50) True or (True and False) = True or False = True 3==3.0 True >>> 435==4.35*100 False 16 February 2018 PFE Section 3.2

The Operators and, or Avoid confusing the operators and, or E.g. x in the range 0 to 100 inclusive 0 <= x and x <= 100 E.g. x outside the range 0 to 100 inclusive x < 0 or x > 100 16 February 2018

Chaining Relational Operators The expression 0 <= value <= 100 is equivalent in Python to value >= 0 and value <= 100 a < x > b is equivalent to a < x and x > b 16 February 2018 PFE Section 3.7

Short Circuit Evaluation Logical expressions are evaluated left to right. Evaluation stops when the value of the expression is known. Examples: True or Anything # True False and Anything # False quantity > 0 and price/quantity < 10 # False if quantity == 0 16 February 2018 PFE Section 3.7

De Morgan’s Law Motivation example Charge a higher shipping rate if the destination is not within the continental United States not US part of the US, but not continental: Alaska and Hawaii if not (country == "USA" and state != "AK" and state != "HI") : shippingCharge = 20.00 When not is applied to the outermost level of the condition, it becomes harder to understand what is meant. 16 February 2018 PFE Section 3.7

De Morgan’s Law Version 1 not (A and B) is the same as (not A) or (not B) Version 2 not (A or B) is the same as (not A) and (not B) To obtain version 2 from version 1, write not (A or B) = = not(not not A or not not B) # not not = identity = not not (not A and not B) # apply version 1 = (not A) and (not B) # not not = identity 16 February 2018 PFE Section 3.7

Example of De Morgan’s Law not (A and not B and not C) = (not A) or not (not B and not C) = (not A) or not not (B or C) = (not A) or B or C if not (country == "USA" and state != "AK" and state != "HI") : shippingCharge = 20.00 if(country != "USA" or state == "AK" or state == "HI") : 16 February 2018 PFE Section 3.7

PFE Review Question R3.20 Of the following pairs of strings, which comes first in lexicographic order? "Tom", "Jerry" "Tom", "Tomato" "church", "Churchill" "car manufacturer", "carburettor" "36", "A1" "36", "a1" 16 February 2018 PFE R3.20

Examples Let x, y, z be variables with integer values. Construct a Boolean expression that is True if and only if exactly one of x, y, z is equal to zero. Construct a Boolean expression with the following truth table, using one or more of the operators and, or, not.  A  B  Expression  True False  False 16 February 2018 PFE Section 3.7