Python Basics III For and While Loops Flashcards
What is the syntax of a for loop?
Create a list, cycle over the list using for notation.
In a For loop, the action does not have to be a function of the list content. Give an example.
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.
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.
Notice the range interval is half-open, it includes the first number but not the last one.
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.
See it here:
If you are happy with starting at 0, it is the default for range, and you can leave it off. Take a look.
What is “list comprehension”?
It is a shorthand notation for programmatically populating a list using a for a loop.
How do you define a function
Like this (it includes a default parameter)
Can you index a tuple?
Yes
How would you convert a string into a list of words?
Using the split method.