Wednesday, December 1, 2010

3kpoll- Vote for your favorite Python package

Although it's been around two years since the release of Python 3, many Python packages including popular ones like django, wxPython and numpy haven't ported to Python 3. Now, the official Python website is running a poll to supposedly get volunteers and package authors to get started on porting the most "wanted" packages to Python 3, although "nominating a package will not mean that its authors now start porting it to Python 3."

According to the poll[up till now], most Python users want django to be supported by Python 3. wxPython and numpy take up the second and third places respectively.  The results of the poll can be found at http://www.python.org/3kpoll. Also, don't forget to vote or nominate your favorite Python package to be ported to Python 3.

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)

Tuesday, October 26, 2010

Simple encryption in Python

In this post I will demonstrate a simple program to encrypt any string in Python. The encryption is based on conversion to ASCII codes, and thus is very basic. What this program does is convert each letter of a string to its ASCII code (using ord() ), increase the ASCII code by 200, convert it back to characters and concatenate all the characters. The concatenated string is thus the encrypted text.

Here is the program-

string=input('Enter a string:')
text_enc_char=''
enc_factor=200
text_dec=''

for char in string:
        enc_char=ord(char)+enc_factor
        text_enc_char+=chr(enc_char)
print(text_enc_char)
I will write about decryption in the next post; meanwhile you can write your own code to decrypt text. Go ahead, decrypt this one- ĘŁēķĬĭĺ

For decrypting the text above go to the second part of this post here.

Monday, October 25, 2010

Calculate the runtime of a program

A lot of times you will want to check how much time it took for your program to complete. The time it takes for your program to complete is called runtime. Generating runtime data can be useful in many instances, like if you want to see how efficient your algorithm is, or how much time it would take for a similar program but with larger number of operations(brute force programs, for example).

To generate runtime data in Python is quite easy:-

import time
tStart=time.time()
# the main operations of your program go here.
print("Program complete")
print("Time taken:", time.time()-tStart)

This is one way to calculate runtime in Python. Do share any alternative ways to do this in the comments section!!

Sum of digits

#sumofdigits.py
#Finds the sum of digits of entered number

num=int(input('Enter a number:'))
sum_of_digits=0
for n in str(num):
    sum_of_digits+=int(n)
print(sum_of_digits)

A pretty short program for finding the sum of digits of a number. As you can see, the program converts the integer to a string, and then finds the sum of digits by extracting each digit(as string), converting the digit to integer, and adding it.

Do share with us any alternative ways you know to perform this same task.

Sunday, October 24, 2010

Generating prime numbers upto n-th number.

#primenumbers.py
num= #insert a number here       e.g num=1000
pp=2
ps=[pp] #here, pp is stored into the list, ps.
pp+=1
ps.append(pp)

while pp<int(num):
    pp+=2
    test=True
    for a in ps:
        if pp%a==0: test=False
    if test: ps.append(pp)
 
print (ps)
This program will generate all prime numbers upto the given number, num. To increase the efficiency of the operation, it only checks odd numbers for whether they are prime(since, 2 is the only even prime number).

If you have better methods of completing this program, do share them here!!

Saturday, October 23, 2010

Python Basics- Getting started

In this post, I will discuss how to get started in Python- running Python on your system and how to write and run code. At the end of this post you will also find a simple Hello World program.


Installing Python

The first thing you need to do in order to start learning Python is to install it. You can download the Python installation file from Python's Downloads page. I recommend using the latest 3.x version as Python has already announced that no new versions of the 2.x series will be released.

Note, that if you are using a Linux OS, your system may already have Python installed. If not then, you can use the Downloads page above to download Python and install it.

Programming in Python

After installing Python on your computer, there are three ways to write a Python code-

1.Interactive command prompt-
    You can use your command prompt to open Python and start coding. In this mode, the output is shown directly after you type a line of code. The downside to coding in this mode is that you cannot run your code more than once(since it cannot be saved).

    To use Python in your command prompt, open your command prompt and type 'python', and then press Enter.

     2.Using a text editor

    You can also use your favorite text editor(but avoid word processing software) to type in Python code. Save the file as .py file and you can run it in your command prompt or in IDLE(Python's IDE). To run the file you have created, type

    python helloworld.py

    in your command prompt[Note that in the above example, the Python program you wrote should be in the same folder as your Python installation.]; or open the file in IDLE(it comes pre-bundled with Python!!) and run it by pressing F5.

    3. Using IDLE

    As I've already mentioned above, Python comes prebundled with IDLE, which is an IDE(Integrated Development Environment) for Python. This is probably the most preferred way to write Python code for most beginners as it is easy to use with features like colour coding and automatic indentation (and so on). Just open IDLE from your Python installation file and open a new window in IDLE to begin creating your Python programs.

    You can also explore other IDEs (there are many!!) for Python.


    A Hello World Program

    Most programmers begin their first program by writing a Hello World program in the language they are learning. All this program does is print some text (here, the text is Hello world).In Python this is extremely simple:

    print("Hello World") #this is a comment.


    or in the command prompt it's just-

    "Hello World"

    Take note that you can use single quotation marks(') instead of the double quotation marks(") I've used above.

    The text followed by a hash(#), is ignored by the Python interpreter and is used for commenting on your code. Writing comments beside your code is usually a good practice as you may not always recall what you had thought of when writing the code. Also, it is good practice because it will help other programmers understand what you are doing in a program.

    Hopefully this post got you started in Python. Goodbye until my next post!!

    Friday, October 22, 2010

    About Python and PyKoder

    Python is a powerful and dynamic high-level programming language that is easy to learn and use. Python is free to use even for commercial purposes. Python has a large community of supportive members who are ready to help and also consists of extensive documentation.

    For those who are set out on a journey into programming, Python is a great choice for your first programming language. Those already well acquainted with programming can also choose to shift to the world of Python programming.

    Some features of the Python programming language are:

    • very clear, readable syntax
    • strong introspection capabilities
    • intuitive object orientation
    • natural expression of procedural code
    • full modularity, supporting hierarchical packages
    • exception-based error handling
    • very high level dynamic data types
    • extensive standard libraries and third party modules for virtually every task
    • extensions and modules easily written in C, C++ (or Java for Jython, or .NET languages for IronPython)
    • embeddable within applications as a scripting interface
    Python has also been used for a wide range of applications such as finance and scientific data processing. Python can be used easily for simple tasks as well as large and complex tasks. Its extensive collection of standard libraries and modules allows you to even perform tasks such as extracting information from the internet!!

    PyKoder is intended to be a web resource for learning Python. PyKoder will feature Python code snippets, tutorials, tips, tricks and news in the form of a blog. As of now, the latest version of Python is 3.1.2 so the code you will find here will work with all 3.x versions. I hope that you will find the information in PyKoder useful.

    Over time, I hope to make PyKoder an extensive resource for those willing to learn Python. I hope that you will support me in this endeavour by providing your feedback. Thanks for visiting PyKoder.