Python Basics III For and While Loops Flashcards

1
Q

What is the syntax of a for loop?

A

Create a list, cycle over the list using for notation.

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

In a For loop, the action does not have to be a function of the list content. Give an example.

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

In contrast to a For Loop, a While Loop does not iterate over a list; it uses a logical condition to control its process. Give an example.

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

You can create a sequence of consecutive integers using the range function. You can use that sequence as the iterator for your for loop. Give an example of that.

A

Notice the range interval is half-open, it includes the first number but not the last one.

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

range(stop): This function generates a sequence from 0 up to, but not including, the stop value. To get a list object (not a sequence), apply the list function. Evaluating the range will just return itself.

A

See it here:

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

If you are happy with starting at 0, it is the default for range, and you can leave it off. Take a look.

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

What is “list comprehension”?

A

It is a shorthand notation for programmatically populating a list using a for a loop.

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

How do you define a function

A

Like this (it includes a default parameter)

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

Can you index a tuple?

A

Yes

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

How would you convert a string into a list of words?

A

Using the split method.

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