Hello,
I have been using the python challenge to teach myself python as my first programming/scripting language outside of HTML. As I go along, I find that while I'm understanding the concepts rather well, I am not being as efficient as I could be. While the code I whip up gets things done, the suggestions others have shared with me make me thing that python can be as simple as speaking in english.

Case in point:

from string import ascii_letters

import re, urllib2, webbrowser as wb

wb.open('http://www.pythonchallenge.com/pc/def/%s.html' %

        ''.join(chr for chr in

        re.findall('<!--(.*?)-->',

        urllib2.urlopen('http://www.pythonchallenge.com/pc/def/ocr.html')

        .read(), re.S)[1] if chr in ascii_letters))

This snippet was suggested to me for the 2nd puzzle. If I read it (although it's organized differently), it comes back like this:

Importing ASCII letters from string, re, and urllib2 modules and webrowser as wb, here is what we want to do:
*open the url http://www.pythonchallenge.com/pc/def/ocr.html
*read the source and find all unique ascii characters between <!-- and --!>
*join those characters and replace % in the following url
http://www.pythonchallenge.com/pc/def/%s.html'

Is this correct? My problem is that when I come up with an idea for a program, while I can get it done, there is usually a way to accomplish the same goal in much less code. How do you learn what to use when?

Recommended Answers

All 2 Replies

You learn through practice.

For me, I read the basic python books, and then I came to this board. I read the questions that people ask and I try to answer ever one that I can. This gives me the ability to apply python to real world needs...and in the process I learn about things that I didn't know before. As I become more comfortable with the language, I am able to do things in ways that are more efficient or readable. It is much the same way that you learn to speak a new language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.