Python Programming Basics
- 122Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
According to the course content, what is one reason Python is often presented as a suitable language for beginners?
According to the course content, what is one reason Python is often presented as a suitable language for beginners?
122 questions
- 1
According to the course content, what is one reason Python is often presented as a suitable language for beginners?
- A
It uses relatively little syntax, making it possible to start writing programs quickly
- B
It prevents beginners from using variables until they learn advanced concepts
- C
It requires every program to be written inside a graphical interface
- D
It automatically corrects all mistakes in a program
Correct answer
AIt uses relatively little syntax, making it possible to start writing programs quickly
Explanation
The course describes Python as beginner-friendly because it has relatively little syntax and learners can begin writing a program quickly.
- A
- 2
What does programming primarily involve, as described in the course?
- A
Creating data without specifying what to do with it
- B
Writing text that a computer ignores
- C
Asking a computer to choose its own goals
- D
Giving a computer instructions that it executes
Correct answer
DGiving a computer instructions that it executes
Explanation
A program specifies instructions for a computer, which then executes those instructions.
- A
- 3
What determines the order in which a basic Python script carries out its instructions?
- A
The order of the code lines in the file
- B
The order in which variables were named
- C
The order in which the text editor displays its menus
- D
The length of each line in the file
Correct answer
AThe order of the code lines in the file
Explanation
Python executes the written lines of code in order, so changing their order can change the program's behavior.
- A
- 4
What does a basic
printstatement do in the examples?- A
Creates a new Python file
- B
Stores its contents permanently in a file
- C
Displays its contents in the console
- D
Changes the program's interpreter
Correct answer
CDisplays its contents in the console
Explanation
The examples use
printto display text or other values in the console. - A
- 5
What is the console used for in the demonstrated programming environment?
- A
Choosing the computer's operating system
- B
Storing every variable after the program ends
- C
Editing the installed Python interpreter
- D
Showing output produced by a running program
Correct answer
DShowing output produced by a running program
Explanation
The console is where program output, such as text produced by
print, appears. - A
- 6
What is an IDE, as described in the course?
- A
A Python operator for comparing values
- B
A special kind of Python variable for storing files
- C
A command used only to print text
- D
An integrated development environment for writing and running code
Correct answer
DAn integrated development environment for writing and running code
Explanation
IDE stands for integrated development environment; it provides an environment for writing and running code and may help identify errors.
- A
- 7
Which distinction does the course make between Python 2 and Python 3?
- A
Python 2 is for text editors, while Python 3 is only for IDEs
- B
Python 2 is described as a legacy version, while Python 3 is described as the actively maintained direction
- C
Python 2 and Python 3 are described as having no syntax differences at all
- D
Python 2 is described as the newer version, while Python 3 is no longer supported
Correct answer
BPython 2 is described as a legacy version, while Python 3 is described as the actively maintained direction
Explanation
The course characterizes Python 2 as a legacy version and Python 3 as the newer version receiving ongoing maintenance and support.
- A
- 8
Why does the course choose Python 3 for its beginner instruction?
- A
It presents Python 3 as the current, supported version and a good starting point
- B
It says Python 3 is required to use any text editor
- C
It claims Python 3 can run only on Mac computers
- D
It claims Python 2 cannot run any previously written code
Correct answer
AIt presents Python 3 as the current, supported version and a good starting point
Explanation
The course chooses Python 3 because it is the newer, actively supported version and is presented as a good choice for beginners.
- A
- 9
What does the course say about moving from learning Python 3 to writing Python 2?
- A
Python 3 programs always require rewriting in a different programming language
- B
The versions are entirely unrelated, so knowledge of one cannot help with the other
- C
The versions have some differences, but learning Python 3 makes it possible to move into Python 2
- D
Python 2 and Python 3 use identical syntax in every situation
Correct answer
CThe versions have some differences, but learning Python 3 makes it possible to move into Python 2
Explanation
The course notes that there are some syntax and other differences, but says a learner of Python 3 can move into Python 2.
- A
- 10
What is the purpose of selecting a Python interpreter when creating a project in the demonstrated IDE?
- A
To specify the name of the first variable
- B
To convert the project into a text file
- C
To choose the text color used by the console
- D
To choose the Python version that will run the project
Correct answer
DTo choose the Python version that will run the project
Explanation
The interpreter selection determines which Python version the project uses; the course instructs learners to select Python 3.
- A
- 11
What does a variable provide in a Python program?
- A
A window that displays program output
- B
A named place to store a data value
- C
A method for converting every value into text
- D
A rule that forces every line to run twice
Correct answer
BA named place to store a data value
Explanation
The course describes a variable as a container for storing data values, which makes data easier to manage and use.
- A
- 12
Why can variables help when a value appears many times in a program?
- A
They automatically search and edit every text file on the computer
- B
Changing the value in one assignment can update what later references use
- C
They prevent the program from storing duplicate values
- D
They make every value permanent, even after the program ends
Correct answer
BChanging the value in one assignment can update what later references use
Explanation
When repeated references use a variable, changing its assigned value lets those references use the updated value without manually editing each occurrence.
- A
- 13
Which variable name follows the naming convention demonstrated for multiple words?
- A
character.age - B
character-age - C
character age - D
character_age
Correct answer
Dcharacter_ageExplanation
The course demonstrates separating words in a variable name with an underscore, as in
character_age. - A
- 14
Which description correctly defines a string in the course's examples?
- A
A value that always evaluates to true or false
- B
A container that cannot be changed after creation
- C
A value that can only represent a whole number
- D
A value that represents plain text
Correct answer
DA value that represents plain text
Explanation
A string is used to store plain text, such as a name or a sentence.
- A
- 15
How is a string literal written in the examples?
- A
With a colon after the text
- B
With quotation marks around the text
- C
With square brackets around the text
- D
With a percent sign before the text
Correct answer
BWith quotation marks around the text
Explanation
The examples use quotation marks to mark text as a string.
- A
- 16
What does the escape character
\ndo within a string?- A
Returns the number of characters
- B
Converts the entire string to uppercase
- C
Removes the next character
- D
Inserts a new line
Correct answer
DInserts a new line
Explanation
The course demonstrates
\nas a way to insert a new line into a string. - A
- 17
Why would a programmer use a backslash before a quotation mark inside a string?
- A
To mark the end of a Python program
- B
To indicate that the string should be sorted
- C
To convert the string into a number
- D
To include the quotation mark as text without ending the string
Correct answer
DTo include the quotation mark as text without ending the string
Explanation
The backslash escape character tells Python to render the following quotation mark literally rather than treating it as the end of the string.
- A
- 18
What does string concatenation do in the examples?
- A
Converts a string to a Boolean value
- B
Counts the characters in a string
- C
Appends one string to another
- D
Finds a character's index in a string
Correct answer
CAppends one string to another
Explanation
Concatenation combines strings by appending one string to another, such as adding text to a phrase.
- A
- 19
What is the result of calling a string's
lowermethod?- A
A version of the string with its letters in lowercase
- B
A number giving the string's character count
- C
The first character of the string
- D
A version of the string with its letters reversed
Correct answer
AA version of the string with its letters in lowercase
Explanation
The
lowermethod converts the string's letters to lowercase. - A
- 20
What does the
isuppermethod report?- A
Whether the string contains a number
- B
Whether all the string's letters are uppercase
- C
Whether the string is stored in a variable
- D
Whether the string has more than one character
Correct answer
BWhether all the string's letters are uppercase
Explanation
The course describes
isupperas returning a Boolean indicating whether the string is entirely uppercase. - A
- 21
What does the
lenfunction return when given a string?- A
The number of characters in the string
- B
The position of the first character
- C
The number of words in every related file
- D
The uppercase version of the string
Correct answer
AThe number of characters in the string
Explanation
The
lenfunction returns the string's length, which is its number of characters. - A
- 22
If a string uses zero-based indexing, what index identifies its first character?
- A
One
- B
The string's total length
- C
Negative one
- D
Zero
Correct answer
DZero
Explanation
Python string indexing starts at zero, so the first character is at index zero.
- A
- 23
What does the string
indexmethod provide when the requested text is present?- A
The number of times every character appears
- B
The position where that text begins
- C
The length of the string after conversion to uppercase
- D
A copy with the requested text removed
Correct answer
BThe position where that text begins
Explanation
The
indexmethod returns the position where the specified character or text is found; the course notes it raises an error if the text is absent. - A
- 24
What does a string's
replacemethod do in the example?- A
Replaces specified text with other specified text
- B
Returns the string's first indexed character
- C
Adds a new line after every character
- D
Reports whether the string is lowercase
Correct answer
AReplaces specified text with other specified text
Explanation
The example passes the text to find and the replacement text to
replace, producing a string with that substitution. - A
- 25
Which statement about the basic data types introduced in the course is correct?
- A
They include only strings and lists
- B
They include strings, numbers, and Boolean true-or-false values
- C
They include strings, dictionaries, and classes but not numbers
- D
They include only whole numbers and file names
Correct answer
BThey include strings, numbers, and Boolean true-or-false values
Explanation
The course introduces strings, numbers, and Boolean values as basic types commonly used by new Python programmers.
- A
- 26
How does Python treat a number literal such as
50differently from the string"50"?- A
The quoted value is a Boolean, while the unquoted value is text
- B
Both are always treated as the same numeric value
- C
The number literal is numeric, while the quoted value is text
- D
The unquoted value can only be used in a list
Correct answer
CThe number literal is numeric, while the quoted value is text
Explanation
Quotation marks make a value a string; a number written without them is a numeric value.
- A
- 27
Which operation does the
%operator perform in the demonstrated arithmetic?- A
It raises one number to a power
- B
It divides and discards the remainder in every case
- C
It converts a number to a string
- D
It gives the remainder after division
Correct answer
DIt gives the remainder after division
Explanation
The course describes
%as the modulus operator, which returns the remainder from division. - A
- 28
How can parentheses affect the result of an arithmetic expression?
- A
They can change which operations are performed first
- B
They make the result a Boolean value
- C
They convert all values in the expression to strings
- D
They cause the expression to be skipped
Correct answer
AThey can change which operations are performed first
Explanation
The examples use parentheses to control order of operations, such as adding before multiplying.
- A
- 29
What is the effect of converting a number with
int?- A
It converts the value to a Boolean
- B
It rounds every decimal upward
- C
It converts the value to a string with quotation marks
- D
It converts the value to an integer, which is a whole-number type
Correct answer
DIt converts the value to an integer, which is a whole-number type
Explanation
The course describes
intas converting a value to an integer, a whole-number type without a decimal part. - A
- 30
Why use
floatrather thanintfor calculator inputs that may include decimal parts?- A
A float can represent decimal numbers
- B
A float converts numbers into lists
- C
A float can only represent whole numbers
- D
A float preserves user input only as plain text
Correct answer
AA float can represent decimal numbers
Explanation
The course uses
floatso calculator inputs can include decimal numbers;intis for whole numbers. - A
- 31
What does the
absfunction return for a negative number?- A
Its square root
- B
Its value as a string
- C
Its absolute value
- D
Its remainder after division by two
Correct answer
CIts absolute value
Explanation
absreturns the absolute value; the example applies it to a negative number and gets its positive magnitude. - A
- 32
What does the
powfunction do in the examples?- A
Converts a number into a string
- B
Rounds a decimal to the nearest integer
- C
Returns the smaller of two values
- D
Raises a number to a specified power
Correct answer
DRaises a number to a specified power
Explanation
The example gives
powa number and a power, and it returns the number raised to that power. - A
- 33
How do the
maxandminfunctions differ?- A
maxcounts values, whileminsorts them - B
maxreturns the largest supplied value, whileminreturns the smallest - C
maxrounds values, whileminconverts them to strings - D
maxreturns the smallest supplied value, whileminreturns the largest
Correct answer
Bmaxreturns the largest supplied value, whileminreturns the smallestExplanation
The course demonstrates
maxas returning the larger supplied number andminas returning the smaller one. - A
- 34
What behavior does the
roundfunction illustrate?- A
It always rounds a number upward
- B
It rounds a number according to ordinary rounding rules
- C
It always removes the decimal part without rounding
- D
It returns the remainder from division
Correct answer
BIt rounds a number according to ordinary rounding rules
Explanation
The examples show
roundrounding a decimal down or up depending on its value. - A
- 35
Which pairing of math functions and behavior matches the course examples?
- A
floormoves to the lower whole number, andceilmoves upward - B
floorreturns the largest input, andceilreturns the smallest - C
floorconverts to text, andceilcounts characters - D
floorfinds a square root, andceilfinds an absolute value
Correct answer
Afloormoves to the lower whole number, andceilmoves upwardExplanation
The course describes
flooras taking the lower whole-number value andceilas rounding upward. - A
- 36
What does
sqrtcalculate in the example?- A
The square root of a number
- B
The absolute value of a number
- C
The square of a number
- D
The remainder after dividing a number
Correct answer
AThe square root of a number
Explanation
The example uses
sqrtto calculate the square root of a number. - A
- 37
Why does the course import from the
mathmodule before using some math functions?- A
Importing prevents functions from returning values
- B
Importing converts all variables to numbers
- C
Importing changes the Python version used by the project
- D
Importing makes additional math functions available to the program
Correct answer
DImporting makes additional math functions available to the program
Explanation
The course explains that importing from
mathprovides access to additional math functions. - A
- 38
What type of value does
inputreturn by default, according to the calculator example?- A
A Boolean value
- B
An integer in all cases
- C
A string
- D
A list of characters
Correct answer
CA string
Explanation
The calculator example explains that user input is treated as a string by default, even when the user types digits.
- A
- 39
Why did adding two user-entered values initially produce a joined-looking result instead of a sum?
- A
The input values were strings, so
+concatenated them - B
The console automatically sorted the entered values
- C
The program used a list instead of a variable
- D
The input values were Booleans, so
+ignored them
Correct answer
AThe input values were strings, so
+concatenated themExplanation
Because
inputreturns strings by default, using+joined the text values rather than adding numeric values. - A
- 40
In the calculator example, why are the user inputs converted to floats before arithmetic?
- A
To prevent users from entering whole numbers
- B
To ensure that they remain strings during addition
- C
To convert the operator into a number
- D
To make them numeric while still allowing decimal values
Correct answer
DTo make them numeric while still allowing decimal values
Explanation
Converting inputs to floats allows arithmetic to work on numbers, including values with decimal parts.
- A
- 41
What does a Python list primarily provide in the examples?
- A
A way to make stored values impossible to change
- B
A command for reading a text file
- C
A structure for organizing and storing multiple values
- D
A special function for returning values from methods
Correct answer
CA structure for organizing and storing multiple values
Explanation
The course presents lists as structures for storing and organizing multiple related values.
- A
- 42
Which statement about the values in a Python list matches the course examples?
- A
A list can hold only one value at a time
- B
A list can hold values such as strings, numbers, and Booleans
- C
A list can hold strings only
- D
Every value in a list must have a different data type
Correct answer
BA list can hold values such as strings, numbers, and Booleans
Explanation
The course notes that lists can store different kinds of values, including strings, numbers, and Booleans.
- A
- 43
With zero-based indexing, what is the index of the third item in a list?
- A
Two
- B
Negative three
- C
One
- D
Three
Correct answer
ATwo
Explanation
List indexes start at zero, so the first, second, and third items have indexes zero, one, and two.
- A
- 44
What does a negative list index allow a programmer to do?
- A
Access only elements whose values are negative numbers
- B
Skip all elements before the selected index
- C
Change the list into a tuple
- D
Access elements counted from the end of the list
Correct answer
DAccess elements counted from the end of the list
Explanation
The examples use negative indexes to access elements from the back of a list;
-1refers to the last element. - A
- 45
What is the purpose of a list slice such as
friends[1:3]in the demonstrated convention?- A
To select only the item at index three
- B
To reverse the list and select its first two items
- C
To select every item from index zero through the end
- D
To select items starting at index one and stopping before index three
Correct answer
DTo select items starting at index one and stopping before index three
Explanation
The course explains that a specified slice includes the starting index but does not include the ending index.
- A
- 46
How can a programmer change an existing item in a list?
- A
Convert the whole list to a string
- B
Call
clearand then print the old item - C
Assign a new value to the item at its index
- D
Use a negative index as the item's new value
Correct answer
CAssign a new value to the item at its index
Explanation
The example modifies a list by assigning a new value to a selected index.
- A
- 47
What does the list method
extenddo in the example?- A
Removes the final item from the list
- B
Sorts the list in descending order
- C
Adds one item at a specified index
- D
Adds the elements of another list to the end of the list
Correct answer
DAdds the elements of another list to the end of the list
Explanation
The course uses
extendto add the elements of one list to another list. - A
- 48
How does
appenddiffer frominsertin the demonstrated list operations?- A
appendsorts the list, whileinsertreverses it - B
appendadds an item at the end, whileinsertadds it at a specified index - C
appendremoves an item, whileinsertcopies a list - D
appendclears the list, whileinsertchecks for an item
Correct answer
Bappendadds an item at the end, whileinsertadds it at a specified indexExplanation
The examples show
appendadding to the end andinserttaking an index at which to add the item. - A
- 49
What is the effect of calling
removewith a value in a list?- A
It removes every value from the list
- B
It removes a matching value from the list
- C
It removes the last value regardless of its contents
- D
It returns the index without changing the list
Correct answer
BIt removes a matching value from the list
Explanation
The course demonstrates
removedeleting the specified value from the list. - A
- 50
How do
clearandpopdiffer in the examples?- A
Both methods add a copy of the list
- B
Both methods sort the list in place
- C
clearremoves all items, whilepopremoves the last item - D
clearremoves the last item, whilepopremoves all items
Correct answer
Cclearremoves all items, whilepopremoves the last itemExplanation
The course shows
clearmaking the list empty andpopremoving its final item. - A
- 51
What information does the list method
indexprovide?- A
A reversed copy of the list
- B
The number of times a value appears
- C
The index of a specified value when that value is present
- D
The total number of values in every list
Correct answer
CThe index of a specified value when that value is present
Explanation
The example uses
indexto find where a specified item occurs; it produces an error if the value is absent. - A
- 52
What does the list method
countreturn?- A
The index of the first item
- B
The number of times a specified value occurs
- C
The number of lists nested inside the list
- D
The total length of the list after sorting
Correct answer
BThe number of times a specified value occurs
Explanation
The course demonstrates
countreturning how many times a value appears in a list. - A
- 53
What does calling
sortdo in the list examples?- A
Orders the list in ascending order
- B
Deletes repeated items
- C
Copies the list into a tuple
- D
Reverses the current order without comparing values
Correct answer
AOrders the list in ascending order
Explanation
The course demonstrates
sortputting values in ascending order, such as arranging text alphabetically. - A
- 54
What does calling
reversedo to a list?- A
Reverses the order of its elements
- B
Sorts its elements in ascending order
- C
Makes a separate copy and leaves the original untouched
- D
Removes its first element
Correct answer
AReverses the order of its elements
Explanation
The example uses
reverseto turn the list's existing order around. - A
- 55
Which statement correctly distinguishes a tuple from a list in the course?
- A
A tuple must contain a single value, while a list must contain several
- B
A tuple is indexed from one, while a list is indexed from zero
- C
A tuple is immutable, while a list can be modified
- D
A tuple can store only text, while a list can store numbers
Correct answer
CA tuple is immutable, while a list can be modified
Explanation
The central distinction presented is that tuple contents cannot be modified after creation, while lists can be changed.
- A
- 56
Why might coordinates be stored in a tuple?
- A
They need to be cleared whenever a new coordinate is added
- B
They represent data that is not intended to be changed after creation
- C
They must be converted to strings before being stored
- D
They need to be sorted automatically after every assignment
Correct answer
BThey represent data that is not intended to be changed after creation
Explanation
The course presents coordinates as a good tuple use case because such data may not need to be mutated.
- A
- 57
How are key-value pairs organized in a Python dictionary?
- A
Each pair is stored as an unchangeable sequence of characters
- B
Each value identifies an associated list index
- C
Each key must be a duplicate of another key
- D
Each key identifies an associated value
Correct answer
DEach key identifies an associated value
Explanation
A dictionary stores key-value pairs; a key identifies the value associated with it.
- A
- 58
What requirement does the course give for dictionary keys?
- A
Keys should be unique
- B
Keys must be stored in alphabetical order
- C
Keys must all be numbers
- D
Keys must match their values exactly
Correct answer
AKeys should be unique
Explanation
The course warns that dictionary keys should be unique and demonstrates that duplicate keys are not appropriate.
- A
- 59
What is an advantage of using a dictionary's
getmethod with a default value?- A
It sorts all keys before returning their values
- B
It automatically changes a missing key into a list
- C
It can return a chosen fallback when the requested key is absent
- D
It prevents the dictionary from storing string keys
Correct answer
CIt can return a chosen fallback when the requested key is absent
Explanation
The example shows that
getcan be given a default value to return when the requested key is not in the dictionary. - A
- 60
What is a function in the course's description?
- A
A container that can store only a single string
- B
A special type of loop that always runs forever
- C
A reusable collection of code that performs a specific task
- D
A text file that Python cannot execute
Correct answer
CA reusable collection of code that performs a specific task
Explanation
The course describes a function as a collection of code that performs a specific task and can be called when needed.
- A
- 61
What does the
defkeyword indicate in the demonstrated syntax?- A
The start of a list definition
- B
The end of a loop
- C
The beginning of a function definition
- D
The conversion of a value to a float
Correct answer
CThe beginning of a function definition
Explanation
The course uses
defto begin defining a function. - A
- 62
Why is code inside a function indented in the demonstrated Python style?
- A
Indentation marks the code as part of the function body
- B
Indentation makes the function execute before it is called
- C
Indentation converts its parameters into strings
- D
Indentation changes the function into a list
Correct answer
AIndentation marks the code as part of the function body
Explanation
The course explains that the code belonging to a function must be indented under its definition.
- A
- 63
What happens to a function's body when a program runs but the function is never called?
- A
The body is automatically printed in the console
- B
The body is not executed just because it was defined
- C
The body runs once for every variable in the program
- D
The function's parameters are assigned random values
Correct answer
BThe body is not executed just because it was defined
Explanation
The examples show that defining a function does not execute its body; the function must be called.
- A
- 64
What is a parameter in the course's explanation of functions?
- A
A special character that ends a string
- B
A line of code that Python ignores
- C
A named piece of information a function is set up to receive
- D
A value that a function can return only after a loop
Correct answer
CA named piece of information a function is set up to receive
Explanation
A parameter is information specified for a function to receive when it is called.
- A
- 65
What is the purpose of the
returnstatement in a function?- A
To repeat the function until its parameter changes
- B
To print the function's name in the console
- C
To send a value back to the code that called the function
- D
To add a new parameter to the function definition
Correct answer
CTo send a value back to the code that called the function
Explanation
The course demonstrates
returnsending a computed value back to the caller, where it can be assigned to a variable. - A
- 66
What happens to execution inside a function when Python reaches
return?- A
The program automatically calls the function again
- B
The function ends and control returns to its caller
- C
The return value is discarded and the program always prints
None - D
The function continues through every line below the return
Correct answer
BThe function ends and control returns to its caller
Explanation
The course explains that
returnbreaks out of the function, so statements after it are not reached during that call. - A
- 67
What is the main role of an
ifstatement?- A
To store a group of related values
- B
To make every function return a value
- C
To execute all program lines in reverse order
- D
To run code conditionally based on whether a condition is true
Correct answer
DTo run code conditionally based on whether a condition is true
Explanation
An
ifstatement lets a program choose whether to execute code based on a true-or-false condition. - A
- 68
When does an
elseblock run in the examples?- A
Only when the program has no variables
- B
Whenever the preceding
ifcondition is true - C
When the preceding
ifcondition is false and no earlier branch was taken - D
Before Python checks the
ifcondition
Correct answer
CWhen the preceding
ifcondition is false and no earlier branch was takenExplanation
The course uses
elsefor the alternative case when the associatedifcondition is not true. - A
- 69
What does
elifallow a program to do?- A
Return a value from the current function
- B
Define a new class inside an if statement
- C
Check another condition when a preceding condition was false
- D
Repeat a true condition indefinitely
Correct answer
CCheck another condition when a preceding condition was false
Explanation
The examples use
elifto test additional conditions after earlier conditions were not met. - A
- 70
In a condition using
and, when is the combined condition true?- A
When either component condition is true
- B
Only when both component conditions are false
- C
When both component conditions are true
- D
Whenever one component condition is omitted
Correct answer
CWhen both component conditions are true
Explanation
The course explains that
andrequires both conditions to be true. - A
- 71
In a condition using
or, when is the combined condition true?- A
Only when both component conditions are false
- B
When at least one component condition is true
- C
Only when both component conditions are true
- D
Only when the conditions contain numbers
Correct answer
BWhen at least one component condition is true
Explanation
The course explains that
oris true when one or both of its conditions are true. - A
- 72
What effect does
nothave on a Boolean condition?- A
It adds the condition to a number
- B
It compares the condition with a string
- C
It makes the condition permanently true
- D
It negates the condition's truth value
Correct answer
DIt negates the condition's truth value
Explanation
The example uses
notto reverse a condition: a true value becomes false and a false value becomes true. - A
- 73
What does a comparison such as
num_one >= num_twoproduce for use in anifstatement?- A
A function definition
- B
A true-or-false result
- C
A string containing the comparison symbols
- D
A new list containing both numbers
Correct answer
BA true-or-false result
Explanation
A comparison evaluates to a Boolean result, which can be used as an
ifcondition. - A
- 74
Which operator checks whether two values are equal in the comparisons described?
- A
== - B
!= - C
= - D
>=
Correct answer
A==Explanation
The course identifies the double equal sign as the equality comparison operator; a single equals sign is used for assignment.
- A
- 75
What does the comparison operator
!=mean?- A
The first value is assigned to the second
- B
Both values must be true
- C
The two values are not equal
- D
The first value is greater than the second
Correct answer
CThe two values are not equal
Explanation
The course describes
!=as the not-equal comparison operator. - A
- 76
How can a function that finds the largest of three numbers make its decision?
- A
Compare the numbers and return the one that is greater than or equal to the others
- B
Sort a dictionary of the numbers and return its first key
- C
Convert all three numbers to strings and return the longest one
- D
Use a while loop without checking any values
Correct answer
ACompare the numbers and return the one that is greater than or equal to the others
Explanation
The example uses comparisons in conditional branches to determine which input is largest and return it.
- A
- 77
How does the more advanced calculator determine which arithmetic operation to perform?
- A
It assumes every operator means addition
- B
It uses a dictionary key to change the Python interpreter
- C
It reads the second number as the operator
- D
It compares the user's operator input with symbols and selects a corresponding branch
Correct answer
DIt compares the user's operator input with symbols and selects a corresponding branch
Explanation
The calculator checks whether the operator string matches addition, subtraction, division, or multiplication, then performs the corresponding operation.
- A
- 78
What condition keeps a
whileloop running in the basic example?- A
Its loop condition remains true
- B
Its variable has a string value
- C
Its code has been written in a separate file
- D
Its loop body contains a
printstatement
Correct answer
AIts loop condition remains true
Explanation
A
whileloop continues executing its body as long as its condition is true. - A
- 79
When does Python check the condition of a
whileloop in the demonstrated process?- A
Once when the program first starts, never again
- B
Only after the loop has finished
- C
Before each pass through the loop body
- D
Only when the loop body contains an
ifstatement
Correct answer
CBefore each pass through the loop body
Explanation
The course explains that Python checks the loop condition before entering the body and again after each iteration.
- A
- 80
Why does the counting
whileloop increment its counter?- A
To make the condition permanently true
- B
To make the condition eventually false and end the loop
- C
To sort the values printed by the loop
- D
To convert the counter into a string
Correct answer
BTo make the condition eventually false and end the loop
Explanation
Incrementing the counter moves it toward the value that makes the loop condition false, allowing the loop to end.
- A
- 81
How does the guessing game repeat prompts when a guess is incorrect?
- A
A list index causes the prompt to appear again
- B
A print statement restarts the whole program
- C
A while loop continues while the guess differs from the secret word
- D
A function definition automatically repeats its body
Correct answer
CA while loop continues while the guess differs from the secret word
Explanation
The guessing game uses a while loop whose condition checks whether the guess is still different from the secret word.
- A
- 82
In the limited guessing game, what does the guess counter track?
- A
How many times the console has opened
- B
How many letters are in the secret word
- C
How many guesses the user has made
- D
How many questions are in the program file
Correct answer
CHow many guesses the user has made
Explanation
The game increments the guess counter each time it gives the user a guess.
- A
- 83
What does the
out_of_guessesBoolean represent in the guessing game?- A
Whether the program can open a file
- B
Whether the secret word is stored in a list
- C
Whether the user has exhausted the permitted guesses
- D
Whether the current guess is uppercase
Correct answer
CWhether the user has exhausted the permitted guesses
Explanation
The game uses this Boolean to distinguish having guesses left from having reached the guess limit.
- A
- 84
What does a
forloop do when used with a string in the examples?- A
Iterates over the string's characters one at a time
- B
Removes all spaces from the string
- C
Returns the index of a requested substring
- D
Converts the string into a number automatically
Correct answer
AIterates over the string's characters one at a time
Explanation
The course demonstrates a
forloop assigning each successive character of a string to its loop variable. - A
- 85
What does a
forloop over a list do in the demonstrated example?- A
Visits only the first and last elements
- B
Changes every list element into an index
- C
Visits each list element in turn
- D
Removes each element after printing it
Correct answer
CVisits each list element in turn
Explanation
The example loops over the friends list and prints each friend in turn.
- A
- 86
What values are produced by
range(3, 6)under the convention shown?- A
Zero, one, and two
- B
Three, four, five, and six
- C
Four, five, and six
- D
Three, four, and five
Correct answer
DThree, four, and five
Explanation
The course explains that a range includes its starting value and stops before its ending value.
- A
- 87
How can a programmer use a list's length to loop over its indexes?
- A
Use the list's final value as the start of the range
- B
Use
len(list)as the only index - C
Use a range from zero up to, but not including,
len(list) - D
Use a range from one through
len(list)inclusive
Correct answer
CUse a range from zero up to, but not including,
len(list)Explanation
The example uses
range(len(friends))to generate the valid indexes from zero through the last index. - A
- 88
What makes the two-dimensional list example a grid-like structure?
- A
It stores only one value in each program
- B
Its values are all converted to strings
- C
It uses a dictionary instead of lists
- D
Its outer list contains other lists as elements
Correct answer
DIts outer list contains other lists as elements
Explanation
The example creates a list whose elements are themselves lists, forming rows and columns.
- A
- 89
How does the nested-loop example visit every value in a two-dimensional list?
- A
The outer loop visits each inner list, and the inner loop visits each value in that list
- B
The outer loop sorts the grid while the inner loop prints its length
- C
Both loops visit only the first row
- D
The inner loop selects one value, and the outer loop repeats only that value
Correct answer
AThe outer loop visits each inner list, and the inner loop visits each value in that list
Explanation
The outer loop iterates through rows, while the inner loop iterates through the values within each row.
- A
- 90
How does the exponent-function example use a
forloop?- A
It multiplies the running result by the base once per iteration of the requested power
- B
It prints each number in the range without calculating a result
- C
It divides the base by the power on every iteration
- D
It changes the power into a list of strings
Correct answer
AIt multiplies the running result by the base once per iteration of the requested power
Explanation
The function initializes a result and repeatedly multiplies it by the base, with the number of iterations determined by the power.
- A
- 91
What is the purpose of the translator's loop over the phrase?
- A
To examine each letter and build a translated string
- B
To ask the user for a new phrase after every letter
- C
To count how many files are in a directory
- D
To sort the phrase's words alphabetically
Correct answer
ATo examine each letter and build a translated string
Explanation
The translator loops over the phrase's letters and adds either a replacement or the original letter to the translation.
- A
- 92
What do comments beginning with
#do in a Python file?- A
They convert the following line into a function
- B
They provide notes that Python ignores during execution
- C
They print the comment text in the console
- D
They force the next line to run twice
Correct answer
BThey provide notes that Python ignores during execution
Explanation
The course describes
#comments as notes for people; Python ignores them when running the program. - A
- 93
What does it mean to comment out a line of code?
- A
Convert its result into a comment string
- B
Make Python execute it before every other line
- C
Make Python ignore it without deleting it from the file
- D
Move it automatically into another file
Correct answer
CMake Python ignore it without deleting it from the file
Explanation
Adding a comment marker can disable a line temporarily while leaving the code in the file.
- A
- 94
What is the course's recommended style for comments spanning multiple lines?
- A
Put a single
#only at the end of the final line - B
Use quotation marks around each individual word
- C
Use a
#on each comment line - D
Use a separate Python file for every line
Correct answer
CUse a
#on each comment lineExplanation
The course says that although triple quotes can be used, the demonstrated recommended style is to use
#on each line. - A
- 95
What problem does a
try/exceptblock help a program handle?- A
A list that contains several values
- B
A function that has a return statement
- C
An error that would otherwise interrupt normal execution
- D
A variable whose name contains an underscore
Correct answer
CAn error that would otherwise interrupt normal execution
Explanation
The course uses
try/exceptblocks to catch errors and handle them rather than allowing the program to stop unexpectedly. - A
- 96
Why is a bare
exceptblock discouraged in the course example?- A
It prevents Python from running any code in the
tryblock - B
It can catch a very broad range of errors, including ones the handler was not meant to address
- C
It can be used only when reading files
- D
It catches only errors caused by invalid user input
Correct answer
BIt can catch a very broad range of errors, including ones the handler was not meant to address
Explanation
The course warns that a bare
exceptis too broad because it can catch many unrelated errors. - A
- 97
What is a benefit of catching a specific exception type?
- A
The exception can no longer occur
- B
The program can respond differently to different kinds of errors
- C
The exception is automatically converted into a valid number
- D
The program no longer needs any conditional statements
Correct answer
BThe program can respond differently to different kinds of errors
Explanation
The examples distinguish a value error from a zero-division error and show how each can receive a different response.
- A
- 98
What does an
except ... as errclause allow the program to do?- A
Add the error to a list without assigning a name
- B
Refer to the caught error through a variable
- C
Convert the error into a function parameter
- D
Automatically retry every line in the program
Correct answer
BRefer to the caught error through a variable
Explanation
The course shows assigning the caught exception to a variable and printing it to see what went wrong.
- A
- 99
What does opening a file with mode
rindicate in the examples?- A
The program intends to append text to the file
- B
The program intends to replace the file's contents
- C
The program intends to rename the file
- D
The program intends to read from the file
Correct answer
DThe program intends to read from the file
Explanation
The course identifies
ras read mode. - A
- 100
What does opening a file with mode
aallow in the examples?- A
Replacing the file's existing contents
- B
Reading the file while preventing any changes
- C
Appending new content to the end of the file
- D
Deleting the file after it is read
Correct answer
CAppending new content to the end of the file
Explanation
The course identifies
aas append mode, which adds content to the end of an existing file. - A
- 101
What is the effect of opening a file with mode
win the demonstrated use?- A
It converts the file into a Python module
- B
It writes content and can replace existing file contents
- C
It adds content only to the end without affecting existing content
- D
It makes the file readable but not writable
Correct answer
BIt writes content and can replace existing file contents
Explanation
The example shows that
woverwrites the existing file contents and can also be used to create a file. - A
- 102
What does the
r+mode indicate in the course's list of file modes?- A
Write-only access that always creates a new name
- B
Read and write access
- C
Read-only access
- D
Append-only access
Correct answer
BRead and write access
Explanation
The course describes
r+as a mode for reading and writing. - A
- 103
Why should a program close a file after it is finished using it?
- A
Closing it converts all its contents into a list
- B
Closing it is part of good practice after opening and using the file
- C
Closing it automatically saves every Python variable
- D
Closing it permanently prevents later programs from reading it
Correct answer
BClosing it is part of good practice after opening and using the file
Explanation
The course emphasizes that a file should be closed once the program is done working with it.
- A
- 104
What does the file method
readreturn in the demonstrated example?- A
The number of characters in the file name
- B
The file's contents
- C
The file's mode as a Boolean value
- D
Only the file's first line
Correct answer
BThe file's contents
Explanation
The example uses
readto retrieve and print all of the file's contents. - A
- 105
How does
readlinebehave in the example?- A
It returns whether the file can be read
- B
It appends a line to the file
- C
It reads the entire file into a list
- D
It reads one line at a time
Correct answer
DIt reads one line at a time
Explanation
The course demonstrates
readlinereading a line and moving on so later calls can read subsequent lines. - A
- 106
What does
readlinesdo in the file-reading example?- A
Checks whether the file exists
- B
Writes a new line to the end of the file
- C
Returns the file's lines as a list
- D
Returns only the first line as a string
Correct answer
CReturns the file's lines as a list
Explanation
The example explains that
readlinesplaces the file's lines into an array-like list. - A
- 107
What does the
readablemethod report?- A
Whether every line in the file is unique
- B
Whether the file contains a Python class
- C
Whether the file has already been closed
- D
Whether the file can be read in its current mode
Correct answer
DWhether the file can be read in its current mode
Explanation
The course uses
readableto get a Boolean indicating whether reading from the opened file is possible. - A
- 108
Why might a program write
\nbefore appended text?- A
To make the file read-only
- B
To begin the appended content on a new line
- C
To remove the preceding line
- D
To convert the text into a variable
Correct answer
BTo begin the appended content on a new line
Explanation
The course demonstrates using the newline escape character so appended content begins on a new line.
- A
- 109
What is a Python module, as described in the course?
- A
A kind of console window that prints errors
- B
A Python file that can be imported to reuse its code
- C
A variable that stores only a file extension
- D
A list whose elements cannot be changed
Correct answer
BA Python file that can be imported to reuse its code
Explanation
The course describes a module as a Python file whose functions, variables, and other code can be imported into another file.
- A
- 110
What is one advantage of importing a module instead of copying its functions into another file?
- A
The code can be reused without physically duplicating it
- B
The importing file no longer needs to run
- C
The imported code is guaranteed never to contain errors
- D
The imported functions become text strings
Correct answer
AThe code can be reused without physically duplicating it
Explanation
Importing lets a program use code from another file without manually copying and pasting that code.
- A
- 111
What does
pipdo in the course's discussion of external modules?- A
Installs and manages Python packages
- B
Creates a class from a text file
- C
Runs a Python file's print statements
- D
Converts Python 2 code to Python 3 code
Correct answer
AInstalls and manages Python packages
Explanation
The course describes
pipas a package manager used to install, manage, update, and uninstall Python modules. - A
- 112
Where does the course say many installed third-party packages are placed?
- A
In a
site-packagesfolder within the Python library area - B
In the computer's list of text-editor themes
- C
In the console's output history
- D
Inside every Python file that imports them
Correct answer
AIn a
site-packagesfolder within the Python library areaExplanation
The course shows third-party packages installed under the Python library area, in a
site-packagesfolder. - A
- 113
Which statement correctly describes the relationship between a class and an object?
- A
A class defines a type or template, and an object is an instance of that class
- B
A class is a function that can never create objects
- C
An object is always a text file imported into a class
- D
An object defines a type, and a class is one value stored by it
Correct answer
AA class defines a type or template, and an object is an instance of that class
Explanation
The course describes a class as a template for a data type and an object as a particular instance created from it.
- A
- 114
Why might a programmer define a class for students rather than represent each student only with a string?
- A
A class can group several relevant attributes into a student data type
- B
A class allows Python to run without an interpreter
- C
A class prevents student information from being stored in variables
- D
A class automatically converts every student into a list
Correct answer
AA class can group several relevant attributes into a student data type
Explanation
The student class groups information such as a name, major, GPA, and probation status into a representation of a student.
- A
- 115
What is the role of the
__init__method in the course's class examples?- A
It repeatedly runs the class's methods
- B
It closes files opened by the class
- C
It imports the class into every Python file
- D
It initializes a newly created object with its attributes
Correct answer
DIt initializes a newly created object with its attributes
Explanation
The examples use
__init__to assign the values passed when a new object is created to that object's attributes. - A
- 116
In an instance method, what does
selfrefer to in the examples?- A
The first value printed by the console
- B
The particular object whose attributes or methods are being used
- C
The Python interpreter that runs the project
- D
The class's source file on disk
Correct answer
BThe particular object whose attributes or methods are being used
Explanation
The course uses
selfto refer to the current object, such as the particular student whose GPA is being checked. - A
- 117
What is a class method or function used for in the student example?
- A
Preventing objects from having attributes
- B
Replacing the class with a dictionary
- C
Providing behavior or information associated with objects of the class
- D
Automatically creating a separate interpreter
Correct answer
CProviding behavior or information associated with objects of the class
Explanation
The
on_honor_rollmethod uses an object's GPA to determine and return whether the student meets the stated condition. - A
- 118
What does inheritance allow a class to do in the chef example?
- A
Make its objects impossible to create
- B
Reuse functionality defined in another class
- C
Delete the parent class after importing it
- D
Turn all of its methods into strings
Correct answer
BReuse functionality defined in another class
Explanation
The Chinese chef class inherits methods from the chef class, so it can use those methods without duplicating their definitions.
- A
- 119
In the inheritance example, why can a Chinese chef make chicken without defining
make_chickenin its own class?- A
It inherits that method from the chef class
- B
The
make_special_dishmethod creates it - C
The method is stored in the console
- D
Python automatically gives every class that method
Correct answer
AIt inherits that method from the chef class
Explanation
The Chinese chef inherits the generic chef's
make_chickenmethod. - A
- 120
What does overriding a method allow a subclass to do in the chef example?
- A
Make the inherited method run before every other method
- B
Prevent the subclass from having any additional methods
- C
Change every object of the parent class
- D
Provide its own behavior for a method inherited from the parent class
Correct answer
DProvide its own behavior for a method inherited from the parent class
Explanation
The Chinese chef defines its own
make_special_dishmethod so its special dish differs from the generic chef's. - A
- 121
What is the Python interpreter environment described in the course useful for?
- A
Replacing an IDE for writing every large program
- B
Quickly trying Python commands and small pieces of code
- C
Storing program output permanently in a text file
- D
Editing the computer's operating system with graphical controls
Correct answer
BQuickly trying Python commands and small pieces of code
Explanation
The course presents the interpreter as a convenient environment for testing commands and small code snippets, rather than writing substantial programs.
- A
- 122
How does the course distinguish a terminal or command prompt from a graphical interface?
- A
It lets users interact only by moving windows and clicking buttons
- B
It is a special kind of Python list
- C
It displays only the results of
printstatements and accepts no commands - D
It lets users interact with the computer by entering text commands
Correct answer
DIt lets users interact with the computer by entering text commands
Explanation
The course describes the terminal or command prompt as an environment for interacting with a computer through text commands.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.