First Step Towards Python

First Step

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 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 the program. 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.

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 our program every time in the interpreter prompt because we might need to run our single program many times .So we cannot just type it everytime . That is why we need to write our program in a file.

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 very crucial. A good editor will help you write Python programs easily.

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.

PLEASE CHOOSE THE EDITOR OF YOUR CHOICE.

Run Your Program

Always ensure that you give it the file extension of .py

To run your Python program:

Open a terminal of window Change directory to where you saved the file, for example,cd /temporary/py Run the program by entering the command python follwed by your file name. If you got the output then congratulations! - you have successfully run your first Python program. You have successfully crossed the hardest part of learning programming, which is, getting started with your first program!

How It Works

A Python program is composed of many statements. In our first program, we have only have one statement. In this statement, we hane the print statement to which we supply the text “hello world”.The print satatement used to output values on the screen.

Getting Help

If you need quick information about any function or statement or modules in Python, then you can use the built-in help functionality. This is very useful especially when using the interpreter prompt. For example, run help(‘len’) - this displays the help for the len function which is used to count number of items.

Note: Press q to exit the help.

Similarly, you can obtain information about almost anything in Python. Use help() to learn more about using help itself! In case you need help for keyworsd like return, then you need to put those inside quotes such as help(‘return’) so that Python doesn’t get confused on what we’re trying to do.