Differences
This shows you the differences between two versions of the page.
en:python:string_methods [2024/08/22 17:13] – created brahmantra | en:python:string_methods [2024/08/23 05:30] () – brahmantra | ||
---|---|---|---|
9: | 9: | ||
* string printing | * string printing | ||
* String length | * String length | ||
+ | |||
+ | |||
+ | paper and cup story | ||
+ | |||
+ | a, b = 0, 1 | ||
+ | while a < 10: | ||
+ | print(a) | ||
+ | a, b = b, a+b | ||
+ | |||
+ | |||
+ | Algorithm Breakdown: | ||
+ | |||
+ | Initialization: | ||
+ | |||
+ | The variables a and b are initialized to 0 and 1, respectively. These values will serve as the starting point for the Fibonacci sequence. | ||
+ | Loop Condition: | ||
+ | |||
+ | The while loop checks if the value of a is less than 10. As long as this condition is true, the loop continues to execute. | ||
+ | Printing: | ||
+ | |||
+ | Inside the loop, the value of a is printed to the console. This displays each number in the Fibonacci sequence that is less than 10. | ||
+ | Updating Values: | ||
+ | |||
+ | The values of a and b are updated using tuple assignment: | ||
+ | * a is assigned the current value of b. | ||
+ | * b is assigned the sum of the old values of a and b. | ||
+ | |||
+ | Fibonacci Sequence Generation: | ||
+ | |||
+ | This code effectively generates the Fibonacci sequence, which is a series of numbers where each number is the sum of the two preceding ones. Here's how it works: | ||
+ | |||
+ | First Iteration: | ||
+ | a is 0, and b is 1. | ||
+ | a is printed (0). | ||
+ | a becomes 1, and b becomes 0 + 1 = 1. | ||
+ | |||
+ | Second Iteration: | ||
+ | a is 1, and b is 1. | ||
+ | a is printed (1). | ||
+ | a becomes 1, and b becomes 1 + 1 = 2. | ||
+ | |||
+ | Third Iteration: | ||
+ | a is 1, and b is 2. | ||
+ | a is printed (1). | ||
+ | a becomes 2, and b becomes 1 + 2 = 3. | ||
+ | And so on, until a reaches 10. | ||
+ | |||
+ | Output: | ||
+ | |||
+ | The code will output the following numbers on the console: | ||
+ | |||
+ | 0 | ||
+ | 1 | ||
+ | 1 | ||
+ | 2 | ||
+ | 3 | ||
+ | 5 | ||
+ | 8 | ||
+ | These are the Fibonacci numbers less than 10. | ||
+ | |||
+ | |||
+ |