Steemers Coding Class 03 - Learn Coding 10 Minutes a Day For Very BeginnerssteemCreated with Sketch.

in #howto8 years ago

Yo It's Pooria, What's up?

This is episode three of a series of articles for Steemrers to learn coding with only 5-10 minutes of practice a day. If it's the first time you see this topic follow from the beginning.

Previous episodes:

Steemers Coding Class - Episode 01
Steemers Coding Class - Episode 02

Coding

Iterations

Iteration or loop, is the act of repeating a process in order to accomplish something. Have you ever heard some poeple say computers are much faster than humans in repeatitive tasks? Iterations are one of the ways to give repeatitive tasks to computers and expect them to accomplish it very very fast.

Why iterations?

Let's say you want to print the sum of 1 to 10. We can do it like this:

sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
print(sum)

It works very well. Now what do you do if I ask you to write a program to calculate and print the sum of 1 to 10000?

As you can see it's not easy to do it the same way we did it on 1 to 10. We should write a lot of numbers on our code and after all it's not that smart to do so. Iterations are for situations like this.

For loop

A for loop is one of the loops we can use in python (and most of the other programming languages). It's like this:

for i in range(5):
    print(i)

Try the code above in yout Editor and see the result.

Awesome huh? As you can see the result is numbers from 0 to 4. Consider that i is just a variable which holds the current value of the loop. range() is helping us to list five numbers and repeat it inside our loop. So simply in the code above the print(i) is executing 5 times and each time the value of i is different. first time 0 then 1 till 5. Makes sense huh?

Question: range is 5 but why numbers finish at 4?
Answer: Because this is how range() works. It is to the number it gets, not including that number.

range()

With changing range() you can make your loop repeat as much as you want and you can also change how it starts and ends. for example we can give range a starting value:

for i in range(2, 7):
    print(i)

This loop iterate from 2 to 6.

We can also give range a third parameter like this: range (2, 7, 2) What do you think the third number is?

Think about what the third number could be in your mind and come up with an answer.

It's a step. Our loops were iterating only one number each time until they reach to the end. We can change that with step.

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

Try and see the result yourself. Change the parameters in range as much as you can until you understand it completely.

Try it yourself

Remember the first example that I asked you in the beginning of the article? Yes, write a program that calculate this:

1 + 2 + 3 + 4 + 5 + 6 + .... + 10000

It's fantastic for you to see how fast a computer can calculate the sum of 10000 numbers and you wrote that program! What are you waiting for? You have all the knowledge, write the program now!!

Don't forget to comment and share your experience with other learners (and me). Best Wishes, Pooria

Image Source: https://www.instagram.com/minimalsetups/

Sort:  

I used the following code to get the answer:
i = sum(range(1, 1001))
print(i)

It doesn't look like you need to use a loop to get a result.

Excellent. The function sum() is also running a loop under the hood to calculate that. But the goal of this article is not to learn how to sum numbers, it's to learn how to use iterations because they have a much wider use cases in coding. Thank you for contribution

Coin Marketplace

STEEM 0.28
TRX 0.11
JST 0.030
BTC 67895.92
ETH 3743.70
USDT 1.00
SBD 3.64