Help command and Data Types in Python

In this post we are going to see Help command and Data Types in Python.

Use help along with command which you want need to know more that command.

Here is the example:

Below command will shows the help about print command.

[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.
>>> help ('print')

 

Data types:

  1. Numbers
  2. Strings
  3. Lists
  4. Tuple
  5. Dictionary

Numbers:

Three types of numbers data type available in Python.

Integers: 10

Floating point: 2.1, 3.45

Complex numbers: (4+1J), (3.6 – 5.4a).

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.

Python installation in Linux/Unix

In RedHat by default python installed. We are going to see the Python installation in Linux/Unix.

First we have to prepare our system to install Python.

Preparing system:

login as root user, as its a administrator and having full privileges.

Use subscription-manager to know whether you have access to RedHat software repository or not.

[root@server ~]# subscription-manager repos --list-enabled

if you don’t see any repository enabled, than your machine not registered or not having subscription.

Now update using yum command.

[root@server ~]# yum update

Once you executed the above command with valid subscription, all the packages will get updated in your OS.

Now setup your environment:

As you aware already, we are going to use yum to install and check Python package.

Check whether python installed or not using yum.

[root@server ~]# yum list installed | grep python

If the package not installed, use below command to install python.

[root@server ~]# yum install python

above command will ask your confirmation to proceed install. Simply type  and press enter.

Once executed above command, again check whether its installed or not.

[root@server ~]# yum list installed | grep python

Use python  command to run python in interactive mode.

[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.
>>> quit()
[root@server ~]#

Finally we installed python in linux.

About Python

About Python:

Python language created by Guido van Rossum.

Features of Python

Built on c  routines

Easy to use and debug

Portable

Object oriented

Interface with other languages

compiled and Interpreted language

Open and free

 

Download link:        www.python.org

To know python version use below command

python -v

Note: Any line starts with # will commented and not used by script while executing.

Will create a script file with .py extension and should use python command to run the python script.

Example:     

python first.py