Chapter 7 & 8 Strings and Iterations Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How do you divide strings into a separate element when there is a space?

A

variable.split(‘ ‘) will divide the string from the spaces and put the results in a list.
Example: b.split(‘ ‘)
Result: [‘string’, ‘string’, ‘string’, ‘string’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

If I want to find where a certain item in a string is located at, how would I accomplish this?

A

By using the variable.find() function.
Example: b.find(‘a’)
Result: 2
Meaning it’s in the 2nd position of the string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you evaluate a string expression?

A

By using the eval() function.
Example: variable = ‘2+4+5’
eval(variable)
Result: 11

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What function can you use to verify an integer is in the argument?

A

By using the isinstance function.
def factorial(n):
if not isinstance(n, int):
print(‘Factorial is only defined for integers.’)
return None

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is it called when the new variable depends on the old variable? example: a = a + 1

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define Initialize

A

Assigning a value to a new variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is it called when you update a variable by adding 1?

A

An Increment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Define the flow of execution for a While loop.

A
  1. Determine whether the condition is true or false.
  2. If false, exit the while statement and continue execution at the next statement.
  3. If the condition is true, run the body and then go back to step 1.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Define an infinite loop.

A

A loop that goes on forever since the values never change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Most rational numbers, like 1/3, and irrational numbers, like ✔2, can’t be represented exactly with a…..

A

Float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

I want to find out the absolute value of an integer, float, how could I accomplish that?

A

By using the abs function.
»>abs(1/3)
0.33333
»>abs(2.3)
2.3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Define algorithm

A

A mechanical process for solving a category of problems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does “debugging by bisection” mean?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

An update that increases the value of a variable (often by one)

A

Increment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

An update that decreases the value of a variable.

A

Decrement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Repeated execution of a set of statements using either a recursive function call
or a loop

A

Iteration

17
Q

An assignment that gives an initial value to a variable that will be updated

A

Initialization

18
Q

The “ “ indicates which character in the sequence you want.

A

Index

19
Q

A built-in function that returns the number of characters in a string.

A

Len

20
Q

If I want to choose the last letter of a string, how would I do this?

A

By indexing a negative number
»> fruit = ‘banana’
»> print(fruit[-1])
a

21
Q

Start at the beginning, select each character in turn, do something to it, and continue to the end. What pattern of process is this?

A

Traversal

22
Q

A part of a string specified by a range of indices is called what?

A

Slice

23
Q

Strings are considered …..

A

immutable

24
Q

Traversing a sequence and returning when finding what you are looking for is called what?

A

Search

25
Q

The method “ “ takes a string and returns a new string with all uppercase letters.

A

upper
variable = variable.upper()
This will return a new string with all uppercase

26
Q

A statement that calls a method is called what?

A

Invocation

27
Q

The word “ “ is a boolean operator that takes two strings and returns True if the first appears as a substring in the second:

A

in
»>’a’ in ‘banana’
True

28
Q

Something a variable can refer to. For now, you can use “…..” and “value” interchangeably.

A

Object

29
Q

An ordered collection of values where each value is identified by an integer index.

A

Sequence

30
Q

One of the values in a sequence

A

Item

31
Q

A string with no characters and length 0, represented by two quotation marks:

A

empty string

32
Q

A variable used to count something, usually initialized to zero and then incremented:

A

counter

33
Q

A function or method argument that is not required is called what?

A

optional argument