background preloader

Python

Facebook Twitter

The "Invent with Python" Blog — Nobody Wants to Learn How to Program. I frequently see a problem when people (especially techies) try to teach programming to someone (especially non-techies). Many programming tutorials begin with basic programming principles: variables, loops, data types. This is both an obvious way to teach programming and almost certainly a wrong way to teach programming.

It’s wrong because nobody wants to learn how to program. If you are teaching a class of adults who are paying with their own money for an education, then this is an appropriate and direct way to teach programming. It’s their money. They expect that they’ll have to focus and slug through concepts to come out the other end with programming knowledge. The start-with-variables-loops-data-types approach is fine for this. But for the casually interested or schoolchildren with several activities competing for their attention, programming concepts like variables and loops and data types aren’t interesting in themselves. 1. 2. 3.

I began learning BASIC in the third grade. 4. 3 useful Python scritps. I use Python more and more for simple scripting - it really excels at this! The scripts I create with Python are very simple and easy to understand (unlike many Perl or shell scripts). Here are 3 useful Python scripts that I wrote for some time ago. Clean .pyc files I use Subversion for version control and I don't version the .pyc (Python bytecode) files. Before I issue the status command to see what has changed to my source, I issue this script to delete all the .pyc files. import os , sys def pyc_clean ( dir ): findcmd = 'find %s -name "*.pyc" -print' % dir count = 0 for f in os . popen ( findcmd ) . readlines (): count += 1 print str ( f [: - 1 ]) os . remove ( str ( f [: - 1 ])) print "Removed %d .pyc files" % count if __name__ == "__main__" : pyc_clean ( ".

" ) Searching for something specific with Python I wanted to find "cherrypy.request" in all the files, but not in the .svn directories (which are Subversion's directories). Thanks to Kate for pointing out the grep equivalent. 3. Projects for the Beginner - Python. Python Projects. As I write this, there are almost 3,000 projects in freshmeat's C category and almost 1,500 in the Perl category, but there are only about 400 projects in the Python category. SourceForge has similar statistics. In this article, I hope to get more people to consider using Python in their next projects. My first programming languages were C and Perl. I found C very fast and powerful, but I had constant battles with ever finishing the projects I started in it.

I mainly used Perl for one-liners and throw-away scripts. I found that the C programs I wrote contained many lines of code for even the most trivial programs and the Perl programs contained very few lines of code but, due to Perl's cryptic nature, were so hard to read that it didn't really matter. When I found what Python had to offer, I was amazed. If you want to write programs with a Graphical User Interface, there are a few options. If you're interested in writing games, Pete Shinners's Pygame library is for you.