How to Write a Basic Python Program

Python is a high-level programming language. The language utilizes a straightforward syntax which can make it easy for new users to get started. ==== Install the Dependencies ====
Part 1 of 2:

Environment Set Up

Install the Dependencies

  1. How to Write a Basic Python Program Picture 1How to Write a Basic Python Program Picture 1
    Open the Terminal. On Linux, you can press the Alt button to open a search bar. Type "terminal" and click on Terminal.
  2. How to Write a Basic Python Program Picture 2How to Write a Basic Python Program Picture 2
    Know the commands. Terminal commands are shown in this document as:
    1. command -options filename.ext 
    2. sudo apt-get install build-essential 
      1. The "sudo" command gives permission to the terminal to modify your computer. This is necessary to install any program. You will be required to enter your password.
    3. The "apt-get install" command tells the computer to install the package "build-essential" which is required to install Python.
    4. sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev 
    5. These are programs that Python uses to run correctly. They are also known as 'dependencies'.

Download and Extract Python

  1. How to Write a Basic Python Program Picture 3How to Write a Basic Python Program Picture 3
    Download the latest version of Python from the Internet. Use the following command:
    1. cd ~/Downloads/ 
    2. The "cd" command changes to the correct working directory so the computer can find and put programs in the right place.
    3. wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tgz 
  2. How to Write a Basic Python Program Picture 4How to Write a Basic Python Program Picture 4
    Decompress the Python file using the following command:
    1. tar -xvf Python-2.7.5.tgz 
    2. cd Python-2.7.5 
    3. Once again, we need to change the working directory. This time, we change to the newly created Python directory.

Install Python

  1. How to Write a Basic Python Program Picture 5How to Write a Basic Python Program Picture 5
    Use the ./configure command checks your computer to ensure you have all the necessary components to install Python. It will alert you of any critical errors.
    1. ./configure 
  2. How to Write a Basic Python Program Picture 6How to Write a Basic Python Program Picture 6
    Use the make command. It compiles the source code and creates the executables.
    1. make 
  3. How to Write a Basic Python Program Picture 7How to Write a Basic Python Program Picture 7
    Move the applications and libraries. With the following command, all of the applications and libraries associated with Python are moved into the correct places on your computer.
    1. sudo make install 
Part 2 of 2:

Use Python

Write the Script

  1. How to Write a Basic Python Program Picture 8How to Write a Basic Python Program Picture 8
    Open a text editor.
    1. Any text editor that can save files with a ".py" extension will do. Ubuntu 12.04 or greater is packaged with the Gedit editor.
  2. How to Write a Basic Python Program Picture 9How to Write a Basic Python Program Picture 9
    Type
    print 'Hello, World!' 
    1. In Python, whatever is enclosed in quotes after the word print will be printed out to the screen.
  3. How to Write a Basic Python Program Picture 10How to Write a Basic Python Program Picture 10
    Save the file as "hello_world.py.
    1. Be careful that the file is not saved as "hello_world.py.txt".

Run the Script

  1. How to Write a Basic Python Program Picture 11How to Write a Basic Python Program Picture 11
    Open the Terminal again.
  2. How to Write a Basic Python Program Picture 12How to Write a Basic Python Program Picture 12
    Navigate to the directory where "hello_world.py" is located.
    1. Remember to use the "cd" command to change directories.
    2. If you need a list of all subdirectories at your current location, use the "ls" command. "ls" stands for "list subdirectories".
  3. How to Write a Basic Python Program Picture 13How to Write a Basic Python Program Picture 13
    Run the script:
    1. python hello_world.py 
4 ★ | 1 Vote