SETTING PATH IN PYTHON AND PYTHON EXAMPLE

Before starting working with Python, a specific path is to set.
  • Your Python program and executable code can reside in any directory of your system, therefore Operating System provides a specific search path that index the directories Operating System should search for executable code.
  • The Path is set in the Environment Variable of My Computer properties:
  • To set path follow the steps:
Right click on My Computer ->Properties ->Advanced System setting ->Environment Variable ->New
In Variable name write path and in Variable value copy path up to C://Python(i.e., path where Python is installed). Click Ok ->Ok.
Path will be set for executing Python programs.
1. Right click on My Computer and click on properties.
2. Click on Advanced System settings

3. Click on Environment Variable tab.

4. Click on new tab of user variables.

5. Write path in variable name

6. Copy the path of Python folder

7. Paste path of Python in variable value.

8. Click on Ok button. 
9. Click on Ok button. 


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 .pyextension. 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. >>>    

Comments