Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

Similar presentations


Presentation on theme: "COSC 1306 COMPUTER SCIENCE AND PROGRAMMING"— Presentation transcript:

1 COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Jehan-François Pâris Fall 2017 1

2 THE ONLINE BOOK CHAPTER VII SELECTIONS

3 Motivation We will learn how programs can make choices:
Boolean variables Boolean expressions if and if/else

4 Boolean type Two values: True and False Created as
Result of a comparison: ==, !=, >, >=, <, <= Conversion using bool(…) function Zero values and anything empty converts into a False Anything else converts into a True value

5 Examples >>> a = 3 >>> b = 5
>>> p = a < b >>> q = b <= a >>> print("p =", p, "and q =", q) p = True and q = False

6 A Boolean function def isDivisible(x, y): return x % y == 0
Returns True if x is divisible by y

7 Boolean operations (I)
AND: True if both operands are true This is called a truth table a b a and b F T

8 Boolean operations (II)
OR: True unless both operands are false a b a or b F T

9 Boolean operations (III)
NOT: True if operand is false a not a F T

10 Boolean operations (IV)
Equality/Mutual Implication: True when both operands are equal Represented by a == sign a b a==b F T

11 Boolean operations (V)
Exclusive OR (XOR): True when operands are not equal Represented by a != sign a b a!=b F T

12 Notes XOR is used for Generating parity bits Hardware hashing

13 Examples Assuming p is True and q is False >>> p or q True
>>> p and q False >>> not p >>> p != q

14 Online Demo

15 Precedence Rules Level Category Operators 7(high) exponent **
6 multiplication *, /, //, % 5 addition +, - 4 relational ==, !=, <=, >=, >, < 3 logical not not 2 logical and and 1(low) logical or or

16 Examples a < b + c or c > 0 same as (a < (b+c)) or c >0)
a and b or c same as (a and b) or c a and not b or c same as (a and (not b)) or c

17 Conversely We must write a = (p or q) and s b = (c and d) != (e or f)

18 Online demo

19 Binary selection if BOOLEAN EXPRESSION : STATEMENTS_1 # when condition is True else: STATEMENTS_2 # when condition is False Observe The columns after if and else The indentation

20 Binary selection If condition: something else: other stuff False
True other stuff something If condition: something else: other stuff

21 Flowchart conventions
Graphic representation of flow of control Loops represented by loops Ifs have two branches ??? False True

22 Online demo

23 Omitting the else clause
if BOOLEAN EXPRESSION: STATEMENTS_1 # if condition is True # Continue normal flow Observe The column after if The indentation

24 Omitting the else clause
If condition : something If tank_empty : get_gas True condition something False

25 Main usage if + else To make a choice between two options if
To handle a special case Zero value, negative value Work is done

26 A problem with letter grades
Semester Average Letter Grade ≥ 90 A ≥ 80 but < 90 B ≥ 70 but < 80 C ≥ 60 but < 70 D < 60 F

27 An ugly solution if average >= 90 : grade = 'A' if average >= 80 and average < 90 : grade = 'B' if average >= 70 and average < 80 : grade = 'C' if average >= 60 and average < 70 : grade = 'D' if average < 60 : grade = 'F'

28 The right way if average >= 90 : grade = 'A' else : if average >= 80: grade = 'B' else : if average >= 70 : grade = 'C' else : if average >= 60 : grade = 'D' else: grade = 'F'

29 Using an elif (I) Too many nested ifs else …
if cond-1 : … else if cond-2 : … else if cond-3 : … else

30 Using an elif (II) With elif, lines align better
if cond-1 : … elif cond-2 : … elif cond-3 : … else :

31 The right way if average >= 90 : grade = 'A' elif average >= 80: grade = 'B' elif average >= 70 : grade = 'C' elif average >= 60 : grade = 'D' else: grade = 'F'

32 A beginner's trap if average >= 90 : grade = 'A' elif average >= 80 and average < 90 : grade = 'B' elif average >= 70 and average < 80 : grade = 'C' elif average >= 60 and average < 70: grade = 'D' elif average < 60: grade = 'F'

33 Useless comparisons if average >= 90 : grade = 'A' elif average >= 80 and average < 90 : grade = 'B' elif average >= 70 and average < 80 : grade = 'C' elif average >= 60 and average < 70: grade = 'D' elif average < 60: # use else instead grade = 'F'

34 Indenting advice Python attaches great importance to indenting
You cannot indent anything that is not inside An if, a while, a for, … A function declaration or a main function Your indentation must be consistent Do not mix spaces and tabs Four spaces is neither too small nor too big

35 Floating point values Differ from integer values
Cannot handle extremely large or extremely small values Limited precision Can manipulate trillions of dollars but results are not exact to the cent Must specify number of decimals in outputs

36 Limited precision # precision.py
""" floating-point computations can be inaccurate """ biggie = 5E9 epsilon = 5E-9 sum = biggie + epsilon delta = sum - biggie print ('epsilon =', epsilon, 'but delta =', delta)

37 The output epsilon = 5e-9 but delta = 0.0 Why?
Computations are carried out with a limited number of significant bits Result has a limited number of significant digits Be careful when computing very small differences between two numbers

38 Note Limitations on floating point number size and precision are
Hardware dependent Shared by most programming languages Unlike Python, most programming languages do not support arbitrary-size integers Python is better

39 Displaying floating point numbers
Easiest way to specify number of decimal places is string interpolation 'Answer is %.nf ' %x does two things It inserts the value of x inside the string It specifies that the string representation of x should have n decimal places

40 Example (I) pi = print('pi equals %.1f' % pi ) print('pi equals %.2f' % pi ) print('pi equals %.3f' % pi ) print('pi equals %.4f' % pi )

41 Example (II) Program prints
pi equals 3.1 pi equals pi equals pi equals Values are nicely rounded


Download ppt "COSC 1306 COMPUTER SCIENCE AND PROGRAMMING"

Similar presentations


Ads by Google