AsegGasiaBlog

Python - Part - 2


Installation on Windows

Visit https://www.python.org/downloads/ and download the latest version. The installation is just like any other Windows-based software.

CAUTION : When you are given the option of unchecking any "optional" components, don't uncheck any.

If you want to be able to use Python from the Windows command line i.e. the DOS prompt, then you need to set the PATH variable appropriately.


For Windows 7 and 8 :

Start -> choose Control Panel -> System and Security -> System -> Advanced system settings -> Advanced tab.
At the bottom click on Environment Variables and under System variables, look for the PATH variable, select and then press Edit. Go to the end of the line under Variable value and append ;C:\Python35 (the path where the python 3.X is installed) Click OK and you are done. No restart is required, however you may have to close and reopen the command line.

Running Python prompt on Windows

If the PATH is set properly, you can run python via command line interpreter. To open the terminal in Windows, click the start button and click Run. In the dialog box, type cmd and press [enter] key. Then, type python3 and ensure there are no errors.

Installation on Mac OS X

For Mac OS X users, use Homebrew: brew install python3 this will install the python3 in you Mac. To verify, open the terminal by pressing [Command + Space] keys (to open Spotlight search), type Terminal and press [enter] key. Now, run python3 and ensure there are no errors.

Installation on GNU/Linux

For GNU/Linux users, use your distribution's package manager to install Python 3, e.g. on Debian & Ubuntu:


sudo apt-get update && sudo apt-get install python3

To verify, open the terminal by opening the Terminal+ application or by pressing [Alt + F2] and entering gnome-terminal.
You can see the version of Python on the screen by running :


$ python3 -V
Python 3.5.1

CAUTION : Output may be different on your computer, depending on the version of Python software installed on your computer.

How to write, Save and Run Python programs

There are two ways of using Python to run your program -

  • using the interactive interpreter prompt
  • using a source file.

We will now see how to use both of these methods.

Using The Interpreter Prompt

Open the terminal in your operating system and then open the Python prompt by typing python3 and pressing [enter] key.
Once you have started Python, you should see >>> where you can start typing your code. This is called the Python interpreter prompt.
At the Python interpreter prompt, type the following and press [Enter] :


print("Hello World")

Output :

Hello World


How to Quit the Interpreter Prompt

If you are using a GNU/Linux or OS X shell, you can exit the interpreter prompt by pressing [ctrl + d] or entering exit() (note: remember to include the parentheses, ( )) followed by the [enter] key.

If you are using the Windows command prompt, press [ctrl + z] followed by the [enter] key.

You can use any text editor to write and save the python source file. It is saved with the extension.py

Open the text editor and write your code it it and after that save it with the .py extension and you python source file is ready.

Lets write the following command in a Text editor and save it as hello.py in the following respected locations :

/tmp/py     on Mac OS X
/tmp/py on GNU/Linux
C:\\py on Windows

To create the above folder (for the operating system you are using), use the mkdir command in the terminal,
for example:-mkdir /tmp/py


print("hello world")

Now to get output of this source file you need to run the following command from the CommandPrompt or the terminal

To run your Python program :

1. Open a terminal window

2. Change directory to where you saved the file, for example, cd /tmp/py

3. Run the program by entering the command python hello.py. The output is as shown below.


$ python hello.py
hello world

You can use any text editor to write and save the python source file. It is saved with the extension.py

Open the text editor and write your code it it and after that save it with the .py extension and you python source file is ready.

Lets write the following command in a Text editor and save it as hello.py in the following respected locations :

/tmp/py     on Mac OS X
/tmp/py on GNU/Linux
C:\\py on Windows

To create the above folder (for the operating system you are using), use the mkdir command in the terminal,
for example:-mkdir /tmp/py


print("hello world")

Now to get output of this source file you need to run the following command from the CommandPrompt or the terminal

To run your Python program :

1. Open a terminal window

2. Change directory to where you saved the file, for example, cd /tmp/py

3. Run the program by entering the command python hello.py. The output is as shown below.


$ python hello.py
hello world


Popular Posts