Monday, 29 April 2019

1. Python Introduction

1.   Introduction to Python   -   20 min read
  • What is Python?
  • Where are all python is using?
  • History of Python?
  • Why the name was python?
  • Python versions
  • Python supports Functional and OOPs as well
  • Solving a few requirements by using C, Java and python
  • Machine language
  • Translator
  • Interpreter
  • Compiler
  • Python keywords (33)
  • Features of Python
  • Flavors of Python
  • Python versions
  • Programs
1. Python Introduction
1. What is Python?
  • Python is a general purpose and high-level programming language.
    • All companies are using a python programming language to develop the applications, testing and maintenance etc.
    • There are mainly two types of programming languages in this world,
      • High level
        • Human readable language.
        • Easy to understand
      • Low level
        • Machine-readable language like bits (1’s and 0’s form)
2. Where are all python is using at the application level?
To develop,
  • Standalone applications
    • An application which needs to install on every machine to work with that application.
  • Web applications
    • An application which follows client-server architecture.
    • The client is a program, which sends a request to the server.
    • The server is a program, mainly it can do three things,
      • Captures the request from the client
      • Process the request
      • Sends the response to the client
  • Database applications.
  • To process a huge amount of data.
    • Hadoop
    • Spark.
  • Machine learning.
  • Artificial Intelligence.
  • Data science.
  • Network servers.
  • IOT
  • Application scripting etc.
3. History of Python?
  • Python was created by Guido Van Rossum in the year of 1989.
  • It is open source software means we can download freely and customize the code as well.
4. Why the name was python?
  • A TV show Monty Python’s Flying Circus was very much a popular fun show in the 1970s.
  • So, Guido like this show and given this name to his programming language.
5. Python versions
  • The first version was released in Feb 20th, 1991.
  • Current version 3.6.x release Dec 23rd, 2016.
6. Python supports
  • Functional programming.
  • Object-oriented programming approach
  • Nowadays to fulfill the requirement both are required.
    • Python = Functional programming + Object-oriented programming
7. Which companies are using Python?
  • Currently, all companies are using python.
8. To solve any requirement,
  • Initial languages like C, Pascal or FORTRAN follows a functional approach.
  • C++, Java and dot net follows the object-oriented approach.
  • Python follows both functional and object-oriented approaches
Requirement 1       :        Java programs to print, Welcome to Java programming
Java program         :        HelloWorld.java


Requirement

Program prints Welcome to Java programming


output

Welcome to Java programming


class HelloWolrd
{
       public static void main (String args [])
       {
              System.out.println(“Welcome to Java programming”);
       }
}


Output

Welcome to Java programming

  • To write Java code at least you should write a single class.
C program             :        HelloWorld.c


Requirement

Program to print, Welcome to C programming


output

Welcome to C programming


#include<stdio.h>
{
void main ()
{
         printf(“Welcome to C programming”);
}
}


Output

Welcome to C programming

  • To write C code at least you should write a function
Python program     :        HelloWorld.py


Requirement

Program to print, Welcome to Python programming


output

Welcome to Python programming



              print(“Welcome to Python programming”)



Output

Welcome to Python programming

  • Above python program is a simple approach and more understandable even for nursery student also.
Requirement 2: Take two numbers and apply addition logic.

  • So, the above requirement we are going to see by using Java, C, and python
Java program                 :        Addition.java


Requirement

Program prints to add two number in Java


output

30


class Addition
{
             public static void main (String args[])
             {
                        int a = 10;
                        int b = 20;
                        System.out.println(a+b);
             }
}


Output

30

  • To write Java code at least you should write a single class to express logic.
  • Sometimes this makes program lengthy and consumes more time.
C program             :        Addition.c

Requirement

Program prints to add two number in Java


output

30


#include<stdio.h>
{
             void main()
             {
                         int a = 10;
                         int b = 20;

                         println(a+b);
             }
}


Output

30

  • To write C code at least you should write a function to express logic.
  • Sometimes this also makes program lengthy and consumes more time.
Python program     :        addition.py

Requirement

Program prints to add two number in Java


output

30





       a = 10
       b = 20

       print(a+b)


Internal representation




Output

30

  • Above python code is simple approach and more understandable even for non-techy also.
Make a note:
  • Semicolons are mandatory in C and Java programming languages.
  • In python, semicolons are not required.
Points to make a note
  • In many programming languages to write a program,
    • We need to create the structure with symbols in a certain order.
    • Next step is, we need to write logic.
    • In python we can see the very informative code by writing a simple way.
  • In Python programming a single line, which prints a short message.
  • It’s a very informative example of Python’s syntax.
9. Machine language
  • Representing the instructions and data in the form of bits (1’s and 0’s) is called machine code or machine language.
  • Example to add two numbers then the machine will convert these number into bits by division with 2.
         Ex      :        12 + 14 = 26
         2       |        12     
                  |        6       -        0       Reminder
                  |        3       -        0       Reminder
                  |        1       -        1       Reminder

    • 12 == 1100 Take the digits from bottom to top digits
         2       |        14
                  |        7       -        0       Reminder
                  |        3       -        1       Reminder
                  |        1       -        1       Reminder
    • 14 == 1110 Take the digits from bottom to top digits
  • Internally these bits values will be adding and generate the sum result as 26
1.9 Translator
  • A Translator is a program that converts any computer programs into machine code.
  • There is ‘n’ number of translators are existing but for us we need to understand 2 types of translators.
1. Interpreter
  • An interpreter is a program, it can convert the program by line by line.
2. Compiler
  • The compiler is a program converts the entire program in a single step.
10. Python Reserved words or keywords (33)
  • The words which are reserved to do a specific functionality is called reserved words are keywords.
  • In Python total 33 keywords are available 

Make a note:
  • All keywords in Python contains only alphabet symbols.
  • All keywords are in lowercase except 3 those are,
    • True
    • False
    • None
10.1 To see all these reserved words from the python shell
>>> import keyword
>>>keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Make a note:
  • Program implementing by using notepad and executing command prompt is best practice

Program       To see all python keywords
Name          demo1.py

                  import keyword
                  print(keyword.kwlist)

Output                

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']



Note:

  • keyword is a module in python
11. Features of python

1.Simple
  • Python syntax is very easy.
  • Developing and understanding of python is very easy than others.
2.Open source
  • We can download freely and customize the code as well
3.High-level language
  • There are two types of languages,
    • Low level
      • Machine code instructions, difficult to learn programmer.
    • High level
      • English words with syntax, the programmer can learn easily.
4. Dynamically typed
  • Dynamically type will be assigned to data.
5.Platform independent
  • Python programs are not dependent on any specific operating systems.
  • We can run on all operating systems happily.
6.Portable
  • If a program gives the same result on any platform then it is a portable program.
  • Python used to give the same result on any platform.
7.Procedure and object-oriented
  • Python supports both procedural and object-oriented features.
8. Huge library
  • Python has a big library to fulfill the requirements.
9. Database connectivity
  • Python provides interfaces to connect with all major databases like Oracle, MySQL
10. Batteries included
  • Python provides inbuilt libraries called batteries,
  • Some of them are below,
    • Boto
      • Amazon web services library
    • MySQL -connector-python
      • To connect with MySQL
    • NumPy       
      • To process arrays
    • Pandas       
      • powerful data structures for data analysis, time series and statistics
    • so many others…
12. Different flavors of python
  • Flavors of python refer to the different types of python compilers.
  • These are useful to integrate various programming languages.
1. CPython
  • Standard python compiler implemented in C language.
  • This is the python software being downloaded and used by the programmers directly.
2. Jython
  • Initially called as JPython, later renamed to Jython.
  • Designed to run on Java program.
3. IronPython
  • Designed for .NET framework.
4. PyPy
  • The main advantage of PyPy is performance will be improved because JIT compiler is available inside PVM.
5. RubyPython
  • This is a bridge between Ruby and python interpreters.
  • It encloses a python interpreter inside Ruby application.
6. AnacondaPython
  • Anaconda is a free and open source Python programming languages.
  • This is mainly for data science and machine learning related applications (large-scale data processing, predictive analytics, scientific computing).
  • This aims to simplify package management and deployment.
13. Python Versions
  • Python 1.0V introduced in Jan 1994
  • Python 2.0V introduced in October 2000
  • Python 3.0V introduced in December 2008.
  • The current version is 3.6
Make a note
  • Python 3 won't provide backward compatibility to Python2 i.e. there is no guarantee that Python2 programs will run in Python3.