Comp sci python
Chapter 5 Lesson 1
What are some example syntax errors?
Missing quotation marks, missing parentheses, incorrect indentation, missing a colon after anifstatement, or not following Python's syntax rules.What kinds of information will you find in syntax error messages?
The file name, line number, the line of code containing the error, a caret (^) pointing near the problem, and a description of the syntax error.What are some example runtime errors?
Using the wrong variable, choosing the wrong comparison operator, writing statements in the wrong order, or producing incorrect program results even though the code runs.Do you receive any Python error messages for runtime errors? Why or why not?
No. Runtime errors produce incorrect behavior or output, but the code is still valid Python, so no error message is generated.What is an exception and when are they thrown?
An exception is a runtime error that completely stops the program. It is thrown when the program attempts an invalid operation.What kinds of information will you find in runtime exception messages?
The file name, line number where the exception occurred, the line of code that caused it, and the type of exception.What are some example runtime exceptions?
Division by zero, which produces aZeroDivisionError, and attempting other invalid operations that Python cannot execute.
Chapter 5 Lesson 2
What information should you gather prior to starting a code review?
Understand the runtime error you are trying to fix and gather clues from incorrect output or exception messages.What are some common questions you would ask as you examine each statement in a code review?
What is the statement supposed to do?
Is it written correctly?
Is it formatted clearly?
What results should it produce?
Do the comments match the code?
What variables does it use?
Could the data be incorrect?
Could this statement be causing the runtime error?
Do comments provide useful information when conducting a code review?
Yes. Comments can provide important clues about what the code is supposed to do, although they can sometimes be incorrect.Do you always need to start a code review at the beginning? Why or why not?
No. You can start in a suspicious area of the program if you are confident earlier code is working correctly.What Python statement do you add to insert program traces in your code?
Theprint()statement.What kind of information might you display in trace statements?
Variable values, calculation results, and messages showing whichif,elif, orelseblocks are running.Are trace statements intended for the programmer or the end user? Do you leave trace statements in your code when done?
They are intended for the programmer. When debugging is complete, they should be removed or commented out.What kind of software tool will let you examine a program and variable contents as the program runs, step-by-step?
A debugger.
Chapter 5 Lesson 3
What are the two main states a program can be in?
The two main states are running and break.When in the running state, can you observe each line of code as it runs?
No. The program runs too quickly to closely observe each line.How do you load the Python debugger for a program?
Addimport pdbat the top of the program.How do you set a breakpoint in Python? Where can your breakpoint statements go?
Usepdb.set_trace(). Breakpoints can be placed anywhere a normal statement can go, including insideif,elif, orelseblocks.What information do you see at the (Pdb) prompt when a program hits a breakpoint?
You see the file name, line number, the statement about to execute, and the(Pdb)command prompt.What are some basic pdb commands?
How do you step to the next line?
Usestepors.How do you run to completion or the next breakpoint?
Usecontinueorc.How do you display variable contents?
Usep <expression>.How do you show a list of nearby code?
Uselistorl.How do you quit the program immediately?
Usequitorq.How do you repeat the last command?
Press Enter or the space bar.Where can you go to find more information on the Python debugger?
The Python debugger documentation: https://docs.python.org/3/library/pdb.html
Chapter 6 Lesson 1
Why would you need to store a group of items in a single variable?
To manage large amounts of related data without creating many separate variables.How do you create a list in Python?
Use square brackets[]with values separated by commas.What kinds of data can you store in lists?
Integers, floating-point numbers, strings, Boolean values, and other data types.Does each element need to be the same data type?
No. A list can contain a mixture of different data types.How do you print list contents?
Pass the list variable to theprint()function.How do index values work to access specific list elements?
Use the list name followed by the index in square brackets, such asmyList[0].For any given list size, what is the minimum and maximum valid index value?
The minimum index is 0 and the maximum index is one less than the number of items in the list.How do ranges allow you to read a sequence of list elements?
Use a starting and ending index separated by a colon, such asmyList[1:4].When using a range, does the output contain the element at the ending index?
No. The range stops before the ending index.How do you declare a tuple instead of a list?
Use parentheses()instead of square brackets[].What is the biggest difference between a tuple and a list?
A list can be changed after it is created, while a tuple is immutable and cannot be changed.
Chapter 6 Lesson 2
Which three Python functions will work on any list or tuple?
len(),max(), andmin().How do you call those three functions with an input parameter?
Pass the list or tuple inside the parentheses:len(listOrTuple)max(listOrTuple)min(listOrTuple)
What is the expected output from each of the three functions?
len()returns the number of items.max()returns the largest value.min()returns the smallest value.
What is the purpose of each of the following list functions? What are the required input parameters?
append(value) - Adds a value to the end of the list.
insert(index, value) - Inserts a value before the specified index.
remove(value) - Removes the first matching value from the list.
pop(index) - Removes and returns the value at the specified index.
clear() - Removes all items from the list.
index(value) - Returns the index of the first matching value.
count(value) - Returns the number of times a value appears in the list.
sort() - Sorts the list from smallest to largest.
reverse() - Reverses the order of the list.
How do you use the "in" keyword to form a logical expression with a list?
Use the format:value in list
Example:
"Cherry" in fruityFlavors
What is the result of the "in" logical expression? How do you use it in an "if" statement?
The expression returns True if the value is present and False if it is not. It can be used directly in anifstatement:if ("Cherry" in fruityFlavors):
# code
Chapter 6 Lesson 3
What is the purpose of a "for" loop?
Aforloop is used to repeatedly execute the same block of code and iterate through values in a collection or range.How would you write a "for" loop to iterate over all of the values in a list or tuple?
for item in listName:
The loop processes each value in the list or tuple.
How do you access the individual values of the list or tuple from within the loop body?
Use the loop variable (such asitem), which contains one value from the list or tuple at a time.How do you use a "for" loop to iterate over all of the characters in a string?
for character in myString:
The loop processes one character at a time.
How do you use a "for" loop to iterate over all of the numeric integers in a range?
for i in range(start, end):
The loop variable receives each integer in the range.
How do you specify the lower and upper bounds of a numeric range? Is the upper bound inclusive?
Userange(lower, upper). The lower bound is included, but the upper bound is not included.What is the purpose of the "break" statement and how does it affect a "for" loop?
breakimmediately exits the loop and continues execution after the loop.What is the purpose of the "continue" statement and how does it affect a "for" loop?
continueskips the rest of the current loop iteration and starts the next iteration.What is the purpose of the "else" statement and when does the "else" body execute? When would it be skipped?
Theelseblock runs after the loop finishes normally. It is skipped if the loop exits early because of abreakstatement.
Chapter 6 Lesson 4
How is a "while" loop different than a "for" loop?
Aforloop repeats a fixed number of times over a list, tuple, string, or range. Awhileloop repeats as long as a logical expression is True.When is the logical expression tested in a "while" loop?
The logical expression is tested at the top of the loop before the loop body executes.How do you identify the statements in the body of a "while" loop?
The statements in the loop body are indented underneath thewhilestatement.What happens if the "while" loop's logical expression is False when first tested? Will the loop run at all?
No. The loop body is skipped and the loop does not run.What is an infinite loop and why is it a bad thing?
An infinite loop occurs when the logical expression never becomes False, causing the loop to run forever. It is bad because the program becomes stuck and cannot continue.How do you ensure that your loops are not infinite?
Make sure the loop body changes variables or conditions so the logical expression can eventually become False.How do the "break", "continue", and "else" statements work with a "while" loop?
break: Immediately exits the loop.
continue: Skips the rest of the current iteration and returns to the top of the loop.
else: Runs after the loop completes normally and is skipped if the loop ends with a
break.
How can you create a loop that will always execute at least once and determine the exit condition towards the bottom of the loop? How is this style of loop halted?
Use:while True:
Put the exit condition inside the loop body and use a break statement to stop the loop when the condition is met.