Getting Started With Python Programming Language

cheeze
My First Program
Python is a powerful programming language known for its flexibility and power. However, it's not well known in the general public despite it being an excellent starting point for beginners. Instead, they usually start with Visual Basic, which is unfortunate because Visual Basic teaches too much of the of "visual" aspects of programming instead of the logic based part necessary to allow one's mind to think effectively.

There is an awesome feature in Python: the IDLE (Integrated DeveLopment Environment) application, which allows a quick and easy way to write test code or algorithms. Start Python by opening pythonw.exe which opens the IDLE application. Next, go to File, New; this will open a blank file. Get into the habit of saving often by saving now as "Hello.py".

With this blank file, we will create the simple "hello world" program. Simply type "print 'hello world'" and press F5. This will compile and execute the code which should display "hello world". There! You've created your very first program in Python.

Thinking Abstractly
The most important thing in programming is to be able think like a computer. Remember: computers don't make mistakes, humans do. So if the program has a bug, it's not the code because the code did exactly what it was programmed to do. You simply need to change it so it does what you want it to do; obviously, if you're able to think like a computer, you will have less errors.

Functions and Variables
There are many different types of variables in programming. Most languages, however, makes it rather difficult to have different types of variables to interact with each other. For example, a string is basically a line of text; an integer would be something like 5. In Java, you would have to call the integer class's method in order to change an integer into a string (or vice versa) whereas in Python, you simply have to say "str(5)", which will return "5". The quotes represent strings. If you recall in mathematics that functions are in the form of f(x), you can think of programming as the same. You put something in the function and you get something in return; in math, if you define f(x) = 5x, then if x is 2, then f(2) returns 10. In str(5), it returns "5". Similarly, int("5") returns 5.

In Python, you can easily create a variable which can be used in a function similar to str() but do whatever you want it to do. The variable that goes in the parenthesis are called arguments. These arguments are then used in the function to be manipulated and returned for some new variable. For example, let's create a new function (type this out in IDLE):

def toString(argument):
return str(argument)

This code does exactly the same thing as str() because it returns the exact same function. Let's look at the two lines of code. The first line defines a new function; this means the computer creates a new function called "toString()". The second line tells the computer what will happen if the function is called or used. The keyword "return" means after using the function, it will return a value. So, in IDLE, after entering the function above, if you type:

toString(5)

This will print out 5. Of course, this is just a simple function; more advanced functions can be hundreds of lines long. Other variables that can be used in python include strings, floats, integers, and even custom variables called classes. However, that is more advanced and will not be covered here.

Published by cheeze

Love math which probably lead to my interest in programming; later started the violin which lead to music which is starting to overpower the programming side. College now. Yay.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.