Ch 4. Flashcards

1
Q

How do you write a loop?

A

letters = [ “a”, “b”, “c”,]
for new_variable in letters:
print(new_variable)

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

How do you use indentation in a loop?

A

When you indent, then that line of code will be inside the for loop and run through all the items in the list. If you do not intend, once it is done running it will then proceed to the line of code that was not indented and run it once.

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

Common types of errors?

A
  1. “IndentationError: expected an indented block”
    2: Logical error (when your code produces no python error but is not your expected result)
  2. “IndentationError: unexpected indent”
  3. Forgetting the colon
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you easily generate a series of numbers?

A

range()

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

What is something to remember about range()?

A

It is “off by one”

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

How do you covert a series of numbers into a list?

A

list(range())

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

How do you specify the increment in a given range?

A

range(start, stop, increment)

Note: increment must be an integer

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

How do you find the min, max, and sum of a list of numbers?

A

min(list_name_), max(list_name), sum(list_name)

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

What does a list comprehension do?

A

It combines the for loop and the creation of new elements into one line, and automatically appends each new element.

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

How do you work with a specific group of items in a list?

A

list_name[index start:index end:increment]

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

What do you do if you want to loop through a subset of the elements in a list?

A

for value in list_name[start:end:increment]

print(value)

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

How do you copy a list?

A

new_list = og_list[:]

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

How do you create a list of items that cannot change?

A

Tuple

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

Difference between a list and a tuple?

A

list uses square brackets and is mutable, tuple uses parantheses and is immutable

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

How do you write a tuple?

A

letters = (“a”, “b”, “c”)

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

How many spaces per indentation level?

A

4

17
Q

Can you mix tabs with spaces?

A

No

18
Q

Line length should be?

A

79 characters

19
Q

Comment line length should be?

A

72 characters