[Tutor] Why is this not an error?

Hameed U. Khan hameed.u.khan at gmail.com
Sun Mar 20 15:20:37 CET 2005


On Sunday 20 Mar 2005 1846, Steve N wrote:
> This code runs, but it seems to me it should generate
> a syntax error.  Can someone explain what's going on?

Why do you think there is a syntax error?
> x = [1,2,3,4]
> z = 4
> if z in x:
>     print 'found:', z

if clause has been ended here.
> for i in x:
>     print 'in the loop with', i
> else:
>     print 'not found:', z

This else is the part of the for loop. And the statements in this else will be 
executed if your for loop will complete all of its iterations. if you want 
this else with 'if' statement then remove the for loop.
>
> found: 4
> in the loop with 1
> in the loop with 2
> in the loop with 3
> in the loop with 4
> not found: 4
>
> --
> Steve
>
>
>
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail - Easier than ever with enhanced search. Learn more.
> http://info.mail.yahoo.com/mail_250
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Hameed U. Khan,  <hameed.u.khan at gmail dot com>
Registered Linux User #: 354374


More information about the Tutor mailing list