1. Home
  2. Explore
  3. Computer Science
  4. Python Programming Basics

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Python Programming Basics

  • 122Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Python Foundations

According to the course content, what is one reason Python is often presented as a suitable language for beginners?

122 questions

  1. 1

    According to the course content, what is one reason Python is often presented as a suitable language for beginners?

    1. A

      It uses relatively little syntax, making it possible to start writing programs quickly

    2. B

      It prevents beginners from using variables until they learn advanced concepts

    3. C

      It requires every program to be written inside a graphical interface

    4. D

      It automatically corrects all mistakes in a program

    Correct answer
    A

    It 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.

  2. 2

    What does programming primarily involve, as described in the course?

    1. A

      Creating data without specifying what to do with it

    2. B

      Writing text that a computer ignores

    3. C

      Asking a computer to choose its own goals

    4. D

      Giving a computer instructions that it executes

    Correct answer
    D

    Giving a computer instructions that it executes

    Explanation

    A program specifies instructions for a computer, which then executes those instructions.

  3. 3

    What determines the order in which a basic Python script carries out its instructions?

    1. A

      The order of the code lines in the file

    2. B

      The order in which variables were named

    3. C

      The order in which the text editor displays its menus

    4. D

      The length of each line in the file

    Correct answer
    A

    The 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.

  4. 4

    What does a basic print statement do in the examples?

    1. A

      Creates a new Python file

    2. B

      Stores its contents permanently in a file

    3. C

      Displays its contents in the console

    4. D

      Changes the program's interpreter

    Correct answer
    C

    Displays its contents in the console

    Explanation

    The examples use print to display text or other values in the console.

  5. 5

    What is the console used for in the demonstrated programming environment?

    1. A

      Choosing the computer's operating system

    2. B

      Storing every variable after the program ends

    3. C

      Editing the installed Python interpreter

    4. D

      Showing output produced by a running program

    Correct answer
    D

    Showing output produced by a running program

    Explanation

    The console is where program output, such as text produced by print, appears.

  6. 6

    What is an IDE, as described in the course?

    1. A

      A Python operator for comparing values

    2. B

      A special kind of Python variable for storing files

    3. C

      A command used only to print text

    4. D

      An integrated development environment for writing and running code

    Correct answer
    D

    An 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.

  7. 7

    Which distinction does the course make between Python 2 and Python 3?

    1. A

      Python 2 is for text editors, while Python 3 is only for IDEs

    2. B

      Python 2 is described as a legacy version, while Python 3 is described as the actively maintained direction

    3. C

      Python 2 and Python 3 are described as having no syntax differences at all

    4. D

      Python 2 is described as the newer version, while Python 3 is no longer supported

    Correct answer
    B

    Python 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.

  8. 8

    Why does the course choose Python 3 for its beginner instruction?

    1. A

      It presents Python 3 as the current, supported version and a good starting point

    2. B

      It says Python 3 is required to use any text editor

    3. C

      It claims Python 3 can run only on Mac computers

    4. D

      It claims Python 2 cannot run any previously written code

    Correct answer
    A

    It 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.

  9. 9

    What does the course say about moving from learning Python 3 to writing Python 2?

    1. A

      Python 3 programs always require rewriting in a different programming language

    2. B

      The versions are entirely unrelated, so knowledge of one cannot help with the other

    3. C

      The versions have some differences, but learning Python 3 makes it possible to move into Python 2

    4. D

      Python 2 and Python 3 use identical syntax in every situation

    Correct answer
    C

    The 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.

  10. 10

    What is the purpose of selecting a Python interpreter when creating a project in the demonstrated IDE?

    1. A

      To specify the name of the first variable

    2. B

      To convert the project into a text file

    3. C

      To choose the text color used by the console

    4. D

      To choose the Python version that will run the project

    Correct answer
    D

    To 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.

  11. 11

    What does a variable provide in a Python program?

    1. A

      A window that displays program output

    2. B

      A named place to store a data value

    3. C

      A method for converting every value into text

    4. D

      A rule that forces every line to run twice

    Correct answer
    B

    A 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.

  12. 12

    Why can variables help when a value appears many times in a program?

    1. A

      They automatically search and edit every text file on the computer

    2. B

      Changing the value in one assignment can update what later references use

    3. C

      They prevent the program from storing duplicate values

    4. D

      They make every value permanent, even after the program ends

    Correct answer
    B

    Changing 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.

  13. 13

    Which variable name follows the naming convention demonstrated for multiple words?

    1. A

      character.age

    2. B

      character-age

    3. C

      character age

    4. D

      character_age

    Correct answer
    D

    character_age

    Explanation

    The course demonstrates separating words in a variable name with an underscore, as in character_age.

  14. 14

    Which description correctly defines a string in the course's examples?

    1. A

      A value that always evaluates to true or false

    2. B

      A container that cannot be changed after creation

    3. C

      A value that can only represent a whole number

    4. D

      A value that represents plain text

    Correct answer
    D

    A value that represents plain text

    Explanation

    A string is used to store plain text, such as a name or a sentence.

  15. 15

    How is a string literal written in the examples?

    1. A

      With a colon after the text

    2. B

      With quotation marks around the text

    3. C

      With square brackets around the text

    4. D

      With a percent sign before the text

    Correct answer
    B

    With quotation marks around the text

    Explanation

    The examples use quotation marks to mark text as a string.

  16. 16

    What does the escape character \n do within a string?

    1. A

      Returns the number of characters

    2. B

      Converts the entire string to uppercase

    3. C

      Removes the next character

    4. D

      Inserts a new line

    Correct answer
    D

    Inserts a new line

    Explanation

    The course demonstrates \n as a way to insert a new line into a string.

  17. 17

    Why would a programmer use a backslash before a quotation mark inside a string?

    1. A

      To mark the end of a Python program

    2. B

      To indicate that the string should be sorted

    3. C

      To convert the string into a number

    4. D

      To include the quotation mark as text without ending the string

    Correct answer
    D

    To 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.

  18. 18

    What does string concatenation do in the examples?

    1. A

      Converts a string to a Boolean value

    2. B

      Counts the characters in a string

    3. C

      Appends one string to another

    4. D

      Finds a character's index in a string

    Correct answer
    C

    Appends one string to another

    Explanation

    Concatenation combines strings by appending one string to another, such as adding text to a phrase.

  19. 19

    What is the result of calling a string's lower method?

    1. A

      A version of the string with its letters in lowercase

    2. B

      A number giving the string's character count

    3. C

      The first character of the string

    4. D

      A version of the string with its letters reversed

    Correct answer
    A

    A version of the string with its letters in lowercase

    Explanation

    The lower method converts the string's letters to lowercase.

  20. 20

    What does the isupper method report?

    1. A

      Whether the string contains a number

    2. B

      Whether all the string's letters are uppercase

    3. C

      Whether the string is stored in a variable

    4. D

      Whether the string has more than one character

    Correct answer
    B

    Whether all the string's letters are uppercase

    Explanation

    The course describes isupper as returning a Boolean indicating whether the string is entirely uppercase.

  21. 21

    What does the len function return when given a string?

    1. A

      The number of characters in the string

    2. B

      The position of the first character

    3. C

      The number of words in every related file

    4. D

      The uppercase version of the string

    Correct answer
    A

    The number of characters in the string

    Explanation

    The len function returns the string's length, which is its number of characters.

  22. 22

    If a string uses zero-based indexing, what index identifies its first character?

    1. A

      One

    2. B

      The string's total length

    3. C

      Negative one

    4. D

      Zero

    Correct answer
    D

    Zero

    Explanation

    Python string indexing starts at zero, so the first character is at index zero.

  23. 23

    What does the string index method provide when the requested text is present?

    1. A

      The number of times every character appears

    2. B

      The position where that text begins

    3. C

      The length of the string after conversion to uppercase

    4. D

      A copy with the requested text removed

    Correct answer
    B

    The position where that text begins

    Explanation

    The index method returns the position where the specified character or text is found; the course notes it raises an error if the text is absent.

  24. 24

    What does a string's replace method do in the example?

    1. A

      Replaces specified text with other specified text

    2. B

      Returns the string's first indexed character

    3. C

      Adds a new line after every character

    4. D

      Reports whether the string is lowercase

    Correct answer
    A

    Replaces 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.

  25. 25

    Which statement about the basic data types introduced in the course is correct?

    1. A

      They include only strings and lists

    2. B

      They include strings, numbers, and Boolean true-or-false values

    3. C

      They include strings, dictionaries, and classes but not numbers

    4. D

      They include only whole numbers and file names

    Correct answer
    B

    They 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.

  26. 26

    How does Python treat a number literal such as 50 differently from the string "50"?

    1. A

      The quoted value is a Boolean, while the unquoted value is text

    2. B

      Both are always treated as the same numeric value

    3. C

      The number literal is numeric, while the quoted value is text

    4. D

      The unquoted value can only be used in a list

    Correct answer
    C

    The 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.

  27. 27

    Which operation does the % operator perform in the demonstrated arithmetic?

    1. A

      It raises one number to a power

    2. B

      It divides and discards the remainder in every case

    3. C

      It converts a number to a string

    4. D

      It gives the remainder after division

    Correct answer
    D

    It gives the remainder after division

    Explanation

    The course describes % as the modulus operator, which returns the remainder from division.

  28. 28

    How can parentheses affect the result of an arithmetic expression?

    1. A

      They can change which operations are performed first

    2. B

      They make the result a Boolean value

    3. C

      They convert all values in the expression to strings

    4. D

      They cause the expression to be skipped

    Correct answer
    A

    They can change which operations are performed first

    Explanation

    The examples use parentheses to control order of operations, such as adding before multiplying.

  29. 29

    What is the effect of converting a number with int?

    1. A

      It converts the value to a Boolean

    2. B

      It rounds every decimal upward

    3. C

      It converts the value to a string with quotation marks

    4. D

      It converts the value to an integer, which is a whole-number type

    Correct answer
    D

    It converts the value to an integer, which is a whole-number type

    Explanation

    The course describes int as converting a value to an integer, a whole-number type without a decimal part.

  30. 30

    Why use float rather than int for calculator inputs that may include decimal parts?

    1. A

      A float can represent decimal numbers

    2. B

      A float converts numbers into lists

    3. C

      A float can only represent whole numbers

    4. D

      A float preserves user input only as plain text

    Correct answer
    A

    A float can represent decimal numbers

    Explanation

    The course uses float so calculator inputs can include decimal numbers; int is for whole numbers.

  31. 31

    What does the abs function return for a negative number?

    1. A

      Its square root

    2. B

      Its value as a string

    3. C

      Its absolute value

    4. D

      Its remainder after division by two

    Correct answer
    C

    Its absolute value

    Explanation

    abs returns the absolute value; the example applies it to a negative number and gets its positive magnitude.

  32. 32

    What does the pow function do in the examples?

    1. A

      Converts a number into a string

    2. B

      Rounds a decimal to the nearest integer

    3. C

      Returns the smaller of two values

    4. D

      Raises a number to a specified power

    Correct answer
    D

    Raises a number to a specified power

    Explanation

    The example gives pow a number and a power, and it returns the number raised to that power.

  33. 33

    How do the max and min functions differ?

    1. A

      max counts values, while min sorts them

    2. B

      max returns the largest supplied value, while min returns the smallest

    3. C

      max rounds values, while min converts them to strings

    4. D

      max returns the smallest supplied value, while min returns the largest

    Correct answer
    B

    max returns the largest supplied value, while min returns the smallest

    Explanation

    The course demonstrates max as returning the larger supplied number and min as returning the smaller one.

  34. 34

    What behavior does the round function illustrate?

    1. A

      It always rounds a number upward

    2. B

      It rounds a number according to ordinary rounding rules

    3. C

      It always removes the decimal part without rounding

    4. D

      It returns the remainder from division

    Correct answer
    B

    It rounds a number according to ordinary rounding rules

    Explanation

    The examples show round rounding a decimal down or up depending on its value.

  35. 35

    Which pairing of math functions and behavior matches the course examples?

    1. A

      floor moves to the lower whole number, and ceil moves upward

    2. B

      floor returns the largest input, and ceil returns the smallest

    3. C

      floor converts to text, and ceil counts characters

    4. D

      floor finds a square root, and ceil finds an absolute value

    Correct answer
    A

    floor moves to the lower whole number, and ceil moves upward

    Explanation

    The course describes floor as taking the lower whole-number value and ceil as rounding upward.

  36. 36

    What does sqrt calculate in the example?

    1. A

      The square root of a number

    2. B

      The absolute value of a number

    3. C

      The square of a number

    4. D

      The remainder after dividing a number

    Correct answer
    A

    The square root of a number

    Explanation

    The example uses sqrt to calculate the square root of a number.

  37. 37

    Why does the course import from the math module before using some math functions?

    1. A

      Importing prevents functions from returning values

    2. B

      Importing converts all variables to numbers

    3. C

      Importing changes the Python version used by the project

    4. D

      Importing makes additional math functions available to the program

    Correct answer
    D

    Importing makes additional math functions available to the program

    Explanation

    The course explains that importing from math provides access to additional math functions.

  38. 38

    What type of value does input return by default, according to the calculator example?

    1. A

      A Boolean value

    2. B

      An integer in all cases

    3. C

      A string

    4. D

      A list of characters

    Correct answer
    C

    A string

    Explanation

    The calculator example explains that user input is treated as a string by default, even when the user types digits.

  39. 39

    Why did adding two user-entered values initially produce a joined-looking result instead of a sum?

    1. A

      The input values were strings, so + concatenated them

    2. B

      The console automatically sorted the entered values

    3. C

      The program used a list instead of a variable

    4. D

      The input values were Booleans, so + ignored them

    Correct answer
    A

    The input values were strings, so + concatenated them

    Explanation

    Because input returns strings by default, using + joined the text values rather than adding numeric values.

  40. 40

    In the calculator example, why are the user inputs converted to floats before arithmetic?

    1. A

      To prevent users from entering whole numbers

    2. B

      To ensure that they remain strings during addition

    3. C

      To convert the operator into a number

    4. D

      To make them numeric while still allowing decimal values

    Correct answer
    D

    To make them numeric while still allowing decimal values

    Explanation

    Converting inputs to floats allows arithmetic to work on numbers, including values with decimal parts.

  41. 41

    What does a Python list primarily provide in the examples?

    1. A

      A way to make stored values impossible to change

    2. B

      A command for reading a text file

    3. C

      A structure for organizing and storing multiple values

    4. D

      A special function for returning values from methods

    Correct answer
    C

    A structure for organizing and storing multiple values

    Explanation

    The course presents lists as structures for storing and organizing multiple related values.

  42. 42

    Which statement about the values in a Python list matches the course examples?

    1. A

      A list can hold only one value at a time

    2. B

      A list can hold values such as strings, numbers, and Booleans

    3. C

      A list can hold strings only

    4. D

      Every value in a list must have a different data type

    Correct answer
    B

    A 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.

  43. 43

    With zero-based indexing, what is the index of the third item in a list?

    1. A

      Two

    2. B

      Negative three

    3. C

      One

    4. D

      Three

    Correct answer
    A

    Two

    Explanation

    List indexes start at zero, so the first, second, and third items have indexes zero, one, and two.

  44. 44

    What does a negative list index allow a programmer to do?

    1. A

      Access only elements whose values are negative numbers

    2. B

      Skip all elements before the selected index

    3. C

      Change the list into a tuple

    4. D

      Access elements counted from the end of the list

    Correct answer
    D

    Access elements counted from the end of the list

    Explanation

    The examples use negative indexes to access elements from the back of a list; -1 refers to the last element.

  45. 45

    What is the purpose of a list slice such as friends[1:3] in the demonstrated convention?

    1. A

      To select only the item at index three

    2. B

      To reverse the list and select its first two items

    3. C

      To select every item from index zero through the end

    4. D

      To select items starting at index one and stopping before index three

    Correct answer
    D

    To 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.

  46. 46

    How can a programmer change an existing item in a list?

    1. A

      Convert the whole list to a string

    2. B

      Call clear and then print the old item

    3. C

      Assign a new value to the item at its index

    4. D

      Use a negative index as the item's new value

    Correct answer
    C

    Assign a new value to the item at its index

    Explanation

    The example modifies a list by assigning a new value to a selected index.

  47. 47

    What does the list method extend do in the example?

    1. A

      Removes the final item from the list

    2. B

      Sorts the list in descending order

    3. C

      Adds one item at a specified index

    4. D

      Adds the elements of another list to the end of the list

    Correct answer
    D

    Adds the elements of another list to the end of the list

    Explanation

    The course uses extend to add the elements of one list to another list.

  48. 48

    How does append differ from insert in the demonstrated list operations?

    1. A

      append sorts the list, while insert reverses it

    2. B

      append adds an item at the end, while insert adds it at a specified index

    3. C

      append removes an item, while insert copies a list

    4. D

      append clears the list, while insert checks for an item

    Correct answer
    B

    append adds an item at the end, while insert adds it at a specified index

    Explanation

    The examples show append adding to the end and insert taking an index at which to add the item.

  49. 49

    What is the effect of calling remove with a value in a list?

    1. A

      It removes every value from the list

    2. B

      It removes a matching value from the list

    3. C

      It removes the last value regardless of its contents

    4. D

      It returns the index without changing the list

    Correct answer
    B

    It removes a matching value from the list

    Explanation

    The course demonstrates remove deleting the specified value from the list.

  50. 50

    How do clear and pop differ in the examples?

    1. A

      Both methods add a copy of the list

    2. B

      Both methods sort the list in place

    3. C

      clear removes all items, while pop removes the last item

    4. D

      clear removes the last item, while pop removes all items

    Correct answer
    C

    clear removes all items, while pop removes the last item

    Explanation

    The course shows clear making the list empty and pop removing its final item.

  51. 51

    What information does the list method index provide?

    1. A

      A reversed copy of the list

    2. B

      The number of times a value appears

    3. C

      The index of a specified value when that value is present

    4. D

      The total number of values in every list

    Correct answer
    C

    The index of a specified value when that value is present

    Explanation

    The example uses index to find where a specified item occurs; it produces an error if the value is absent.

  52. 52

    What does the list method count return?

    1. A

      The index of the first item

    2. B

      The number of times a specified value occurs

    3. C

      The number of lists nested inside the list

    4. D

      The total length of the list after sorting

    Correct answer
    B

    The number of times a specified value occurs

    Explanation

    The course demonstrates count returning how many times a value appears in a list.

  53. 53

    What does calling sort do in the list examples?

    1. A

      Orders the list in ascending order

    2. B

      Deletes repeated items

    3. C

      Copies the list into a tuple

    4. D

      Reverses the current order without comparing values

    Correct answer
    A

    Orders the list in ascending order

    Explanation

    The course demonstrates sort putting values in ascending order, such as arranging text alphabetically.

  54. 54

    What does calling reverse do to a list?

    1. A

      Reverses the order of its elements

    2. B

      Sorts its elements in ascending order

    3. C

      Makes a separate copy and leaves the original untouched

    4. D

      Removes its first element

    Correct answer
    A

    Reverses the order of its elements

    Explanation

    The example uses reverse to turn the list's existing order around.

  55. 55

    Which statement correctly distinguishes a tuple from a list in the course?

    1. A

      A tuple must contain a single value, while a list must contain several

    2. B

      A tuple is indexed from one, while a list is indexed from zero

    3. C

      A tuple is immutable, while a list can be modified

    4. D

      A tuple can store only text, while a list can store numbers

    Correct answer
    C

    A 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.

  56. 56

    Why might coordinates be stored in a tuple?

    1. A

      They need to be cleared whenever a new coordinate is added

    2. B

      They represent data that is not intended to be changed after creation

    3. C

      They must be converted to strings before being stored

    4. D

      They need to be sorted automatically after every assignment

    Correct answer
    B

    They 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.

  57. 57

    How are key-value pairs organized in a Python dictionary?

    1. A

      Each pair is stored as an unchangeable sequence of characters

    2. B

      Each value identifies an associated list index

    3. C

      Each key must be a duplicate of another key

    4. D

      Each key identifies an associated value

    Correct answer
    D

    Each key identifies an associated value

    Explanation

    A dictionary stores key-value pairs; a key identifies the value associated with it.

  58. 58

    What requirement does the course give for dictionary keys?

    1. A

      Keys should be unique

    2. B

      Keys must be stored in alphabetical order

    3. C

      Keys must all be numbers

    4. D

      Keys must match their values exactly

    Correct answer
    A

    Keys should be unique

    Explanation

    The course warns that dictionary keys should be unique and demonstrates that duplicate keys are not appropriate.

  59. 59

    What is an advantage of using a dictionary's get method with a default value?

    1. A

      It sorts all keys before returning their values

    2. B

      It automatically changes a missing key into a list

    3. C

      It can return a chosen fallback when the requested key is absent

    4. D

      It prevents the dictionary from storing string keys

    Correct answer
    C

    It can return a chosen fallback when the requested key is absent

    Explanation

    The example shows that get can be given a default value to return when the requested key is not in the dictionary.

  60. 60

    What is a function in the course's description?

    1. A

      A container that can store only a single string

    2. B

      A special type of loop that always runs forever

    3. C

      A reusable collection of code that performs a specific task

    4. D

      A text file that Python cannot execute

    Correct answer
    C

    A 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.

  61. 61

    What does the def keyword indicate in the demonstrated syntax?

    1. A

      The start of a list definition

    2. B

      The end of a loop

    3. C

      The beginning of a function definition

    4. D

      The conversion of a value to a float

    Correct answer
    C

    The beginning of a function definition

    Explanation

    The course uses def to begin defining a function.

  62. 62

    Why is code inside a function indented in the demonstrated Python style?

    1. A

      Indentation marks the code as part of the function body

    2. B

      Indentation makes the function execute before it is called

    3. C

      Indentation converts its parameters into strings

    4. D

      Indentation changes the function into a list

    Correct answer
    A

    Indentation 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.

  63. 63

    What happens to a function's body when a program runs but the function is never called?

    1. A

      The body is automatically printed in the console

    2. B

      The body is not executed just because it was defined

    3. C

      The body runs once for every variable in the program

    4. D

      The function's parameters are assigned random values

    Correct answer
    B

    The 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.

  64. 64

    What is a parameter in the course's explanation of functions?

    1. A

      A special character that ends a string

    2. B

      A line of code that Python ignores

    3. C

      A named piece of information a function is set up to receive

    4. D

      A value that a function can return only after a loop

    Correct answer
    C

    A 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.

  65. 65

    What is the purpose of the return statement in a function?

    1. A

      To repeat the function until its parameter changes

    2. B

      To print the function's name in the console

    3. C

      To send a value back to the code that called the function

    4. D

      To add a new parameter to the function definition

    Correct answer
    C

    To send a value back to the code that called the function

    Explanation

    The course demonstrates return sending a computed value back to the caller, where it can be assigned to a variable.

  66. 66

    What happens to execution inside a function when Python reaches return?

    1. A

      The program automatically calls the function again

    2. B

      The function ends and control returns to its caller

    3. C

      The return value is discarded and the program always prints None

    4. D

      The function continues through every line below the return

    Correct answer
    B

    The function ends and control returns to its caller

    Explanation

    The course explains that return breaks out of the function, so statements after it are not reached during that call.

  67. 67

    What is the main role of an if statement?

    1. A

      To store a group of related values

    2. B

      To make every function return a value

    3. C

      To execute all program lines in reverse order

    4. D

      To run code conditionally based on whether a condition is true

    Correct answer
    D

    To run code conditionally based on whether a condition is true

    Explanation

    An if statement lets a program choose whether to execute code based on a true-or-false condition.

  68. 68

    When does an else block run in the examples?

    1. A

      Only when the program has no variables

    2. B

      Whenever the preceding if condition is true

    3. C

      When the preceding if condition is false and no earlier branch was taken

    4. D

      Before Python checks the if condition

    Correct answer
    C

    When the preceding if condition is false and no earlier branch was taken

    Explanation

    The course uses else for the alternative case when the associated if condition is not true.

  69. 69

    What does elif allow a program to do?

    1. A

      Return a value from the current function

    2. B

      Define a new class inside an if statement

    3. C

      Check another condition when a preceding condition was false

    4. D

      Repeat a true condition indefinitely

    Correct answer
    C

    Check another condition when a preceding condition was false

    Explanation

    The examples use elif to test additional conditions after earlier conditions were not met.

  70. 70

    In a condition using and, when is the combined condition true?

    1. A

      When either component condition is true

    2. B

      Only when both component conditions are false

    3. C

      When both component conditions are true

    4. D

      Whenever one component condition is omitted

    Correct answer
    C

    When both component conditions are true

    Explanation

    The course explains that and requires both conditions to be true.

  71. 71

    In a condition using or, when is the combined condition true?

    1. A

      Only when both component conditions are false

    2. B

      When at least one component condition is true

    3. C

      Only when both component conditions are true

    4. D

      Only when the conditions contain numbers

    Correct answer
    B

    When at least one component condition is true

    Explanation

    The course explains that or is true when one or both of its conditions are true.

  72. 72

    What effect does not have on a Boolean condition?

    1. A

      It adds the condition to a number

    2. B

      It compares the condition with a string

    3. C

      It makes the condition permanently true

    4. D

      It negates the condition's truth value

    Correct answer
    D

    It negates the condition's truth value

    Explanation

    The example uses not to reverse a condition: a true value becomes false and a false value becomes true.

  73. 73

    What does a comparison such as num_one >= num_two produce for use in an if statement?

    1. A

      A function definition

    2. B

      A true-or-false result

    3. C

      A string containing the comparison symbols

    4. D

      A new list containing both numbers

    Correct answer
    B

    A true-or-false result

    Explanation

    A comparison evaluates to a Boolean result, which can be used as an if condition.

  74. 74

    Which operator checks whether two values are equal in the comparisons described?

    1. A

      ==

    2. B

      !=

    3. C

      =

    4. 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.

  75. 75

    What does the comparison operator != mean?

    1. A

      The first value is assigned to the second

    2. B

      Both values must be true

    3. C

      The two values are not equal

    4. D

      The first value is greater than the second

    Correct answer
    C

    The two values are not equal

    Explanation

    The course describes != as the not-equal comparison operator.

  76. 76

    How can a function that finds the largest of three numbers make its decision?

    1. A

      Compare the numbers and return the one that is greater than or equal to the others

    2. B

      Sort a dictionary of the numbers and return its first key

    3. C

      Convert all three numbers to strings and return the longest one

    4. D

      Use a while loop without checking any values

    Correct answer
    A

    Compare 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.

  77. 77

    How does the more advanced calculator determine which arithmetic operation to perform?

    1. A

      It assumes every operator means addition

    2. B

      It uses a dictionary key to change the Python interpreter

    3. C

      It reads the second number as the operator

    4. D

      It compares the user's operator input with symbols and selects a corresponding branch

    Correct answer
    D

    It 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.

  78. 78

    What condition keeps a while loop running in the basic example?

    1. A

      Its loop condition remains true

    2. B

      Its variable has a string value

    3. C

      Its code has been written in a separate file

    4. D

      Its loop body contains a print statement

    Correct answer
    A

    Its loop condition remains true

    Explanation

    A while loop continues executing its body as long as its condition is true.

  79. 79

    When does Python check the condition of a while loop in the demonstrated process?

    1. A

      Once when the program first starts, never again

    2. B

      Only after the loop has finished

    3. C

      Before each pass through the loop body

    4. D

      Only when the loop body contains an if statement

    Correct answer
    C

    Before 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.

  80. 80

    Why does the counting while loop increment its counter?

    1. A

      To make the condition permanently true

    2. B

      To make the condition eventually false and end the loop

    3. C

      To sort the values printed by the loop

    4. D

      To convert the counter into a string

    Correct answer
    B

    To 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.

  81. 81

    How does the guessing game repeat prompts when a guess is incorrect?

    1. A

      A list index causes the prompt to appear again

    2. B

      A print statement restarts the whole program

    3. C

      A while loop continues while the guess differs from the secret word

    4. D

      A function definition automatically repeats its body

    Correct answer
    C

    A 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.

  82. 82

    In the limited guessing game, what does the guess counter track?

    1. A

      How many times the console has opened

    2. B

      How many letters are in the secret word

    3. C

      How many guesses the user has made

    4. D

      How many questions are in the program file

    Correct answer
    C

    How many guesses the user has made

    Explanation

    The game increments the guess counter each time it gives the user a guess.

  83. 83

    What does the out_of_guesses Boolean represent in the guessing game?

    1. A

      Whether the program can open a file

    2. B

      Whether the secret word is stored in a list

    3. C

      Whether the user has exhausted the permitted guesses

    4. D

      Whether the current guess is uppercase

    Correct answer
    C

    Whether the user has exhausted the permitted guesses

    Explanation

    The game uses this Boolean to distinguish having guesses left from having reached the guess limit.

  84. 84

    What does a for loop do when used with a string in the examples?

    1. A

      Iterates over the string's characters one at a time

    2. B

      Removes all spaces from the string

    3. C

      Returns the index of a requested substring

    4. D

      Converts the string into a number automatically

    Correct answer
    A

    Iterates over the string's characters one at a time

    Explanation

    The course demonstrates a for loop assigning each successive character of a string to its loop variable.

  85. 85

    What does a for loop over a list do in the demonstrated example?

    1. A

      Visits only the first and last elements

    2. B

      Changes every list element into an index

    3. C

      Visits each list element in turn

    4. D

      Removes each element after printing it

    Correct answer
    C

    Visits each list element in turn

    Explanation

    The example loops over the friends list and prints each friend in turn.

  86. 86

    What values are produced by range(3, 6) under the convention shown?

    1. A

      Zero, one, and two

    2. B

      Three, four, five, and six

    3. C

      Four, five, and six

    4. D

      Three, four, and five

    Correct answer
    D

    Three, four, and five

    Explanation

    The course explains that a range includes its starting value and stops before its ending value.

  87. 87

    How can a programmer use a list's length to loop over its indexes?

    1. A

      Use the list's final value as the start of the range

    2. B

      Use len(list) as the only index

    3. C

      Use a range from zero up to, but not including, len(list)

    4. D

      Use a range from one through len(list) inclusive

    Correct answer
    C

    Use 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.

  88. 88

    What makes the two-dimensional list example a grid-like structure?

    1. A

      It stores only one value in each program

    2. B

      Its values are all converted to strings

    3. C

      It uses a dictionary instead of lists

    4. D

      Its outer list contains other lists as elements

    Correct answer
    D

    Its outer list contains other lists as elements

    Explanation

    The example creates a list whose elements are themselves lists, forming rows and columns.

  89. 89

    How does the nested-loop example visit every value in a two-dimensional list?

    1. A

      The outer loop visits each inner list, and the inner loop visits each value in that list

    2. B

      The outer loop sorts the grid while the inner loop prints its length

    3. C

      Both loops visit only the first row

    4. D

      The inner loop selects one value, and the outer loop repeats only that value

    Correct answer
    A

    The 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.

  90. 90

    How does the exponent-function example use a for loop?

    1. A

      It multiplies the running result by the base once per iteration of the requested power

    2. B

      It prints each number in the range without calculating a result

    3. C

      It divides the base by the power on every iteration

    4. D

      It changes the power into a list of strings

    Correct answer
    A

    It 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.

  91. 91

    What is the purpose of the translator's loop over the phrase?

    1. A

      To examine each letter and build a translated string

    2. B

      To ask the user for a new phrase after every letter

    3. C

      To count how many files are in a directory

    4. D

      To sort the phrase's words alphabetically

    Correct answer
    A

    To 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.

  92. 92

    What do comments beginning with # do in a Python file?

    1. A

      They convert the following line into a function

    2. B

      They provide notes that Python ignores during execution

    3. C

      They print the comment text in the console

    4. D

      They force the next line to run twice

    Correct answer
    B

    They provide notes that Python ignores during execution

    Explanation

    The course describes # comments as notes for people; Python ignores them when running the program.

  93. 93

    What does it mean to comment out a line of code?

    1. A

      Convert its result into a comment string

    2. B

      Make Python execute it before every other line

    3. C

      Make Python ignore it without deleting it from the file

    4. D

      Move it automatically into another file

    Correct answer
    C

    Make 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.

  94. 94

    What is the course's recommended style for comments spanning multiple lines?

    1. A

      Put a single # only at the end of the final line

    2. B

      Use quotation marks around each individual word

    3. C

      Use a # on each comment line

    4. D

      Use a separate Python file for every line

    Correct answer
    C

    Use a # on each comment line

    Explanation

    The course says that although triple quotes can be used, the demonstrated recommended style is to use # on each line.

  95. 95

    What problem does a try/except block help a program handle?

    1. A

      A list that contains several values

    2. B

      A function that has a return statement

    3. C

      An error that would otherwise interrupt normal execution

    4. D

      A variable whose name contains an underscore

    Correct answer
    C

    An error that would otherwise interrupt normal execution

    Explanation

    The course uses try/except blocks to catch errors and handle them rather than allowing the program to stop unexpectedly.

  96. 96

    Why is a bare except block discouraged in the course example?

    1. A

      It prevents Python from running any code in the try block

    2. B

      It can catch a very broad range of errors, including ones the handler was not meant to address

    3. C

      It can be used only when reading files

    4. D

      It catches only errors caused by invalid user input

    Correct answer
    B

    It can catch a very broad range of errors, including ones the handler was not meant to address

    Explanation

    The course warns that a bare except is too broad because it can catch many unrelated errors.

  97. 97

    What is a benefit of catching a specific exception type?

    1. A

      The exception can no longer occur

    2. B

      The program can respond differently to different kinds of errors

    3. C

      The exception is automatically converted into a valid number

    4. D

      The program no longer needs any conditional statements

    Correct answer
    B

    The 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.

  98. 98

    What does an except ... as err clause allow the program to do?

    1. A

      Add the error to a list without assigning a name

    2. B

      Refer to the caught error through a variable

    3. C

      Convert the error into a function parameter

    4. D

      Automatically retry every line in the program

    Correct answer
    B

    Refer 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.

  99. 99

    What does opening a file with mode r indicate in the examples?

    1. A

      The program intends to append text to the file

    2. B

      The program intends to replace the file's contents

    3. C

      The program intends to rename the file

    4. D

      The program intends to read from the file

    Correct answer
    D

    The program intends to read from the file

    Explanation

    The course identifies r as read mode.

  100. 100

    What does opening a file with mode a allow in the examples?

    1. A

      Replacing the file's existing contents

    2. B

      Reading the file while preventing any changes

    3. C

      Appending new content to the end of the file

    4. D

      Deleting the file after it is read

    Correct answer
    C

    Appending new content to the end of the file

    Explanation

    The course identifies a as append mode, which adds content to the end of an existing file.

  101. 101

    What is the effect of opening a file with mode w in the demonstrated use?

    1. A

      It converts the file into a Python module

    2. B

      It writes content and can replace existing file contents

    3. C

      It adds content only to the end without affecting existing content

    4. D

      It makes the file readable but not writable

    Correct answer
    B

    It writes content and can replace existing file contents

    Explanation

    The example shows that w overwrites the existing file contents and can also be used to create a file.

  102. 102

    What does the r+ mode indicate in the course's list of file modes?

    1. A

      Write-only access that always creates a new name

    2. B

      Read and write access

    3. C

      Read-only access

    4. D

      Append-only access

    Correct answer
    B

    Read and write access

    Explanation

    The course describes r+ as a mode for reading and writing.

  103. 103

    Why should a program close a file after it is finished using it?

    1. A

      Closing it converts all its contents into a list

    2. B

      Closing it is part of good practice after opening and using the file

    3. C

      Closing it automatically saves every Python variable

    4. D

      Closing it permanently prevents later programs from reading it

    Correct answer
    B

    Closing 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.

  104. 104

    What does the file method read return in the demonstrated example?

    1. A

      The number of characters in the file name

    2. B

      The file's contents

    3. C

      The file's mode as a Boolean value

    4. D

      Only the file's first line

    Correct answer
    B

    The file's contents

    Explanation

    The example uses read to retrieve and print all of the file's contents.

  105. 105

    How does readline behave in the example?

    1. A

      It returns whether the file can be read

    2. B

      It appends a line to the file

    3. C

      It reads the entire file into a list

    4. D

      It reads one line at a time

    Correct answer
    D

    It reads one line at a time

    Explanation

    The course demonstrates readline reading a line and moving on so later calls can read subsequent lines.

  106. 106

    What does readlines do in the file-reading example?

    1. A

      Checks whether the file exists

    2. B

      Writes a new line to the end of the file

    3. C

      Returns the file's lines as a list

    4. D

      Returns only the first line as a string

    Correct answer
    C

    Returns the file's lines as a list

    Explanation

    The example explains that readlines places the file's lines into an array-like list.

  107. 107

    What does the readable method report?

    1. A

      Whether every line in the file is unique

    2. B

      Whether the file contains a Python class

    3. C

      Whether the file has already been closed

    4. D

      Whether the file can be read in its current mode

    Correct answer
    D

    Whether the file can be read in its current mode

    Explanation

    The course uses readable to get a Boolean indicating whether reading from the opened file is possible.

  108. 108

    Why might a program write \n before appended text?

    1. A

      To make the file read-only

    2. B

      To begin the appended content on a new line

    3. C

      To remove the preceding line

    4. D

      To convert the text into a variable

    Correct answer
    B

    To 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.

  109. 109

    What is a Python module, as described in the course?

    1. A

      A kind of console window that prints errors

    2. B

      A Python file that can be imported to reuse its code

    3. C

      A variable that stores only a file extension

    4. D

      A list whose elements cannot be changed

    Correct answer
    B

    A 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.

  110. 110

    What is one advantage of importing a module instead of copying its functions into another file?

    1. A

      The code can be reused without physically duplicating it

    2. B

      The importing file no longer needs to run

    3. C

      The imported code is guaranteed never to contain errors

    4. D

      The imported functions become text strings

    Correct answer
    A

    The 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.

  111. 111

    What does pip do in the course's discussion of external modules?

    1. A

      Installs and manages Python packages

    2. B

      Creates a class from a text file

    3. C

      Runs a Python file's print statements

    4. D

      Converts Python 2 code to Python 3 code

    Correct answer
    A

    Installs and manages Python packages

    Explanation

    The course describes pip as a package manager used to install, manage, update, and uninstall Python modules.

  112. 112

    Where does the course say many installed third-party packages are placed?

    1. A

      In a site-packages folder within the Python library area

    2. B

      In the computer's list of text-editor themes

    3. C

      In the console's output history

    4. D

      Inside every Python file that imports them

    Correct answer
    A

    In a site-packages folder within the Python library area

    Explanation

    The course shows third-party packages installed under the Python library area, in a site-packages folder.

  113. 113

    Which statement correctly describes the relationship between a class and an object?

    1. A

      A class defines a type or template, and an object is an instance of that class

    2. B

      A class is a function that can never create objects

    3. C

      An object is always a text file imported into a class

    4. D

      An object defines a type, and a class is one value stored by it

    Correct answer
    A

    A 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.

  114. 114

    Why might a programmer define a class for students rather than represent each student only with a string?

    1. A

      A class can group several relevant attributes into a student data type

    2. B

      A class allows Python to run without an interpreter

    3. C

      A class prevents student information from being stored in variables

    4. D

      A class automatically converts every student into a list

    Correct answer
    A

    A 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.

  115. 115

    What is the role of the __init__ method in the course's class examples?

    1. A

      It repeatedly runs the class's methods

    2. B

      It closes files opened by the class

    3. C

      It imports the class into every Python file

    4. D

      It initializes a newly created object with its attributes

    Correct answer
    D

    It 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.

  116. 116

    In an instance method, what does self refer to in the examples?

    1. A

      The first value printed by the console

    2. B

      The particular object whose attributes or methods are being used

    3. C

      The Python interpreter that runs the project

    4. D

      The class's source file on disk

    Correct answer
    B

    The particular object whose attributes or methods are being used

    Explanation

    The course uses self to refer to the current object, such as the particular student whose GPA is being checked.

  117. 117

    What is a class method or function used for in the student example?

    1. A

      Preventing objects from having attributes

    2. B

      Replacing the class with a dictionary

    3. C

      Providing behavior or information associated with objects of the class

    4. D

      Automatically creating a separate interpreter

    Correct answer
    C

    Providing behavior or information associated with objects of the class

    Explanation

    The on_honor_roll method uses an object's GPA to determine and return whether the student meets the stated condition.

  118. 118

    What does inheritance allow a class to do in the chef example?

    1. A

      Make its objects impossible to create

    2. B

      Reuse functionality defined in another class

    3. C

      Delete the parent class after importing it

    4. D

      Turn all of its methods into strings

    Correct answer
    B

    Reuse 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.

  119. 119

    In the inheritance example, why can a Chinese chef make chicken without defining make_chicken in its own class?

    1. A

      It inherits that method from the chef class

    2. B

      The make_special_dish method creates it

    3. C

      The method is stored in the console

    4. D

      Python automatically gives every class that method

    Correct answer
    A

    It inherits that method from the chef class

    Explanation

    The Chinese chef inherits the generic chef's make_chicken method.

  120. 120

    What does overriding a method allow a subclass to do in the chef example?

    1. A

      Make the inherited method run before every other method

    2. B

      Prevent the subclass from having any additional methods

    3. C

      Change every object of the parent class

    4. D

      Provide its own behavior for a method inherited from the parent class

    Correct answer
    D

    Provide its own behavior for a method inherited from the parent class

    Explanation

    The Chinese chef defines its own make_special_dish method so its special dish differs from the generic chef's.

  121. 121

    What is the Python interpreter environment described in the course useful for?

    1. A

      Replacing an IDE for writing every large program

    2. B

      Quickly trying Python commands and small pieces of code

    3. C

      Storing program output permanently in a text file

    4. D

      Editing the computer's operating system with graphical controls

    Correct answer
    B

    Quickly 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.

  122. 122

    How does the course distinguish a terminal or command prompt from a graphical interface?

    1. A

      It lets users interact only by moving windows and clicking buttons

    2. B

      It is a special kind of Python list

    3. C

      It displays only the results of print statements and accepts no commands

    4. D

      It lets users interact with the computer by entering text commands

    Correct answer
    D

    It 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.

Question 1 of 122
Question 1 of 122

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent