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