According to Data Science Topic-15 For loop and examples

 # for loop demo

for i in range(1,11):

 print(i)

Result: 1

2

3

4


5

6

7

8

9

10

for i in range(1,11,3):

  print(i)

Result: 1

4

7

10

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

  print(i)

Result: 10

9

8

7

6

5

4

3

2

1

i = 'delhi'

for i in 'delhi':

  print(i)

Result: d

e

l

h

i

for i in [1,2,3,4,5]:

  print(i)


for i in (1,2,3,4,5):

  print(i)


for i in {1,2,3,4,5}:

  print(i)

Result: 1

2

3

4

5

1

2

3

4

5

1

2

3

4

5

# Program - The current population of the town is 10000. The population of the town is increasing at the rate of 10% per year. You have to write a program to find out the population at the end of each of the last 10 years.
curr_pop = 10000
for i in range(10,0,-1):
  print(i,curr_pop)
  curr_pop = curr_pop - (curr_pop/1.1)

Result: 10 10000
9 909.0909090909099
8 82.6446280991737
7 7.513148009015794
6 0.6830134553650726
5 0.062092132305915704
4 0.005644739300537799
3 0.0005131581182307096
2 4.665073802097362e-05
1 4.240976183724878e-06


Comments

Popular posts from this blog

According to Data Science Topic-13 Modules

Functions in Python:

According to Data Science Topic-12 If else program examples