Day #17 - For loops in Python

Day #17 - For loops in Python

ยท

4 min read

Introduction

Welcome to my 17th blog post. Today I learned about magical loops using which now I will be automating all my household stuff. I learned about the power of "for loops" and I literally printed some 1 to 20000 numbers in one go. Now let's dive deep into the details

So let's get started......

Loops

In our daily lives, we perform daily household chores. These are the frequent repetitive task that needs to be performed on daily basis. Now what if I tell you, just imagine this repetitive task also appears in coding and we can automate some of them using "FOR LOOPS" and "WHILE LOOPS"

Obviously, household chores cannot be automated and need to be done on daily basis for our survival, but coding can help you get rid of a repetitive task in a program and can make your life simple as a programmer.

So in this blog let's dive into detail into for loops and in the next one we will see how while loops work.

For Loop

For loops can iterate over a sequence like strings, lists, tuples, sets and dictionaries.

Example (Using for loops in String)-

name = 'Abhishek'
for i in name:
    print(i, end=", ")   #we have also used 'end' seperator
A, b, h, i, s, h, e, k,

Example (Using for loops over a list) -

colors = ["Red", "Green", "Blue", "Yellow"]
for x in colors:
    print(x)
Red
Green
Blue
Yellow

We can also use for inside of for loops -


list = ["Red","Blue","Green","Black"]

for i in list:
  print(i)
  for spell in i:
    print(spell)
Red
R
e
d
Blue
B
l
u
e
Green
G
r
e
e
n
Black
B
l
a
c
k

Similarly, we can use loops for lists, sets and dictionaries.

range()

What if we do not want to iterate a "for loop" over a sequence? What if we want to use "for loop" to iterate an instruction for a certain number of times.

Here, we can use the range() function.

for k in range(5):
    print(k)
0
1
2
3
4

Here, we can see that the loop starts from 0 by default and increments at each iteration.

We can also use "range() " function to specify a range.

for k in range(4,9):
    print(k)
4
5
6
7
8

" Range() " function can also take 3 parameters. The third parameter can be used to skip the elements in the instruction. Let's understand with an example.

for i in range(1,20,3):
  print(i)
1
4
7
10
13
16
19

Here using the third parameter we skip some elements at a specific interval and print the rest ones.

Resources Used

You can watch the video of Day#17 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. In this post, we learnt about for loops and range() function and wrote a bunch of code.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding This is it for Day #17

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practising coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย