PYTHON : Python for beginners part 3

9 views32 pages
28 May 2023
Department
Course
Professor
#38 Python Tutorial for Beginners | Fibonacci
sequence
Understanding Fibonacci Series
Section Overview: In this section, the speaker explains what a Fibonacci
series is and how it can be identified.
## What is Fibonacci Series?
- A Fibonacci series is a sequence of numbers where each number is the
sum of the two preceding ones.
- The first two numbers in a Fibonacci series are 0 and 1.
- To get the next number in a Fibonacci series, you add up the two previous
numbers.
Writing Code to Print Fibonacci Series
Section Overview: In this section, the speaker demonstrates how to write
code to print out a specified number of terms in a Fibonacci series using
Python.
## Defining Function for Printing Fibonacci Series
- To print out a specified number of terms in a Fibonacci series using
Python, you need to define a function that accepts an argument specifying
how many terms should be printed.
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 32 pages and 3 million more documents.

Already have an account? Log in
- The function should start by printing out 0 and 1 (the first two numbers in
any Fibonacci series).
- After printing out these initial values, you can use a loop to calculate and
print out subsequent values in the sequence.
## Using For Loop to Print Subsequent Values
- To use a loop for printing subsequent values in a specified number of
terms for your fibonacci sequence, you can use Python's "for" loop
construct.
- You will need to specify both an iteration variable (to keep track of which
term you're currently calculating) and an upper bound on iterations (i.e.,
how many terms should be printed).
- Within the loop, you can calculate each subsequent term in the sequence
by adding together the two previous terms.
## Troubleshooting Common Errors
- One common error when writing code to print out a Fibonacci series is
forgetting to initialize your variables correctly.
- Another common error is not properly specifying the range of iterations
for your loop.
Fibonacci in Python
Section Overview: In this section, the speaker explains how to implement
the Fibonacci sequence in Python using variables and loops.
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 32 pages and 3 million more documents.

Already have an account? Log in
## Implementing Fibonacci Sequence
- Use a third variable C to add values of A and B.
- Assign C as A + B initially.
- Shift values by assigning A as B and B as C.
- Print the value of C for each iteration to get the Fibonacci sequence.
- Add an if condition to print only one value if the user inputs 1.
## Handling Negative Inputs
- Check if input is negative before processing.
- If input is negative, do not process and return an error message.
## Limiting Output Based on Input
- Modify code to ask for a maximum value instead of total number of values.
- Change output to print all values less than or equal to the maximum value
entered by user.
Modifying Fibonacci Code
Section Overview: In this section, the speaker explains how to modify the
previously implemented Fibonacci code to limit output based on a
maximum value entered by user.
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 32 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers

Related Documents

Related Questions