Python Example

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 .py extension.

hello.py

 

print(“hello world by letuslearn!”)  

Execute this example by using following command.

 

Python3 hello.py  

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

Output

hello world by letuslearn!

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.

 

>>> a=“Welcome To Python”  

>>> print a  

Welcome To Python  

>>>    

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.

 

>>> a=(“Welcome To Python Example”)  

>>> print a  

Welcome To Python Example  

>>>