Q and A for Sections 1 – 2.2.4 CS 106 © 2015 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.) 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 A high-level language is easier to program in, easier to debug, and (perhaps) portable to different computers. Source code Q: What is “source code”? A: Source code is the program you are creating (and typically storing) in a file. For us, it will be written in python. It is either translated by a compiler into bits to be executed by the computer hardware, or, interpreted and executed by an interpreter (as in python). Aka “software”. 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: 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. Picture:http://interactivepython.org/runestone/static/thinkcs py/GeneralIntro/ThePythonProgrammingLanguage.html More about the python interpreter • You write your code and store it in a file, say computeIQ.py. • You run it in the interpreter. In PyCharm, you click the run button. What that really does is execute this: python.exe computeIQ.py • The interpreter does this: – Set the current line pointer to the first line of the file. – While we have a line to execute: • Read in the line from the file. • Execute that line: – Could be an assignment, function call, function definition, class definition, if statement, etc., etc. – Store results in memory: like variable assignments, etc. – Move the pointer to the next line to execute. 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. Syntax vs. semantic errors quiz Q: Which errors are these? 3.14159 = piApprox piSquared = piApprox * 2 (assume a, b, c, and x defined) discrim = ax^2 + bx + c A: syntax, semantic, syntax and semantic (discriminant is b**2 – 4 * a * c) 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). Q: How do we know what operations are allowed on certain objects? A: You can see them online, or use help(), or see the info in the book. Literals Q: What is a literal? A: A value you type in. (Give me an integer literal. How about a string? Float? bool?) Literals In python, you have to specify a literal such that the interpreter can figure out what type it is. So: “something”: the quotes means “this is a string of characters”. 1 : because it is only digits (and optionally + / -), it is an integer. -10.314: because it has digits and a ., it is a floating point number. [ ‘stuff’ ] : the [ means “here is a list literal” 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. 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… 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 =.) Assignment Operator • Syntax: identifier = value or expression – age = 3 – ageIn30Years = age + 30 – cs106 = [ ‘Adam’, ‘Joshua’, ‘Gabrielle’ ] • What the interpreter does: – Look first at the right-hand side (rhs) and evaluate it (“evaluate” means “turn into a value”). – Look up the identifier on the left-hand side (lhs). – If the identifier is new, make it refer to the new value. – Else: change the existing identifier to make it refer to the new value. 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. 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 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. 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 ‘Rachel’ to the end of the list knights_who_say_ni. A: knights_who_say_ni.append(‘Rachel’) 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.) More list practice Q: What does this code result in? cs106 = list() cs106.append(‘Karissa’) cs106.append(‘Kenton’) cs106.append(‘Isabelle’) A: Result is a list object containing 3 strings: [ ‘Karissa’, ‘Kenton’, ‘Isabelle’ ] 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 Inserting in a list Q: Write the code to insert the string ‘Moses’ between the strings ‘Cassidy’ and ‘Levi’ in the following list, called cs106: >>> cs106 [ ‘Benjamin’, ‘Cassidy’, ‘Levi’, ‘Ari’ ] A: cs106.insert(2, ‘Moses’) Inserting into a list Q: Given this list: >>> cs106 [ ‘Dorthea’ ] create this list using only insert statements: [ ‘Justine’, ‘Justin’, ‘Jeremy’, ‘Dorthea’ ] A: cs106.insert(0, ‘Jeremy’) cs106.insert(0, ‘Justin’) cs106.insert(0, ‘Justine’) Using remove() Q: What is the result of this code?: >>> cs106 [ ’Joshua', ’Seth', ’Allison', ’Seth' ] >>> cs106.remove(’Seth') A: [ ’Joshua', ’Allison', ’Seth' ] 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? Legal code? Q: Is this code legal?: >>> cs106 = list() >>> cs106.append(’Elianna') >>> cs106.append(3.1415926) >>> cs108 = [’Jacob', ’Rachel'] >>> cs106.append(cs108) A: Yes! Q: what does cs106 look like now? 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().) 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 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. Extra slides • The following slides are from previous years and might still be helpful for you. 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. 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. 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.