Python For Loops and Loop Control Statements

ComfortingBliss avatar
ComfortingBliss
·
·
Download

Start Quiz

Study Flashcards

7 Questions

What is the purpose of a for loop in Python?

To iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item.

What is the purpose of the break statement in a loop?

To exit the loop prematurely.

How do you iterate over the key-value pairs of a dictionary in Python?

Using the items() method, for example: for key, value in dict.items():

What is the purpose of a nested loop?

To iterate over multiple sequences or perform complex iterations.

What is the difference between the continue and break statements?

The continue statement skips the current iteration and moves to the next one, while the break statement exits the loop prematurely.

How do you iterate over a string in Python?

Using a for loop, for example: for char in string:

What is the purpose of the pass statement in a loop?

To do nothing, used as a placeholder when a statement is required syntactically.

Study Notes

For Loops

  • Used to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item
  • Syntax: for variable in iterable:
  • Example: fruits = ['apple', 'banana', 'cherry']; for fruit in fruits: print(fruit)

Loop Control Statements

  • break: exits the loop prematurely
    • Example: for i in range(5): if i == 3: break; print(i)
  • continue: skips the current iteration and moves to the next one
    • Example: for i in range(5): if i == 3: continue; print(i)
  • pass: does nothing, used as a placeholder when a statement is required syntactically
    • Example: for i in range(5): if i == 3: pass; print(i)

Nested Loops

  • A loop inside another loop
  • Used to iterate over multiple sequences or perform complex iterations
  • Example:
for i in range(3):
    for j in range(3):
        print(f"i: {i}, j: {j}")

Iterating Over Data Structures

  • Lists: for item in list:
  • Tuples: for item in tuple:
  • Strings: for char in string:
  • Dictionaries:
    • for key in dict: iterates over keys
    • for value in dict.values(): iterates over values
    • for key, value in dict.items(): iterates over key-value pairs
  • Sets: for item in set:

For Loops

  • Used to iterate over sequences like lists, tuples, or strings, executing a block of code for each item
  • Syntax is for variable in iterable:

Loop Control Statements

Break Statement

  • Exits the loop prematurely
  • Example: use break to stop a loop when a condition is met (e.g., for i in range(5): if i == 3: break; print(i))

Continue Statement

  • Skips the current iteration and moves to the next one
  • Example: use continue to skip an iteration when a condition is met (e.g., for i in range(5): if i == 3: continue; print(i))

Pass Statement

  • Does nothing and is used as a placeholder when a statement is required syntactically
  • Example: use pass when a statement is needed, but no action is required (e.g., for i in range(5): if i == 3: pass; print(i))

Nested Loops

  • A loop inside another loop
  • Used to iterate over multiple sequences or perform complex iterations
  • Example: iterating over multiple ranges (e.g., for i in range(3): for j in range(3): print(f"i: {i}, j: {j}"))

Iterating Over Data Structures

Lists

  • Iterate over list items using for item in list:

Tuples

  • Iterate over tuple items using for item in tuple:

Strings

  • Iterate over characters in a string using for char in string:

Dictionaries

  • Iterate over dictionary keys using for key in dict:
  • Iterate over dictionary values using for value in dict.values():
  • Iterate over key-value pairs using for key, value in dict.items():

Sets

  • Iterate over set items using for item in set:

Understand how to use Python for loops to iterate over sequences and loop control statements like break and continue.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Python Loop Iteration Quiz
6 questions

Python Loop Iteration Quiz

HeavenlyWildflowerMeadow avatar
HeavenlyWildflowerMeadow
Python For and While Loops
5 questions
Use Quizgecko on...
Browser
Browser