Python For Loops and Range Function
26 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What will the following loop output? for x in range(3): print(x)

  • 0, 1, 2 (correct)
  • 1, 2, 3
  • 0, 1, 2, 3
  • 0, 1, 2, 3, 4
  • What is the output of for x in range(1, 10, 2): print(x)?

  • 1, 3, 5, 7, 9 (correct)
  • 0, 2, 4, 6, 8
  • 2, 4, 6, 8
  • 1, 2, 3, 4, 5
  • Given the list a = [1, 2, 3, 4, 5], what does for i in range(len(a)): print(i, a[i]) output?

  • 1 1, 2 2, 3 3, 4 4, 5 5
  • 1 1, 2 2, 3 3, 4 4
  • 0 1, 1 2, 2 3, 3 4
  • 0 1, 1 2, 2 3, 3 4, 4 5 (correct)
  • What does range(2, 20, 4) produce when printed in a loop?

    <p>2, 6, 10, 14, 18</p> Signup and view all the answers

    What is the output if we run a = ['John', 'Doe', 'Jane', 'Smith']; for i in range(4): print(i, a[i])?

    <p>0 John, 1 Doe, 2 Jane, 3 Smith</p> Signup and view all the answers

    What is the output of the provided while loop meant to collect even numbers?

    <p>[2, 4, 6, 8, 10]</p> Signup and view all the answers

    In the while x < 11: loop, which condition is used to append numbers to the list?

    <p>x % 2 == 1</p> Signup and view all the answers

    What will be the output of the following code snippet? for x in []: print(x)

    <p>No output</p> Signup and view all the answers

    What is the purpose of the second while x 10: loop in the provided code?

    <p>It does not perform any meaningful operation due to a syntax error.</p> Signup and view all the answers

    What does the range() function return?

    <p>Both B and C</p> Signup and view all the answers

    In the second last code snippet, what condition correctly identifies a prime number?

    <p>if n % x != 0</p> Signup and view all the answers

    Which of the following correctly describes the for loop checking for prime numbers?

    <p>It uses the correct method to identify prime numbers.</p> Signup and view all the answers

    Given the code primes = [2, 3, 5, 7, 9, 11] and for x in primes: print(x, end=' '), what will be the output?

    <p>2 3 5 7 9 11</p> Signup and view all the answers

    What is the default starting point of the range() function?

    <p>0</p> Signup and view all the answers

    In the string loop for x in 'banana': print(x), how many times will 'a' be printed?

    <p>2</p> Signup and view all the answers

    What will the following code output? words = ['cat','window','defenestrate']; for w in words: print(w,len(w))

    <p>cat 3 window 6 defenestrate 12</p> Signup and view all the answers

    What happens when the range() function is called with two arguments, such as range(1, 5)?

    <p>It creates a sequence from 1 to 4</p> Signup and view all the answers

    Which of the following is true regarding a for loop with an empty iterable?

    <p>It will not execute the block of code and exit gracefully.</p> Signup and view all the answers

    What output will the command 'list(range(0,10,3))' generate?

    <p>[0, 3, 6, 9]</p> Signup and view all the answers

    How many times will the while loop 'while i < 3: print(i); i+=1' print a number if 'i' starts at 1?

    <p>2</p> Signup and view all the answers

    What will happen when you run the following code: 'while x > 3:' without initializing 'x'?

    <p>An error will occur due to 'x' not being defined.</p> Signup and view all the answers

    In what scenario would the 'continue' statement be used within a loop?

    <p>To skip the current iteration and continue with the next.</p> Signup and view all the answers

    What is the purpose of the 'else' clause in a while loop?

    <p>To run once after the loop when the condition is false.</p> Signup and view all the answers

    What will the while loop 'while i < 3: print(i); i+=1' print when initialized with i = 3?

    <p>Nothing will be printed.</p> Signup and view all the answers

    What is the result of the operation 'sum(range(4))'?

    <p>6</p> Signup and view all the answers

    What will the 'break' statement accomplish in a while loop?

    <p>It exits the loop immediately.</p> Signup and view all the answers

    Study Notes

    For Loops

    • For loops iterate over collections of items, executing a block of code for each item.
    • The general syntax:
      for item in iterable:
          block of code 
      
    • The for loop requests the next member of the iterable. If empty, it exits without running the body. Otherwise, the member is assigned (or redefined) to item.
    • Then, the enclosed body of code is executed. This process repeats until the iterable is exhausted.
    • Example:
      primes = [2,3,5,7,9,11]
      for x in primes:
          print(x, end =' ')
      
      This loop prints each number in the primes list.

    The range() Function

    • The range() function generates a sequence of numbers as a range object.
    • Key features:
      • Starts from 0 by default.
      • Increments by 1 by default.
      • Ends at a specified number.
    • Syntax:
      range(stop)
      range(start, stop[, step])
      

    While Loops

    • While loops repeat a block of code until a specific condition is no longer true.
    • Syntax:
      while condition: 
          block of code 
      
    • The while loop checks the condition. If True, the block of code is executed.
    • After execution, the loop goes back to step one, checking the condition again.
    • The loop continues until the condition becomes False.

    Loop Control Statements

    • break: Exits the innermost enclosing loop, immediately terminating its execution.
    • continue: Stops the current iteration of the loop. The loop skips the rest of the code in the body and moves to the next iteration.
    • else: Executes once the condition of the loop is no longer true, but only if the loop was not exited by a break statement.

    Prime Number Check

    • Demonstrates using nested loops to check for prime numbers within a specified range.
    • The outer loop iterates through numbers within the range.
    • The inner loop checks if the current number is divisible by any number from 2 to the current number minus 1.
    • If the inner loop finds a divisor, the number is not prime and the outer loop continues.
    • If the inner loop doesn't find a divisor, the number is prime and is printed.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    INFO5002-OCT10(10).pdf

    Description

    This quiz covers the key concepts of for loops and the range function in Python programming. It outlines the syntax, workings of for loops, and how to utilize the range function for generating sequences. Test your understanding and coding skills related to these essential programming constructs.

    More Like This

    Use Quizgecko on...
    Browser
    Browser