CS 115 Lecture 4 More Python; testing software Neil Moore - - PowerPoint PPT Presentation

cs 115 lecture 4
SMART_READER_LITE
LIVE PREVIEW

CS 115 Lecture 4 More Python; testing software Neil Moore - - PowerPoint PPT Presentation

CS 115 Lecture 4 More Python; testing software Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 8 September 2015 Syntax: Statements A statement is the smallest unit of code that can


slide-1
SLIDE 1

CS 115 Lecture 4

More Python; testing software Neil Moore

Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu

8 September 2015

slide-2
SLIDE 2

Syntax: Statements

A statement is the smallest unit of code that can be executed on its own. So far we’ve seen:

◮ Assignment: sum = first + second ◮ Function call: print("Hello") Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 2 / 26

slide-3
SLIDE 3

Syntax: Statements

A statement is the smallest unit of code that can be executed on its own. So far we’ve seen:

◮ Assignment: sum = first + second ◮ Function call: print("Hello")

Usually one line, but compound statements can be bigger.

◮ def, for, if, etc. ◮ We’ll see more of these in a few weeks. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 2 / 26

slide-4
SLIDE 4

Syntax: Statements

A statement is the smallest unit of code that can be executed on its own. So far we’ve seen:

◮ Assignment: sum = first + second ◮ Function call: print("Hello")

Usually one line, but compound statements can be bigger.

◮ def, for, if, etc. ◮ We’ll see more of these in a few weeks.

Comments aren’t statements: they can’t be executed.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 2 / 26

slide-5
SLIDE 5

Syntax: Statements

A statement is the smallest unit of code that can be executed on its own. So far we’ve seen:

◮ Assignment: sum = first + second ◮ Function call: print("Hello")

Usually one line, but compound statements can be bigger.

◮ def, for, if, etc. ◮ We’ll see more of these in a few weeks.

Comments aren’t statements: they can’t be executed.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 2 / 26

slide-6
SLIDE 6

Syntax: Expressions

An expression is a piece of code that has a value. It is an even smaller and more fundamental unit than the statement. Something you could use on the right hand side of assignment. Can be used as part of bigger expressions. Examples:

◮ Literals: 2, 3.4, "Python" Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 3 / 26

slide-7
SLIDE 7

Syntax: Expressions

An expression is a piece of code that has a value. It is an even smaller and more fundamental unit than the statement. Something you could use on the right hand side of assignment. Can be used as part of bigger expressions. Examples:

◮ Literals: 2, 3.4, "Python" ◮ Variable name: user name Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 3 / 26

slide-8
SLIDE 8

Syntax: Expressions

An expression is a piece of code that has a value. It is an even smaller and more fundamental unit than the statement. Something you could use on the right hand side of assignment. Can be used as part of bigger expressions. Examples:

◮ Literals: 2, 3.4, "Python" ◮ Variable name: user name ◮ Arithmetic: 3 * (x + 2) Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 3 / 26

slide-9
SLIDE 9

Syntax: Expressions

An expression is a piece of code that has a value. It is an even smaller and more fundamental unit than the statement. Something you could use on the right hand side of assignment. Can be used as part of bigger expressions. Examples:

◮ Literals: 2, 3.4, "Python" ◮ Variable name: user name ◮ Arithmetic: 3 * (x + 2) ⋆ (x + 2) is itself an expression ⋆ . . . as are x and 2 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 3 / 26

slide-10
SLIDE 10

Syntax: Expressions

An expression is a piece of code that has a value. It is an even smaller and more fundamental unit than the statement. Something you could use on the right hand side of assignment. Can be used as part of bigger expressions. Examples:

◮ Literals: 2, 3.4, "Python" ◮ Variable name: user name ◮ Arithmetic: 3 * (x + 2) ⋆ (x + 2) is itself an expression ⋆ . . . as are x and 2 ⋆ It’s expressions all the way down! Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 3 / 26

slide-11
SLIDE 11

Syntax: Expressions

An expression is a piece of code that has a value. It is an even smaller and more fundamental unit than the statement. Something you could use on the right hand side of assignment. Can be used as part of bigger expressions. Examples:

◮ Literals: 2, 3.4, "Python" ◮ Variable name: user name ◮ Arithmetic: 3 * (x + 2) ⋆ (x + 2) is itself an expression ⋆ . . . as are x and 2 ⋆ It’s expressions all the way down! ◮ Function call: input("What is your name?") Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 3 / 26

slide-12
SLIDE 12

Syntax: Expressions

An expression is a piece of code that has a value. It is an even smaller and more fundamental unit than the statement. Something you could use on the right hand side of assignment. Can be used as part of bigger expressions. Examples:

◮ Literals: 2, 3.4, "Python" ◮ Variable name: user name ◮ Arithmetic: 3 * (x + 2) ⋆ (x + 2) is itself an expression ⋆ . . . as are x and 2 ⋆ It’s expressions all the way down! ◮ Function call: input("What is your name?") Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 3 / 26

slide-13
SLIDE 13

Semantics: Data types

Inside the computer, everything is just bits. A data type says how to interpret those bits, and what we can do with them. Every expression in Python has a type. Some of the built-in types: Type Description Examples int integer numbers 2, -44 float floating-point numbers 3.0, -0.9, 6.022e23 bool Boolean (true-false) values True, False str strings of characters ’hello’, "0" list lists of values [ "Valjean", 24601 ], [ 2, 3, 5, 7 ]

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 4 / 26

slide-14
SLIDE 14

Semantics: Data types

Inside the computer, everything is just bits. A data type says how to interpret those bits, and what we can do with them. Every expression in Python has a type. Some of the built-in types: Type Description Examples int integer numbers 2, -44 float floating-point numbers 3.0, -0.9, 6.022e23 bool Boolean (true-false) values True, False str strings of characters ’hello’, "0" list lists of values [ "Valjean", 24601 ], [ 2, 3, 5, 7 ]

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 4 / 26

slide-15
SLIDE 15

Integers

Type int represents integers: whole numbers that may be positive, negative, or zero. Literal integers: a sequence of digits: 24601

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 5 / 26

slide-16
SLIDE 16

Integers

Type int represents integers: whole numbers that may be positive, negative, or zero. Literal integers: a sequence of digits: 24601

◮ . . . with no leading zeros! ◮ 0 is okay, 007 is not. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 5 / 26

slide-17
SLIDE 17

Integers

Type int represents integers: whole numbers that may be positive, negative, or zero. Literal integers: a sequence of digits: 24601

◮ . . . with no leading zeros! ◮ 0 is okay, 007 is not.

In Python, integers have no limit to their size.

◮ As many digits as you have memory for. ◮ That isn’t true in most languages, like C++ and Java. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 5 / 26

slide-18
SLIDE 18

Integers

Type int represents integers: whole numbers that may be positive, negative, or zero. Literal integers: a sequence of digits: 24601

◮ . . . with no leading zeros! ◮ 0 is okay, 007 is not.

In Python, integers have no limit to their size.

◮ As many digits as you have memory for. ◮ That isn’t true in most languages, like C++ and Java. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 5 / 26

slide-19
SLIDE 19

Floating-point

Type float represents floating-point numbers: numbers with a decimal point.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 6 / 26

slide-20
SLIDE 20

Floating-point

Type float represents floating-point numbers: numbers with a decimal point. Limited precision and range.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 6 / 26

slide-21
SLIDE 21

Floating-point

Type float represents floating-point numbers: numbers with a decimal point. Limited precision and range. Two forms of literal floats:

◮ Number with a decimal point:

3.14, .027, 0.001, 3., 1.0

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 6 / 26

slide-22
SLIDE 22

Floating-point

Type float represents floating-point numbers: numbers with a decimal point. Limited precision and range. Two forms of literal floats:

◮ Number with a decimal point:

3.14, .027, 0.001, 3., 1.0

⋆ Must have a decimal point! ⋆ 1.0 or 1. is a float, but 1 is an integer! Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 6 / 26

slide-23
SLIDE 23

Floating-point

Type float represents floating-point numbers: numbers with a decimal point. Limited precision and range. Two forms of literal floats:

◮ Number with a decimal point:

3.14, .027, 0.001, 3., 1.0

⋆ Must have a decimal point! ⋆ 1.0 or 1. is a float, but 1 is an integer! ◮ “E”-notation (scientific notation): ⋆ 6.022e23, 1.0E9, 31e-2 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 6 / 26

slide-24
SLIDE 24

Floating-point

Type float represents floating-point numbers: numbers with a decimal point. Limited precision and range. Two forms of literal floats:

◮ Number with a decimal point:

3.14, .027, 0.001, 3., 1.0

⋆ Must have a decimal point! ⋆ 1.0 or 1. is a float, but 1 is an integer! ◮ “E”-notation (scientific notation): ⋆ 6.022e23, 1.0E9, 31e-2 ⋆ Write e for “times 10 to the” ⋆ Does not need a decimal point: the e is enough. ⋆ Exponent must be an integer. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 6 / 26

slide-25
SLIDE 25

Floating-point

Type float represents floating-point numbers: numbers with a decimal point. Limited precision and range. Two forms of literal floats:

◮ Number with a decimal point:

3.14, .027, 0.001, 3., 1.0

⋆ Must have a decimal point! ⋆ 1.0 or 1. is a float, but 1 is an integer! ◮ “E”-notation (scientific notation): ⋆ 6.022e23, 1.0E9, 31e-2 ⋆ Write e for “times 10 to the” ⋆ Does not need a decimal point: the e is enough. ⋆ Exponent must be an integer.

In some languages these are called “doubles”

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 6 / 26

slide-26
SLIDE 26

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-27
SLIDE 27

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point.

In Python, the mantissa holds about 15 significant digits.

◮ Any digits past that are lost (rounding error). ⋆ (leading and trailing zeros don’t count) Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-28
SLIDE 28

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point.

In Python, the mantissa holds about 15 significant digits.

◮ Any digits past that are lost (rounding error). ⋆ (leading and trailing zeros don’t count) ◮ Limits the precision of a float. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-29
SLIDE 29

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point.

In Python, the mantissa holds about 15 significant digits.

◮ Any digits past that are lost (rounding error). ⋆ (leading and trailing zeros don’t count) ◮ Limits the precision of a float. ◮ Try: 10000000000000002.0 - 10000000000000001.0 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-30
SLIDE 30

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point.

In Python, the mantissa holds about 15 significant digits.

◮ Any digits past that are lost (rounding error). ⋆ (leading and trailing zeros don’t count) ◮ Limits the precision of a float. ◮ Try: 10000000000000002.0 - 10000000000000001.0 ⋆ Python’s answer is 2.0: the 1 was rounded down! Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-31
SLIDE 31

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point.

In Python, the mantissa holds about 15 significant digits.

◮ Any digits past that are lost (rounding error). ⋆ (leading and trailing zeros don’t count) ◮ Limits the precision of a float. ◮ Try: 10000000000000002.0 - 10000000000000001.0 ⋆ Python’s answer is 2.0: the 1 was rounded down!

The exponent can go from about -300 to 300.

◮ Limits the range of a float. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-32
SLIDE 32

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point.

In Python, the mantissa holds about 15 significant digits.

◮ Any digits past that are lost (rounding error). ⋆ (leading and trailing zeros don’t count) ◮ Limits the precision of a float. ◮ Try: 10000000000000002.0 - 10000000000000001.0 ⋆ Python’s answer is 2.0: the 1 was rounded down!

The exponent can go from about -300 to 300.

◮ Limits the range of a float. ◮ Try: 1e309 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-33
SLIDE 33

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point.

In Python, the mantissa holds about 15 significant digits.

◮ Any digits past that are lost (rounding error). ⋆ (leading and trailing zeros don’t count) ◮ Limits the precision of a float. ◮ Try: 10000000000000002.0 - 10000000000000001.0 ⋆ Python’s answer is 2.0: the 1 was rounded down!

The exponent can go from about -300 to 300.

◮ Limits the range of a float. ◮ Try: 1e309—gives inf (infinity) Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-34
SLIDE 34

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point.

In Python, the mantissa holds about 15 significant digits.

◮ Any digits past that are lost (rounding error). ⋆ (leading and trailing zeros don’t count) ◮ Limits the precision of a float. ◮ Try: 10000000000000002.0 - 10000000000000001.0 ⋆ Python’s answer is 2.0: the 1 was rounded down!

The exponent can go from about -300 to 300.

◮ Limits the range of a float. ◮ Try: 1e309—gives inf (infinity) ◮ Try: 1e-324 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-35
SLIDE 35

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point.

In Python, the mantissa holds about 15 significant digits.

◮ Any digits past that are lost (rounding error). ⋆ (leading and trailing zeros don’t count) ◮ Limits the precision of a float. ◮ Try: 10000000000000002.0 - 10000000000000001.0 ⋆ Python’s answer is 2.0: the 1 was rounded down!

The exponent can go from about -300 to 300.

◮ Limits the range of a float. ◮ Try: 1e309—gives inf (infinity) ◮ Try: 1e-324—gives 0.0 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-36
SLIDE 36

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point.

In Python, the mantissa holds about 15 significant digits.

◮ Any digits past that are lost (rounding error). ⋆ (leading and trailing zeros don’t count) ◮ Limits the precision of a float. ◮ Try: 10000000000000002.0 - 10000000000000001.0 ⋆ Python’s answer is 2.0: the 1 was rounded down!

The exponent can go from about -300 to 300.

◮ Limits the range of a float. ◮ Try: 1e309—gives inf (infinity) ◮ Try: 1e-324—gives 0.0

The exact limits are on the number of bits, not digits.

◮ Even 0.1 can’t be represented exactly ⋆ Try: 0.1 + 0.1 + 0.1 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-37
SLIDE 37

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point.

In Python, the mantissa holds about 15 significant digits.

◮ Any digits past that are lost (rounding error). ⋆ (leading and trailing zeros don’t count) ◮ Limits the precision of a float. ◮ Try: 10000000000000002.0 - 10000000000000001.0 ⋆ Python’s answer is 2.0: the 1 was rounded down!

The exponent can go from about -300 to 300.

◮ Limits the range of a float. ◮ Try: 1e309—gives inf (infinity) ◮ Try: 1e-324—gives 0.0

The exact limits are on the number of bits, not digits.

◮ Even 0.1 can’t be represented exactly ⋆ Try: 0.1 + 0.1 + 0.1—gives 0.30000000000000004 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-38
SLIDE 38

Floating-point limitations

Floats are stored in a binary form of scientific notation:

◮ Mantissa: the digits (in binary). ◮ Exponent: how far to move the decimal (binary) point.

In Python, the mantissa holds about 15 significant digits.

◮ Any digits past that are lost (rounding error). ⋆ (leading and trailing zeros don’t count) ◮ Limits the precision of a float. ◮ Try: 10000000000000002.0 - 10000000000000001.0 ⋆ Python’s answer is 2.0: the 1 was rounded down!

The exponent can go from about -300 to 300.

◮ Limits the range of a float. ◮ Try: 1e309—gives inf (infinity) ◮ Try: 1e-324—gives 0.0

The exact limits are on the number of bits, not digits.

◮ Even 0.1 can’t be represented exactly ⋆ Try: 0.1 + 0.1 + 0.1—gives 0.30000000000000004 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 7 / 26

slide-39
SLIDE 39

Arithmetic on integers and floats

You can perform arithmetic on both ints and floats. For most arithmetic

  • perators (+ - * **) the rules are:

If both operands are ints, the result is an int.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 8 / 26

slide-40
SLIDE 40

Arithmetic on integers and floats

You can perform arithmetic on both ints and floats. For most arithmetic

  • perators (+ - * **) the rules are:

If both operands are ints, the result is an int.

◮ 3 + 5 → 8 ◮ 2 ** 100 → 1267650600228229401496703205376 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 8 / 26

slide-41
SLIDE 41

Arithmetic on integers and floats

You can perform arithmetic on both ints and floats. For most arithmetic

  • perators (+ - * **) the rules are:

If both operands are ints, the result is an int.

◮ 3 + 5 → 8 ◮ 2 ** 100 → 1267650600228229401496703205376

If one or both operand is a float, the result is a float.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 8 / 26

slide-42
SLIDE 42

Arithmetic on integers and floats

You can perform arithmetic on both ints and floats. For most arithmetic

  • perators (+ - * **) the rules are:

If both operands are ints, the result is an int.

◮ 3 + 5 → 8 ◮ 2 ** 100 → 1267650600228229401496703205376

If one or both operand is a float, the result is a float.

◮ 3.0 + 0.14 → 3.14 ◮ 100 - 1.0 → 99.0 ◮ 2.0 ** 100 → 1.2676506002282294e+30 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 8 / 26

slide-43
SLIDE 43

Arithmetic on integers and floats

You can perform arithmetic on both ints and floats. For most arithmetic

  • perators (+ - * **) the rules are:

If both operands are ints, the result is an int.

◮ 3 + 5 → 8 ◮ 2 ** 100 → 1267650600228229401496703205376

If one or both operand is a float, the result is a float.

◮ 3.0 + 0.14 → 3.14 ◮ 100 - 1.0 → 99.0 ◮ 2.0 ** 100 → 1.2676506002282294e+30

There is one exception. . .

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 8 / 26

slide-44
SLIDE 44

Arithmetic on integers and floats

You can perform arithmetic on both ints and floats. For most arithmetic

  • perators (+ - * **) the rules are:

If both operands are ints, the result is an int.

◮ 3 + 5 → 8 ◮ 2 ** 100 → 1267650600228229401496703205376

If one or both operand is a float, the result is a float.

◮ 3.0 + 0.14 → 3.14 ◮ 100 - 1.0 → 99.0 ◮ 2.0 ** 100 → 1.2676506002282294e+30

There is one exception. . .

◮ What is 1/2 ? Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 8 / 26

slide-45
SLIDE 45

Arithmetic on integers and floats

You can perform arithmetic on both ints and floats. For most arithmetic

  • perators (+ - * **) the rules are:

If both operands are ints, the result is an int.

◮ 3 + 5 → 8 ◮ 2 ** 100 → 1267650600228229401496703205376

If one or both operand is a float, the result is a float.

◮ 3.0 + 0.14 → 3.14 ◮ 100 - 1.0 → 99.0 ◮ 2.0 ** 100 → 1.2676506002282294e+30

There is one exception. . .

◮ What is 1/2 ? Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 8 / 26

slide-46
SLIDE 46

Division

Python actually has two division operators, / and //. / always gives a float.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 9 / 26

slide-47
SLIDE 47

Division

Python actually has two division operators, / and //. / always gives a float.

◮ 1 / 2 → 0.5 ◮ 6 / 3 → 2.0 ◮ 3.0 / 0.5 → 6.0 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 9 / 26

slide-48
SLIDE 48

Division

Python actually has two division operators, / and //. / always gives a float.

◮ 1 / 2 → 0.5 ◮ 6 / 3 → 2.0 ◮ 3.0 / 0.5 → 6.0

// does floor division: rounds the answer down to a whole number.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 9 / 26

slide-49
SLIDE 49

Division

Python actually has two division operators, / and //. / always gives a float.

◮ 1 / 2 → 0.5 ◮ 6 / 3 → 2.0 ◮ 3.0 / 0.5 → 6.0

// does floor division: rounds the answer down to a whole number.

◮ If both operands are integers, so is the result. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 9 / 26

slide-50
SLIDE 50

Division

Python actually has two division operators, / and //. / always gives a float.

◮ 1 / 2 → 0.5 ◮ 6 / 3 → 2.0 ◮ 3.0 / 0.5 → 6.0

// does floor division: rounds the answer down to a whole number.

◮ If both operands are integers, so is the result. ⋆ 22 // 7 → 3 ⋆ 1 // 2 → 0 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 9 / 26

slide-51
SLIDE 51

Division

Python actually has two division operators, / and //. / always gives a float.

◮ 1 / 2 → 0.5 ◮ 6 / 3 → 2.0 ◮ 3.0 / 0.5 → 6.0

// does floor division: rounds the answer down to a whole number.

◮ If both operands are integers, so is the result. ⋆ 22 // 7 → 3 ⋆ 1 // 2 → 0 ◮ If either operand is a float, so is the result. ⋆ But it still has a whole-number value. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 9 / 26

slide-52
SLIDE 52

Division

Python actually has two division operators, / and //. / always gives a float.

◮ 1 / 2 → 0.5 ◮ 6 / 3 → 2.0 ◮ 3.0 / 0.5 → 6.0

// does floor division: rounds the answer down to a whole number.

◮ If both operands are integers, so is the result. ⋆ 22 // 7 → 3 ⋆ 1 // 2 → 0 ◮ If either operand is a float, so is the result. ⋆ But it still has a whole-number value. ⋆ 22 // 7.0 → 3.0 ⋆ 3.1 // 0.5 → 6.0 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 9 / 26

slide-53
SLIDE 53

Division

Python actually has two division operators, / and //. / always gives a float.

◮ 1 / 2 → 0.5 ◮ 6 / 3 → 2.0 ◮ 3.0 / 0.5 → 6.0

// does floor division: rounds the answer down to a whole number.

◮ If both operands are integers, so is the result. ⋆ 22 // 7 → 3 ⋆ 1 // 2 → 0 ◮ If either operand is a float, so is the result. ⋆ But it still has a whole-number value. ⋆ 22 // 7.0 → 3.0 ⋆ 3.1 // 0.5 → 6.0

In either case, dividing by zero is a run-time error!

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 9 / 26

slide-54
SLIDE 54

Division

Python actually has two division operators, / and //. / always gives a float.

◮ 1 / 2 → 0.5 ◮ 6 / 3 → 2.0 ◮ 3.0 / 0.5 → 6.0

// does floor division: rounds the answer down to a whole number.

◮ If both operands are integers, so is the result. ⋆ 22 // 7 → 3 ⋆ 1 // 2 → 0 ◮ If either operand is a float, so is the result. ⋆ But it still has a whole-number value. ⋆ 22 // 7.0 → 3.0 ⋆ 3.1 // 0.5 → 6.0

In either case, dividing by zero is a run-time error!

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 9 / 26

slide-55
SLIDE 55

Remainder (modulo)

The % operator (modulo or mod) finds the remainder of a division. Between 0 (inclusive) and the right hand side (exclusive).

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 10 / 26

slide-56
SLIDE 56

Remainder (modulo)

The % operator (modulo or mod) finds the remainder of a division. Between 0 (inclusive) and the right hand side (exclusive).

◮ 6 % 3 → 0 ◮ 7 % 3 → 1 ◮ 8 % 3 → 2 ◮ 9 % 3 → 0 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 10 / 26

slide-57
SLIDE 57

Remainder (modulo)

The % operator (modulo or mod) finds the remainder of a division. Between 0 (inclusive) and the right hand side (exclusive).

◮ 6 % 3 → 0 ◮ 7 % 3 → 1 ◮ 8 % 3 → 2 ◮ 9 % 3 → 0

Uses of modulo:

◮ Even/odd: n is even if n % 2 is zero. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 10 / 26

slide-58
SLIDE 58

Remainder (modulo)

The % operator (modulo or mod) finds the remainder of a division. Between 0 (inclusive) and the right hand side (exclusive).

◮ 6 % 3 → 0 ◮ 7 % 3 → 1 ◮ 8 % 3 → 2 ◮ 9 % 3 → 0

Uses of modulo:

◮ Even/odd: n is even if n % 2 is zero. ◮ Digits: n % 10 is the last digit of n. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 10 / 26

slide-59
SLIDE 59

Remainder (modulo)

The % operator (modulo or mod) finds the remainder of a division. Between 0 (inclusive) and the right hand side (exclusive).

◮ 6 % 3 → 0 ◮ 7 % 3 → 1 ◮ 8 % 3 → 2 ◮ 9 % 3 → 0

Uses of modulo:

◮ Even/odd: n is even if n % 2 is zero. ◮ Digits: n % 10 is the last digit of n. ◮ “Clock arithmetic” ⋆ Minutes are mod 60: 3:58 + 15 minutes = 4:13 ⋆ Hours are mod 12: 10:00 + 4 hours = 2:00 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 10 / 26

slide-60
SLIDE 60

Remainder (modulo)

The % operator (modulo or mod) finds the remainder of a division. Between 0 (inclusive) and the right hand side (exclusive).

◮ 6 % 3 → 0 ◮ 7 % 3 → 1 ◮ 8 % 3 → 2 ◮ 9 % 3 → 0

Uses of modulo:

◮ Even/odd: n is even if n % 2 is zero. ◮ Digits: n % 10 is the last digit of n. ◮ “Clock arithmetic” ⋆ Minutes are mod 60: 3:58 + 15 minutes = 4:13 ⋆ Hours are mod 12: 10:00 + 4 hours = 2:00

Python can do modulo on floats.

◮ 5 % 2.4 → 0.2 ◮ Far more common with ints, though. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 10 / 26

slide-61
SLIDE 61

Remainder (modulo)

The % operator (modulo or mod) finds the remainder of a division. Between 0 (inclusive) and the right hand side (exclusive).

◮ 6 % 3 → 0 ◮ 7 % 3 → 1 ◮ 8 % 3 → 2 ◮ 9 % 3 → 0

Uses of modulo:

◮ Even/odd: n is even if n % 2 is zero. ◮ Digits: n % 10 is the last digit of n. ◮ “Clock arithmetic” ⋆ Minutes are mod 60: 3:58 + 15 minutes = 4:13 ⋆ Hours are mod 12: 10:00 + 4 hours = 2:00

Python can do modulo on floats.

◮ 5 % 2.4 → 0.2 ◮ Far more common with ints, though. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 10 / 26

slide-62
SLIDE 62

Booleans

Type bool represents boolean values. Named after George Boole, English mathematician and logician.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 11 / 26

slide-63
SLIDE 63

Booleans

Type bool represents boolean values. Named after George Boole, English mathematician and logician.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 11 / 26

George Boole, English computer scientist.

Image: Computer History Museum

slide-64
SLIDE 64

Booleans

Type bool represents boolean values. Named after George Boole, English mathematician and logician. Exactly two values: True, False

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 11 / 26

slide-65
SLIDE 65

Booleans

Type bool represents boolean values. Named after George Boole, English mathematician and logician. Exactly two values: True, False

◮ No quotes! They aren’t strings. ◮ Case-sensitive as always: capital T and F. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 11 / 26

slide-66
SLIDE 66

Booleans

Type bool represents boolean values. Named after George Boole, English mathematician and logician. Exactly two values: True, False

◮ No quotes! They aren’t strings. ◮ Case-sensitive as always: capital T and F.

Boolean values are the basis of computer circuits: EE 280.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 11 / 26

slide-67
SLIDE 67

Booleans

Type bool represents boolean values. Named after George Boole, English mathematician and logician. Exactly two values: True, False

◮ No quotes! They aren’t strings. ◮ Case-sensitive as always: capital T and F.

Boolean values are the basis of computer circuits: EE 280. Can’t do arithmetic on them.

◮ Can do and, or, and not. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 11 / 26

slide-68
SLIDE 68

Booleans

Type bool represents boolean values. Named after George Boole, English mathematician and logician. Exactly two values: True, False

◮ No quotes! They aren’t strings. ◮ Case-sensitive as always: capital T and F.

Boolean values are the basis of computer circuits: EE 280. Can’t do arithmetic on them.

◮ Can do and, or, and not. ◮ Most often used with if and while statements. ◮ More about boolean operations in chapter 4. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 11 / 26

slide-69
SLIDE 69

Booleans

Type bool represents boolean values. Named after George Boole, English mathematician and logician. Exactly two values: True, False

◮ No quotes! They aren’t strings. ◮ Case-sensitive as always: capital T and F.

Boolean values are the basis of computer circuits: EE 280. Can’t do arithmetic on them.

◮ Can do and, or, and not. ◮ Most often used with if and while statements. ◮ More about boolean operations in chapter 4. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 11 / 26

slide-70
SLIDE 70

Strings

Type str represents strings: sequences of characters. Literal strings: a sequence of characters in single or double quotes.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 12 / 26

slide-71
SLIDE 71

Strings

Type str represents strings: sequences of characters. Literal strings: a sequence of characters in single or double quotes.

◮ ’hello’, "world", "" ◮ Use whichever quote isn’t in the string:

’some "quotes"’, "a’postrophe"

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 12 / 26

slide-72
SLIDE 72

Strings

Type str represents strings: sequences of characters. Literal strings: a sequence of characters in single or double quotes.

◮ ’hello’, "world", "" ◮ Use whichever quote isn’t in the string:

’some "quotes"’, "a’postrophe"

◮ If you have to include the quote character, escape it with a backslash:

msg = ’the word "don\’t" is 5 characters long’

◮ Have to escape backslashes, too:

folder = "C:\\Python 3.4"

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 12 / 26

slide-73
SLIDE 73

Strings

Type str represents strings: sequences of characters. Literal strings: a sequence of characters in single or double quotes.

◮ ’hello’, "world", "" ◮ Use whichever quote isn’t in the string:

’some "quotes"’, "a’postrophe"

◮ If you have to include the quote character, escape it with a backslash:

msg = ’the word "don\’t" is 5 characters long’

◮ Have to escape backslashes, too:

folder = "C:\\Python 3.4"

◮ Special characters: tab \t, newline \n. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 12 / 26

slide-74
SLIDE 74

Strings

Type str represents strings: sequences of characters. Literal strings: a sequence of characters in single or double quotes.

◮ ’hello’, "world", "" ◮ Use whichever quote isn’t in the string:

’some "quotes"’, "a’postrophe"

◮ If you have to include the quote character, escape it with a backslash:

msg = ’the word "don\’t" is 5 characters long’

◮ Have to escape backslashes, too:

folder = "C:\\Python 3.4"

◮ Special characters: tab \t, newline \n.

Can perform a few operations on strings:

◮ Concatenate (join) strings with +:

greeting = "Hello, " + name

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 12 / 26

slide-75
SLIDE 75

Strings

Type str represents strings: sequences of characters. Literal strings: a sequence of characters in single or double quotes.

◮ ’hello’, "world", "" ◮ Use whichever quote isn’t in the string:

’some "quotes"’, "a’postrophe"

◮ If you have to include the quote character, escape it with a backslash:

msg = ’the word "don\’t" is 5 characters long’

◮ Have to escape backslashes, too:

folder = "C:\\Python 3.4"

◮ Special characters: tab \t, newline \n.

Can perform a few operations on strings:

◮ Concatenate (join) strings with +:

greeting = "Hello, " + name

◮ Repeat a string by “multiplying” with an integer:

rating = ’⋆’ * 4 # ⋆ ⋆ ⋆⋆ bird = 2 * ’do’ # dodo

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 12 / 26

slide-76
SLIDE 76

Converting between types

Converting between types is also called type casting. Write the name of the type you are converting to, then the expression to convert in parentheses

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 13 / 26

slide-77
SLIDE 77

Converting between types

Converting between types is also called type casting. Write the name of the type you are converting to, then the expression to convert in parentheses:

◮ float(2) → 2.0 ◮ int(3.14) → 3 ◮ str(1.2e3) → "1200.0" ◮ int("02") → 2 ◮ float("0") → 0.0 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 13 / 26

slide-78
SLIDE 78

Converting between types

Converting between types is also called type casting. Write the name of the type you are converting to, then the expression to convert in parentheses:

◮ float(2) → 2.0 ◮ int(3.14) → 3 ◮ str(1.2e3) → "1200.0" ◮ int("02") → 2 ◮ float("0") → 0.0

Converting double to int rounds towards zero (+ down, − up)

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 13 / 26

slide-79
SLIDE 79

Converting between types

Converting between types is also called type casting. Write the name of the type you are converting to, then the expression to convert in parentheses:

◮ float(2) → 2.0 ◮ int(3.14) → 3 ◮ str(1.2e3) → "1200.0" ◮ int("02") → 2 ◮ float("0") → 0.0

Converting double to int rounds towards zero (+ down, − up) Run-time error if a string could not be converted:

◮ n = int("hello")

# CRASHES with ValueError

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 13 / 26

slide-80
SLIDE 80

Converting between types

Converting between types is also called type casting. Write the name of the type you are converting to, then the expression to convert in parentheses:

◮ float(2) → 2.0 ◮ int(3.14) → 3 ◮ str(1.2e3) → "1200.0" ◮ int("02") → 2 ◮ float("0") → 0.0

Converting double to int rounds towards zero (+ down, − up) Run-time error if a string could not be converted:

◮ n = int("hello")

# CRASHES with ValueError

◮ p = int("3.2")

# CRASHES, but int(float("3.2")) is OK

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 13 / 26

slide-81
SLIDE 81

Converting between types

Converting between types is also called type casting. Write the name of the type you are converting to, then the expression to convert in parentheses:

◮ float(2) → 2.0 ◮ int(3.14) → 3 ◮ str(1.2e3) → "1200.0" ◮ int("02") → 2 ◮ float("0") → 0.0

Converting double to int rounds towards zero (+ down, − up) Run-time error if a string could not be converted:

◮ n = int("hello")

# CRASHES with ValueError

◮ p = int("3.2")

# CRASHES, but int(float("3.2")) is OK

Converting a string does not do arithmetic:

◮ half = float("1/2")

# CRASHES

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 13 / 26

slide-82
SLIDE 82

Converting between types

Converting between types is also called type casting. Write the name of the type you are converting to, then the expression to convert in parentheses:

◮ float(2) → 2.0 ◮ int(3.14) → 3 ◮ str(1.2e3) → "1200.0" ◮ int("02") → 2 ◮ float("0") → 0.0

Converting double to int rounds towards zero (+ down, − up) Run-time error if a string could not be converted:

◮ n = int("hello")

# CRASHES with ValueError

◮ p = int("3.2")

# CRASHES, but int(float("3.2")) is OK

Converting a string does not do arithmetic:

◮ half = float("1/2")

# CRASHES

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 13 / 26

slide-83
SLIDE 83

Output: print

Every program needs to do output of some kind: to the screen, a file, etc. In Python, we use the print function. Sends output to “standard output”.

◮ This is usually the shell window. ◮ Or the window that appears when you double-click a Python program. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 14 / 26

slide-84
SLIDE 84

Output: print

Every program needs to do output of some kind: to the screen, a file, etc. In Python, we use the print function. Sends output to “standard output”.

◮ This is usually the shell window. ◮ Or the window that appears when you double-click a Python program.

Syntax: print(arguments )

◮ arguments is a comma-separated list of things to print. ⋆ Can have zero, one, or more arguments. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 14 / 26

slide-85
SLIDE 85

Output: print

Every program needs to do output of some kind: to the screen, a file, etc. In Python, we use the print function. Sends output to “standard output”.

◮ This is usually the shell window. ◮ Or the window that appears when you double-click a Python program.

Syntax: print(arguments )

◮ arguments is a comma-separated list of things to print. ⋆ Can have zero, one, or more arguments. ◮ Each argument can be a literal, variable, expression, . . . ◮ Arguments can be any type: string, integer, float, . . . Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 14 / 26

slide-86
SLIDE 86

Output: print

Every program needs to do output of some kind: to the screen, a file, etc. In Python, we use the print function. Sends output to “standard output”.

◮ This is usually the shell window. ◮ Or the window that appears when you double-click a Python program.

Syntax: print(arguments )

◮ arguments is a comma-separated list of things to print. ⋆ Can have zero, one, or more arguments. ◮ Each argument can be a literal, variable, expression, . . . ◮ Arguments can be any type: string, integer, float, . . . ⋆ print("Welcome to my program") Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 14 / 26

slide-87
SLIDE 87

Output: print

Every program needs to do output of some kind: to the screen, a file, etc. In Python, we use the print function. Sends output to “standard output”.

◮ This is usually the shell window. ◮ Or the window that appears when you double-click a Python program.

Syntax: print(arguments )

◮ arguments is a comma-separated list of things to print. ⋆ Can have zero, one, or more arguments. ◮ Each argument can be a literal, variable, expression, . . . ◮ Arguments can be any type: string, integer, float, . . . ⋆ print("Welcome to my program") ⋆ print(6 * 7) Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 14 / 26

slide-88
SLIDE 88

Output: print

Every program needs to do output of some kind: to the screen, a file, etc. In Python, we use the print function. Sends output to “standard output”.

◮ This is usually the shell window. ◮ Or the window that appears when you double-click a Python program.

Syntax: print(arguments )

◮ arguments is a comma-separated list of things to print. ⋆ Can have zero, one, or more arguments. ◮ Each argument can be a literal, variable, expression, . . . ◮ Arguments can be any type: string, integer, float, . . . ⋆ print("Welcome to my program") ⋆ print(6 * 7) ⋆ print("Hello", name) Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 14 / 26

slide-89
SLIDE 89

Output: print

Every program needs to do output of some kind: to the screen, a file, etc. In Python, we use the print function. Sends output to “standard output”.

◮ This is usually the shell window. ◮ Or the window that appears when you double-click a Python program.

Syntax: print(arguments )

◮ arguments is a comma-separated list of things to print. ⋆ Can have zero, one, or more arguments. ◮ Each argument can be a literal, variable, expression, . . . ◮ Arguments can be any type: string, integer, float, . . . ⋆ print("Welcome to my program") ⋆ print(6 * 7) ⋆ print("Hello", name) ⋆ print() Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 14 / 26

slide-90
SLIDE 90

Output: print

Every program needs to do output of some kind: to the screen, a file, etc. In Python, we use the print function. Sends output to “standard output”.

◮ This is usually the shell window. ◮ Or the window that appears when you double-click a Python program.

Syntax: print(arguments )

◮ arguments is a comma-separated list of things to print. ⋆ Can have zero, one, or more arguments. ◮ Each argument can be a literal, variable, expression, . . . ◮ Arguments can be any type: string, integer, float, . . . ⋆ print("Welcome to my program") ⋆ print(6 * 7) ⋆ print("Hello", name) ⋆ print() Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 14 / 26

slide-91
SLIDE 91

Semantics of print

Evaluates the arguments (computes their values). Prints values to standard output, starting at the cursor.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 15 / 26

slide-92
SLIDE 92

Semantics of print

Evaluates the arguments (computes their values). Prints values to standard output, starting at the cursor. If multiple arguments are given, puts a space between.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 15 / 26

slide-93
SLIDE 93

Semantics of print

Evaluates the arguments (computes their values). Prints values to standard output, starting at the cursor. If multiple arguments are given, puts a space between. Outputs a “newline” character at the end.

◮ Moves the cursor to the beginning of the next line. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 15 / 26

slide-94
SLIDE 94

Semantics of print

Evaluates the arguments (computes their values). Prints values to standard output, starting at the cursor. If multiple arguments are given, puts a space between. Outputs a “newline” character at the end.

◮ Moves the cursor to the beginning of the next line. ◮ No-argument print() prints just a newline. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 15 / 26

slide-95
SLIDE 95

Semantics of print

Evaluates the arguments (computes their values). Prints values to standard output, starting at the cursor. If multiple arguments are given, puts a space between. Outputs a “newline” character at the end.

◮ Moves the cursor to the beginning of the next line. ◮ No-argument print() prints just a newline.

The print function does not return a value.

◮ That means you can’t (usefully) use it in an expression:

x = print(2) # BAD

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 15 / 26

slide-96
SLIDE 96

Semantics of print

Evaluates the arguments (computes their values). Prints values to standard output, starting at the cursor. If multiple arguments are given, puts a space between. Outputs a “newline” character at the end.

◮ Moves the cursor to the beginning of the next line. ◮ No-argument print() prints just a newline.

The print function does not return a value.

◮ That means you can’t (usefully) use it in an expression:

x = print(2) # BAD

⋆ This isn’t a syntax error, but x’s value will be None. ⋆ Not very useful: usually a semantic error. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 15 / 26

slide-97
SLIDE 97

Semantics of print

Evaluates the arguments (computes their values). Prints values to standard output, starting at the cursor. If multiple arguments are given, puts a space between. Outputs a “newline” character at the end.

◮ Moves the cursor to the beginning of the next line. ◮ No-argument print() prints just a newline.

The print function does not return a value.

◮ That means you can’t (usefully) use it in an expression:

x = print(2) # BAD

⋆ This isn’t a syntax error, but x’s value will be None. ⋆ Not very useful: usually a semantic error. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 15 / 26

slide-98
SLIDE 98

Extra arguments to print

Sometimes you don’t want spaces between the arguments,

  • r don’t want a newline at the end.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 16 / 26

slide-99
SLIDE 99

Extra arguments to print

Sometimes you don’t want spaces between the arguments,

  • r don’t want a newline at the end.

You can control these with so-called keyword arguments. sep=string : Use string to separate arguments instead of space.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 16 / 26

slide-100
SLIDE 100

Extra arguments to print

Sometimes you don’t want spaces between the arguments,

  • r don’t want a newline at the end.

You can control these with so-called keyword arguments. sep=string : Use string to separate arguments instead of space.

◮ print(month, day, year, sep = "/") ⋆ Might output: 1/27/2015 Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 16 / 26

slide-101
SLIDE 101

Extra arguments to print

Sometimes you don’t want spaces between the arguments,

  • r don’t want a newline at the end.

You can control these with so-called keyword arguments. sep=string : Use string to separate arguments instead of space.

◮ print(month, day, year, sep = "/") ⋆ Might output: 1/27/2015

end=string : Print string at the end, not newline.

◮ That means the next print will start on the same line: ⋆ print("The answer is", end=":")

print(answer)

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 16 / 26

slide-102
SLIDE 102

Extra arguments to print

Sometimes you don’t want spaces between the arguments,

  • r don’t want a newline at the end.

You can control these with so-called keyword arguments. sep=string : Use string to separate arguments instead of space.

◮ print(month, day, year, sep = "/") ⋆ Might output: 1/27/2015

end=string : Print string at the end, not newline.

◮ That means the next print will start on the same line: ⋆ print("The answer is", end=":")

print(answer)

⋆ Output: The answer is:42

Either string can be empty:

◮ print(f initial, m initial, l initial, sep=’’) Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 16 / 26

slide-103
SLIDE 103

Extra arguments to print

Sometimes you don’t want spaces between the arguments,

  • r don’t want a newline at the end.

You can control these with so-called keyword arguments. sep=string : Use string to separate arguments instead of space.

◮ print(month, day, year, sep = "/") ⋆ Might output: 1/27/2015

end=string : Print string at the end, not newline.

◮ That means the next print will start on the same line: ⋆ print("The answer is", end=":")

print(answer)

⋆ Output: The answer is:42

Either string can be empty:

◮ print(f initial, m initial, l initial, sep=’’) ◮ Output: NFM Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 16 / 26

slide-104
SLIDE 104

Extra arguments to print

Sometimes you don’t want spaces between the arguments,

  • r don’t want a newline at the end.

You can control these with so-called keyword arguments. sep=string : Use string to separate arguments instead of space.

◮ print(month, day, year, sep = "/") ⋆ Might output: 1/27/2015

end=string : Print string at the end, not newline.

◮ That means the next print will start on the same line: ⋆ print("The answer is", end=":")

print(answer)

⋆ Output: The answer is:42

Either string can be empty:

◮ print(f initial, m initial, l initial, sep=’’) ◮ Output: NFM

Can use both, but keyword arguments must come at the end!

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 16 / 26

slide-105
SLIDE 105

Extra arguments to print

Sometimes you don’t want spaces between the arguments,

  • r don’t want a newline at the end.

You can control these with so-called keyword arguments. sep=string : Use string to separate arguments instead of space.

◮ print(month, day, year, sep = "/") ⋆ Might output: 1/27/2015

end=string : Print string at the end, not newline.

◮ That means the next print will start on the same line: ⋆ print("The answer is", end=":")

print(answer)

⋆ Output: The answer is:42

Either string can be empty:

◮ print(f initial, m initial, l initial, sep=’’) ◮ Output: NFM

Can use both, but keyword arguments must come at the end!

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 16 / 26

slide-106
SLIDE 106

Input

Most programs also need to get input from the user. In Python we do this with input function. Syntax: input(prompt)

◮ One argument (unlike print) ◮ The argument is optional: input() Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 17 / 26

slide-107
SLIDE 107

Input

Most programs also need to get input from the user. In Python we do this with input function. Syntax: input(prompt)

◮ One argument (unlike print) ◮ The argument is optional: input()

Returns (evaluates to) a string.

◮ Usually used with assignment:

name = input("What is your name? ")

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 17 / 26

slide-108
SLIDE 108

Input

Most programs also need to get input from the user. In Python we do this with input function. Syntax: input(prompt)

◮ One argument (unlike print) ◮ The argument is optional: input()

Returns (evaluates to) a string.

◮ Usually used with assignment:

name = input("What is your name? ")

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 17 / 26

slide-109
SLIDE 109

Semantics of input

The input function first prints the prompt.

◮ Without adding a newline! Usually ends in a space:

name = input("What is your name? ")

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 18 / 26

slide-110
SLIDE 110

Semantics of input

The input function first prints the prompt.

◮ Without adding a newline! Usually ends in a space:

name = input("What is your name? ")

◮ Include a newline \n to get input on its own line:

name = input("What is your name?\n")

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 18 / 26

slide-111
SLIDE 111

Semantics of input

The input function first prints the prompt.

◮ Without adding a newline! Usually ends in a space:

name = input("What is your name? ")

◮ Include a newline \n to get input on its own line:

name = input("What is your name?\n")

◮ If no prompt was given, prints nothing. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 18 / 26

slide-112
SLIDE 112

Semantics of input

The input function first prints the prompt.

◮ Without adding a newline! Usually ends in a space:

name = input("What is your name? ")

◮ Include a newline \n to get input on its own line:

name = input("What is your name?\n")

◮ If no prompt was given, prints nothing.

Pauses the execution of the program, displays a blinking cursor.

◮ Waits for the user to press Enter. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 18 / 26

slide-113
SLIDE 113

Semantics of input

The input function first prints the prompt.

◮ Without adding a newline! Usually ends in a space:

name = input("What is your name? ")

◮ Include a newline \n to get input on its own line:

name = input("What is your name?\n")

◮ If no prompt was given, prints nothing.

Pauses the execution of the program, displays a blinking cursor.

◮ Waits for the user to press Enter.

Returns the entire line of input.

◮ If the user pressed just Enter, returns an empty string. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 18 / 26

slide-114
SLIDE 114

Semantics of input

The input function first prints the prompt.

◮ Without adding a newline! Usually ends in a space:

name = input("What is your name? ")

◮ Include a newline \n to get input on its own line:

name = input("What is your name?\n")

◮ If no prompt was given, prints nothing.

Pauses the execution of the program, displays a blinking cursor.

◮ Waits for the user to press Enter.

Returns the entire line of input.

◮ If the user pressed just Enter, returns an empty string. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 18 / 26

slide-115
SLIDE 115

Using input

The function returns a string value.

◮ Usually used as the right hand side of an assignment.

name = input("What is your name? ")

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 19 / 26

slide-116
SLIDE 116

Using input

The function returns a string value.

◮ Usually used as the right hand side of an assignment.

name = input("What is your name? ")

◮ If you don’t, throws away the input!

input("Press Enter to continue.")

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 19 / 26

slide-117
SLIDE 117

Using input

The function returns a string value.

◮ Usually used as the right hand side of an assignment.

name = input("What is your name? ")

◮ If you don’t, throws away the input!

input("Press Enter to continue.")

What if you need a number instead?

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 19 / 26

slide-118
SLIDE 118

Using input

The function returns a string value.

◮ Usually used as the right hand side of an assignment.

name = input("What is your name? ")

◮ If you don’t, throws away the input!

input("Press Enter to continue.")

What if you need a number instead?

◮ Combine it with type casting!

age = int(input("How old are you? "))

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 19 / 26

slide-119
SLIDE 119

Using input

The function returns a string value.

◮ Usually used as the right hand side of an assignment.

name = input("What is your name? ")

◮ If you don’t, throws away the input!

input("Press Enter to continue.")

What if you need a number instead?

◮ Combine it with type casting!

age = int(input("How old are you? ")) temp = float(input("What is the temperature? "))

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 19 / 26

slide-120
SLIDE 120

Using input

The function returns a string value.

◮ Usually used as the right hand side of an assignment.

name = input("What is your name? ")

◮ If you don’t, throws away the input!

input("Press Enter to continue.")

What if you need a number instead?

◮ Combine it with type casting!

age = int(input("How old are you? ")) temp = float(input("What is the temperature? "))

◮ What if the input cannot be converted? Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 19 / 26

slide-121
SLIDE 121

Using input

The function returns a string value.

◮ Usually used as the right hand side of an assignment.

name = input("What is your name? ")

◮ If you don’t, throws away the input!

input("Press Enter to continue.")

What if you need a number instead?

◮ Combine it with type casting!

age = int(input("How old are you? ")) temp = float(input("What is the temperature? "))

◮ What if the input cannot be converted? ⋆ Run-time error (ValueError exception) Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 19 / 26

slide-122
SLIDE 122

Using input

The function returns a string value.

◮ Usually used as the right hand side of an assignment.

name = input("What is your name? ")

◮ If you don’t, throws away the input!

input("Press Enter to continue.")

What if you need a number instead?

◮ Combine it with type casting!

age = int(input("How old are you? ")) temp = float(input("What is the temperature? "))

◮ What if the input cannot be converted? ⋆ Run-time error (ValueError exception) Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 19 / 26

slide-123
SLIDE 123

Testing

We now know enough Python to write a simple program. But how do you know if the program works correctly?

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 20 / 26

slide-124
SLIDE 124

Testing

We now know enough Python to write a simple program. But how do you know if the program works correctly? Testing!

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 20 / 26

slide-125
SLIDE 125

Testing

We now know enough Python to write a simple program. But how do you know if the program works correctly? Testing! Verify that the progam:

◮ Gives the correct outputs. ◮ Doesn’t crash unexpectedly. ◮ Doesn’t run forever (infinite loop). Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 20 / 26

slide-126
SLIDE 126

Testing

We now know enough Python to write a simple program. But how do you know if the program works correctly? Testing! Verify that the progam:

◮ Gives the correct outputs. ◮ Doesn’t crash unexpectedly. ◮ Doesn’t run forever (infinite loop).

For a four- or five-line program, could verify by inspection.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 20 / 26

slide-127
SLIDE 127

Testing

We now know enough Python to write a simple program. But how do you know if the program works correctly? Testing! Verify that the progam:

◮ Gives the correct outputs. ◮ Doesn’t crash unexpectedly. ◮ Doesn’t run forever (infinite loop).

For a four- or five-line program, could verify by inspection.

◮ But once it gets longer than that, needs systematic testing. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 20 / 26

slide-128
SLIDE 128

Testing

We now know enough Python to write a simple program. But how do you know if the program works correctly? Testing! Verify that the progam:

◮ Gives the correct outputs. ◮ Doesn’t crash unexpectedly. ◮ Doesn’t run forever (infinite loop).

For a four- or five-line program, could verify by inspection.

◮ But once it gets longer than that, needs systematic testing.

Could plug in some random value and check the output.

◮ But what if we missed something? Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 20 / 26

slide-129
SLIDE 129

Testing

We now know enough Python to write a simple program. But how do you know if the program works correctly? Testing! Verify that the progam:

◮ Gives the correct outputs. ◮ Doesn’t crash unexpectedly. ◮ Doesn’t run forever (infinite loop).

For a four- or five-line program, could verify by inspection.

◮ But once it gets longer than that, needs systematic testing.

Could plug in some random value and check the output.

◮ But what if we missed something? ◮ Need a plan. . . Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 20 / 26

slide-130
SLIDE 130

Test cases

We will test our programs by trying out a number of test cases.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 21 / 26

slide-131
SLIDE 131

Test cases

We will test our programs by trying out a number of test cases. A typical test case has four parts:

◮ Description: what are you testing for? ◮ Input data you will give to the program. ◮ The expected output or outcome from that input. ◮ The actual output or outcome from that input. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 21 / 26

slide-132
SLIDE 132

Test cases

We will test our programs by trying out a number of test cases. A typical test case has four parts:

◮ Description: what are you testing for? ◮ Input data you will give to the program. ◮ The expected output or outcome from that input. ◮ The actual output or outcome from that input.

Do the first three parts before writing the program.

◮ Then fill out the actual output by running the program. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 21 / 26

slide-133
SLIDE 133

Test cases

We will test our programs by trying out a number of test cases. A typical test case has four parts:

◮ Description: what are you testing for? ◮ Input data you will give to the program. ◮ The expected output or outcome from that input. ◮ The actual output or outcome from that input.

Do the first three parts before writing the program.

◮ Then fill out the actual output by running the program. ◮ In a software company, the last step is often done by dedicated testers

(not the author).

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 21 / 26

slide-134
SLIDE 134

Test cases

We will test our programs by trying out a number of test cases. A typical test case has four parts:

◮ Description: what are you testing for? ◮ Input data you will give to the program. ◮ The expected output or outcome from that input. ◮ The actual output or outcome from that input.

Do the first three parts before writing the program.

◮ Then fill out the actual output by running the program. ◮ In a software company, the last step is often done by dedicated testers

(not the author).

◮ In CS 115, we’ll usually omit the “actual output”. ⋆ If it’s different from the expected output, you have a bug. ⋆ Fix the bugs before turning in the program. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 21 / 26

slide-135
SLIDE 135

Test cases

We will test our programs by trying out a number of test cases. A typical test case has four parts:

◮ Description: what are you testing for? ◮ Input data you will give to the program. ◮ The expected output or outcome from that input. ◮ The actual output or outcome from that input.

Do the first three parts before writing the program.

◮ Then fill out the actual output by running the program. ◮ In a software company, the last step is often done by dedicated testers

(not the author).

◮ In CS 115, we’ll usually omit the “actual output”. ⋆ If it’s different from the expected output, you have a bug. ⋆ Fix the bugs before turning in the program. ⋆ At the very least, document the bug with a comment. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 21 / 26

slide-136
SLIDE 136

Test cases

We will test our programs by trying out a number of test cases. A typical test case has four parts:

◮ Description: what are you testing for? ◮ Input data you will give to the program. ◮ The expected output or outcome from that input. ◮ The actual output or outcome from that input.

Do the first three parts before writing the program.

◮ Then fill out the actual output by running the program. ◮ In a software company, the last step is often done by dedicated testers

(not the author).

◮ In CS 115, we’ll usually omit the “actual output”. ⋆ If it’s different from the expected output, you have a bug. ⋆ Fix the bugs before turning in the program. ⋆ At the very least, document the bug with a comment. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 21 / 26

slide-137
SLIDE 137

Test plan

A test plan is a table with a number of test cases. Quality is more important than quantity! Test cases shouldn’t overlap too much.

◮ If all your tests use positive numbers, how will you know whether

negative numbers work?

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 22 / 26

slide-138
SLIDE 138

Test plan

A test plan is a table with a number of test cases. Quality is more important than quantity! Test cases shouldn’t overlap too much.

◮ If all your tests use positive numbers, how will you know whether

negative numbers work?

Making a good test plan requires thought and attention to the problem specifications.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 22 / 26

slide-139
SLIDE 139

Test plan

A test plan is a table with a number of test cases. Quality is more important than quantity! Test cases shouldn’t overlap too much.

◮ If all your tests use positive numbers, how will you know whether

negative numbers work?

Making a good test plan requires thought and attention to the problem specifications. You should identify and test:

◮ Normal cases. ◮ Special cases. ◮ Boundary cases. ◮ Error cases. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 22 / 26

slide-140
SLIDE 140

Test plan

A test plan is a table with a number of test cases. Quality is more important than quantity! Test cases shouldn’t overlap too much.

◮ If all your tests use positive numbers, how will you know whether

negative numbers work?

Making a good test plan requires thought and attention to the problem specifications. You should identify and test:

◮ Normal cases. ◮ Special cases. ◮ Boundary cases. ◮ Error cases. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 22 / 26

slide-141
SLIDE 141

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R).

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 23 / 26

slide-142
SLIDE 142

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 23 / 26

slide-143
SLIDE 143

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 23 / 26

slide-144
SLIDE 144

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke. Inexact change D C

  • Neil Moore (UK CS)

CS 115 Lecture 4 Fall 2015 23 / 26

slide-145
SLIDE 145

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke. Inexact change D C

  • Vend one Coke, return
  • ne quarter.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 23 / 26

slide-146
SLIDE 146

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke. Inexact change D C

  • Vend one Coke, return
  • ne quarter.

Not enough Q Q C

  • Neil Moore (UK CS)

CS 115 Lecture 4 Fall 2015 23 / 26

slide-147
SLIDE 147

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke. Inexact change D C

  • Vend one Coke, return
  • ne quarter.

Not enough Q Q C

  • Flash “0.75”.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 23 / 26

slide-148
SLIDE 148

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke. Inexact change D C

  • Vend one Coke, return
  • ne quarter.

Not enough Q Q C

  • Flash “0.75”.

Continue Q C D C

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 23 / 26

slide-149
SLIDE 149

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke. Inexact change D C

  • Vend one Coke, return
  • ne quarter.

Not enough Q Q C

  • Flash “0.75”.

Continue Q C D C Flash “0.75”, vend one Coke, return two quarters.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 23 / 26

slide-150
SLIDE 150

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke. Inexact change D C

  • Vend one Coke, return
  • ne quarter.

Not enough Q Q C

  • Flash “0.75”.

Continue Q C D C Flash “0.75”, vend one Coke, return two quarters. Refund Q Q R

  • Neil Moore (UK CS)

CS 115 Lecture 4 Fall 2015 23 / 26

slide-151
SLIDE 151

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke. Inexact change D C

  • Vend one Coke, return
  • ne quarter.

Not enough Q Q C

  • Flash “0.75”.

Continue Q C D C Flash “0.75”, vend one Coke, return two quarters. Refund Q Q R

  • Return two quarters.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 23 / 26

slide-152
SLIDE 152

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke. Inexact change D C

  • Vend one Coke, return
  • ne quarter.

Not enough Q Q C

  • Flash “0.75”.

Continue Q C D C Flash “0.75”, vend one Coke, return two quarters. Refund Q Q R

  • Return two quarters.

Refund, no money R

  • Neil Moore (UK CS)

CS 115 Lecture 4 Fall 2015 23 / 26

slide-153
SLIDE 153

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke. Inexact change D C

  • Vend one Coke, return
  • ne quarter.

Not enough Q Q C

  • Flash “0.75”.

Continue Q C D C Flash “0.75”, vend one Coke, return two quarters. Refund Q Q R

  • Return two quarters.

Refund, no money R

  • Do nothing.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 23 / 26

slide-154
SLIDE 154

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke. Inexact change D C

  • Vend one Coke, return
  • ne quarter.

Not enough Q Q C

  • Flash “0.75”.

Continue Q C D C Flash “0.75”, vend one Coke, return two quarters. Refund Q Q R

  • Return two quarters.

Refund, no money R

  • Do nothing.

Even more test cases could be described. . .

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 23 / 26

slide-155
SLIDE 155

Sample test plan

Suppose you are writing code for a vending machine. Inputs are quarters (Q, 25 cents), dollars (D, 100 cents), Coke button (C, costs 75 cents), and Refund button (R). Description Inputs Expected output Exact change Q Q Q C Vend one Coke. Inexact change D C

  • Vend one Coke, return
  • ne quarter.

Not enough Q Q C

  • Flash “0.75”.

Continue Q C D C Flash “0.75”, vend one Coke, return two quarters. Refund Q Q R

  • Return two quarters.

Refund, no money R

  • Do nothing.

Even more test cases could be described. . .

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 23 / 26

slide-156
SLIDE 156

Off-by-one errors

You need to build a fence 100 feet long, with a fence post every 10 feet. How many posts do you need?

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 24 / 26

slide-157
SLIDE 157

Off-by-one errors

You need to build a fence 100 feet long, with a fence post every 10 feet. How many posts do you need? Need 11, not 10!

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 24 / 26

slide-158
SLIDE 158

Off-by-one errors

You need to build a fence 100 feet long, with a fence post every 10 feet. How many posts do you need? Need 11, not 10! This is a common source of errors in programming.

◮ “Fencepost errors” or “off-by-one errors”. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 24 / 26

slide-159
SLIDE 159

Off-by-one errors

You need to build a fence 100 feet long, with a fence post every 10 feet. How many posts do you need? Need 11, not 10! This is a common source of errors in programming.

◮ “Fencepost errors” or “off-by-one errors”.

Whenever your program involves ranges (1–10, letters “L”–“R”):

◮ Test the boundary cases. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 24 / 26

slide-160
SLIDE 160

Off-by-one errors

You need to build a fence 100 feet long, with a fence post every 10 feet. How many posts do you need? Need 11, not 10! This is a common source of errors in programming.

◮ “Fencepost errors” or “off-by-one errors”.

Whenever your program involves ranges (1–10, letters “L”–“R”):

◮ Test the boundary cases. ◮ Not just the endpoints, also adjacent numbers. ⋆ So 0, 1, 2 (lower boundary), 9, 10, 11 (upper). ⋆ “K”, “L”, “M” and “Q”, “R”, “S”. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 24 / 26

slide-161
SLIDE 161

Off-by-one errors

You need to build a fence 100 feet long, with a fence post every 10 feet. How many posts do you need? Need 11, not 10! This is a common source of errors in programming.

◮ “Fencepost errors” or “off-by-one errors”.

Whenever your program involves ranges (1–10, letters “L”–“R”):

◮ Test the boundary cases. ◮ Not just the endpoints, also adjacent numbers. ⋆ So 0, 1, 2 (lower boundary), 9, 10, 11 (upper). ⋆ “K”, “L”, “M” and “Q”, “R”, “S”.

Why test boundary cases?

◮ It’s easy to miss one endpoint. ◮ Or to go too far, past the endpoint. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 24 / 26

slide-162
SLIDE 162

Off-by-one errors

You need to build a fence 100 feet long, with a fence post every 10 feet. How many posts do you need? Need 11, not 10! This is a common source of errors in programming.

◮ “Fencepost errors” or “off-by-one errors”.

Whenever your program involves ranges (1–10, letters “L”–“R”):

◮ Test the boundary cases. ◮ Not just the endpoints, also adjacent numbers. ⋆ So 0, 1, 2 (lower boundary), 9, 10, 11 (upper). ⋆ “K”, “L”, “M” and “Q”, “R”, “S”.

Why test boundary cases?

◮ It’s easy to miss one endpoint. ◮ Or to go too far, past the endpoint. ◮ Make sure in-range inputs are accepted. ◮ Make sure out-of-range inputs are rejected. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 24 / 26

slide-163
SLIDE 163

Off-by-one errors

You need to build a fence 100 feet long, with a fence post every 10 feet. How many posts do you need? Need 11, not 10! This is a common source of errors in programming.

◮ “Fencepost errors” or “off-by-one errors”.

Whenever your program involves ranges (1–10, letters “L”–“R”):

◮ Test the boundary cases. ◮ Not just the endpoints, also adjacent numbers. ⋆ So 0, 1, 2 (lower boundary), 9, 10, 11 (upper). ⋆ “K”, “L”, “M” and “Q”, “R”, “S”.

Why test boundary cases?

◮ It’s easy to miss one endpoint. ◮ Or to go too far, past the endpoint. ◮ Make sure in-range inputs are accepted. ◮ Make sure out-of-range inputs are rejected. Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 24 / 26

slide-164
SLIDE 164

Regression testing

What happens when you find a bug? When running your tests, you find an error on test 5.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 25 / 26

slide-165
SLIDE 165

Regression testing

What happens when you find a bug? When running your tests, you find an error on test 5.

◮ So you fix the bug in your program. ◮ Now what? Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 25 / 26

slide-166
SLIDE 166

Regression testing

What happens when you find a bug? When running your tests, you find an error on test 5.

◮ So you fix the bug in your program. ◮ Now what? Run test 5 again? ⋆ Make sure you actually fixed it! Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 25 / 26

slide-167
SLIDE 167

Regression testing

What happens when you find a bug? When running your tests, you find an error on test 5.

◮ So you fix the bug in your program. ◮ Now what? Run test 5 again? ⋆ Make sure you actually fixed it!

What about tests 1–4?

◮ Those tests passed already, right? Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 25 / 26

slide-168
SLIDE 168

Regression testing

What happens when you find a bug? When running your tests, you find an error on test 5.

◮ So you fix the bug in your program. ◮ Now what? Run test 5 again? ⋆ Make sure you actually fixed it!

What about tests 1–4?

◮ Those tests passed already, right? ◮ But what if your change broke something? Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 25 / 26

slide-169
SLIDE 169

Regression testing

What happens when you find a bug? When running your tests, you find an error on test 5.

◮ So you fix the bug in your program. ◮ Now what? Run test 5 again? ⋆ Make sure you actually fixed it!

What about tests 1–4?

◮ Those tests passed already, right? ◮ But what if your change broke something?

A regression is when something used to work, but doesn’t anymore.

◮ Because you (or Python, the OS, . . . ) changed something. ◮ How to avoid regressions? Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 25 / 26

slide-170
SLIDE 170

Regression testing

What happens when you find a bug? When running your tests, you find an error on test 5.

◮ So you fix the bug in your program. ◮ Now what? Run test 5 again? ⋆ Make sure you actually fixed it!

What about tests 1–4?

◮ Those tests passed already, right? ◮ But what if your change broke something?

A regression is when something used to work, but doesn’t anymore.

◮ Because you (or Python, the OS, . . . ) changed something. ◮ How to avoid regressions?

Regression testing: whenever you change the code, go back to the beginning and repeat all the tests.

◮ To make sure you didn’t just add a bug too! ◮ This will save you points on CS 115 programs! Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 25 / 26

slide-171
SLIDE 171

Regression testing

What happens when you find a bug? When running your tests, you find an error on test 5.

◮ So you fix the bug in your program. ◮ Now what? Run test 5 again? ⋆ Make sure you actually fixed it!

What about tests 1–4?

◮ Those tests passed already, right? ◮ But what if your change broke something?

A regression is when something used to work, but doesn’t anymore.

◮ Because you (or Python, the OS, . . . ) changed something. ◮ How to avoid regressions?

Regression testing: whenever you change the code, go back to the beginning and repeat all the tests.

◮ To make sure you didn’t just add a bug too! ◮ This will save you points on CS 115 programs! Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 25 / 26

slide-172
SLIDE 172

The end

Next time: Libraries and the math library. Putting it all together: writing a complete program.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 26 / 26

slide-173
SLIDE 173

The end

Next time: Libraries and the math library. Putting it all together: writing a complete program.

Neil Moore (UK CS) CS 115 Lecture 4 Fall 2015 26 / 26