Hey, just started taking up Python, and was going to post any questions I might have about it here. Starting off, I'm trying to figure out how this code from the tutorial works, specifically the else on line 6. Does it only go to the else if nested for loop doesn't break, or how exactly would it go there?


Code:
for n in range(2, 10):
     for x in range(2, n):
         if n % x == 0:
             print n, 'equals', x, '*', n/x
             break
     else:
         # loop fell through without finding a factor
         print n, 'is a prime number'
The else statement runs only if the for loop runs to completion. A break statement terminates the loop prematurely and bypasses the else statement. Here's what the tutorial says about the else statement:
Quote:
Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.
Frankly you can forget the "for/else" statement exists - it's a little weird and niche.

To figure out what it does, though, just open up an interactive shell and find out. If you just run python without a script argument, it'll let you just start typing commands and interactively running them. By far the best way to figure out python.
Yeah, doing that right now, almost have my first "program" completed by programming functions.
Heartily agreed on using the Python interactive command line. It's a great tool for testing things out without having to make a test script.
Kllrnohj wrote:
Frankly you can forget the "for/else" statement exists - it's a little weird and niche.

It is fantastic for people who populate their for loops with break statements to make single-entry/exit functions.


Code:

for(;;){ if(condition){ flag = true; break;} }
 if(!flag){ ... }

becomes

Code:

for _ in _:
 if condition: break
else:
 ...




Quote:
To figure out what it does, though, just open up an interactive shell and find out. If you just run python without a script argument, it'll let you just start typing commands and interactively running them. By far the best way to figure out python.

Seconded.
elfprince13 wrote:
It is fantastic for people who populate their for loops with break statements to make single-entry/exit functions.


I didn't say it wasn't useful, I said he could forget it exists. Syntax sugar should be one of the last things covered when learning a new language, and for/else falls very much into that category. Your example also falls into the "niche" part of what I said Wink
Kllrnohj wrote:
Your example also falls into the "niche" part of what I said Wink


Where "niche" is the majority of for loops I've had to write in the last 2 years.


And your point about syntactic sugar really depends on your past programming experience - at some point learning new languages is really mostly about learning syntax and the most common bits of the API.
I find similar control structures pretty useful too, such as for-case and for-while loops. Razz
elfprince13 wrote:
Where "niche" is the majority of for loops I've had to write in the last 2 years.


Sorry, I'm assuming well designed code here Razz (I kid, I kid)

Quote:
And your point about syntactic sugar really depends on your past programming experience - at some point learning new languages is really mostly about learning syntax and the most common bits of the API.


Absolutely, but you still start with the core syntax, figure out how the API works, then pick up the sugar. Sugar should really always come last unless it is a core part of whatever API you are using (for example, if your working with SQL in C#, absolutely don't skip the LINQ syntax sugar)

And I get the impression he doesn't have much past programming experience here Wink
Alright, I've been playing around with the console for a while now, and think I'm starting to get the grasp of how to handle Python, however I do have a question I wasn't able to figure out with only the console. I'm wanting to make an IRC Bot, but I don't know what modules to import or where to even start. Any suggestions?
MufinMcFlufin wrote:
Alright, I've been playing around with the console for a while now, and think I'm starting to get the grasp of how to handle Python, however I do have a question I wasn't able to figure out with only the console. I'm wanting to make an IRC Bot, but I don't know what modules to import or where to even start. Any suggestions?


http://tinyurl.com/qb33ef
Jonimus swears by the Python SupyBot package. I myself use irclib for things like SaxJax, and have been quite happy with it.
Kllrnohj wrote:
MufinMcFlufin wrote:
Alright, I've been playing around with the console for a while now, and think I'm starting to get the grasp of how to handle Python, however I do have a question I wasn't able to figure out with only the console. I'm wanting to make an IRC Bot, but I don't know what modules to import or where to even start. Any suggestions?


http://tinyurl.com/qb33ef
Yes, I googled it immediately after I posted the question, I was simply asking for if anyone had any pointers here. :P

No offense, but there's not much a point to a forum if the reply is "go ask google."
Do either of the two packages that I mentioned appeal to you, Mufin? Smile
I remember hearing about irclib from one of my friends who made his own python IRC bots, so I think I'll try that out for a bit. I keep getting errors when trying to connect to irc.esper.net through the console, so might as well try it out.
MufinMcFlufin wrote:
No offense, but there's not much a point to a forum if the reply is "go ask google."


So don't ask questions that are easily answered by Google, which has the awesome effect of not wasting everyone's time, either. Ask questions Google is unable to answer, and everyone is happy.
Kllrnohj wrote:
MufinMcFlufin wrote:
No offense, but there's not much a point to a forum if the reply is "go ask google."


So don't ask questions that are easily answered by Google, which has the awesome effect of not wasting everyone's time, either. Ask questions Google is unable to answer, and everyone is happy.


How to Ask Smart Questions has some good pointers.
MufinMcFlufin wrote:
I remember hearing about irclib from one of my friends who made his own python IRC bots, so I think I'll try that out for a bit. I keep getting errors when trying to connect to irc.esper.net through the console, so might as well try it out.
I believe you most recently said on #cemetech that you got it to connect, but you hadn't gotten it to speak yet?
KermMartian wrote:
MufinMcFlufin wrote:
I remember hearing about irclib from one of my friends who made his own python IRC bots, so I think I'll try that out for a bit. I keep getting errors when trying to connect to irc.esper.net through the console, so might as well try it out.
I believe you most recently said on #cemetech that you got it to connect, but you hadn't gotten it to speak yet?
I actually literally just finished getting it working, pretty much perfectly.

What I did was added a "zombie mode" so I can chat through it on any channel, and a Minecraft Block/Item ID checking script, which will display the official name of a block and the data value when you ask it diamond (id?).

The zombie mode works perfectly so far, and I'm working on adding a few things to the MC block/item checker. Currently it'll only look at the previous word, which is problematic for blocks like Block of Iron (yes, that's the official name, apparently) so it would only check Iron. So I'm trying to come up with some way for it to check multiple words to be a block, or requiring that you input as one word, or with underscores replacing spaces. I'm also working on adding unofficial names, so Iron Block will report the info of Block of Iron, and the same goes with most all other blocks as well.

Only once both of those are complete, will I attempt to make the spell check I was talking about earlier.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 3
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement