In this section, you will learn how to use iterative statements, or simply loops, to carry on something you have done rough and are often going to repeat. Additionally, you will learn more about string manipulation with slicing, indexing and formatting.
Learning Objectives
Understand concatenation, indexing, slicing and formatting techniques used in string manipulation.
Understand the syntax and use case of the range() function.
Explain the objective and working of iterative statements, as ‘for’ and ‘while’ loops.
Format strings as you like.
Index, slice and format strings.
Introduce string operations in both concepts.
Explain when to use the for loop versus while statement.
Analyze and understand nested for loop constructs.
Control loop execution using the range() function.
Use ‘for’ statements to iterate collections, integers, strings, or something else.
Enhance function capabilities by embedding loops within them.
Identify and fix errors commonly associated with loops.
Correctly initialize variables for loop-related actions.
Apply logical operators in a while loop.
Define and explain the meaning of loops and iterations.
PRACTICE QUIZ: TEST YOUR KNOWLEDGE: WHILE LOOPS
1. Fill in the blank: A while loop instructs your computer to continuously execute your code based on the value of a _____.
data type
condition (CORRECT)
function
comparator
Correct: In a while loop, you tell your computer to repetitively execute a certain block of code until some condition doesn’t evaluate to True. The loop continues iterating until that condition is met and the false condition finally meets.
2. A data professional wants to set up a while loop that will iterate as long as the variable x is less than 7. They assign the value 0 to the variable x. What code should they write next?
while x < 7: (CORRECT)
iterate x < 7:
repeat x < 7:
loop x < 7:
Correct: This particular code consists of the line description while x < 7:, which states a condition for the while loop that it should be executed until the variable x becomes 7. The code starts with the keyword while and ends with a colon (:), indicating the beginning of a specific block of code. The loop will keep iterating until the condition x < 7 returns False.
3. In Python, the keyword break lets you escape a loop without triggering any else statement that follows it in the loop.
True (CORRECT)
False
Correct: It instantly interrupts running from the loop by the break keyword in python. No matter what the condition of that loop is, break does not bother. If the loop has the else clause, breaking the loop prevents the else block from executing.
PRACTICE QUIZ: TEST YOUR KNOWLEDGE: FOR LOOPS
1. A data professional can use a for loop to perform which of the following tasks?
To define a function
To repeat a specific block of code until a condition is met
To convert one data type to another
To iterate over a series of numbers (CORRECT)
Correct: This means that data professional can iterate on any sequence of numbers or for any of other values using a for loop. The for statement in Python is a construct for traversing a sequence like numbers in a list or letters in a string, or items in any iterable object. The block of code is executed for each element in the sequence.
2. A data professional wants to set up a for loop. They write the following code: for x in range(3): . What values will the variable x take?
1, 2, and 3
0, 1, 2, and 3
Only 3
0, 1, and 2 (CORRECT)
Correct: When the variable x is included within the example for x in range(3):, the iterator happens to take on the values of 0, 1, and 2. The way range() works is that it starts a sequence of numbers from 0 (by default); it is incremented by 1 and it stops before the specified number (3 here). The statement for-, like other such code structures defining a new block, ends with a colon (:).
3. What parameter of Python’s range() function specifies the size of the increments in a sequence of numbers?
Stop value
Loop value
Step value (CORRECT)
Start value
Correct: The function range() in Python generates a sequence of numbers starting from zero by default and increments it by 1 unless otherwise specified and ends before reaching the given end number. It takes the parameters as: start value, stop value, and step value; the step value determines the increment interval between the numbers that are next on the sequence.
PRACTICE QUIZ: TEST YOUR KNOWLEDGE: STRINGS
1. In Python, what is the term for the portion of a string that can contain more than one character? Select all that apply.
String segment
Superstring
String slice (CORRECT)
Substring (CORRECT)
Correct: Slice of a string in Python refers to the portion of a string of more than a character – alternatively called a substring.
2. If you’re reading from left to right, what is the index of the first character in a string?
0 (CORRECT)
1
2
3
Correct: String indexing treats a string in Python as a sequence of characters, each assigned a numbered slot. Thus when reading from left to right, the very first character is at slot zero, the second character at slot one, the third at slot two, and so forth.
3. A data professional wants to insert specific substrings in a larger string. What method can they use to do so?
print()
type()
format() (CORRECT)
range()
Correct: It’s possible for a professional data analyst to use the format() method for inserting specific substrings into a longer string. This would make the required formatting of the string easy with the specified substrings in the required position of the placeholders within the longer string.
QUIZ: MODULE 3 CHALLENGE
1. What type of loop can a data professional use to repeat a specific block of code until a condition is no longer met?
for loop
else loop
if loop
while loop (CORRECT)
Correct!
2. Fill in the blank: The Python range() function returns a sequence of numbers starting from zero; then increments by _____, by default; then stops before the given number.
One (CORRECT)
two
zero
three
Correct!
3. What Python code instructs the computer to loop through values from 20 to 90?
for x in range(21, 91):
for x in range(20, 90):
for x in range(21, 90):
for x in range(20, 91): (CORRECT)
Correct!
4. A data professional wants to set up a for loop. They write the following code: for x in range(100, 501, 20): . What is the step value of the range() function?
501
20 (CORRECT)
500
100
Correct!
5. What Python code can a data professional use to concatenate the strings ‘jelly’ and ‘fish’?
‘jelly’ + ‘fish’ (CORRECT)
‘jelly’ > ‘fish’
‘jelly’ == ‘fish’
‘jelly’ < ‘fish’
Correct!
6. A data professional wants to identify the location of a character in a string. What Python method can they use to do so?
range()
print()
index() (CORRECT)
format()
Correct!
7. A data professional assigns the string ‘palm and pine’ to the variable trees. What Python code can they use to find the index of the character ‘m’?
palm.index(‘m’)
trees.index(‘m’) (CORRECT)
index.trees(‘m’)
index.palm(‘m’)
Correct!
8. A data professional assigns the string ‘classical’ to the variable genre. What Python code will return the slice ‘class’?
genre[5: ]
genre[-1]
genre[ :5] (CORRECT)
genre[1:5]
Correct!
9. Fill in the blank: A data professional can use the format() method to _____ specific substrings in a larger string.
evaluate
measure
insert (CORRECT)
print
Correct!
10. Fill in the blank: The Python _____ function returns a sequence of numbers starting from zero; then increments by one, by default; then stops before the given number.
range() (CORRECT)
index()
type()
format()
Correct!
11. What Python code instructs the computer to loop through values from 100 to 500?
for x in range(101, 500):
for x in range(100, 500):
for x in range(101, 501):
for x in range(100, 501): (CORRECT)
Correct!
12. A data professional wants to set up a for loop. They write the following code: for x in range(5, 101, 10): . What is the step value of the range() function?
101
5
100
10 (CORRECT)
Correct!
13. In Python, what method works by interpreting a string as a sequence of characters, where each character has a numbered slot?
range()
type()
index() (CORRECT)
format()
Correct!
14. A data professional assigns the string ‘pie and cake’ to the variable desserts. What Python code can they use to find the index of the character ‘p’?
index.desserts(‘p’)
index.pie(‘p’)
pie.index(‘p’)
desserts.index(‘p’) (CORRECT)
Correct!
15. A data professional assigns the string ‘penguin’ to the variable animal. What Python code will return the slice ‘pen’?
animal[ :3] (CORRECT)
animal[1:3]
animal[-1]
animal[3: ]
Correct!
16. Fill in the blank: In Python, a data professional can use the _____ method to insert specific substrings in a larger string.
format() (CORRECT)
print()
range()
type()
Correct!
17. Fill in the blank: A data professional can use a _____ to repeat a specific block of code until a condition is no longer met.
for loop
else loop
while loop (CORRECT)
if loop
Correct!
18. Fill in the blank: The Python range() function returns a sequence of numbers starting from _____; then increments by one, by default; then stops before the given number.
two
three
one
zero (CORRECT)
Correct!
19. What Python code can a data professional use to concatenate the strings ‘brain’ and ‘storm’?
‘brain’ % ‘storm’
‘brain’ + ‘storm’ (CORRECT)
‘brain’ != ‘storm’
‘brain’ == ‘storm’
Correct!
20. Fill in the blank: A data professional can use the format() method to insert specific _____ in a larger string.
libraries
while loops
substrings (CORRECT)
for loops
Correct!
21. A data professional can use a while loop to perform which of the following tasks?
To repeat a specific block of code until a condition is no longer met (CORRECT)
To define a function
To convert one data type to another
To iterate over a sequence of values
Correct!
22. What Python code instructs the computer to loop through values from 750 to 850?
for x in range(750, 851): (CORRECT)
for x in range(751, 851):
for x in range(750, 850):
for x in range(751, 850):
Correct!
23. A data professional assigns the string ‘spinach and asparagus’ to the variable vegetables. What code can they use to find the index of the character ‘c’?
index.vegetables(‘c’)
vegetables.index(‘c’) (CORRECT)
spinach.index(‘c’)
index.spinach(‘c’)
Correct!
24. A data professional assigns the string ‘football’ to the variable sport. What Python code will return the slice ‘ball’?
sport[ :4]
sport[4: ] (CORRECT)
sport[1:4]
sport[-1]
Correct!
25. Fill in the blank: In Python, the index() method interprets a string as a _____.
string slice
substring
sequence of characters (CORRECT)
boolean
Correct!
26. Fill in the blank: A loop is a block of code used to carry out _____.
evaluations
conversions
calculations
iterations (CORRECT)
Correct: A loop is a block of code used to carry out iterations. Data professionals use loops to automate repetitive tasks.
27. In Python, what type of loop iterates over a sequence of values?
Else loop
For loop (CORRECT)
If loop
While loop
Correct: In Python, a for loop is a piece of code that iterates over a sequence of values.
28. Python’s range() function includes which of the following parameters? Select all that apply.
Loop value
Stop value (CORRECT)
Step value (CORRECT)
Start value (CORRECT)
Correct: Python features the built-in range() function which consists of three parameters; they are start, stop, and step. By default, it returns a sequence of numbers starting from zero up to the specified stop value and increments it by one.
29. Fill in the blank: _____ strings makes a single longer string from two or more shorter ones.
Iterating
Branching
Concatenating (CORRECT)
Converting
Correct: Concatenating strings makes a single longer string from two or more shorter ones. To concatenate means to link or join together.
30. Python uses one-based indexing.
True
False (CORRECT)
Correct: Python uses zero-based indexing, which means that the first element of a sequence is indexed as zero. Now with strings, an indexing will treat the string as a sequence of characters, and each will be assigned a numbered slot starting from zero.