Friday, 10 February 2017

Range Functions and Sequences in Python


Range() generates lists containing arithmetic progression.
Three variations of range() function
>> range(stop) – Starts from 0 till (stop – 1)
>> range(start,stop) – Ends at (stop – 1)
>> range(start,stop,step) – Step cannot be 0, default is 1
Example of Range Function
range(stop) - If the range is defined as 5, it would simply show the list of numbers falling in the range from 0 to 5. The default range starts from 0 and stops before 5, as defined.
range(start, stop) – The point of starting as well as stopping is defined in this. As shown in the example below, the start range has been defined as 5, while stop range as 10. Hence, it would display the numbers 5, 6, 7, 8, 9, which range between 5 to 10.
range(start, stop, step) - The first two values defined here are the same, start and stop, while the third one is step, which means the difference between every two consecutive numbers. For example, if range is defined in this way: range(0, 10, 2). It will give away numbers between 0 to 10, but with a difference of 2, in this way: [0, 2, 4, 6, 8]. The step here cannot be given 0 value. It has to be 1 or greater than 1.
Sequences in Python
A sequence is the succession of values bound together by a container that reflects their type. Almost every stream that we put in Python is a sequence.
Types of Sequences
  • Lists
  • Tuples
  • Xrange
  • String
The python is supported by some other sequences are strings, lists, tuples and Xrange objects. Python has a bevy of methods and formatting operations that can perform.
List
  • A list is a sort of container which holds the number of other objects, in a given order.
  • The list type implements the sequence protocol which allows adding and removing objects from the sequence.
  • It is an ordered set of elements enclosed in square brackets.
Simple definition of list – li = []
li = list() # empty list
li = list(sequence)
li = list(expression for variable in sequence)
example
            >>> list(a)
            [‘e’, ‘x’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’]
            >>> list3 = [‘Hadoop’, ‘Python’, ‘Data Science’, ‘Pig’, ‘hive’]
            >>> list3
            [‘Hadoop’, ‘Python’, ‘Data Science’, ‘Pig’, ‘hive’]
            >>> list3[2:]
            [‘Data Science’, ‘Pig’, ‘hive’]
            >>> list3[2:3]
            [‘Data Science’]
            >>> list3[2:4]
            [‘Data Science’, ‘Pig’]
            >>> list3[2:5]
            [‘Data Science’, ‘Pig’, ‘hive’]
            >>>
Accessing List Elements
To access the elements of a list:
n = len(li)
item = li[index] #Indexing
slice = li[start:stop] #Slicing
List Indexing

list[i] returns the value at index i, where i is an integer. A negative index accesses elements from the end of the list counting backwards. The last element of any non-empty list is always li[-1]. Python raises an IndexError exception, if the index is outside the list.

Accessing Command Line Arguments

Python supports the creation of programs that would run on the command line, completely with command-line arguments. It provides getopt modules that parse the command line options and arguments. The Python sys module provides access to any of the command-line arguments via sys.argv. It solves two purposes:
  • sys.argv is the list of command line arguments
  • len(sys.argv) is the number of command line arguments that are in the command line
  • sys.argv[0] is the program, i.e. script name

Executing Python

The python should be executed in the following
$python Commands.py inp1, inp2 inp3

Example

import sys
print ‘Number of arguments:’, len (sys.argv), ‘arguments.’
print ‘Argument List:’, str(sys.argv)
It will produce the following output:
Number of arguments: 4 arguments.
Argument List: [‘sample.py’, ‘inp1’, ‘inp2’, ‘inp3’]
If you have any queries? Mention them in the comments section and we will clarify you.

Wednesday, 8 February 2017

Introduction to strings in python


In Python, the strings should be created by simply enclosing the characters in quotes. Python does not support the character types. These are treated as length-one strings, and are also considered as substrings. Substrings are immutable and can’t be changed once created.
Strings are the ordered blocks of text that are enclosed in single or double quotations. Thus, whatever is written in quotes, is considered as string. Though it can be written in single or double quotations, double quotation marks allow the user to extend strings over multiple lines without backslashes, which is usually the signal of continuation of an expression, e.g., ‘abc’, “ABC”.

Concatenation and Repetition

  • Strings are concatenated with the +sign:
>>> ‘abc’+‘def’
‘abcdef’
  • Strings are repeated with the *sign:
>>> ‘abc’*3
‘abcabcabc’

Indexing and Slicing Operation

  • Python starts the indexing at ‘0’
  • A string s will have indexes running from 0 to len(s)-1 (where len(s) is the length of s) in integer quantities.
  • S[i] fetches the ‘i’th element in the s.

Built-in String Methods

Following are the built-in String Methods that can be used in Python:
  • capitalize() – This method is used to capitalize the first letter of string.
  • count(str, beg= 0, end=len(string)) – Used to count how many times  the str occurs in string or in a substring of string, if beginning index ‘beg’ and ending index ‘end’ are given.
  • encode(encoding=‘UTF-8’,errors=‘strict’) – This method is used to return the encoded string version of string; on error, default raises a ValueError, unless the error is given with ‘ignore’ or ‘replace’.
  • decode (encoding=‘UTF-8’, errors=‘strict’) – This method is used to decode the string using the codec registered for Encoding. Encoding defaults to the default string function.
  • index(str, beg=0, end=len(string))- Same as find(), but it raises an exception if str is not found.
  • max(str)- Used to return the max alphabetical character from the string str.
  • min(str)- This is used to return the min alphabetical character from the string str.
  • replace(old, new [, max])- This method is used replace all the occurrences of ‘old’ in string with ‘new’ or maximum occurrences if max is given.
  • upper()- This method is used to convert the lowercase letters in a string to uppercase.

If you have any queries? Mention them in the comments section and we will clarify you.

Tuesday, 7 February 2017

Python for DataScience


Python is the selection of information researchers. In nowadays python plays an important role to do their everyday exercises, as it has a differing scope of open-source libraries, and everything is free. The work of data scientists involves several interrelated activities, such as:
  • Accessing and manipulating data
  • Computing statistics
  • Creating visual reports on that data
  • Configuring predictive and explanatory models
  • Evaluating the models based on the additional data
  • Integrating the models into the production systems
If a data scientist wants to do some ad hoc analysis on data, he doesn’t write a Java code; the reason is java is too complicated for a data scientist to start programming. It has its own syntax and semantics, and every time there is a chance for developing a program in which one might run into a syntax or a semantic error, which nobody needs. Consequently, Pig and Hive were developed, however additionally we have Python in parallel, wherein you don't need to compose a great deal of lines of code.
The only thing that you need to remember in Python is indentation. Whenever a code is being written, in this time one needs to take care of spacing. If the indentation (spacing) is not proper, the program would be failed. If you are running a ‘for loop’, anything within the ‘for loop’ has to come a few inches inside the ‘for loop’. All lines of code should have same indentation or should be in one line.
SciPy
SciPy (pronounced as “sigh pie”) is Scientific Python which empowers the scientific analysis. It is a Python-based biological system of open-source software for mathematics, science, and engineering. We all have done differentiation, equation, etc. in mathematics, in school and college. Presently, how is it done in computers? It can be done in Octave as well, but Python provides us with SciPy which is the one that can perform such types of operations very easily. The Python that coordinates some libraries namely NumPy, SciPy library, Matplotib, IPython, Sympy, and pandas, and each one has its own role to play.

If you have any queries ? Specify them in the remarks area we will clarify you !..

Monday, 6 February 2017

Introduction to python IDE

Introduction to python IDE

Python IDE (Integrated Development Environment) is a code editor, which allows editing of code making use of a series of peripheral components and attachments. The code editor we are using which almost gives the same result.

Features of Python IDE

  • It has  an ordinary text-editor
  • It offers a variety of language with specific shortcut editing functions.
  • Very fast and comfortable to use
  • Python is also an interpreter
PyCharm is one of the Python IDE code editors that are generally used by programmers.

Downloading PyCharm 

The Free Community Edition of PyCharm can be downloaded from the link below:
http://www.jetbrains.com/pycharm/download/

Starting Python

To start Python, you just need to type Python in your terminal command line and after that press enter.
The $ sign denotes the start of a terminal command line, and then the # sign denotes a comment. Python ignores anything written on the right side of # sign on a given line.

Using the Interpreter

  • In addition to being a programming language, Python is also an interpreter. It reads other Python programs and commands, and executes them.
  • Python programs are compiled automatically before being scanned by the interpreter. The hidden scanning process makes Python faster than a pure interpreter.
  • Once you’re inside Python, you can type commands at your own will.
  • Quantities stored in memory are not displayed by default
  • If the quantity is stored in memory, typing its name will display it.
  • To exit the interpreter, you need to press [Ctrl + D]

If you have any queries? Mention them in the comments section and we will clarify you.

Friday, 3 February 2017

Python Database Access

Python Database Access

The Python standard for database interfaces is the Python DB-API. Most Python database interfaces adhere to this standard. Python Database API supports a wide range of database servers such as −
·        GadFly
·        MySQL
·        MySQL
·        PostgreSQL
·        Microsoft SQL Server 2000
·        Informix
·        Interbase
·        Oracle
·        Sybase
The DB API provides a minimal standard for working with databases using Python structures and syntax wherever possible. The MySQLdb module explains all concepts using MySQL. This API includes the following:
·        Importing the API module.
·        Acquiring a connection with the database.
·        Issuing SQL statements and stored procedures.
·        Closing the connection.
What is MySQL db?
MySQLdb is an interface for connecting to a MySQL database server from Python which implements the Python Database API v2.0 and it is built on the top of the MySQL C API.
How do I Install MySQLdb?
Before proceeding, make sure the MySQLdb is installed on the machine. Just type the following in Python script and execute it:
#!/usr/bin/python
Import MySQLdb
If it produces the below result, it means MySQLdb module is not installed:
           Traceback (most recent call last):
                       Import MySQLdb
           ImportError: No module named MySQLdb
Database Connection
Before connecting to the MySQL database, check the followings −
·        Create a database TESTDB.
·        Create a table EMPLOYEE in TESTDB.
·        This table has fields such as FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.
·        User ID "testuser" and password "test123" are set to access TESTDB.
·        Python module MySQLdb is installed properly on the machine.
While running this script, the below result would be produced.
           Database version: 5.0.45
Creating Database Table
Once a database connection is established, we are ready to create tables or records into the database tables using execute method of the created cursor.
INSERT Operation
It is required when we want to create the records into a database table.
READ Operation
Fetch some useful information from the database. Once our database connection is established and then makes a query into this database.
·        fetchone() -  Fetches the next row of a query result set. A result set is an object that is returned when a cursor object is used to query a table.
·        fetchall() - Fetches all the rows in a result set. If some rows have already been extracted from the result set, then it retrieves the remaining rows from the result set.
·        Rowcount -  This is a read-only attribute, returns the number of rows that were affected by an execute() method.
Update Operation
Used to update one or more records that are already available in the database.
DELETE Operation
Used to delete some records from the database.
Performing Transactions
It is a mechanism that ensures data consistency having the following properties:
·        Atomicity - Either a transaction completes or nothing happens at all.
·        Consistency - A transaction must start in a consistent state and leave in a consistent state.
·        Isolation - Intermediate results of a transaction are not visible outside the current transaction.
·        Durability - Once a transaction was committed, the effects are persistent, even after a system failure.
The Python DB API 2.0 provides two methods to either commit or rollback a transaction.
COMMIT Operation
It gives a green signal to database to finalize the changes, and after this operation, no change can be reverted back.
ROLLBACK Operation
Revert back the changes completely by using use rollback() method.
Disconnecting Database
By using the close() method to disconnect the Database connection.
Handling Errors
There are many sources of errors. The DB API defines a number of errors that must exist in each database module. The exceptions are mentioned below.
·        Warning – Used for non-fatal issues.
·        Error – Base class for errors.
·        Interface Error – Used for errors in the database module not for the database itself.
·        Database Error – Used for errors in database.
·        Data Error – Subclass of database error which refers the errors in the data.
·        Operational Error – Refers the loss of connection to the database that are outside of control of the python scripter.
·        Integrity Error – Damage the relational integrity such as uniqueness constraints or foreign keys.
·        Internal Error – Refers to the errors internal to the database module.
·        Programming Error - Refers to the errors such as bad table name.
·        Not Supported Error – Refers that trying to call unsupported functionality.


Wednesday, 1 February 2017

Python Network Programming

Python Network Programming

Python provides two levels of access to network services. At a low level, we can access the basic socket support in the underlying operating system, which would allows to implement clients and servers for both connection-oriented and connectionless protocols.
Python also has libraries that would provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on.
What is Sockets?
Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a process, between processes on the same machine, or between processes on different continents.
Sockets would be implemented over a number of different channel types such as Unix domain sockets, TCP, UDP, and so on. The socket library provides specific classes for handling the common transports as well as a generic interface for handling the rest.
Socket Module
To create a socket, we must use the socket.socket() function available in socket module, which has the general syntax that are given as follows:
s = socket.socket (socket_family, socket_type, protocol=0)
The parameters can be described as follows
socket_family - This is either AF_UNIX or AF_INET.
socket_type - This is either SOCK_STREAM or SOCK_DGRAM.
protocol - This is usually left out, defaulting to 0.
Server Socket Methods
s.bind() - This method is used to binds the address (hostname, port number pair) to socket.
s.listen() - This method is used to sets up and start TCP listener.
s.accept() - This method passively accept TCP client connection, waiting until connection arrives (blocking).
Client Socket Methods
s.connect()This method actively initiates TCP server connection.

General Socket Methods

s.recv() - This method receives TCP message
s.send() - This method transmits TCP message
s.recvfrom() - This method receives UDP message
s.sendto() - This method transmits UDP message
s.close() - This method closes socket
socket.gethostname() – This method would returns the hostname.
A Simple Server
To write Internet servers, we use the socket function available in socket module to create a socket object. A socket object is used to call other functions to setup a socket server.
Call the bind(hostname, port) function to specify a port for the service on the given host.
Next, call the accept method of the returned object. This method waits until a client connects to the port specified, and then returns a connection object that represents the connection to that client.
A Simple Client
Write a very simple client program which opens a connection to a given port 12345 and given host. This is very simple to create a socket client using Python's socket module function.
The socket.connect(hostname, port) opens a TCP connection to hostname on the port. Once the socket is open, read from it like any IO object. When done, remember to close it, as to close a file.