en:python:string_methods

STRING METHODS

  • string indexing ( sequence from either sides)
    • String orientation
      • ( positive axis )
      • negative axis
  • String Slicing ( word cutting in ratios or proportions)
  • String Concatenation ( String joining )
  • string printing
  • 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.

Enter your comment:
M H P᠎ Z S
 

This topic does not exist yet

You've followed a link to a topic that doesn't exist yet. If permissions allow, you may create it by clicking on Create this page.

  • en/python/string_methods.txt
  • 2024/08/23 05:30
  • brahmantra