Tuesday, March 12, 2024

 

For Loop

A while loop is perfect to use when we don't know how many times we want the loop to repeat.

If we have an idea of how many times we want the loop to repeat, we can use a for loop to loop code in exactly the same way the while loop did.

However, we can set up the variable, control condition, and increment all in ONE line of code.


Let's compare:-

Here is how we created a counter with a while loop:

counter = 0
while counter < 10:
  print(counter)
  counter += 1  

And here is the same counter using a for loop:

for counter in range(10):
  print(counter)

Range

range(10)

Note: The variable is only there during the loop, not after it is completed.

Coding Challenge 
Building a loan calculator where inputs are total amount of loan , for how much years and the interest rate will be the input. output will be the total amount to be paid back after the tenures.

print("Loan Calculator")
print("")
total_loan =0
loan_amount=int(input("How much money u want to take as a loan:"))
APR_percentage=int(input("tell me the APR percentage:"))
total_years = int(input("How many years u need to pay the loan:"))
total_loan_yoy = loan_amount
for total_years in range(total_years):
  total_loan_yoy= total_loan_yoy + (total_loan_yoy*APR_percentage)/100
print("year",total_years+1,"is",round(total_loan_yoy,2))


What can range really do?

Give range one number, and it will count up to that number. However, you can actually give the range function a few options...
starting value: what number do you want to start with?
ending value: the number after the number you want to end with (example: if you type 10 as the ending value, the computer will count until 9)
increment: How much should it increase by every time it loops? (example: Do you want to count by 1s, 5s, 10s?)


The ending value has an unsaid 'less than'. Meaning the computer will stop one number before the ending number that is written in the range.

Let's try it out

The first number in this range, 100, is the starting value. The second number in this range, 110, is the ending value (Remember to always put the ending number as one more than where you want to end up).

for i in range(100, 110):
  print(i)

What do you expect to print with the range below?

for i in range(1, 7):
  print("Day", i)

Did you notice that the counter stopped at 'Day 6'? Change the ending value to be one more than the last number you want shown---in this case, 8 because we want to display 7 days of the week

for i in range(1, 8):
  print("Day", i)

Code - Thirteen Times Table

print("Thirteen Times Table")
for i in range(1, 13):
  print(i, "x 13 =", i * 13)

Increments
We know that this range will start at 0 and continue to 999,999 (which is the number right before the ending value written). The number will increase in increments of 25 each time.

for i in range (0, 1000000, 25):
print(i) 

Counting Backward  

In this example, we are starting at 10 and counting backward to 0 (because 0 is what comes right before the ending value listed), and counting backward 1 each time.

for i in range(10, -1, -1):
print(i)


No comments:

Post a Comment

  2D Dictionaries Remember that dictionaries are very similar to lists, except that they store data as key:value pairs. The value is what it...