Saturday, November 20, 2010

Basic physics problem solver GUI

I just wrote a simple GUI in wxPython which calculates speed, distance or time according to the input.

Here is the code-




And, here is what the output looks like-


As you can see, this is a very simple program, see the code, find out how it works and you can modify it yourself too!! Share any of your comment/feedbacks in the comments section below.

PS. Please note that to run the program you will need to have wxPython installed. 

Thursday, November 11, 2010

Stuck in Python 2.7

I started off with learning Python 3 in the first place but now I am working my way backwards and I'm using Python 2.7. That happened because many popular libraries that I want to use like wxPython, django and many more haven't yet ported to Python 3 yet.

I also recommend others who like me began with Python 3 to get and install Python 2.7(latest in 2.x versions). This won't be a problem though as many features have been backported to Python 2.7 in the form of the 'future' module(see here how to do it) and most of your programs will be able to run on Python 3 too.

Python awarded best programming language for second year

The Readers' Choice awards by Linux Journal picked out Python as the best programming language as well as best scripting language for the second year in a row.

In programming languages, the second position is held by C++, followed by Java, C and Perl in the 3rd, 4th and 5th places respectively.

Whereas in scripting languages PHP is the runner-up followed by bash, Perl and Ruby.

Cheers to all the Pythonistas out there!!

See the full results of the Awards at Linux Journal.

Wednesday, November 10, 2010

The Zen of Python

In this post I am going to introduce you to what began as a kind of joke by long time Pythoneer Tim Peters but later sustained through more than 20 years to become a outline of the philosophy of the Python programming language. Developers at the Python Software Foundation as well as programmers using Python have found guidance in the Zen of Python.

You can find the Zen of Python in a module called 'this' in your very own Python installation.



As you can see, the Zen of Python is written in a rather poetic and humorous way. Looks like Python programmers do have a lot of sense of humor. For one more Python joke type in:



in your command prompt.

Monday, November 8, 2010

Color-code your programming blogs with Friendpaste

I was just going through some of my earlier blog entries and I noticed how ugly all the code looks. Besides, every time I had to post a code I had to change the font just to let you guys know that the code part had begun. Well.... not any more :D!!

I just found Friendpaste which automatically color-codes a program and formats it according to the selected language. Just the service I needed!! I hope that you'll have a easier time around the blog after I start using the service.

Do give us your feedback/suggestions.

Saturday, November 6, 2010

Developing programming skills with Project Euler

Would you like to take your programming and problem-solving skills to the next level? These days I've been spending some time daily at a website called Project Euler. Project Euler is a series of programming challenges containing at present more than 300 problems(and it is growing!!).

From what I've experienced up till now, most of the problems at Project Euler require both mathematics and programming. Many problems require learning about various mathematical and programming concepts and thus, is very suitable for getting introduced to new and useful topics. Also, as "problems range in difficulty and for many the experience is inductive chain learning" so you will be able to learn new things from solving a simple problem and then move on to relatively more difficult problems.

As, most of the problems are numeric in nature, using Python is quite an advantage. Once you understand what the question is asking, you may find the answer right away by using a naive brute-force, however, most of the times this approach won't be efficient enough and you'll be out again searching for improving your program. And, that is what makes it a wholesome educational experience. This can become frustrating at times, but don't just give up; most of the times the solution is just right under your nose waiting to be found out.

Check out Project Euler here- http://projecteuler.net/

Thursday, November 4, 2010

Popularity tag cloud program

Popularity tag clouds are used very commonly in blogs, forums and websites these days to show off the topics those sites talk about frequently. The code snippet I am going to present here is a simple program that finds out the frequency of each word in a text file.

file=open('workfile.txt', 'r')#save a text file with some text in same #directory as your .py file
fin=file.read()

list1=(fin.split(' '))
excl_words=['and','is','in','the','an','which','both','of','off'] #words #                                                        to be excluded

freq_data={}
for char in list1:
    if char in excl_words:continue
    #print(char,":",list1.count(char))
    freq_data[char]=list1.count(char)
print (freq_data)

As I've already mentioned above, this program does nothing except count the number of times each word appears in the text file. This could be a base for building a full-fledged popularity cloud generator I'll be working on.I'll keep you updated on that.

PS. Some possible improvements to the program-
  1. Change all words to lower case before counting.

Do send in your suggestions and ideas!!

Wednesday, November 3, 2010

Simple encryption (part2- Decryption)

In the last post, Simple encryption in Python, we discussed a simple way to use ASCII characters for encryption. If you haven't read that post I recommend that you do so before moving on.

In this post we will discuss how to decrypt the text we encrypted in the last post. The basic idea here is just to reverse the process we followed while encrypting.

text_enc_char=input('Enter encrypted text:')
enc_factor=200
for encchar in text_enc_char:
                dec_char=ord(encchar)+enc_factor
                text_dec+=chr(dec_char)

print(text_dec)