COURSE 2: GET STARTED WITH PYTHON

Module 3: Loops and Strings

GOOGLE ADVANCED DATA ANALYTICS PROFESSIONAL CERTIFICATE

Complete Coursera Study Guide

INTRODUCTION – Loops and Strings

Here, you’ll learn how to use iterative statements, or loops, to automate repetitive tasks. You’ll also learn how to manipulate strings using slicing, indexing, and formatting.

Learning Objectives

  • Describe how to manipulate strings using techniques such as concatenating, indexing, slicing, and formatting
  • Summarize the syntax of the range() function
  • Explain the purpose and logic of iterative statements such as for loops and while loops
  • Format strings
  • Manipulate strings using indexing, slicing, and formatting
  • Introduce strings
  • Know when to use for and when to use while loops
  • Analyze nested for loops
  • Use the range() function to control for loops
  • Utilize for loops to iterate over sets of data, integers, strings and more
  • Use loops within functions
  • Identify common errors in loops
  • Initialize variables
  • Implement logical operators in while loops
  • Define loops
  • Define iteration

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: A while loop instructs your computer to continuously execute your code based on the value of a condition. The loop will keep iterating as long as the condition remains true.

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: The code while x < 7: sets a condition for a while loop stating that the variable x must be less than 7. The code begins with the distinguishing keyword while. And, like functions and other expressions that start a distinct code block, it ends with a colon. The while loop will iterate until the condition is 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: In Python, the keyword break lets you escape a loop without triggering any else statement that follows it in the loop.

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: A data professional can use a for loop to iterate over a series of numbers. In Python, a for loop is a piece of code that iterates over a sequence of values, such as numbers in a list or characters in a string.

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: In the example for x in range(3):, the variable x will take the values 0, 1, and 2. The range() function returns a sequence of numbers starting from zero; then increments by one, by default; then stops before the given number. The code begins with the distinguishing keyword for. And, like functions and other expressions that start a distinct code block, it 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: Python’s range() function returns a sequence of numbers starting from zero; then increments by one, by default; then stops before the given number. The function includes the following parameters: start value, stop value, and step value. Step value specifies the size of the increments in a sequence of numbers. 

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: In Python, a string slice is the portion of a string that can contain more than one character. A string slice is also known as 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: With strings, indexing works by interpreting a string as a sequence of characters, where each character has a numbered slot. If you’re reading from left to right, the first character is located at slot zero. The second character is located at slot one. The third at slot two. And so on.

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: A data professional can use the format() method to insert specific substrings in a larger string. The format() method formats and inserts specific substrings into designated places within a larger string.

Liking our content? Then, don’t forget to ad us to your bookmarks so you can find us easily!

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’s range() function includes the following parameters: start value, stop value, and step value. The range() function returns a sequence of numbers starting from zero; then increments by one, by default; then stops before the given number.

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. This means that the first element of a sequence is indexed as zero. With strings, indexing works by interpreting a string as a sequence of characters, where each character has a numbered slot.