Chapter-2: For Loop (Python)
For learn how to download and install python link here: How to download and install python in window
For learn how to run and execute the code in python link here:How to write and run the code
In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.
Example-1: Print name multiple time
Code:for i in range(10):
print('Hello')
Result:
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
- In the 1st line we write 10 after range that means code run for 10 times and 2nd line not directly start by touch left margin but slightly away from left margin.
- 2nd line actually under the for loop so whatever you write in 2nd line in print command it print 10 times.
- In 1st line we use colon (:) in last this colon is necessary otherwise it show error
Example-2: Syntax Error
Code:
for i in range(10):
print('Hello')
Result:
Error
Explanation: It occur because you write print('Hello') just below the 1st line and in touch with left margin.
Now a question arise in your mind?
Question:- As we study in previous blog that whatever we write in print command in between quotes it appear in result then in this example why 'Hello' not appear in result instead it show error?
Answer:- In this example when you use colon (:) then python think this code is half and its further code given under it. But when you write something just in touch with left margin then python not able to find further command of for loop and it show error.
Example-3: Square the number
Code:
for i in range (3):
num=eval(input('Enter a number:'))
print('The square of your number is', num*num)
print('The loop is now done')
Result:
Enter a number: 5
The square of your number is 25
Enter a number:10
The square of your number is 100
Enter a number: 15
The square of your number is 225
The loop is now done.
Explanation:
- In the first line you write 3 in the range() so it means it run for three times.
- In 2nd line we ask input from the user and eval function use for calculation purpose.
- In 3 line use print('The square of number is', num*num) so ,The square of number directly print because it in between quotes and then further it do calculation by multiply given number two times (num*num)
- In 4th line we use print('The loop is now done')
- Both 2nd and 3rd line under the loop, so when code start then it first read 1st line then it goes to 2nd line and ask input then reach to third line and give the result, but it not reach to 4th line and show the loop is now done, because we run loop 3 times so again it start from 1st line repeat this process three times. After complete all the 3 loop now code reach to 4th line and in result show the loop is now done.
- Anything we write in loop now repeat itself three times.
- After the loop complete now it reach to 4th line in which we write The loop is now done so now it print this command.
Example-4:- loop that repeat multiple time
Code:
print('A')
print('B')
for i in range(5):
print('C')
print('D')
print('E')
Result:
A
B
C
D
C
D
C
D
C
D
C
D
E
Explanation:
- In the 1st line we use print('A') so A directly appear in result.
- In 2nd line we use print('B') so it directly appear in result.
- In 3rd line we use for loop and we give range 5 so now loop repeat for 5 times.
- In 4th line we use print('C') and then print('D') under the for loop so, C and D now appear 5 times in result. C and D appear in result alternatively means first C appear then followed by D then C then D like that because as I describe above code start above to below.
- In 5th line we use print('E') so it directly print in result.
Example-5:- Understand the loop how it work
Code:
print('A')
print('B')
for i in range(5):
print('C')
for i in range(5):
print('D')
print('E')
Result:
A
B
C
C
C
C
C
D
D
D
D
D
E
Explanation:
- If you compare it with previous example then you see in previous example C and D are alternate because in the 3rd line we use loop and under this loop first loop is done for C in 4th line and in the 5th line loop for D.
- But in this example we separate the loop for C and D so, first C appear 5 times and then D appear 5 times. Because python first give result of the code which write before and then it goes to next line.
Example-6:- To print the counting
Code:
for i in range(10):
print(i)
Result:
0
1
2
3
4
5
6
7
8
9
Explanation:
- Here 'i' is the variable name in this for loop.
- If you write the same variable name in print function here in this example is 'i', then it start print the number upto range you given in the loop.
- In python numbers start by 0. So, this loop start with zero but the last digit i.e., 10 not print because loop is already complete 10 times when we count from 0 to 9. For more clearance see in next example.
Example-7:- No effect if you choose here 'i' instead of guru
Code:
for guru in range(10):
print(guru)
Result:
0
1
2
3
4
5
6
7
8
9
Explanation:
- It result same with previous example. This example want to tell that whatever you choose i or guru or any name not affect the result because the format is:
for variable name in range (number of times to repeat):
statements to be repeated
- So, guru is variable name in this example and you can choose any name.
Example-8:- Print numbers in between some range
Code:
for i in range(1,4):
print(i)
Result:
1
2
3
Explanation:
- If you want number not start from zero then give range in between you want
- Here in this example we give range(1,4) it means loop start from value 1 and end on 3
- It start from 1 but the 4 is not print because Python not include the last digit so it end on 3.
Example-9:- Print numbers in between some range with difference
Code:
for i in range(5,0,-1):
print(i)
Result:
5
4
3
2
1
Explanation:
- Here you see in the 1st line, we insert three digit in the for loop i.e., (5,0,-1) means 5 is the starting digit and 0 is the end digit and -1 for the change in number with gap of -1.
- When you see in result then you see 5 4 3 2 1 means value appear with the gap of -1 because in the last of loop we use -1 so value start decrease with the change of -1. It means the digit are change from 5 to 0 with the gap of -1.
- If you change this example with value (10,0,-2) then the result is: 10 8 6 4 2
- Last digit zero(0) not appear because python not include last value.
Example-10:- For detail about for loop
Example-11: Print the counting with addition of 1
Code:
for i in range(3):
print(i+1)
Result:
1
2
3
Explanation:
- In the first line we use loop and give range 3 it means it repeat three time.
- In second line you see (i+1) which means add 1 after every digit.
- We know python start from 0 value then it add 1 in this because in command we use i+1 , so 1 appear in result then 2 and then 3.
- If we not use i+1 and instead of this only use i then result is 0 1 2, but in this result we add 1 so now final result is 1 2 3.
Example-12: Print a symbol multiple time in loop
Code:
for i in range(4):
print('#' * 6)
Result:
######
######
######
######
Explanation:
- In the first line we use loop and give range 4 so it repeat for four time.
- In the 2nd line you see hashtag(#) , in between quotes so it directly print and then multiply 6 times.
- 6 hashtag appear in a line due to multiply by 6.
- But we run loop 4 times so these 6 hashtag appear in 4 row .
Example-12:- Print a symbol with addition of 1
Code:
for i in range(4):
print('#' * (i+1))
Result:
#
##
###
####
Explanation:
- In the 1st line we use loop which repeat for 4 time
- In the 2nd line you see hashtag(#) is multiply with i+1. It means it first multiply with 1 then 2 after with 3 and then 4.
- Simply, if you want to understand these problem first think the result when we use of only 'i' instead of i+1. When we use only 'i' then result is 0 1 2 3. Now add 1 in this so final result will be 1 2 3 4
- So, hashtag (#) first multiply by 1 and then by 2 and so on. That's why first one hashtag appear then 2 and then 3 and then 4.
- It also not start with zero because you add i+1 that means you add 1 after every digit which first start with zero.
No comments:
Post a Comment
If you have any doubt, let me know.