32
Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Embed Size (px)

Citation preview

Page 1: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Q and A for Sections 1 – 2.2.4

CS 106© 2014 Victor Norman

Page 2: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Difference between data and information

Q: What is the difference between data and information?

A: It is a difference of focus. Data is more interested in how something is represented in bits in a computer. Information is more about what the data represents. (This is not an important concept.)

Page 3: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Bits

Q: Why are bits only 0s or 1s? Why not 2s or 3s?

A: A bit in a computer is stored in a kind of switch. Like a light switch, it can be only on or off. On = 1, off = 0.

Page 4: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

High- vs. low-level language

Q: What is the difference between a high-level and low-level language? If a computer cannot run a high-level language, then why have it?

A: (not in book). A low-level language is a series of bits – the only thing a machine can truly run. No one wants to program in bits:0010000100101111010101000101110001110

Page 5: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Compiled vs. Interpreted Languages

Q: Why would there be interpreters and compilers and what would the advantages be to using one over the other?A: Picture of each.Compiler: creates object/executable code specific to the computer – Windows or Mac OS or Linux or Android. Code runs fast. But, you have to recompile each time you make a change before you can try your change.Interpreter: runs your source code directly (converts it to machine-specific instructions internally). No recompiling! But, usually much slower.

Page 6: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Terminology!

Student: I am FREAKING OUT because there is so much terminology here! What is an object? What is the difference between a method and a function? What is a type? Ahhhhh!!!!

Professor: OK, deep breath! In… out… in… out…

Page 7: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Syntax vs. semantic errors

Q: I did not understand the difference between syntax and semantic errors. Can you explain?

A: A syntax error is illegal code. It is like not English saying correct. A semantic error is wrong logic. The code is fine, but computes the wrong thing.

Page 8: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Syntax vs. semantic errors quiz

Q: Which errors are these?

3.14159 = piApproxpiSquared = piApprox * 2(assume a, b, c, and x defined)discrim = ax^2 + bx + cA: syntax, semantic, syntax and semantic (discriminant is b**2 – 4 * a * c)

Page 9: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

How a program runs

Q: I am not confident that I could give a complete description of how a program runs in sequential statements.A: I’ll draw a picture of python running code, line by line.

Page 10: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

What is an object?

Q: What is an object?

A: It is a variable or literal of a certain type (and thus can hold certain values, and can do certain operations).

Page 11: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Literals

Q: What is a literal?

A: A value you type in.(Give me an integer literal. How about a string? Float? bool?)

Page 12: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Variables

Q: What is a variable?

A: A variable is a name referring to a value of a certain type stored in memory. A variable is one kind of identifier – a name for something.

Page 13: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Identifiers

Q: In groceries = list()what is the identifier?

A: groceries(It is a variable that “labels” the object created on the right hand side of the =.)

Page 14: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Legal identifier names

Q: Select the legal identifiers (i.e., labels for objects (in this case)):

miles _per_hour 1_Miles 1_ab1_1 1ab_gallons __1abc

A: 1_, 1_ab1, 1ab are illegal.

Page 15: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

List creation

Q: How do you make a new list? I.e., how do you construct a list? I.e., how do you call the constructor of the list class?

A: You call list(). E.g., groceries = list()

This calls the list constructor to make a new list instance which is referred to by variable groceries

Page 16: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

list() vs. []

Q: Does typing list() and just using brackets accomplish the same thing? A: Yes.The latter is called the “literal method” of constructing a list. It is also a way to put stuff in the list when you create it.

Page 17: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

List append method

Q: append() is a method of a list object that takes one parameter -- the object to be appended to the end of a list. Write the code to append the name ‘Zach’ to the end of the list knights_who_say_ni.

A: knights_who_say_ni.append(‘Zach’)

Page 18: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

append() vs. add()

Q: It seems that it's just the 'append' method, but if so, why isn't it just called the 'add' method? Is there something else that can be done with 'append'?A: Someone decided it should be called ‘append’… Perhaps ‘append’ is a little more descriptive than ‘add’? (There is no ‘add’ method defined.)

Page 19: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

More list practice

Q: What does this code result in?cs106 = list()cs106.append(‘Morgan’)cs106.append(‘Arielle’)cs106.append(‘Jesse’)

A: Result is a list object containing 3 strings:[ ‘Morgan’, ‘Arielle’, ‘Jesse’ ]

Page 20: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Append() is a ….

Q: append() is a method that changes the object upon which it is called. Thus, it is called a _____________ method. (p. 34)

A: mutator

Page 21: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Inserting in a list

Q: Write the code to insert the string ‘Kristen’ between the strings ‘Isaac’ and ‘Jen Ai’ in the following list, called cs106:>>> cs106[ ‘Amber’, ‘Isaac’, ‘Jen Ai’, ‘Amy’ ]

A: cs106.insert(2, ‘Kristen’)

Page 22: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Inserting into a list

Q: Given this list:>>> cs106[ ‘Shiki’ ]create this list using only insert statements:[ ‘Andrew’, ‘Christina’, ‘Ben’, ‘Shiki’ ]

A: cs106.insert(0, ‘Ben’)cs106.insert(0, ‘Christina’)cs106.insert(0, ‘Andrew’)

Page 23: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Using remove()

Q: What is the result of this code?:>>> cs106[ ’Jacob', ’Paul', ’Elizabeth', ’Paul' ]>>> cs106.remove(’Paul')

A: [ ’Jacob', ’Elizabeth', ’Paul' ]

Page 24: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Remove second one?

Q: If there are two identical names on a list and I want to remove the latter one, how do I do that?

A: We don’t know how to do it, but let’s figure out the algorithm to do it. How would you like to do it?

Page 25: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Legal code?

Q: Is this code legal?:>>> cs106 = list()>>> cs106.append(’Julie')>>> cs106.append(3.1415926)>>> cs108 = [’Peter', ’Matt']>>> cs106.append(cs108)

A: Yes!Q: what does cs106 look like now?

Page 26: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Return value

Q: Define return value.

A: A return value is a value returned to the caller by a method call. (Some method calls do not return anything, like list’s append().)

Page 27: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Combining lists

Q: Given two lists rock and roll, create a new list called rock_n_roll that contains all the elements of rock followed by all the elements of roll.

A: rock_n_roll = rock + roll

Page 28: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

The range function

range() is very useful function. • Creates a list of integers.• range(start, stop, step)– start and step are optional: if omitted, start is 0,

step is 1.– list contains numbers up to but not including stop.

Page 29: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Extra slides

• The following slides are from previous years and might still be helpful for you.

Page 30: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

What is an object?

Q: What is an object?

A: An object is an instance (or example) of a certain type. (Thus, the object has legal values and operations associated with it.)

We often say that the variable is an object, but really the value in memory is the object.

Page 31: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Trick question… bwa ha ha…

Q: What does this statement do?list()(i.e., not a_list = list())

A: It creates a list object but doesn’t label it, so it can’t be used. I.e., it throws it away immediately.

Page 32: Q and A for Sections 1 – 2.2.4 CS 106 © 2014 Victor Norman

Literal list

Q: In section 2.2.4, what does it mean when they say “Square brackets delimit the list”? Like in a statement like this:groceries = [‘bread’, ‘cheese’, ‘eggs’]

A: It means the square brackets tell the interpreter that 1) it is a list, and 2) where the list definition is starting and ending.