In the previous blog post, we saw how to install the python. Now, we will see how to run a simple “Hello World” program in python. This will help you to start learning python.
There are two ways to use python to run a python program –
1. using the interactive interpreter prompt or
2. using a source file.
Table of Contents
Using Interpreter prompt:
Open terminal/cmd as per your operating system, if paths have been set up properly by the Python install process, you should be able to just type python
. Then, you should see a response from the Python interpreter.
Now enter print(‘Hello World’) followed by the Enter key. You should see the words Hello World as output.
Example 1. Using the Python interpreter prompt (in the window)
D:\>python Python 3.7.2 (v2.7.17:c2f86d86e6, Oct 19 2019, 21:01:17) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print('Hellow World') Hellow World >>>
If you are not seeing the
Point to remember>>>
prompt, then you are not talking to the Python interpreter. This could be because Python is either not installed or not in your terminal window session’s path. It’s also possible that you just haven’t found the correct command to execute it.
If everything went well then you will able to see similar output as above🎉 , if not there is some issue 😥
Possible error(s) in code:
- Missing parentheses in call to ‘print’
>>> print "Hello" print "Hello" File "<stdin>", line 1 print "Hello world" ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello")? >>>
- Missing Sting quotation marks “”
>>> print(Hello world) print(Hello world) File "<stdin>", line 1 print(Hello world) ^ SyntaxError: invalid syntax >>>
- Forgot to close sting quotation mark
>>> print("Hello world) print("Hello world) File "<stdin>", line 1 print("Hello world) ^ SyntaxError: EOL while scanning string literal
- Improper space issue. (python is Indentation base language)
>>> print("Hello World") File "<stdin>", line 1 print("Hello World") ^ IndentationError: unexpected indent >>>
- Python is a case-sensitive language. Might be you used uppercase instead of lower case
>>> Print("Hellow World") Print("Hellow World") Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'Print' is not defined >>>
How to quit the Python prompt?
There are several ways to quit python prompt:
- Type exit() and press keyboard <Enter> button
C:\>python Python 3.7.2 (v2.7.17:c2f86d86e6, Oct 19 2019, 21:01:17) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> exit() C:\>
- To exit the prompt, press Ctrl-d if you are using IDLE or are using a Linux/BSD shell. The interpreter terminates immediately; pressing Enter is not needed.
- In the Windows command prompt, press Ctrl-z followed by <Enter>.
C:\>python Python 3.7.2 (v2.7.17:c2f86d86e6, Oct 19 2019, 21:01:17) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ^Z C:\>
- In the Windows command prompt, press Ctrl-c. The interpreter terminates immediately; pressing Enter is not needed. (tested in W10)
C:\>python Python 3.7.2 (v2.7.17:c2f86d86e6, Oct 19 2019, 21:01:17) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ^C C:\>
Using a Source File:
Python interpreter interactively is great for quick testing and exploring features or functionality. But, when you start writing the large program then it is difficult to use the interpreter, so we have to use the editor to write the programs.
There are many free editors available to write programs, for example, notepade++, vim, vscode, etc.
Now let’s get back to programming. There is a tradition that whenever you learn a new programming language, the first program that you write and run is the ‘Hello World’ program – all it does is just say ‘Hello World’ when you run it.
Start your choice of editor, enter the following program and save it as helloworld.py
print("Hello World")
Run this program by opening a shell (Linux terminal or DOS prompt) or command prompt and entering the command python helloworld.py. The output is as shown below.
D:\>cd Python D:\Python>dir Volume in drive D is Data Volume Serial Number is 4CBD-3973 Directory of D:\Python 02/23/2020 12:52 PM <DIR> . 02/23/2020 12:52 PM <DIR> .. 02/23/2020 12:48 PM 20 hellowworld.py 1 File(s) 20 bytes 2 Dir(s) 121,409,896,448 bytes free D:\Python>python hellowworld.py Hello World D:\Python>
If you got the output, as shown above, congratulations! – you have successfully run your first Python program. In case you got an error, please type the above program exactly as shown and above and run the program again.
Indentation:
Whitespace is important in Python. Actually, whitespace at the beginning of the line is important. This is called indentation.
Leading whitespace (spaces and tabs) at the beginning of the logical line is used to determine the indentation level of the logical line, which in turn is used to determine the grouping of statements.
This means that statements which go together must have the same indentation.
>>> i = 1 >>> print(i) File "<stdin>", line 1 print(i) ^ IndentationError: unexpected indent >>> i = 1 >>> print(i) >>> 1
How to indent:
- Do not use a mixture of tabs and spaces for the indentation as it does not work across different platforms properly.
- It is advisable to you use a single tab or two or four spaces for each indentation level.
- Choose any of these three indentation styles. More importantly, choose one and use it consistently i.e. use that indentation style only
In the next blog post, we will learn the Basic Data Types of python.
Reference: https://docs.python.org/3/tutorial