Basic’s of Python scripting

In this post we are going to see Basic’s of Python scripting.

Use Python command to move to python interpreter like below and using quit() to exit from the interpreter.

and here we used simle print command to print any string.

[root@server ~]# python
Python 2.7.5 (default, Aug 2 2016, 04:20:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print ('Hello World')
Hello World
>>> quit()
[root@server ~]#

As you all aware will write bash script in linux/unix. In Python also will create shell script. For that, we need to do simple change in first line like below and even we can create with extension with .sh.

#!/usr/bin/env python

Now we will create our first simple python script to display set of strings. To create this script will use any editor and here i’m going to use vi editor.

[root@server ~]# vi first.py
#!/usr/bin/env python
print "Hello, This is my first script"

use :wq to save and exit from this file and now should provide execute permission using chmod command to run this script.

[root@server ~]# chmod +x first.py

Finally will run the script using ./ like shell scripting.

[root@server ~]# ./first.py
Hello, This is my first script

We successfully created our  first python script.

Leave a Reply

Your email address will not be published. Required fields are marked *