Python tutorial part 9 : Hello world

Python Example


 

Python is easy to learn and code and can be execute with python interpreter. We can also use Python interactive shell to test python code immediately. A simple hello world example is given below. Write below code in a file and save with .py extension. Python source file has .pyextension.

 

hello.py

1.    print("hello world by python!")  

Execute this example by using following command.

1.    Python3 hello.py  

After executing, it produces the following output to the screen.

Output

hello world by python!

 

Python Example using Interactive Shell

Python interactive shell is used to test the code immediately and does not require to write and save code in file.

Python code is simple and easy to run. Here is a simple Python code that will print "Welcome to Python".

A simple python example is given below.

 

1.    >>> a="Welcome To Python"  


2.    >>> print a  

3.    Welcome To Python  

4.    >>>    

 

Explanation:

Here we are using IDLE to write the Python code. Detail explanation to run code is given in Execute Python section.


A variable is defined named "a" which holds "Welcome To Python".


"print" statement is used to print the content. Therefore "print a" statement will print the content of the variable. Therefore, the output "Welcome To Python" is produced.


 

Python 3.4 Example

In python 3.4 version, you need to add parenthesis () in a string code to print it.

 

1.    >>> a=("Welcome To Python Example")  


2.    >>> print a  

3.    Welcome To Python Example  

4.    >>>    

 

No comments:

Post a Comment

Quick Tech