Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

5.1 While loop

1.
b. The loop runs six times for values of g = 1 , 2 , 3 , 5 , 8 , and 13 .
2.
c. 21 is the first Fibonacci number larger than 20, and for g = 21, the condition will evaluate to False for the first time.
3.
a. The code first prints the variable f's initial value. Then, at each iteration, g's value is printed, and g's value throughout the code's execution will be 1 , 2 , 3 , 5 , 8 , and 13 . Thus, all printed values are 1 , 1 , 2 , 3 , 5 , 8 , and 13 .
4.
b. The loop runs for n = 4, 3 , 2 , 1 .
5.
b. The loop's termination condition is n = 0; therefore, the value of n after the loop equals 0 . The last printed line will be: value of n after the loop is 0.
6.
c. Since the initialization value for n equals 4 and the value of n is only increasing, the loop condition will always remain true. The loop will never terminate.

5.2 For loop

1.
b. Eight characters exist in the str_var. Iterating over all characters and incrementing the value of count variable at each iteration results in the count's value of 8 .
2.
a. Since the initial count's value is 0 , multiplying count by any number results in the output of 0 .
3.
c. The added line prints each character that is looped on (character c), and prints an additional * after the character.
4.
a. The output will be an increasing sequence between 10 and 22 (inclusive of both): 10 , 13 , 16 , 19 , and 22 .
5.
c. The expression starts a sequence from 5 , decreases by 1 at every step, and ends at 1 .
6.
a. The range() function generates a descending sequence, beginning from -1 until -2. Hence, the output is -1.
7.
c. When the step size is negative, the generated sequence will be in descending order. Hence, start must be larger than end; otherwise, no sequence will be generated.
8.
c. When two arguments are provided to the range() function, arguments are considered as start and end of the sequence, and the step size will be 1 . When step size is positive, the generated sequence will be in ascending order, and hence, start must be less than end. Otherwise, no sequence will be generated.

5.3 Nested loops

1.
b. The combination of i and j values, (i, j), that meet the inner and outer loop conditions are as follows:
(1, 1), (1, 2), (1, 3), (1, 4),
(2, 1), (2, 2), (2, 3),
(3, 1), (3, 2),
(4, 1)
2.
b. Each inner loop's full execution prints three "*" characters, and the outer loop executes twice.
3.
b. Each line i of the output (i = 1 , 2 , 3 , and 4 ) contains i times values 1 through 4 .
Hence, the outer loop executes while i <= 4, and the inner loop iterates overj while j's value is less than or equal to 4 .
4.
a. The outer loop's execution range is 0 , 1 , and 2 ; hence, the outer loop executes three times.
5.
b. The inner loop is evaluated four times (j = 0, 1 , 2 , and 3 ) for every i's value set by the outer loop. In the given example i = 0, 1 , and 2 ; hence, the total number of times the inner loop executes is 3 * 4 = 12.
6.
c. The printed output displays three rows with each row i displaying i * values in the range of 0 to 3 . Hence, the code must iterate over row ID in the outer loop, and in each row, iterate over elements' ID in the range of 0 to 3 .

5.4 Break and continue

1.
c. The code prints all characters before the first occurrence of a space character in separate lines. Hence the output is:
H
e
l
l
o
2.
b. As soon as the print statement is executed, the break statement causes the loop's termination. Also, for i = 15, the condition (i%3 == 0 and i%5 == 0) is met, and hence the print statement will be executed exactly once.
3.
b. The loop finds the first five numbers that are divisible by 2 or 3 . These numbers are 2 , 3 , 4 , 6 , and 8 , and thus 8 is the final i's value.
4.
b. The print statement is executed for numbers within the range of 0 and 10 (inclusive) that are divisible by 3. The printed output is:
9
6
3
0
5.
b. The loop checks whether the character being evaluated is a space character or not. If so, the remainder of the loop will be skipped and the space character will not be printed.

5.5 Loop else

1.
b. The variable i is initialized to 16 , and the while loop executes with i = 16, 8 , 4 , and 2 . In each loop's iteration, the variable exp's value is incremented by 1. Hence, at the end of the loop, exp = 4. Since the loop terminates without interruption, the else statement after the loop is executed and prints 16 is 2 to the 4.
2.
a. The initial i's value is 7 . Hence, in the first loop's iteration, the else statement within the loop executes, and the break statement terminates the loop. Since the loop's execution is interrupted, the else after the loop will not execute and there will be no output.
3.
b. The loop iterates over all integer values in the list. If a value is less than 5 , the value will be printed, and if the value is greater than 5 , the code prints "Not all numbers are less than 5." and the code's execution terminates.
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.