12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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.
JavaScript, Fourth Edition
JavaScript, Third Edition
Introduction to C Programming
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
© 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.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Introduction to Python
 Pearson Education, Inc. All rights reserved Formatted Output.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
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.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
29 September 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
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.
CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
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.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
29 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.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
© 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.
Control Structures I Chapter 3
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
Java Variables and Types
Documentation Need to have documentation in all programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Data Types, Arithmetic Operations
TMF1414 Introduction to Programming
The Selection Structure
OUTPUT STATEMENTS GC 201.
Introduction to Programming
And now for something completely different . . .
Introduction to Programming
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.
Presentation transcript:

12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 2016 Week 6: Relational Operators and Boolean Variables

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" 12 February 2016PFE Section

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 12 February 2016PFE Section

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 12 February 2016PFE Section 2.44

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, each 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" % ) # one space then  print("%8s" % "Hello") # three spaces then "Hello" 12 February 2016PFE Section

Relational Operators 12 February 2016PFE Section 3.26 Python Math Notation Description > > Greater than >= Greater than or equal < < Less than <= Less than or equal == = Equal != Not equal

Examples of Relational Operators 12 February 2016PFE Section 3.27 Expression ValueComment 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 False For True, the left-hand side must be strictly smaller than the right hand side 4 <= 4 True The two sides are equal 3 == 5-2 True == tests for equality 3 != 5-1 True != tests for inequality. It is true that 3 is not = 6/2 Error Use == to test for equality 1.0/3.0 == False The values are close but they are not exactly equal "10" > 5 Error A string cannot be compared with a number

Relational Operators and Strings  name1 = "John"  name2 = "John"  name3 = "Smith"  name1 == name2 # True  name1 == name3 # False  name1 != name3 # True 12 February 2016PFE Section 3.28

Ordering of Single Characters  All uppercase letters come before lower case letters  The space character comes before all printable characters  Numbers come before letters  Example " " < "0" < "1" < "9" < "A" < "B" < "Z" < "a" < "b" < "z" 12 February 2016PFE Section 3.29

Examples of Lexicographic Ordering 12 February 2016PFE Section s1 s2 u1 u2 u1[0]<u2[0]?order "catch""cart" "tch""rt"No s1 > s2 "coal""coat" "l""t" Yes s1 < s2 "tone""ton" "e"""~ s1 > s2 "pit""pith" """h" ~s1 < s2 "pitch" "" ~ s1 == s2 Given strings s1, s2 find the longest string s such that s1 = s+u1 s2 = s+u2

Summary of Lexicographic Ordering 12 February 2016PFE Section 3.211

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. 12 February 2016PFE Section 3.712

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 12 February 2016PFE Section 3.713

Truth Tables 12 February 2016PFE Section AB A and B True False True False AB A or B True False True False True False Anot A True False True

Boolean Operator Examples 12 February 2016PFE Section ExpressionValue Comment 0 < 200 and 200 < 100False Only the first condition is true 0 < 200 or 200 < 100True The first condition is true 0 < 200 or 100 < 200TrueThe 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 frozenIt is clearer to use not than to compare with False

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 February

Chaining Relational Operators  The expression 0 <= value <= 100 is equivalent in Python to value >= 0 and value <= 100  The expression a b is equivalent to a b 12 February 2016PFE Section 3.717

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 12 February 2016PFE Section 3.718

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 (not A and not B) = not A and not B 12 February 2016PFE Section 3.719

Example of De Morgan’s Law  if not (country == "USA" and state != "AK" and state != "HI") : shippingCharge =  if(country != "USA" or state == "AK" or state == "HI") : shippingCharge = February 2016PFE Section 3.720

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" 12 February 2016PFE R3.2021

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. 12 February 2016PFE Section A B Expression True False True False True False True False