Repeat a block for as long as a condition holds โ colon and indentation instead of braces, and the multiplication table exercise that ties it together.
This topic covers the while loop in Python โ one of Python's two main looping constructs (the other being for). The while loop repeats a block of code as long as a given condition remains True. The notes also touch on how while looks in C-style languages vs. Python, and briefly mention do-while (which Python does not have natively).
while loop keeps executing its body while the condition evaluates to True.{} for blocks โ it uses a colon : and indentation instead.The instructor compares the general/C-style while syntax with Python's syntax to highlight the difference:
C-style (curly braces):
while(condition){
code
}
Python-style (colon + indentation):
while condition:
code
๐ก Tip: In Python, indentation is not just for readability โ it defines the block of code that belongs to the loop. Incorrect indentation will cause errors or unexpected behavior.
A related comment block also lists # while, # do while, and # for โ indicating these are the loop types being discussed/compared in the video.
do-while loop (unlike C/C++/Java). This was likely mentioned only as a comparison point, not something implemented in Python. โ ๏ธ Screenshot text unclear โ the video doesn't show a Python do-while example, only the label in a comment.A while loop generally needs 3 parts:
i = 1)i < 11)False (e.g., i += 1)If you forget the update step, the condition never becomes False โ infinite loop.
number = int(input("Enter the number"))
i = 1
while i < 11:
print(number * i)
i += 1
Explanation of the code:
int(input("Enter the number")) โ takes user input (as a string) and converts it to an integer, storing it in number.i = 1 โ initializes the counter used to track which multiple we're on.while i < 11: โ loop runs 10 times (while i is 1 through 10).print(number * i) โ prints the product of number and the current counter i.i += 1 โ increments i by 1 each iteration so the loop eventually stops.Example run (input = 11):
Enter the number11
11
22
33
44
55
66
77
88
99
110
Important Points:
i < 11 combined with starting i at 1 gives exactly 10 iterations (1 through 10).number = int(input("Enter the number"))
i = 1
while i < 11:
print(number, "*", i, "=", number * i)
i += 1
Explanation of the code:
print().print(number, "*", i, "=", number * i) โ Python's print() function automatically adds spaces between comma-separated arguments, which is why the output looks like a clean equation.Example run (input = 24):
Enter the number24
24 * 1 = 24
24 * 2 = 48
24 * 3 = 72
24 * 4 = 96
24 * 5 = 120
24 * 6 = 144
24 * 7 = 168
24 * 8 = 192
24 * 9 = 216
24 * 10 = 240
Important Points:
print() is a simple way to format readable output without needing f-strings or .format().while loop syntax: while condition: followed by an indented block (no parentheses or braces required, though parentheses around the condition are optional/allowed).while i < 11 with i starting at 1 โ loop runs for i = 1 to i = 10 (10 times total).print() with comma-separated arguments auto-inserts spaces โ handy for quick formatted output.do-while loop (this only appeared as a labeled comparison point, not an actual implementation).int(input(...)) is the standard pattern to read a number from the user in Python.A while loop in Python repeats a block while a condition is True, using a colon and indentation instead of curly braces (unlike C-style languages). Every while loop needs an initialized variable, a condition, and an update step inside the loop โ skip the update and you get an infinite loop. The example used throughout is a multiplication table generator: take a number as input, loop i from 1 to 10, and print either number * i or the full equation number * i = result. This is a classic beginner exercise for practicing loop initialization, condition-checking, and incrementing.