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!!

    No comments:

    Post a Comment