Pages

Sunday, June 5, 2016

Python

What is Python?


It is a widely used

               - High Level           =>   has a strong abstraction from the details of the  computer making the  process of developing the program more simpler and understandable

               - General Purpose  =>  designed for writing software in a wide variety of application domains

               - Interpreted            =>  performs the instructions written without previously compiling them  into machine language/ directly executes

               - Dynamic               => many common programming behaviors are executed at Runtime. These behaviors could include extension of the program, by adding  new code, by extending objects and definitions, or by modifying the type  system

Programming Language.


Some features of Python

  • Easy to learn, read and maintain

                             Python is simple and has extraordinary simple syntax. This enables the programmers to express concepts in fewer lines of codes than many other languages like C++, Java.  

                             Reading python is also almost similar to English. This pseudo-code  nature of python allows to concentrate on the solution to the problem rather than on the syntax.

      
  •  It is Open-Source and Portable
                                 Since Python is Open-Source,  it is ported to many platforms. All the Python programs will work no any  platform without requiring any change unless any  system dependent features are avoided.

  •  Supports multiple Programming paradigms.
                                 Program paradigm is a way to classify programming languages depending on the concepts and abstractions used to represent the elements of a program such as objects, functions, variables and constraints.

                                 Python supported programming paradigms are object oriented, imperative and functional programming /procedural  styles.In procedure-oriented style, the program is built around procedures or functions which are nothing but reusable pieces of programs. In object-oriented languages, the program is built around objects which combine data and functionality.
                               
  • Embeddable & Extensible

                            Python can be embedded in C/C++ programs to give scripting abilities to the users of the program.

                           At the same time, piece of code written in C can be combined with Python code. 
     

Python is indeed an exciting and powerful language.


 It has the right combination of performance and features that makes writing programs in Python both fun and easy.

History of Python

  •  Was implemented by Guido van Rossum in Netherland in 1989

  • He played the central role in deciding the direction of python and was given the title  benevolent dictator for life  by python community
  • Python 2.0 was released on 16 October 2000
  • Python 3.0 was released on 3 December 2008 after a long period of testing.This is a a major, backwards-incompatible release


Running Python 

  instance of a simple python program is shown below implemented with different running methods

1) On interactive Interpreter
>>> print ("Hello, Python!")


1) Using Script
           Copy the below lines and save the file as test.py. 


# First comment
print ("Hello, Python!")
            Then run it on terminal as 


On Linux
$ python test.py 
on Windows
C:\Python34>Python test.py

          There is another way to execute a python script. According to this method include the path of the currently running python interpreter as the top most line in the file as shown below.


#!/usr/bin/python3

print ("Hello, Python!")

Then execute it as below



$ chmod +x test.py     # This is to make file executable
$./test.py


 Python Basics


Keywords


  •          Keywords are reserved words in python.
  •              They cannot be used as variables
  •              Case Sensitive   

Identifiers
  •           Identifiers are the names given to functions, classes, variables.
  •           Some rules in writing Identifiers
                              1) Identifiers are a combination of letters in lowercase or UPPERCASE or digits                                       or an Underscore(_)
                                                            eg: myClassvar_1, print_this_to_screen

              2) class names start with a UPPERCASE letter while all other start with a                                                 lowercase
                                                       eg: class Employee:
                               def displayEmployee(self):

                             3) Starting the identifier with a leading underscore means it is a private
                                                       eg:  _foo => private variable
def  _get_errors(self):  => private method

                             4) Starting the identifier with two leading underscores means it is strongly private
                             
http://igorsobreira.com/2010/09/16/difference-between-one-underline-and-two-underlines-in-python.html

   eg: 1)  The main goal here is to avoid the method to be over-ridden by a subclass.

class A(object):
 def __method(self):  
        print "I'm a method in A"
def method(self):
    self.__method()
a = A()
a.method()

     2)  __foo => the interpreter replaces this with _classname__foo to ensure that the name will not overlap with a simmilar name in another class
       

                             5) Ending the identifier with two trailing underscores means it is a language-                                         defined  special name

eg:  is typically reserved for builtin methods or variables
def __init__(self, n):
    self.n = n


                             6) Cannot use especial symbols like !, @, #, $, % in a identifier.

>>> a@ = 0
  File "<interactive input>", line 1
    a@ = 0
     ^
SyntaxError: invalid syntax

                             7) Keywords cannot be used as Identifiers

>>> global = 1
  File "<interactive input>", line 1
    global = 1
           ^
SyntaxError: invalid syntax


Statements


  • A statement is an instruction that the Python interpreter can execute.
  • 2 types
              1) print statements => produce a result
              2) assignment statement  => no result output

  • A script usually contains a sequence of statements.

              eg:      print 1
                     x = 2
                     print x
 
  • Statements in Python typically end with a new line.
  • Line can be continued using line continuation character (\) 
  • the line continuation character (\) to denote that the line should continue.
          Multi-Line Statements
                     eg:   total = item_one + \
                             item_two + \
                             item_three
           
  • The semicolon ( ; ) allows multiple statements on the single line 
          Multiple Statements on a single Line
                
                   eg:   import sys; x = 'foo'; sys.stdout.write(x + '\n')



Quotations & Comments

legal quotations
 
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
 

comments

 
#!/usr/bin/python

# First comment
print ("Hello, Python!") # second comment

blank line

A line containing only whitespace, possibly with a comment
 python totally ignores it

 Data Types



 





Operators

Expect  more detail info......

No comments:

Post a Comment