First Steps after Python installation

 

First Steps

We will now see how to run a traditional \’Hello World\’ program in Python. This will teach you how to write, save and run Python programs.

There are two ways of using Python to run your program – using the interactive interpreter prompt or 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 (as discussed previously in the Installation chapter) 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 stuff. This is called the Python interpreter prompt.

At the Python interpreter prompt, type:

print(\"Hello World\")

followed by the [enter] key. You should see the words Hello World printed to the screen.

Here is an example of what you should be seeing, when using a Mac OS X computer. The details about the Python software will differ based on your computer, but the part from the prompt (i.e. from >>>onwards) should be the same regardless of the operating system.

$ python3
Python 3.6.0 (default, Jan 12 2017, 11:26:36)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.
>>> print(\"Hello World\")
Hello World

Notice that Python gives you the output of the line immediately! What you just entered is a single Python statement. We use print to (unsurprisingly) print any value that you supply to it. Here, we are supplying the text Hello World and this is promptly printed to the screen.

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.

Choosing An Editor

We cannot type out our program at the interpreter prompt every time we want to run something, so we have to save them in files and can run our programs any number of times.

To create our Python source files, we need an editor software where you can type and save. A good programmer\’s editor will make your life easier in writing the source files. Hence, the choice of an editor is crucial indeed. You have to choose an editor as you would choose a car you would buy. A good editor will help you write Python programs easily, making your journey more comfortable and helps you reach your destination (achieve your goal) in a much faster and safer way.

One of the very basic requirements is syntax highlighting where all the different parts of your Python program are colorized so that you can see your program and visualize its running.

If you have no idea where to start, I would recommend using PyCharm Educational Edition software which is available on Windows, Mac OS X and GNU/Linux. Details in the next section.

If you are using Windows, do not use Notepad – it is a bad choice because it does not do syntax highlighting and also importantly it does not support indentation of the text which is very important in our case as we will see later. Good editors will automatically do this.

If you are an experienced programmer, then you must be already using Vim or Emacs. Needless to say, these are two of the most powerful editors and you will benefit from using them to write your Python programs. I personally use both for most of my programs, and have even written an entire book on Vim.

In case you are willing to take the time to learn Vim or Emacs, then I highly recommend that you do learn to use either of them as it will be very useful for you in the long run. However, as I mentioned before, beginners can start with PyCharm and focus the learning on Python rather than the editor at this moment.

To reiterate, please choose a proper editor – it can make writing Python programs more fun and easy.

PyCharm

PyCharm Educational Edition is a free editor which you can use for writing Python programs.

When you open PyCharm, you\’ll see this, click on Create New Project:

\"When

Select Pure Python:

\"PyCharm

Change untitled to helloworld as the location of the project, you should see details similar to this:

\"PyCharm

Click the Create button.

Right-click on the helloworld in the sidebar and select New -> Python File:

\"PyCharm

You will be asked to type the name, type hello:

\"PyCharm

You can now see a file opened for you:

\"PyCharm

Delete the lines that are already present, and now type the following:

print(\"hello world\")

Now right-click on what you typed (without selecting the text), and click on Run \'hello\'.

\"PyCharm

You should now see the output (what it prints) of your program:

\"PyCharm

Phew! That was quite a few steps to get started, but henceforth, every time we ask you to create a new file, remember to just right-click on helloworld on the left -> New -> Python File and continue the same steps to type and run as shown above.

+

You can find more information about PyCharm in the PyCharm Quickstart page.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Select Language »