Hello-World

H

The “Hello, World!” is the first, simple and complete, program for beginners as well as a good program for testing systems and programming environments. “Hello, World!” is the basic syntax of programming languages.

This tutorial will help you write the “Hello, World” program in Python 3 on different programming platforms.

We need Python 3 and an editor.

An editor helps you read and write code. There are many, and this is one of the most personal choices a programmer can make. For starters, you’ll want a simple, easy-to-use and non-intuitive editor, but it’s still active writing Python code.
Sublime Text: An excellent editor that is simple to use. Its shortcut Ctrl + B allows you to execute the Python file you are working on immediately.

Running on Windows, Mac, and Linux.

Python is something called a programming language. Take what you write (frequently called code), turn it into instructions for your computer and run them. We’ll learn how to write code to make things interesting and useful. You will no longer have to use the programs of others to do things with your computer!
Python is just another program on your computer. The first thing to learn is how to interact with it. There are many ways to do this; the first is to communicate with the Python interpreter using the OS operating system console, short for the Operating System.
A console is a form of interacting with your text-based operating system, just as ‘desktop,’ in combination with your mouse, is the graphical method of interacting with your system.

1. Open a console in Mac OS X

OS X’s standard console is a program called Terminal. Open Terminal by going to Applications, then Utilities, then double-clicking Terminal. You can also search with the right-hand side navigation.

The Terminal Command Line is a tool for interacting with your computer. A window will open with a message, such as:

computer: ~ username

2. Open a console in Linux

Various Linux distributions (Ubuntu, Fedora, Mint) can have various console programs, usually called terminals. The one you have available and the way you launch it depends on the distribution. On Ubuntu, you will probably want to open the Gnome Terminal. It should provide a prompt like:

username @ Computer: ~

3. Open a console in Windows

The Windows Console is Command Prompt, called cmd. A simple way to get it is to use the Windows + R combination (where Windows is the Windows logo key), which should open a Run dialog. Then write cmd and press Enter or click Ok. You can also search for it in the home menu. It should look like this:

C:\Users\username>

The Windows Command Prompt is not as powerful as its counterparts in Linux and OS X, so you might want to call the Python Interpreter directly, or use the Python-supplied IDLE program. You can find them in the Start menu.

Using Python

The Python program that you have installed will automatically behave as something called an interpreter. An interpreter picks up orders and executes them as you insert them – very convenient to try things out.

Just type python in the console, press Enter and you should enter the Python Interpreter.

To find out which Python version you are running, use python -V.

Interaction with Python

After it starts, Python will print some context information, similar to:

Python 5.4.3 (default, June 10 2019, 22:55:21)
[GCC 5.4.3 (prerelease)] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>>

Note:
The >>> prompt on the last line indicates that you are in an interactive Python session called Python shell. This is different from the normal terminal prompt!
You can now enter Python code. Try:

print (“Hello world”)
Press Enter and see what happens. After displaying your results, Python will return to the interactive prompt, where you can enter another command:

>>> print (“Hello world”)
Hello world
>>> (1 + 4) * 2
10

A handy command is help (), which accesses documenting functionality to explore all the things Python allows you to do directly from the interpreter. Press q to close the documentation window and return to the Python prompt.

To exit the interactive shell, press Ctrl-Z and then Enter in Windows, or Ctrl-D on OS X or Linux. Alternatively, you can also execute the exit () command!

Run Python files

When you have to run a lot of Python code, you’ll want to save it to a file so that you can, for example, edit pieces from it (resolve an error) and rerun it without having to re-enter the rest of the code. Alternatively, you can save the code to a file and send it to the python executable. It will run the file instead of launching the interactive interpreter.
Let’s try this. Create a python file in the current directory using your favorite editor and write the print command above. Now save this file. In Linux or OS X, you can also use touch hello.py to create an empty file for editing. It’s straightforward to run this Python file:

python hello.py

Note
Make sure you’re in the system prompt, which has $ or> at the end, not Python’s (ending with >>>)!
In Windows, you should be able to double-click on the Python file to execute it.

Now, when you hit Enter, the file is executed and you will see the result as before. But this time, after Python will finish executing, all the commands in this file will come back to the system prompt instead of going back to the interactive shell.

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories