How to Write a Basic Python Program
Part 1 of 2:
Environment Set Up
Install the Dependencies
- Open the Terminal. On Linux, you can press the Alt button to open a search bar. Type "terminal" and click on Terminal.
- Know the commands. Terminal commands are shown in this document as:
-
command -options filename.ext
-
sudo apt-get install build-essential
- 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.
- The "apt-get install" command tells the computer to install the package "build-essential" which is required to install Python.
-
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
- These are programs that Python uses to run correctly. They are also known as 'dependencies'.
-
Download and Extract Python
- Download the latest version of Python from the Internet. Use the following command:
-
cd ~/Downloads/
- The "cd" command changes to the correct working directory so the computer can find and put programs in the right place.
-
wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tgz
-
- Decompress the Python file using the following command:
-
tar -xvf Python-2.7.5.tgz
-
cd Python-2.7.5
- Once again, we need to change the working directory. This time, we change to the newly created Python directory.
-
Install Python
- 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.
-
./configure
-
- Use the make command. It compiles the source code and creates the executables.
-
make
-
- 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.
-
sudo make install
-
Part 2 of 2:
Use Python
Write the Script
- Open a text editor.
- Any text editor that can save files with a ".py" extension will do. Ubuntu 12.04 or greater is packaged with the Gedit editor.
- Type
print 'Hello, World!'
- In Python, whatever is enclosed in quotes after the word print will be printed out to the screen.
- Save the file as "hello_world.py.
- Be careful that the file is not saved as "hello_world.py.txt".
Run the Script
- Open the Terminal again.
- Navigate to the directory where "hello_world.py" is located.
- Remember to use the "cd" command to change directories.
- If you need a list of all subdirectories at your current location, use the "ls" command. "ls" stands for "list subdirectories".
- Run the script:
-
python hello_world.py
-
4 ★ | 1 Vote
You should read it
- Break and continue commands in Python
- How to Launch Python Files Using Windows Command Prompt
- Install Python Package with PIP on Windows, Mac and Linux
- How to Install Python on Windows
- How to Install Python
- How to Install Tkinter
- Programming Python on Android device
- How to Install Python Packages on Windows 7
May be interested
- How to Set Up a Python Environment for Deep Learningsetting up a python-based machine learning (ml) environment on a particular platform can be somewhat challenging if you don't have the right resources. you would need to install python first and then add a number of different packages. to...
- How to Write a Server with Pythoncreating a server from scratch is a big task. however doing so can greatly improve your programing skills and can allow for you to modify it to your needs. this tutorial will be using python and low level socket programing to create a...
- How to Install Pythonpython is an interpreted, object-oriented, high-level programming language that is a great place for beginners to start learning how to program. python comes installed on macs and with linux, but you'll need to install it yourself if...
- How to Program a Game in Python with Pygamethis is an introduction to pygame for people who already know python. this article will teach you the steps to building a simple game that has the player dodging bouncing balls. download pygame. find it for your platform from...
- How to Compile Python Scriptpython is a very popular language for programming. but what if the person running your program does not want or know how to run a python script? this article will teach you how to compile a python script into an executable. download...
- How to Install Tkintertkinter (tk) is a python default gui and comes with the python installation on linux, mac, and windows. since tk comes with most python installations, you don't generally need to install it yourself. since python 2 and python 3 vary so...