1. Home
  2. Explore
  3. Computer Science
  4. Python Strings, Input, and Decisions

Study methods

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

Python Strings, Input, and Decisions

  • 68Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Objects and Assignment
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is an object’s type in Python, and what does it determine?

Tap to flipClick to flipspace

An object is a value in memory, and its type tells Python what operations are allowed on it.

Or swipe the card

68 cards

  1. What is an object’s type in Python, and what does it determine?

    An object is a value in memory, and its type tells Python what operations are allowed on it.

  2. What does assigning an object to a variable do?

    A variable binds a name to an object in memory, so Python can retrieve that object when the name is used.

  3. What is an expression in Python?

    An expression combines objects or operations and evaluates to a value. Python can then use that value in a larger expression or assignment.

  4. How does Python evaluate an assignment such as radius = radius + 1?

    Python evaluates the right-hand side first, then binds its resulting value to the name on the left.

  5. When an assignment gives a variable a new value, what happens to its previous binding?

    The name is rebound to a new object; the original object is not changed by the assignment.

  6. Why is type(5 * 4) an expression, and what does it evaluate to?

    A function call such as type(5 * 4) is an expression: Python evaluates the argument, applies the function, and returns a value—in this case, the type int.

  7. What kinds of objects can a Python variable refer to?

    Variables can refer to objects other than numbers, including strings and even type objects such as int.

  8. What is a string, and what does it mean for strings to be case-sensitive?

    A string is a sequence of characters, and it is case-sensitive: uppercase and lowercase characters are distinct.

  9. What kinds of characters can a Python string contain?

    Strings can contain letters, digits, keyboard symbols, and whitespace characters such as spaces, tabs, and newlines.

  10. How do you indicate to Python that a value should be a string?

    Enclose the characters in quotation marks; the quotation marks tell Python to create a string object.

  11. What does + do when applied to two strings?

    The + operator concatenates strings by joining their characters into a new string.

  12. Does concatenating two strings automatically insert a space between them?

    No. Concatenation joins only the characters provided. Add a space explicitly, such as by concatenating a string containing a space.

  13. What does multiplication of a string by an integer do?

    Multiplying a string by an integer repeats its character sequence that many times. The string and integer can appear in either order.

  14. What type of multiplier is valid for repeating a string?

    String repetition requires an integer multiplier. A string multiplier, or a non-integer multiplier, causes a type error.

  15. What does len(s) return?

    len(s) evaluates to the number of characters in s.

  16. What is the difference between the string "3" and the result of int("3")?

    The input "3" is a string, not an integer. Converting it with int("3") produces the integer 3.

  17. What does it mean that strings are immutable?

    Python strings are immutable: their characters cannot be changed in place. To get a changed string, create a new string and, if desired, rebind a variable to it.

  18. How are string characters indexed in Python?

    Python counts string positions from zero. For s = "abc", a is at index 0, b at index 1, and c at index 2.

  19. How do you retrieve one character at a particular position in a string?

    Use square brackets with an index, as in s[0], to retrieve one character.

  20. What happens if you index beyond the end of a string?

    An index outside the string’s valid positions raises an IndexError.

  21. How do negative string indices work?

    Negative indices count from the right: s[-1] is the final character, s[-2] the character before it, and so on.

  22. How can you access the last character using the string’s length, and what happens if a negative index goes too far left?

    For a string of length nnn, the final character is at index n−1n - 1n−1, so s[len(s) - 1] accesses it. An index too far to the left also raises an IndexError.

  23. What do the three parts of Python’s string-slicing syntax represent?

    Use s[start:stop:step]: start is where the slice begins, stop is the boundary it does not include, and step controls how indices advance.

  24. Does a Python slice include its stop index?

    The stop index is excluded. For example, s[3:6] selects indices 3, 4, and 5.

  25. How does the slice step affect which characters are selected?

    A step of 1 takes every character; a step of 2 takes every other character. If the step is omitted, it defaults to 1.

  26. With a positive step, what direction does a slice move, and what do s[3:6] and s[3:6:2] return for "abcdefgh"?

    A positive step moves from left to right. For s = "abcdefgh", s[3:6] returns "def", while s[3:6:2] returns "df".

  27. How does a negative slice step work, and what does s[::-1] do?

    A negative step moves from right to left. For example, s[::-1] returns the string in reverse order.

  28. Why does the slice s[4:0:-1] not include the character at index 0?

    In s[4:0:-1], Python starts at index 4, moves left, and stops before index 0; therefore index 0 is not included.

  29. What does a slice return when its step direction cannot reach its stop index?

    A slice whose direction cannot reach its stop can be empty. For example, s[6:3] uses the default positive step, so it returns an empty string.

  30. How does entering an expression in the Python shell differ from putting it in a program file?

    In the shell, Python displays the value of an expression you enter. In a program file, evaluating an expression alone does not display it to the user.

  31. How do you explicitly show a value or expression result to the user from a Python program?

    Use print(...) to explicitly display the result of an expression to the user.

  32. What happens to the output line after a print call?

    Each print call ends by moving to a new line.

  33. How can you print several objects on one line with spaces between them?

    Separate objects with commas inside one print call. Python displays them on the same line with a space between them.

  34. What happens if you use + to concatenate a string and an integer?

    Concatenation with + requires strings; trying to concatenate a string and an integer raises a type error.

  35. How can you concatenate a string with an integer’s textual representation?

    Convert non-string objects to strings with str(...) before concatenating them with strings.

  36. What does input(prompt) do when a program runs?

    input(prompt) displays the prompt and waits for the user to type a response and press Enter.

  37. What type of value does input return?

    input returns the user’s response as a string, even when the response looks like a number.

  38. How can a program keep a user’s input for later use?

    Assign the result of input to a variable to keep and use the user’s response later in the program.

  39. If a user enters 3, what does repeating the input string five times produce?

    If the user enters 3, the input is the string "3"; repeating it five times produces "33333".

  40. How do you make numeric user input available for integer arithmetic?

    Convert the input to an integer, for example with int(input(...)). Then multiplying an input of 3 by 5 performs arithmetic and produces 15.

  41. How can a program use an input verb to print a sentence and then print the verb five times with spaces between repetitions?

    Store the input verb, print a line that combines it with the surrounding text, then print the verb repeated five times. Concatenate a space with the verb before repeating it if spaces are needed between repetitions.

  42. How do f-strings combine literal text with evaluated expressions?

    An f-string begins with f before its quotation mark. Text outside braces is printed literally; expressions inside {} are evaluated and inserted into the output.

  43. What makes f-strings convenient for displaying text mixed with values?

    F-strings can insert values directly into text, avoiding manual string concatenation and explicit conversion of inserted values to strings.

  44. How can an f-string avoid an unwanted space between a displayed number and a percent sign?

    In an f-string, literal spaces and punctuation are printed as written. For example, placing a percent sign directly after an expression’s braces puts it next to the inserted value without a comma-added space.

  45. Where can expressions be placed in Python code?

    An expression can appear in many contexts, including an index, a print call, or an f-string; Python evaluates it where it appears.

  46. What formula does the described Newton-method step use to update a guess for the cube root of xxx?

    For the cube-root approximation described, the next guess is the current guess minus the function value at that guess divided by the derivative there: g_{ ext{next}} = g - rac{g^3 - x}{3g^2}.

  47. What function and derivative are used in the cube-root Newton step?

    For finding a cube root, the function used is f(g)=g3−xf(g) = g^3 - xf(g)=g3−x, and its derivative with respect to ggg is 3g23g^23g2.

  48. What does the described cube-root program do with the user’s target and initial guess?

    The program asks for the target value and an initial guess, displays the current guess cubed, and calculates one next guess using the update formula.

  49. How does the described program obtain the derivative used in its Newton step?

    The derivative is supplied manually in the code; the program does not automatically differentiate the function.

  50. Does the described Newton-method program repeatedly refine its guess?

    It calculates only one next guess. Repeating the update requires additional programming constructs not used in that example.

  51. How does Python’s assignment operator differ from its equality-comparison operator?

    Assignment uses one equals sign (=) to bind a value to a variable. Equality comparison uses two (==) and evaluates to a Boolean.

  52. What values can a Boolean expression produce?

    A Boolean expression evaluates to either True or False; for example, 2 < 3 evaluates to True.

  53. Which comparison operators test equality, inequality, and ordering in Python?

    == tests equality; != tests inequality; <, <=, >, and >= test ordering relationships.

  54. How does case affect equality comparisons between strings?

    String comparisons are case-sensitive, so lowercase a and uppercase A are not equal.

  55. What does the Boolean operator not do?

    not reverses a Boolean value: not True is False, and not False is True.

  56. When does a Boolean expression using and evaluate to true?

    An and expression is true only if both component expressions are true.

  57. When does a Boolean expression using or evaluate to false?

    An or expression is false only if both component expressions are false; it is true if either or both are true.

  58. How do you write Python’s logical AND operator?

    Use the word and in Python for the logical operator; a symbol used for bitwise operations is not the same logical operator.

  59. What does an if statement do?

    An if checks a condition and runs its indented code block only when the condition is true.

  60. How does Python determine which statements belong to an if block?

    Python uses indentation to determine which statements belong to a conditional block. Statements at the original indentation level continue the main program afterward.

  61. When does an else block run?

    An else block runs when the preceding if condition is false.

  62. How does Python choose a block in an if/elif chain?

    elif checks another condition when earlier conditions in the same chain were false. The first true condition’s block runs; later alternatives are skipped.

  63. What happens when no condition in an if/elif chain is true, and how can you provide a catch-all case?

    If no condition in an if/elif chain is true and there is no else, none of those blocks runs. An else provides a catch-all block.

  64. How do multiple independent if statements differ from an if/elif chain?

    Separate if statements are evaluated independently, so more than one of their blocks can run. An if/elif chain runs at most one block.

  65. How does a nested conditional behave?

    Conditionals can be nested: an inner conditional is part of the outer block and is reached only when the outer condition is true.

  66. How can comparison operators classify a number guess as correct, too high, or too low?

    A decision can compare a user’s guess with a secret number: equality identifies a correct guess, a greater-than comparison identifies a high guess, and a less-than comparison identifies a low guess.

  67. How can a program ensure a correct guess is not mistakenly classified as too high?

    The if/elif/else chain is suitable: test for too high, then equality, and use the remaining case for too low.

  68. Why should you test a conditional program with several different inputs?

    Test the program with different inputs, including boundary and alternative cases, so logic errors are found before they affect later code.

Card 1 of 68
Card 1 of 68

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent