chapter_four Flashcards

1
Q

How do I loop through a list of names?

A

for

When you want to do the same action with every item in a list, you can use Python “for” loop.
Doing this action individually could cause several problems. For one, it would be repetitive to do this with a long list of names.
Also, we’d have to change our code each time the list’s lenght changed. A “for” loop avoids both of these issues by letting Python manage the issues internally.

example:

numbers = [  'one', 'two', 'three' ]
for number in numbers:
    print( number )
output: one
output: two
output: three

For every number in the list numbers, print the written number.

for x in list:
-do this-

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

What should I keep in mind in relation to “for” loops?

A

When you’re using loops for the first time, keep in mind that the set of steps is repeated once for each item in the list, no matter how many items are in the list.

Also keep in mind when writing your own “for” loops that you can choose any name you want for the temporary variable that will be associated with each value in the list. However, it’s helpful to choose a meaningful name that represents a single item from the list.

example:
for cat in cats:
for dog in dogs:
for item in list_of_items:

These naming conventions can help you follow the action being done on each item within a for loop.
Using singular and plural names can help you identify whether a section of code is working with a single element from the list or the entire list.

Every indented line following the “for item in list_of_items:” is considered inside the loop, and each indented line is executed once for each value in the list.

You can use as many lines as you like in your “for” loops. In practice you’ll find it useful to do a number of different operations with each item in a list when you use a “for” loop

The colon at the end of a “for” statement tells Python to interpret the next line as the start of a loop.
If you accidentally forget a colon, you’ll get a syntax error because Python doesn’t know what you’re trying to do.

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

What happens after a “for” loop has finished executing?

A

Any lines of code after the “for” loop that are not indented are executed once without repetition.

When you’re processing data using a “for” loop, you’ll find that this is a good way to summarize an operation that was performed on an entire data set.

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

What is an Indentation Error?

A

Python uses indentation to determin how a line, or group of lines, is related to the rest of the program.

If there are no indented lines after a statement that requires at least one, Python will show an indentation error.

If you accidentally indent a line that doesn’t need to be indented, Python informs you about the unexpected indent

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

What is a logical error?

A

The syntax is valid Python code, but the code does not produce the desired result because a problem occurs in its logic.

If you expect to see a certain action repeated once for each item in a list and it’s executed only once, determine wether you need to simply indent a line or group of lines.

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

How do I generate a series of numbers?

A

range( )

Python’s “range( )” function makes it easy to generate a series of numbers.

The “range( )” function causes Python to start counting at the first value you give it, and it stops when it reaches the second value you provide. Because it strops at that second value, the output never contains the end value.

This is another result of the off-by-one behavior you’ll see often in programming languages.

If your output is different than what you expect when you’re using “range( )”, try adjusting your end value by 1.

You can also pass “range( )” only one argument, and it will start the sequence of numbers at 0.
For example, range( 6 ) would return the numbers from 0 through 5.

example:

for value in range( 1, 4 ):
    print( value )
output: 1
output: 2
output: 3

We can also use the “range( )” function to tell Python to skip numbers in a given range.
If you pass a third argument to “range( )”, Python uses that value as a step size when generating numbers.

example:
for value in range( 2, 11, 2 ):
    print( value )
output: 2
output: 4
output: 6
output: 8
output: 10
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do I make a list of numbers in combination with the range( ) function?

A

list( )

If you want a list of numbers, you can convert the results of “range( )” directly into a list using the “list( )” function.
When you wrap “list( )” around a call to the “range( )” function, the output will be a list of numbers.

example:
numbers = list( range( 1, 6 ) )
print( numbers )
output: [1, 2, 3, 4, 5]

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

Which functions can be used to show simple statistics with lists of numbers?

A

Minimum: min( )
Maximum: max( )
Summe: sum( )

example:
list = [1, 2, 3]
min( list )
output: 1

max( list )
output: 3

sum( list )
output: 6

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

What are comprehensions ? Show an example.

A

A comprehension allows you to generate the same list in just one line of code instead of 3 to 4.

Standard example:
squares = [ ]
for value in range( 1, 11 ):
squares.append( value ** 2 )

Comprehension example:
squares = [ value ** 2 for value in range( 1, 11 )]

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

What is a “slice” of a list?

How do I “slice” a list?

A

To make a slice, you specify the index of the first and last element you want to work with.
As with the “range( )” function, Python stops one item before the second index you specify.

example:
numbers = [ ‘one’, ‘two’, ‘three’, ‘four’, ‘five’ ]
print( numbers[ 1:3 ] )
output: [ ‘two’, ‘three’ ]

If you omit the first index in a slice, Python automatically starts your slice at the beginning of the list.

print( numbers[ :4 ]
output: [ ‘one’, ‘two’, ‘three’, ‘four’ ]

A similar syntax works if you want a slice that includes the end of a list.
This syntax allows you to output all of the elements from any point in your list to the end regardless of the lenght of the list.

print( numbers[ 2: ]
output: [ ‘three’, ‘four’ ]

Recall that a negative index returns an element a certain distance from the end of the list

print( numbers[ -2: }
output: [ ‘three’, ‘four’ ]

You can include a third value in the brackets indicating a slice. If a third value is in included, this tells Python how many times to skip between items in the specified range.

You can use “slices” in a “for” loop in place of the “range” function.

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

How do I copy a list and in which situations is it useful?

A

To copy a list, you make a slice that includes the entire original list by omitting the first index and the second index( [ : ] ).
This tells Python to make a slice that the first item and ends with the last item, producing a copy of the entire list.

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

What are lists that cannot change?

How do you create them?

A

Python refers to values that cannot change as immutable, and an immutable list is called tuple.

A tuple looks just like a list except you use parentheses instead of square brackets.
Once you define a tuple, you can access individual elements by using each item’s index, just as you would for a list.

example:
nice = ( 420, 69 )
print( nice[ 0 ] )
output: 420

When we try to assign a new value to one of the tuples elements, we get a TypeError, because it can’t be done to this type of object.
This is beneficial because we want Python to raise an error when a line of code tries to change any element of the tuple

Tuples are technically defined by the presence of a comma; the parentheses make them look neater and more readable.
If you want to define a tuple with one element, you need to include a trailing comma.

my_t = ( 3, )

It doesn’t often make sense to build a tuple with one element, but this can happen when tuples are generated automatically.

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

How do I overwrite a tuple?

A

Although you can’t modify a tuple, you can assign a new value to a variable that represents a tuple.

nice = ( 420, 69 )

nice = ( 69, ‘sixtynine’ )

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

What is a PEP?

Whats one of the oldest PEPs and what are its contents?

A

When someone wants to make a change to the Python language, they write a “Python Enhancement Proposal” (PEP).
One of the oldest PEPs is “PEP 8”, which instructs Python programmers on how to style their code.

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

1) Indentation:
PEP 8 recommends that you use four spaces per indentation level. Using four spaces improves readability while leaving room for multiple levels of indentation on each line. Mixing tabs and spaces in your file can cause problems that are very difficult to diagnose.

2) Line Lenght
Many Python programmers recommend that each line should be less than 80 characters. Historically, this guidline developed because most computers fit only 79 characters on a single line in a terminal window. Professional programmers often have several files open on the same screen, and using the standard line length allows them to see entire lines in two or three files that are open side by side onscreen.

3) Limit Comments
PEP 8 also recommends that you limit all of your comments to 72 characters per line, because some of the tools that generate automaticdocumentation for larger projects add formating characters at the beginning of each commented line.

4) Blank Lines
To group parts of your program visually, use blank lines. You should use blank lines to organize your files, but don’t do so excessively. You should not place three or four blank lines between two sections.
If you have five lines of your code that build a list, and then another three lines that do something with that list, it’s appropriate to place a blank line between the two sections.

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