Python Iterate a list : Sample Problem

Python Iterations with while loop
Python Iterations with while loop

Iteration is a key concept and it represents to do a specific task repeatedly. Iterations can be executed based on certain conditions. We can implement iterations in multiple ways in Python i.e. using for loop, while loop etc. (discussed in separate sections).

In this post, I’ve used Python iteration concept using Python while loop over Python list. The post is structured as brief description of problem statement followed by a formula and then Python implementation.

Problem statement: Suppose you deposit $1,000 into a savings account and let it accumulate at 5% interest compounded annually. Write a function to determine how many years would you take to accumulate $10,000. Return number of years.

Compound Interest Formula:

Interest = Principal * (1 + ( Rate_Of_Interest/ 100))years

Where:

Principal = Amount initially

Rate_Of_Interest = Rate of Interest at an annual level.

years = Number of years, for which the compound interest needs to be calculated

Python Implementation

depost_amt = 1000
roi = 5
years = 0
final_amt = 0

while( final_amt <= 10000):
    years+= 1
    final_amt = depost_amt * pow((1+(roi/100)), years)    

print(final_amt, years)

#python #pythoniterations #pythonwhile #pythonexample #learnwithneeraj #learnpythonwithneeraj

Other Python problems:

Learn MongoDB with my following detailed video series

MongoDB Blogs

AWS learning resources over my YouTube channel.

⚠️ Disclaimer: The information provided on LearnWithNeeraj.com regarding Astrology, Numerology, and other topics is for educational and guidance purposes only.

Not Professional Advice: This content should not be used as a substitute for professional medical, legal, or financial advice. Always consult a certified professional for specific concerns.

Guest Authors: This site features articles by various contributors. The views and interpretations expressed are those of the individual authors and do not necessarily reflect the views of the website administrator.

Your destiny is in your hands. Use this information as a map, not a mandate.

Related Posts

Python – find most repeated element in a list

Problem statement: To find out the most repeated element element in a list using Python. Or in other words, to extract the most frequent element in the…