Python Strings, Input, and Decisions
- 68Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- 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?
An object is a value in memory, and its type tells Python what operations are allowed on it.
Or swipe the card
68 cards
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.
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.
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.
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.
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.
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 typeint.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.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.
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.
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.
What does
+do when applied to two strings?The
+operator concatenates strings by joining their characters into a new string.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.
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.
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.
What does
len(s)return?len(s)evaluates to the number of characters ins.What is the difference between the string
"3"and the result ofint("3")?The input
"3"is a string, not an integer. Converting it withint("3")produces the integer3.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.
How are string characters indexed in Python?
Python counts string positions from zero. For
s = "abc",ais at index0,bat index1, andcat index2.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.What happens if you index beyond the end of a string?
An index outside the string’s valid positions raises an
IndexError.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.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 n, the final character is at index n−1, so
s[len(s) - 1]accesses it. An index too far to the left also raises anIndexError.What do the three parts of Python’s string-slicing syntax represent?
Use
s[start:stop:step]:startis where the slice begins,stopis the boundary it does not include, andstepcontrols how indices advance.Does a Python slice include its stop index?
The stop index is excluded. For example,
s[3:6]selects indices3,4, and5.How does the slice step affect which characters are selected?
A step of
1takes every character; a step of2takes every other character. If the step is omitted, it defaults to1.With a positive step, what direction does a slice move, and what do
s[3:6]ands[3:6:2]return for"abcdefgh"?A positive step moves from left to right. For
s = "abcdefgh",s[3:6]returns"def", whiles[3:6:2]returns"df".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.Why does the slice
s[4:0:-1]not include the character at index0?In
s[4:0:-1], Python starts at index4, moves left, and stops before index0; therefore index0is not included.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.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.
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.What happens to the output line after a
printcall?Each
printcall ends by moving to a new line.How can you print several objects on one line with spaces between them?
Separate objects with commas inside one
printcall. Python displays them on the same line with a space between them.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.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.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.What type of value does
inputreturn?inputreturns the user’s response as a string, even when the response looks like a number.How can a program keep a user’s input for later use?
Assign the result of
inputto a variable to keep and use the user’s response later in the program.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".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 of3by5performs arithmetic and produces15.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.
How do f-strings combine literal text with evaluated expressions?
An f-string begins with
fbefore its quotation mark. Text outside braces is printed literally; expressions inside{}are evaluated and inserted into the output.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.
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.
Where can expressions be placed in Python code?
An expression can appear in many contexts, including an index, a
printcall, or an f-string; Python evaluates it where it appears.What formula does the described Newton-method step use to update a guess for the cube root of x?
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}.
What function and derivative are used in the cube-root Newton step?
For finding a cube root, the function used is f(g)=g3−x, and its derivative with respect to g is 3g2.
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.
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.
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.
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.What values can a Boolean expression produce?
A Boolean expression evaluates to either
TrueorFalse; for example,2 < 3evaluates toTrue.Which comparison operators test equality, inequality, and ordering in Python?
==tests equality;!=tests inequality;<,<=,>, and>=test ordering relationships.How does case affect equality comparisons between strings?
String comparisons are case-sensitive, so lowercase
aand uppercaseAare not equal.What does the Boolean operator
notdo?notreverses a Boolean value:not TrueisFalse, andnot FalseisTrue.When does a Boolean expression using
andevaluate to true?An
andexpression is true only if both component expressions are true.When does a Boolean expression using
orevaluate to false?An
orexpression is false only if both component expressions are false; it is true if either or both are true.How do you write Python’s logical AND operator?
Use the word
andin Python for the logical operator; a symbol used for bitwise operations is not the same logical operator.What does an
ifstatement do?An
ifchecks a condition and runs its indented code block only when the condition is true.How does Python determine which statements belong to an
ifblock?Python uses indentation to determine which statements belong to a conditional block. Statements at the original indentation level continue the main program afterward.
When does an
elseblock run?An
elseblock runs when the precedingifcondition is false.How does Python choose a block in an
if/elifchain?elifchecks another condition when earlier conditions in the same chain were false. The first true condition’s block runs; later alternatives are skipped.What happens when no condition in an
if/elifchain is true, and how can you provide a catch-all case?If no condition in an
if/elifchain is true and there is noelse, none of those blocks runs. Anelseprovides a catch-all block.How do multiple independent
ifstatements differ from anif/elifchain?Separate
ifstatements are evaluated independently, so more than one of their blocks can run. Anif/elifchain runs at most one block.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.
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.
How can a program ensure a correct guess is not mistakenly classified as too high?
The
if/elif/elsechain is suitable: test for too high, then equality, and use the remaining case for too low.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.

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