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.