Get Going with Python

History of Python 
Python was invented in Netherlands in 1989 by Guido Van Rossum and was publically released in the year 1991.
Python is named after the comic troupe Monty Python. Python was developed by Guido van Rossum in the late
eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the
Netherlands. Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68,
SmallTalk, Unix shell, and other scripting languages. Python is copyrighted. Like Perl, Python source code
is now available under the GNU General Public License (GPL).
Python is now maintained by a core development team at the institute, although Guido van Rossum still holds
a vital role in directing its progress. Python was created as scripting language for performing
administrative tasks. Python is object oriented, scalable and functional and is used vastly by IT sector by
companies like Google. 
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed
to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it
has fewer syntactical constructions than other languages.
Python is Interpreted: Python is processed at runtime by the interpreter. You do not need to compile your
program before executing it. This is similar to PERL and PHP.
Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter directly to
write your programs.
Python is Object-Oriented: Python supports Object-Oriented style or technique of programming that
encapsulates code within objects.
Python is a Beginner's Language: Python is a great language for the beginner-level programmers and supports
the development of a wide range of applications from simple text processing to WWW browsers to games.
Python Features 
Easy-to-Learn: Python has few keywords, simple structure, and a clearly defined syntax. This allows the
student to pick up the language quickly.
Easy-to-Read: Python code is more clearly defined and visible to the eyes.
Easy-to-Maintain: Python's source code is fairly easy-to-maintain.
A Broad Standard Library: Python's bulk of the library is very portable and cross-platform compatible on
UNIX, Windows, and Macintosh.
Interactive Mode: Python has support for an interactive mode which allows interactive testing and debugging
of snippets of code.
Portable: Python can run on a wide variety of hardware platforms and has the same interface on all
platforms.
Extendable: You can add low-level modules to the Python interpreter. These modules enable programmers to add
to or customize their tools to be more efficient.
Databases: Python provides interfaces to all major commercial databases.
GUI Programming: Python supports GUI applications that can be created and ported to many system calls,
libraries, and windows systems, such as Windows MFC, Macintosh, and the X Window system of Unix.
Scalable: Python provides a better structure and support for large programs than shell scripting.
Additional Features of Python
Apart from the above-mentioned features, Python has a big list of good features, few are listed below: 
It supports functional and structured programming methods as well as OOP. 
It can be used as a scripting language or can be compiled to byte-code for building large applications. 
It provides very high-level dynamic data types and supports dynamic type checking. It supports automatic
garbage collection. 
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
Programming Basic Terminologies 
A Program is collection of statements that have been crafted to perform certain task. 
Input: Get data from the “outside world”. This might be reading data from keyboard, a file, or even some kind
of sensor like a microphone or GPS. 
Output: Display the results of the program on a screen or store them in a file or perhaps write them to a
device like a speaker to play music or speak text. 
Sequential Execution: Perform statements one after another in the order they are encountered in the script.
Conditional Execution: Check for certain conditions and then execute or skip a sequence of statements.
Repeated Execution: Perform some set of statements repeatedly, usually with some variation. 
Reuse: Write a set of instructions once and give them a name and then reuse those instructions as needed
throughout your program. 
Bug: An error in a program 
Compile: To translate a program written in a high-level language into a low-level language all at once, in
preparation for later execution.
High-Level Language: A programming language like Python that is designed to be easy for humans to read and
write. 
Interactive Mode: A way of using the Python interpreter by typing commands and expressions at the prompt.
Interpret: To execute a program in a high-level language by translating it one line at a time. 
Low-Level Language: A programming language that is designed to be easy for a computer to execute; also
called “machine code” or “assembly language”. 
Machine Code: The lowest-level language for software, which is the language that is directly executed by the
central processing unit (CPU). 
Parse: To examine a program and analyze the syntactic structure. Portability: A property of a program that
can run on more than one kind of computer. 
Print Function: An instruction that causes the Python interpreter to display a value on the screen. 
Problem Solving: The process of formulating a problem, finding a solution, and expressing the solution.
Program: A set of instructions that specifies a computation. 
Prompt: When a program displays a message and pauses for the user to type some input to the program.
Programming Language Translators 
Programming language translators fall into two general categories:
Interpreter: An interpreter reads the source code of the program as written by the programmer, parses the
source code, and interprets the instructions on the fly. Python is an interpreter and when we are running
Python interactively, we can type a line of Python (a sentence) and Python processes it immediately and is
ready for us to type another line of Python.
Compiler: A compiler needs to be handed the entire program in a file, and then it runs a process to translate
the high-level source code into machine language and then the compiler puts the resulting machine language
into a file for later execution.
Types of Errors in Python 
Programming Syntax Errors: A syntax error means that you have violated the “grammar” rules of Python. Python
does its best to point right at the line and character where it noticed it was confused. The only tricky bit
of syntax errors is that sometimes the mistake that needs fixing is actually earlier in the program than
where Python noticed it was confused. So the line and character that Python indicates in a syntax error may
just be a starting point for your investigation. Syntax errors occur when you make a typo. Not ending an if
statement with the colon is an example of a syntax error, as is misspelling a Python keyword (e.g. using
prit instead of print). Syntax error usually appear at compile time and are reported by the interpreter.
Logic Errors: A logic error is when your program has good syntax but there is a mistake in the order of the
statements or perhaps a mistake in how the statements relate to one another. A good example of a logic error
might be, “take a drink from your water bottle, put it in your backpack, walk to the library, and then put
the top back on the bottle.” Consider the below example,

x = float(input('Enter a number: '))                              
y = float(input('Enter a number: '))                             
z = x+y/2
print('The average of the two numbers you have entered is:',z)

The example above should calculate the average of the two numbers the user enters. But, because of the order
of operations in arithmetic (the division is evaluated before addition) the program will not give the right
answer.
Semantic Errors: A semantic error is when your description of the steps to take is syntactically perfect and
in the right order, but there is simply a mistake in the program. The program is perfectly correct but it
does not do what you intended for it to do. A simple example would be if you were giving a person directions
to a restaurant and said, “...when you reach the intersection with the gas station, turn left and go one
mile and the restaurant is a red building on your left.” Your friend is very late and calls you to tell you
that they are on a farm and walking around behind a barn, with no sign of a restaurant. Then you say “did
you turn left or right at the gas station?” and they say, “I followed your directions perfectly, I have them
written down, it says turn left and go one mile at the gas station.” Then you say, “I am very sorry, because
while my instructions were syntactically correct, they sadly contained a small but undetected semantic
error.” Consider the below examples,

total = 0
for x in xrange(1,11):
     total = x       # Sums up x

The actual intent was to use += for total.
Sums up numbers 1-10 inclusive
total = 0
for x in xrange(1,10):
total += x

Of course (x)range doesn't include the end index, so in both cases the program would compile and execute,
but neither would give the programmers intended result.
Running Python
Interactive Mode Programming
The Python interpreter is a program that reads and executes Python code. Depending on your environment, you
might start the interpreter by clicking on an icon, or by typing python on a command line. When it starts,
you should see output like this:
Interactive Mode
The first two lines contain information about the interpreter and the operating system it’s running on, so it
might be different for you. But you should check that the version number, which is 3.4.0 in this example,
begins with 3, which indicates that you are running Python 3. If it begins with 2, you are running Python 2.
The last line is a prompt that indicates that the interpreter is ready for you to enter code. If you type a
line of code and hit Enter, the interpreter displays the result as shown below:
Interactive Mode Programming
Script Mode Programming
Invoking the interpreter with a script parameter begins execution of the script and continues until the
script is finished. When the script is finished, the interpreter is no longer active. Python files have
extension .py. Consider the following source code in a sample.py file:
print(“Hello Python!”)
Running the script file:
Script Mode Programming
Character Set in Python
Alphabets: Uppercase --> A - Z Lowercase --> a - z
Digits: 0 - 9
Special Characters: ~ ` ! @ # $ % ^ & * ( ) _ - + = { [ } ] | \ : ; " ' < , > . ? /
Python Identifiers
A Python identifier is a name used to identify a variable, function, class, module, or other object. An
identifier starts with a letter A to Z or a to z, or an underscore ( _ ) followed by zero or more letters,
underscores and digits (0 to 9). Python does not allow punctuation characters such as @, $, and % within
identifiers. Python is a case sensitive programming language.
The naming conventions used for Python identifiers are:
• Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
• Starting an identifier with a single leading underscore indicates that the identifier is private.
• Starting an identifier with two leading underscores indicates a strongly private identifier.
• If the identifier also ends with two trailing underscores, the identifier is a language-defined special
name.
For Example: num, _num, num1, num_2 etc.
Keywords in Python
Keywords are sequenced collection of characters from character set which have a special predefined meaning
in the context of the language.
Keywords in Python are:
and        as       assert       break       class       continue       def       del       elif       else
except     False    finally      for         from        global         if        import    in         is
lamda      None     nonlocal     not         or          pass           raise     return    True       try
while      with     yield  
Multi-Line Statements
Statements in Python end with a new line. Python allows the use of the line continuation character ( \ ) to
denote that the line should continue.
For Example:
total = item_one + \
        item_two + \
        item_three
Quotation in Python
Python accepts single ( ' ), double ( " ) and triple ( ''' or """ ) quotes to denote string literals, as
long as the same type of quote starts and ends the string.
The triple quotes are used to span the string across multiple lines.
For Example:
word = ’Hi’
sentence = ”Python your my new crush”
paragraph = ”””Hi hope your finding easy to get in my track of learning Python,
            In case of any difficulty let me know before I take a bit pace.”””
Comments in Python
A hash sign ( # ) is used to indicate a comment in python. All characters after the # and up to the end of
the physical line are part of the comment and the Python interpreter ignores them. Hash sign inside a string
literal is not consider as a comment.
Code Snippet:
#Program to illustrate usage of comment in python
print("Comment Successfully Illustrated”)
Output:
Comment Successfully Illustrated
Value and Types
A value is one of the basic things a program works with, like a letter or a number.
The values belong to different types: 2 is an integer, and “Hello, World!” is a string.
Data Types in Python
Value and Type Illustration
Note: Semantic Error in display of values
On typing large integers in python if commas are used to group three digits, as in 1,000,000. This is not a
legal integer in python but it is legal.
Python interprets 1,000,000 as a comma separated sequence of integers, which it prints with spaces between.
Python interprets 1,000,000 as a comma separated sequence of integers, which it prints with spaces between.