Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8253579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:55:31+00:00 2026-06-08T00:55:31+00:00

I am a Python beginner. I find that the else in for – else

  • 0

I am a Python beginner. I find that the else in for–else and while–else is completely unnecessary. Because for and while will finally run to else, and we can use the usual lines instead.

For example:

for i in range(1, 5):
    print i
else:
    print 'over'

and

for i in range(1, 5):
    print i
print 'over'

are the same.

So why does Python have else in for–else and while–else?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:55:33+00:00Added an answer on June 8, 2026 at 12:55 am

    You are wrong about the semantics of for/else. The else clause runs only if the loop completed, for example, if a break statement wasn’t encountered.

    The typical for/else loop looks like this:

    for x in seq:
        if cond(x):
            break
    else:
        print "Didn't find an x I liked!"
    

    Think of the “else” as pairing with all of the “if’s” in the loop body. Your samples are the same, but with “break” statements in the mix, they are not.

    A longer description of the same idea: http://nedbatchelder.com/blog/201110/forelse.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python beginner here. I have a text file that is sorted into columns: fields
I am beginner in python, and I need to use some thirdparty function which
I am beginner in python and facing this problem. So how i can break
I'm a beginner in python and I'm trying to use a octal number in
I don't use Python regularly, but now I have to. I should run a
Python beginner here. I've looked around and found similar questions but can't quite cobble
First, let me say that I'm a complete beginner at Python. I've never learned
I'm python beginner: how can I iterate over csv files in one directory and
beginner to python here. I have 2 nested lists that I want to merge:
I am a complete beginner to Python or any serious programming language for that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.