Chapter 7 & 8 Strings and Iterations Flashcards
How do you divide strings into a separate element when there is a space?
variable.split(‘ ‘) will divide the string from the spaces and put the results in a list.
Example: b.split(‘ ‘)
Result: [‘string’, ‘string’, ‘string’, ‘string’]
If I want to find where a certain item in a string is located at, how would I accomplish this?
By using the variable.find() function.
Example: b.find(‘a’)
Result: 2
Meaning it’s in the 2nd position of the string
How would you evaluate a string expression?
By using the eval() function.
Example: variable = ‘2+4+5’
eval(variable)
Result: 11
What function can you use to verify an integer is in the argument?
By using the isinstance function.
def factorial(n):
if not isinstance(n, int):
print(‘Factorial is only defined for integers.’)
return None
What is it called when the new variable depends on the old variable? example: a = a + 1
An Update. If you were trying to update a variable that doesn’t already exist, you will get an error. Python evaluates the right side before it assigns a value to a.
Define Initialize
Assigning a value to a new variable
What is it called when you update a variable by adding 1?
An Increment
Define the flow of execution for a While loop.
- Determine whether the condition is true or false.
- If false, exit the while statement and continue execution at the next statement.
- If the condition is true, run the body and then go back to step 1.
Define an infinite loop.
A loop that goes on forever since the values never change.
Most rational numbers, like 1/3, and irrational numbers, like ✔2, can’t be represented exactly with a…..
Float
I want to find out the absolute value of an integer, float, how could I accomplish that?
By using the abs function.
»>abs(1/3)
0.33333
»>abs(2.3)
2.3
Define algorithm
A mechanical process for solving a category of problems.
What does “debugging by bisection” mean?
The bigger the function, the more time it takes to debug line by line. Instead, break the problem in half and look for an intermediate value you can check with the print statement. If the mid-point check is incorrect, there must be a problem in the first half of the program. If it is correct, the problem is in the second half.
An update that increases the value of a variable (often by one)
Increment
An update that decreases the value of a variable.
Decrement
Repeated execution of a set of statements using either a recursive function call
or a loop
Iteration
An assignment that gives an initial value to a variable that will be updated
Initialization
The “ “ indicates which character in the sequence you want.
Index
A built-in function that returns the number of characters in a string.
Len
If I want to choose the last letter of a string, how would I do this?
By indexing a negative number
»> fruit = ‘banana’
»> print(fruit[-1])
a
Start at the beginning, select each character in turn, do something to it, and continue to the end. What pattern of process is this?
Traversal
A part of a string specified by a range of indices is called what?
Slice
Strings are considered …..
immutable
Traversing a sequence and returning when finding what you are looking for is called what?
Search
The method “ “ takes a string and returns a new string with all uppercase letters.
upper
variable = variable.upper()
This will return a new string with all uppercase
A statement that calls a method is called what?
Invocation
The word “ “ is a boolean operator that takes two strings and returns True if the first appears as a substring in the second:
in
»>’a’ in ‘banana’
True
Something a variable can refer to. For now, you can use “…..” and “value” interchangeably.
Object
An ordered collection of values where each value is identified by an integer index.
Sequence
One of the values in a sequence
Item
A string with no characters and length 0, represented by two quotation marks:
empty string
A variable used to count something, usually initialized to zero and then incremented:
counter
A function or method argument that is not required is called what?
optional argument