CS 115 Lecture 3 A first look at Python Neil Moore Department of - - PowerPoint PPT Presentation

cs 115 lecture 3
SMART_READER_LITE
LIVE PREVIEW

CS 115 Lecture 3 A first look at Python Neil Moore Department of - - PowerPoint PPT Presentation

CS 115 Lecture 3 A first look at Python Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 3 September 2015 Getting Python and WingIDE Instructions for installing Python and WingIDE


slide-1
SLIDE 1

CS 115 Lecture 3

A first look at Python Neil Moore

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

3 September 2015

slide-2
SLIDE 2

Getting Python and WingIDE

Instructions for installing Python and WingIDE 101 are on the web page: http://www.cs.uky.edu/~keen/help/installingpython.html We’ll use WingIDE today.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 2 / 22

slide-3
SLIDE 3

Changing the font in WingIDE

Use a big font (18 or 20 point) for labs! It’s easier for both us and your teammates.

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

slide-4
SLIDE 4

Changing the font in WingIDE

Use a big font (18 or 20 point) for labs! It’s easier for both us and your teammates. Edit → Preferences Under “User Interface”, select “Fonts”

◮ May be in a slightly different location on Mac OS.

Next to “Display Font/Size”:

◮ “Use selected”, then “Change”. ◮ Select a size and click “OK”. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 3 / 22

slide-5
SLIDE 5

Changing the font in WingIDE

Use a big font (18 or 20 point) for labs! It’s easier for both us and your teammates. Edit → Preferences Under “User Interface”, select “Fonts”

◮ May be in a slightly different location on Mac OS.

Next to “Display Font/Size”:

◮ “Use selected”, then “Change”. ◮ Select a size and click “OK”.

For “Editor Font/Size” (controls your code’s font):

◮ Either do the same. . . ◮ Or select “Match Display Font/Size” Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 3 / 22

slide-6
SLIDE 6

Changing the font in WingIDE

Use a big font (18 or 20 point) for labs! It’s easier for both us and your teammates. Edit → Preferences Under “User Interface”, select “Fonts”

◮ May be in a slightly different location on Mac OS.

Next to “Display Font/Size”:

◮ “Use selected”, then “Change”. ◮ Select a size and click “OK”.

For “Editor Font/Size” (controls your code’s font):

◮ Either do the same. . . ◮ Or select “Match Display Font/Size” ◮ Many people prefer a monospace font for code. ⋆ Consolas, Lucida Console, Courier New, . . . Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 3 / 22

slide-7
SLIDE 7

Changing the font in WingIDE

Use a big font (18 or 20 point) for labs! It’s easier for both us and your teammates. Edit → Preferences Under “User Interface”, select “Fonts”

◮ May be in a slightly different location on Mac OS.

Next to “Display Font/Size”:

◮ “Use selected”, then “Change”. ◮ Select a size and click “OK”.

For “Editor Font/Size” (controls your code’s font):

◮ Either do the same. . . ◮ Or select “Match Display Font/Size” ◮ Many people prefer a monospace font for code. ⋆ Consolas, Lucida Console, Courier New, . . . Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 3 / 22

slide-8
SLIDE 8

A first Python program, with bugs

# Compute the greatest common divisor (GCD) of two numbers. def main(): # Inputs: two positive integers (whole numbers) a and b. a = input("Please enter a first number: ") b = input("Please enter another number: ") # 1. Repeat as long as b is not zero: while b != 0: # 1.1. If a > b, then set a <- (a - b) if a > b: a = a - b # 1.2. Otherwise, set b <- (b - a) else: b = b - b # 2. Output a as the answer. print("The GCM of your numbers is", a) main()

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

slide-9
SLIDE 9

Structure of a Python program

def main():

◮ This is the “main function” where the program does all its work ⋆ (for now) ◮ More about functions in chapter 5. ◮ Python doesn’t need a main function, but use one in this class! ⋆ (It’s good practice for later.) Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 5 / 22

slide-10
SLIDE 10

Structure of a Python program

def main():

◮ This is the “main function” where the program does all its work ⋆ (for now) ◮ More about functions in chapter 5. ◮ Python doesn’t need a main function, but use one in this class! ⋆ (It’s good practice for later.)

Indentation and blocks.

◮ Code is arranged in indented blocks. ◮ The body of main is one block. ◮ It has several blocks inside. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 5 / 22

slide-11
SLIDE 11

Structure of a Python program

def main():

◮ This is the “main function” where the program does all its work ⋆ (for now) ◮ More about functions in chapter 5. ◮ Python doesn’t need a main function, but use one in this class! ⋆ (It’s good practice for later.)

Indentation and blocks.

◮ Code is arranged in indented blocks. ◮ The body of main is one block. ◮ It has several blocks inside.

main()

◮ Calls the main function. ◮ Not inside the main function. ⋆ The main() is not indented at all! Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 5 / 22

slide-12
SLIDE 12

Structure of a Python program

def main():

◮ This is the “main function” where the program does all its work ⋆ (for now) ◮ More about functions in chapter 5. ◮ Python doesn’t need a main function, but use one in this class! ⋆ (It’s good practice for later.)

Indentation and blocks.

◮ Code is arranged in indented blocks. ◮ The body of main is one block. ◮ It has several blocks inside.

main()

◮ Calls the main function. ◮ Not inside the main function. ⋆ The main() is not indented at all! ◮ If you forget this line, the program doesn’t do anything! Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 5 / 22

slide-13
SLIDE 13

Structure of a Python program

def main():

◮ This is the “main function” where the program does all its work ⋆ (for now) ◮ More about functions in chapter 5. ◮ Python doesn’t need a main function, but use one in this class! ⋆ (It’s good practice for later.)

Indentation and blocks.

◮ Code is arranged in indented blocks. ◮ The body of main is one block. ◮ It has several blocks inside.

main()

◮ Calls the main function. ◮ Not inside the main function. ⋆ The main() is not indented at all! ◮ If you forget this line, the program doesn’t do anything! Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 5 / 22

slide-14
SLIDE 14

Documentation and comments

Syntax: Comments in Python start with a # character and extend to the end of the line.

◮ A variant of comments starts and ends with three single quotes. ◮ This version can include multiple lines, paragraphs, pages.

Semantics: Does nothing: ignored by Python entirely. Why would we want to do that?

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 6 / 22

slide-15
SLIDE 15

Documentation and comments

Syntax: Comments in Python start with a # character and extend to the end of the line.

◮ A variant of comments starts and ends with three single quotes. ◮ This version can include multiple lines, paragraphs, pages.

Semantics: Does nothing: ignored by Python entirely. Why would we want to do that? Comments are for humans, not the computer.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 6 / 22

slide-16
SLIDE 16

Documentation and comments

Syntax: Comments in Python start with a # character and extend to the end of the line.

◮ A variant of comments starts and ends with three single quotes. ◮ This version can include multiple lines, paragraphs, pages.

Semantics: Does nothing: ignored by Python entirely. Why would we want to do that? Comments are for humans, not the computer.

◮ Teammates. ◮ Your boss (or instructor, grader, . . . ) ⋆ You can talk to your grader while they are grading it! Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 6 / 22

slide-17
SLIDE 17

Documentation and comments

Syntax: Comments in Python start with a # character and extend to the end of the line.

◮ A variant of comments starts and ends with three single quotes. ◮ This version can include multiple lines, paragraphs, pages.

Semantics: Does nothing: ignored by Python entirely. Why would we want to do that? Comments are for humans, not the computer.

◮ Teammates. ◮ Your boss (or instructor, grader, . . . ) ⋆ You can talk to your grader while they are grading it! ◮ Yourself next week! Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 6 / 22

slide-18
SLIDE 18

Documentation and comments

Syntax: Comments in Python start with a # character and extend to the end of the line.

◮ A variant of comments starts and ends with three single quotes. ◮ This version can include multiple lines, paragraphs, pages.

Semantics: Does nothing: ignored by Python entirely. Why would we want to do that? Comments are for humans, not the computer.

◮ Teammates. ◮ Your boss (or instructor, grader, . . . ) ⋆ You can talk to your grader while they are grading it! ◮ Yourself next week! Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 6 / 22

slide-19
SLIDE 19

Where to use comments

Comments don’t usually need to say how you are doing something

  • r what you are doing.

◮ That’s what the code is for.

Instead, they should say why

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 7 / 22

slide-20
SLIDE 20

Where to use comments

Comments don’t usually need to say how you are doing something

  • r what you are doing.

◮ That’s what the code is for.

Instead, they should say why: BAD: counter = 0 # set variable to zero GOOD: counter = 0 # initialize number of lines

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 7 / 22

slide-21
SLIDE 21

Where to use comments

Comments don’t usually need to say how you are doing something

  • r what you are doing.

◮ That’s what the code is for.

Instead, they should say why: BAD: counter = 0 # set variable to zero GOOD: counter = 0 # initialize number of lines If the comment is long, put it on a line of its own before the statement.

◮ That way you don’t have to scroll horizontally to read it. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 7 / 22

slide-22
SLIDE 22

Where to use comments

Comments don’t usually need to say how you are doing something

  • r what you are doing.

◮ That’s what the code is for.

Instead, they should say why: BAD: counter = 0 # set variable to zero GOOD: counter = 0 # initialize number of lines If the comment is long, put it on a line of its own before the statement.

◮ That way you don’t have to scroll horizontally to read it. ⋆ In general, try to keep code lines to < 80 characters. ⋆ Less on team labs, where you should use big fonts. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 7 / 22

slide-23
SLIDE 23

Where to use comments

Comments don’t usually need to say how you are doing something

  • r what you are doing.

◮ That’s what the code is for.

Instead, they should say why: BAD: counter = 0 # set variable to zero GOOD: counter = 0 # initialize number of lines If the comment is long, put it on a line of its own before the statement.

◮ That way you don’t have to scroll horizontally to read it. ⋆ In general, try to keep code lines to < 80 characters. ⋆ Less on team labs, where you should use big fonts. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 7 / 22

slide-24
SLIDE 24

Header comments

All your programming assignments should have a header comment at the top. See the “Programming Standard” page under “Program Assignments”. Doesn’t hurt to have them in lab assignments either!

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 8 / 22

slide-25
SLIDE 25

Header comments

All your programming assignments should have a header comment at the top. See the “Programming Standard” page under “Program Assignments”. Doesn’t hurt to have them in lab assignments either! Name, email, section number Purpose of program Date completed Preconditions: inputs to the program

◮ And what you assume about the inputs.

Postconditions: outputs of the program.

◮ And what you guarantee about the outputs. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 8 / 22

slide-26
SLIDE 26

Kinds of errors

Back to our program. . . it has several errors right now. Syntax errors Semantic errors Run-time errors

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 9 / 22

slide-27
SLIDE 27

Syntax errors

These are the easiest kind to find and fix. Syntax is the rules that say how to write statements in the language.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 10 / 22

slide-28
SLIDE 28

Syntax errors

These are the easiest kind to find and fix. Syntax is the rules that say how to write statements in the language.

◮ Programming languages are very rigid about syntax rules. ◮ Misspelling, wrong punctuation, bad grammar, etc. ◮ Humans can figure out what you meant: not computers. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 10 / 22

slide-29
SLIDE 29

Syntax errors

These are the easiest kind to find and fix. Syntax is the rules that say how to write statements in the language.

◮ Programming languages are very rigid about syntax rules. ◮ Misspelling, wrong punctuation, bad grammar, etc. ◮ Humans can figure out what you meant: not computers.

The interpreter (or compiler) will give you an error message.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 10 / 22

slide-30
SLIDE 30

Syntax errors

These are the easiest kind to find and fix. Syntax is the rules that say how to write statements in the language.

◮ Programming languages are very rigid about syntax rules. ◮ Misspelling, wrong punctuation, bad grammar, etc. ◮ Humans can figure out what you meant: not computers.

The interpreter (or compiler) will give you an error message.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 10 / 22

slide-31
SLIDE 31

Semantic errors

Also known as logic errors. Semantics = meaning.

◮ The program doesn’t do what you wanted it to do. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 11 / 22

slide-32
SLIDE 32

Semantic errors

Also known as logic errors. Semantics = meaning.

◮ The program doesn’t do what you wanted it to do. ◮ Maybe you multiplied instead of dividing. ◮ . . . or used the wrong variable. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 11 / 22

slide-33
SLIDE 33

Semantic errors

Also known as logic errors. Semantics = meaning.

◮ The program doesn’t do what you wanted it to do. ◮ Maybe you multiplied instead of dividing. ◮ . . . or used the wrong variable.

The interpreter won’t detect these for you! So how do we find them?

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 11 / 22

slide-34
SLIDE 34

Semantic errors

Also known as logic errors. Semantics = meaning.

◮ The program doesn’t do what you wanted it to do. ◮ Maybe you multiplied instead of dividing. ◮ . . . or used the wrong variable.

The interpreter won’t detect these for you! So how do we find them? Testing!

◮ Test plan: what to test, provided input, expected output. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 11 / 22

slide-35
SLIDE 35

Semantic errors

Also known as logic errors. Semantics = meaning.

◮ The program doesn’t do what you wanted it to do. ◮ Maybe you multiplied instead of dividing. ◮ . . . or used the wrong variable.

The interpreter won’t detect these for you! So how do we find them? Testing!

◮ Test plan: what to test, provided input, expected output. ◮ Coming up with a good set of test cases is one of the hard parts of

programming.

◮ The first part of program 1 will be writing a test plan. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 11 / 22

slide-36
SLIDE 36

Semantic errors

Also known as logic errors. Semantics = meaning.

◮ The program doesn’t do what you wanted it to do. ◮ Maybe you multiplied instead of dividing. ◮ . . . or used the wrong variable.

The interpreter won’t detect these for you! So how do we find them? Testing!

◮ Test plan: what to test, provided input, expected output. ◮ Coming up with a good set of test cases is one of the hard parts of

programming.

◮ The first part of program 1 will be writing a test plan. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 11 / 22

slide-37
SLIDE 37

Run-time errors

The program or interpreter encounters a situation it can’t handle.

◮ Usually cause the program to halt with an error message. ◮ Not detected until the situation actually happens! Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 12 / 22

slide-38
SLIDE 38

Run-time errors

The program or interpreter encounters a situation it can’t handle.

◮ Usually cause the program to halt with an error message. ◮ Not detected until the situation actually happens!

Often caused by the environment (operating system):

◮ File not found. ◮ Network connection closed. ◮ Out of memory. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 12 / 22

slide-39
SLIDE 39

Run-time errors

The program or interpreter encounters a situation it can’t handle.

◮ Usually cause the program to halt with an error message. ◮ Not detected until the situation actually happens!

Often caused by the environment (operating system):

◮ File not found. ◮ Network connection closed. ◮ Out of memory.

Sometimes caused by programming errors:

◮ Used a string where a number was expected. ◮ Undefined variable. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 12 / 22

slide-40
SLIDE 40

Run-time errors

The program or interpreter encounters a situation it can’t handle.

◮ Usually cause the program to halt with an error message. ◮ Not detected until the situation actually happens!

Often caused by the environment (operating system):

◮ File not found. ◮ Network connection closed. ◮ Out of memory.

Sometimes caused by programming errors:

◮ Used a string where a number was expected. ◮ Undefined variable.

It is possible, but tricky, to catch and handle these errors

◮ Exception handling: near the end of the semester. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 12 / 22

slide-41
SLIDE 41

Run-time errors

The program or interpreter encounters a situation it can’t handle.

◮ Usually cause the program to halt with an error message. ◮ Not detected until the situation actually happens!

Often caused by the environment (operating system):

◮ File not found. ◮ Network connection closed. ◮ Out of memory.

Sometimes caused by programming errors:

◮ Used a string where a number was expected. ◮ Undefined variable.

It is possible, but tricky, to catch and handle these errors

◮ Exception handling: near the end of the semester. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 12 / 22

slide-42
SLIDE 42

Fixing bugs

Let’s fix the bugs in our program.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 13 / 22

slide-43
SLIDE 43

Fixing bugs

Let’s fix the bugs in our program.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 13 / 22

slide-44
SLIDE 44

Fixing bugs

Let’s fix the bugs in our program. Syntax error: missing indentation. Run-time error: input is a string, not a number. Semantic error: wrong formula for b. Semantic error: output message says “GCM”.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 13 / 22

slide-45
SLIDE 45

Fixing bugs

Let’s fix the bugs in our program. Syntax error: missing indentation. Run-time error: input is a string, not a number. Semantic error: wrong formula for b. Semantic error: output message says “GCM”.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 13 / 22

slide-46
SLIDE 46

Fixed program

# Compute the greatest common divisor (GCD) of two numbers. def main(): # Inputs: two positive integers (whole numbers) a and b. a = int(input("Please enter a first number: ")) b = int(input("Please enter another number: ")) # 1. Repeat as long as b is not zero: while b != 0: # 1.1. If a > b, then set a <- (a - b) if a > b: a = a - b # 1.2. Otherwise, set b <- (b - a) else: b = b - a # 2. Output a as the answer. print("The GCD of your numbers is", a) main()

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 14 / 22

slide-47
SLIDE 47

Variables

A variable is a “slot” or “location” that refers to a value. a and b were variables in our program.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 15 / 22

slide-48
SLIDE 48

Variables

A variable is a “slot” or “location” that refers to a value. a and b were variables in our program. A value is something like 42 or "Hello".

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 15 / 22

slide-49
SLIDE 49

Variables

A variable is a “slot” or “location” that refers to a value. a and b were variables in our program. A value is something like 42 or "Hello". Variables are stored in RAM. They refer to different values as the program runs (vary-able)

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 15 / 22

slide-50
SLIDE 50

Variables

A variable is a “slot” or “location” that refers to a value. a and b were variables in our program. A value is something like 42 or "Hello". Variables are stored in RAM. They refer to different values as the program runs (vary-able)

◮ Assignment (the equals sign) makes a variable refer to a new value. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 15 / 22

slide-51
SLIDE 51

Variables

A variable is a “slot” or “location” that refers to a value. a and b were variables in our program. A value is something like 42 or "Hello". Variables are stored in RAM. They refer to different values as the program runs (vary-able)

◮ Assignment (the equals sign) makes a variable refer to a new value.

A fundamental building block of (most) programming languages.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 15 / 22

slide-52
SLIDE 52

Variables

A variable is a “slot” or “location” that refers to a value. a and b were variables in our program. A value is something like 42 or "Hello". Variables are stored in RAM. They refer to different values as the program runs (vary-able)

◮ Assignment (the equals sign) makes a variable refer to a new value.

A fundamental building block of (most) programming languages.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 15 / 22

slide-53
SLIDE 53

Properties of a variable

Name – what to call the variable?

◮ Also called an “identifier”. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 16 / 22

slide-54
SLIDE 54

Properties of a variable

Name – what to call the variable?

◮ Also called an “identifier”.

Value – what is in the variable?

◮ In Python, the value of a variable is an object. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 16 / 22

slide-55
SLIDE 55

Properties of a variable

Name – what to call the variable?

◮ Also called an “identifier”.

Value – what is in the variable?

◮ In Python, the value of a variable is an object.

Type — what kind of value?

◮ Integer, string, floating-point number, . . . Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 16 / 22

slide-56
SLIDE 56

Properties of a variable

Name – what to call the variable?

◮ Also called an “identifier”.

Value – what is in the variable?

◮ In Python, the value of a variable is an object.

Type — what kind of value?

◮ Integer, string, floating-point number, . . .

Scope – where in the program is the name valid?

◮ In Python, goes from the definition to the end of that block. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 16 / 22

slide-57
SLIDE 57

Properties of a variable

Name – what to call the variable?

◮ Also called an “identifier”.

Value – what is in the variable?

◮ In Python, the value of a variable is an object.

Type — what kind of value?

◮ Integer, string, floating-point number, . . .

Scope – where in the program is the name valid?

◮ In Python, goes from the definition to the end of that block. ◮ Can have variables with the same name as long as their scopes don’t

  • verlap.

⋆ They’re entirely unrelated variables! Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 16 / 22

slide-58
SLIDE 58

Properties of a variable

Name – what to call the variable?

◮ Also called an “identifier”.

Value – what is in the variable?

◮ In Python, the value of a variable is an object.

Type — what kind of value?

◮ Integer, string, floating-point number, . . .

Scope – where in the program is the name valid?

◮ In Python, goes from the definition to the end of that block. ◮ Can have variables with the same name as long as their scopes don’t

  • verlap.

⋆ They’re entirely unrelated variables! Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 16 / 22

slide-59
SLIDE 59

Identifiers (variable names)

A sequence of letters, digits, and underscores

◮ “Alphanumeric” characters. ◮ Case sensitive: students and Students and STUDENTS are all

different.

◮ Cannot start with a digit (Python thinks that’s a number). Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 17 / 22

slide-60
SLIDE 60

Identifiers (variable names)

A sequence of letters, digits, and underscores

◮ “Alphanumeric” characters. ◮ Case sensitive: students and Students and STUDENTS are all

different.

◮ Cannot start with a digit (Python thinks that’s a number). ◮ Cannot be a reserved word (if, while, etc.) Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 17 / 22

slide-61
SLIDE 61

Identifiers (variable names)

A sequence of letters, digits, and underscores

◮ “Alphanumeric” characters. ◮ Case sensitive: students and Students and STUDENTS are all

different.

◮ Cannot start with a digit (Python thinks that’s a number). ◮ Cannot be a reserved word (if, while, etc.) ⋆ Dark blue in WingIDE.

OK: x, size, name2, long name, CamelCase, ugly

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 17 / 22

slide-62
SLIDE 62

Identifiers (variable names)

A sequence of letters, digits, and underscores

◮ “Alphanumeric” characters. ◮ Case sensitive: students and Students and STUDENTS are all

different.

◮ Cannot start with a digit (Python thinks that’s a number). ◮ Cannot be a reserved word (if, while, etc.) ⋆ Dark blue in WingIDE.

OK: x, size, name2, long name, CamelCase, ugly BAD: 2bad4u, no spaces, no-punctuation

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 17 / 22

slide-63
SLIDE 63

Identifiers (variable names)

A sequence of letters, digits, and underscores

◮ “Alphanumeric” characters. ◮ Case sensitive: students and Students and STUDENTS are all

different.

◮ Cannot start with a digit (Python thinks that’s a number). ◮ Cannot be a reserved word (if, while, etc.) ⋆ Dark blue in WingIDE.

OK: x, size, name2, long name, CamelCase, ugly BAD: 2bad4u, no spaces, no-punctuation Just because it’s legal doesn’t mean it’s good.

◮ Avoid single-letter variables. ⋆ Except in loop counters, simple math functions. ⋆ thing, number aren’t any better. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 17 / 22

slide-64
SLIDE 64

Identifiers (variable names)

A sequence of letters, digits, and underscores

◮ “Alphanumeric” characters. ◮ Case sensitive: students and Students and STUDENTS are all

different.

◮ Cannot start with a digit (Python thinks that’s a number). ◮ Cannot be a reserved word (if, while, etc.) ⋆ Dark blue in WingIDE.

OK: x, size, name2, long name, CamelCase, ugly BAD: 2bad4u, no spaces, no-punctuation Just because it’s legal doesn’t mean it’s good.

◮ Avoid single-letter variables. ⋆ Except in loop counters, simple math functions. ⋆ thing, number aren’t any better. ◮ Instead of n, perhaps count Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 17 / 22

slide-65
SLIDE 65

Identifiers (variable names)

A sequence of letters, digits, and underscores

◮ “Alphanumeric” characters. ◮ Case sensitive: students and Students and STUDENTS are all

different.

◮ Cannot start with a digit (Python thinks that’s a number). ◮ Cannot be a reserved word (if, while, etc.) ⋆ Dark blue in WingIDE.

OK: x, size, name2, long name, CamelCase, ugly BAD: 2bad4u, no spaces, no-punctuation Just because it’s legal doesn’t mean it’s good.

◮ Avoid single-letter variables. ⋆ Except in loop counters, simple math functions. ⋆ thing, number aren’t any better. ◮ Instead of n, perhaps count ⋆ Even better: num students Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 17 / 22

slide-66
SLIDE 66

Can variable properties change?

The name and scope of a variable never change.

◮ If it looks like it did: it’s a different variable. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 18 / 22

slide-67
SLIDE 67

Can variable properties change?

The name and scope of a variable never change.

◮ If it looks like it did: it’s a different variable.

In a “dynamically typed” language like Python, the value and type of a variable can change.

◮ With an assignment statement:

score = 0.0 score = "incomplete"

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 18 / 22

slide-68
SLIDE 68

Can variable properties change?

The name and scope of a variable never change.

◮ If it looks like it did: it’s a different variable.

In a “dynamically typed” language like Python, the value and type of a variable can change.

◮ With an assignment statement:

score = 0.0 score = "incomplete"

In “statically typed” languages like C++, the type cannot change.

◮ Even in Python, it’s less confusing if each variable has one type. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 18 / 22

slide-69
SLIDE 69

Can variable properties change?

The name and scope of a variable never change.

◮ If it looks like it did: it’s a different variable.

In a “dynamically typed” language like Python, the value and type of a variable can change.

◮ With an assignment statement:

score = 0.0 score = "incomplete"

In “statically typed” languages like C++, the type cannot change.

◮ Even in Python, it’s less confusing if each variable has one type. ◮ One common style: include the type in the variable name: ⋆ user list, name str, . . . Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 18 / 22

slide-70
SLIDE 70

Can variable properties change?

The name and scope of a variable never change.

◮ If it looks like it did: it’s a different variable.

In a “dynamically typed” language like Python, the value and type of a variable can change.

◮ With an assignment statement:

score = 0.0 score = "incomplete"

In “statically typed” languages like C++, the type cannot change.

◮ Even in Python, it’s less confusing if each variable has one type. ◮ One common style: include the type in the variable name: ⋆ user list, name str, . . .

In “pure functional” languages like Haskell, the value cannot change!

◮ So maybe “variable” is not the right word there! Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 18 / 22

slide-71
SLIDE 71

Can variable properties change?

The name and scope of a variable never change.

◮ If it looks like it did: it’s a different variable.

In a “dynamically typed” language like Python, the value and type of a variable can change.

◮ With an assignment statement:

score = 0.0 score = "incomplete"

In “statically typed” languages like C++, the type cannot change.

◮ Even in Python, it’s less confusing if each variable has one type. ◮ One common style: include the type in the variable name: ⋆ user list, name str, . . .

In “pure functional” languages like Haskell, the value cannot change!

◮ So maybe “variable” is not the right word there! Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 18 / 22

slide-72
SLIDE 72

Assignment

Syntax: variable = expression Semantics: Calculates the value of (evaluates) the right hand side (RHS), then changes the value of the variable on the left hand side (LHS).

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 19 / 22

slide-73
SLIDE 73

Assignment

Syntax: variable = expression Semantics: Calculates the value of (evaluates) the right hand side (RHS), then changes the value of the variable on the left hand side (LHS).

◮ In a later class we’ll see other things that can go on the LHS. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 19 / 22

slide-74
SLIDE 74

Assignment

Syntax: variable = expression Semantics: Calculates the value of (evaluates) the right hand side (RHS), then changes the value of the variable on the left hand side (LHS).

◮ In a later class we’ll see other things that can go on the LHS.

Not an equation!

◮ In math, x = x + 1 has no solution. ◮ But in Python, x = x + 1 means “add one to x”. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 19 / 22

slide-75
SLIDE 75

Assignment

Syntax: variable = expression Semantics: Calculates the value of (evaluates) the right hand side (RHS), then changes the value of the variable on the left hand side (LHS).

◮ In a later class we’ll see other things that can go on the LHS.

Not an equation!

◮ In math, x = x + 1 has no solution. ◮ But in Python, x = x + 1 means “add one to x”. ◮ Maybe better to pronounce it “gets” than “equals”. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 19 / 22

slide-76
SLIDE 76

Assignment

Syntax: variable = expression Semantics: Calculates the value of (evaluates) the right hand side (RHS), then changes the value of the variable on the left hand side (LHS).

◮ In a later class we’ll see other things that can go on the LHS.

Not an equation!

◮ In math, x = x + 1 has no solution. ◮ But in Python, x = x + 1 means “add one to x”. ◮ Maybe better to pronounce it “gets” than “equals”. ◮ “Assign x + 1 to x” or “Assign x with/from x + 1”. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 19 / 22

slide-77
SLIDE 77

Assignment

Syntax: variable = expression Semantics: Calculates the value of (evaluates) the right hand side (RHS), then changes the value of the variable on the left hand side (LHS).

◮ In a later class we’ll see other things that can go on the LHS.

Not an equation!

◮ In math, x = x + 1 has no solution. ◮ But in Python, x = x + 1 means “add one to x”. ◮ Maybe better to pronounce it “gets” than “equals”. ◮ “Assign x + 1 to x” or “Assign x with/from x + 1”.

Order matters!

◮ Performs the calculation on the right. ◮ Changes only the variable on the left. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 19 / 22

slide-78
SLIDE 78

Assignment

Syntax: variable = expression Semantics: Calculates the value of (evaluates) the right hand side (RHS), then changes the value of the variable on the left hand side (LHS).

◮ In a later class we’ll see other things that can go on the LHS.

Not an equation!

◮ In math, x = x + 1 has no solution. ◮ But in Python, x = x + 1 means “add one to x”. ◮ Maybe better to pronounce it “gets” than “equals”. ◮ “Assign x + 1 to x” or “Assign x with/from x + 1”.

Order matters!

◮ Performs the calculation on the right. ◮ Changes only the variable on the left. ◮ x + 1 = x # Syntax error! Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 19 / 22

slide-79
SLIDE 79

Assignment

Syntax: variable = expression Semantics: Calculates the value of (evaluates) the right hand side (RHS), then changes the value of the variable on the left hand side (LHS).

◮ In a later class we’ll see other things that can go on the LHS.

Not an equation!

◮ In math, x = x + 1 has no solution. ◮ But in Python, x = x + 1 means “add one to x”. ◮ Maybe better to pronounce it “gets” than “equals”. ◮ “Assign x + 1 to x” or “Assign x with/from x + 1”.

Order matters!

◮ Performs the calculation on the right. ◮ Changes only the variable on the left. ◮ x + 1 = x # Syntax error!

If the LHS variable doesn’t already exist in this scope, creates it.

◮ “Initialization”: giving a variable its initial value. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 19 / 22

slide-80
SLIDE 80

Assignment

Syntax: variable = expression Semantics: Calculates the value of (evaluates) the right hand side (RHS), then changes the value of the variable on the left hand side (LHS).

◮ In a later class we’ll see other things that can go on the LHS.

Not an equation!

◮ In math, x = x + 1 has no solution. ◮ But in Python, x = x + 1 means “add one to x”. ◮ Maybe better to pronounce it “gets” than “equals”. ◮ “Assign x + 1 to x” or “Assign x with/from x + 1”.

Order matters!

◮ Performs the calculation on the right. ◮ Changes only the variable on the left. ◮ x + 1 = x # Syntax error!

If the LHS variable doesn’t already exist in this scope, creates it.

◮ “Initialization”: giving a variable its initial value. Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 19 / 22

slide-81
SLIDE 81

Using assignment: swapping

Suppose we have two variables and want to swap their values. So each variable’s new value is the other variable’s old value

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 20 / 22

slide-82
SLIDE 82

Using assignment: swapping

Suppose we have two variables and want to swap their values. So each variable’s new value is the other variable’s old value:

x = 10 y = 42 # do something print(x, y) # should print: 42 10

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 20 / 22

slide-83
SLIDE 83

Using assignment: swapping

Suppose we have two variables and want to swap their values. So each variable’s new value is the other variable’s old value:

x = 10 y = 42 # do something print(x, y) # should print: 42 10

Will this work?

x = y y = x print(x, y)

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 20 / 22

slide-84
SLIDE 84

Using assignment: swapping

Suppose we have two variables and want to swap their values. So each variable’s new value is the other variable’s old value:

x = 10 y = 42 # do something print(x, y) # should print: 42 10

Will this work?

x = y y = x print(x, y) ⇒ 42 42

We lost the old value of x!

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 20 / 22

slide-85
SLIDE 85

Using assignment: swapping

Suppose we have two variables and want to swap their values. So each variable’s new value is the other variable’s old value:

x = 10 y = 42 # do something print(x, y) # should print: 42 10

Will this work?

x = y y = x print(x, y) ⇒ 42 42

We lost the old value of x! Need a temporary variable:

temp = x x = y y = temp

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 20 / 22

slide-86
SLIDE 86

Using assignment: swapping

Suppose we have two variables and want to swap their values. So each variable’s new value is the other variable’s old value:

x = 10 y = 42 # do something print(x, y) # should print: 42 10

Will this work?

x = y y = x print(x, y) ⇒ 42 42

We lost the old value of x! Need a temporary variable:

temp = x x = y y = temp

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 20 / 22

slide-87
SLIDE 87

Simple arithmetic

The expression on the right hand side can be an arithmetic expression. Arithmetic operators in Python are:

◮ +, - (add and subtract: a + b, c - d) ◮ * (multiply), / (divide) ◮ ** (exponentiate, “to the”) Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 21 / 22

slide-88
SLIDE 88

Simple arithmetic

The expression on the right hand side can be an arithmetic expression. Arithmetic operators in Python are:

◮ +, - (add and subtract: a + b, c - d) ◮ * (multiply), / (divide) ◮ ** (exponentiate, “to the”)

Order of operations:

◮ ** first (highest precedence) ◮ Then * and / ◮ Then + and - (lowest precedence) Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 21 / 22

slide-89
SLIDE 89

Simple arithmetic

The expression on the right hand side can be an arithmetic expression. Arithmetic operators in Python are:

◮ +, - (add and subtract: a + b, c - d) ◮ * (multiply), / (divide) ◮ ** (exponentiate, “to the”)

Order of operations:

◮ ** first (highest precedence) ◮ Then * and / ◮ Then + and - (lowest precedence) ◮ Can use parentheses to make the order explicit:

total = price * (tax + 100) / 100

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 21 / 22

slide-90
SLIDE 90

Simple arithmetic

The expression on the right hand side can be an arithmetic expression. Arithmetic operators in Python are:

◮ +, - (add and subtract: a + b, c - d) ◮ * (multiply), / (divide) ◮ ** (exponentiate, “to the”)

Order of operations:

◮ ** first (highest precedence) ◮ Then * and / ◮ Then + and - (lowest precedence) ◮ Can use parentheses to make the order explicit:

total = price * (tax + 100) / 100

We’ll see more details about these operators next time when we talk about types.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 21 / 22

slide-91
SLIDE 91

Simple arithmetic

The expression on the right hand side can be an arithmetic expression. Arithmetic operators in Python are:

◮ +, - (add and subtract: a + b, c - d) ◮ * (multiply), / (divide) ◮ ** (exponentiate, “to the”)

Order of operations:

◮ ** first (highest precedence) ◮ Then * and / ◮ Then + and - (lowest precedence) ◮ Can use parentheses to make the order explicit:

total = price * (tax + 100) / 100

We’ll see more details about these operators next time when we talk about types.

Neil Moore (UK CS) CS 115 Lecture 3 Fall 2015 21 / 22

slide-92
SLIDE 92

Next time

Data types in Python. More about arithmetic. Getting input. Printing. Testing.

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