DEV Community

Cover image for Loops in python
Aswin Barath
Aswin Barath

Posted on • Edited on

1 1 1 1 1

Loops in python

Have you ever felt like you are doing certain tasks again and again in life?
Well, I'm no expert in helping with your life!
Although, if you would ever face the same situation in a programming language I can help you with that.

In programming, we can repeat certain tasks using loops.
Let's go through them one by one.

for loop

  • for loop in python repeats over each item of a given sequence (a list or a string), in the order that they appear in the sequence.

Example:
Alt Text

Output:

I want to watch a movie directed by James Cameron
I want to watch a movie directed by Christopher Nolan
I want to watch a movie directed by Steven Spielberg
Enter fullscreen mode Exit fullscreen mode
  • But, let's say you are a programmer who needs to iterate over a sequence of numbers, given the limit you want. Well, then you can make use of the built-in function range() which generates arithmetic progressions: Alt Text

Output:

0
1
2
3
4
Enter fullscreen mode Exit fullscreen mode

while loop

  • while loop in python repeats any given set of code until the give condition returns true.

Example:
Alt Text

Output:

0
1
1
2
3
5
8
Enter fullscreen mode Exit fullscreen mode

Best Resources

Who Am I?

I’m Aswin Barath, a Software Engineering Nerd who loves building Web Applications, now sharing my knowledge through Blogging during the busy time of my freelancing work life. Here’s the link to all of my socials categorized by platforms under one place: https://linktr.ee/AswinBarath

Thank you so much for reading my blog🙂.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay