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
- Supports multiple Programming paradigms.
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.
- Extensive Libraries
Python has a large and a comprehensive Standard Library which can help you do various things involving regular expressions, documentation generation, unit testing, threading, databases, web browsers, CGI, ftp, email.
Besides the standard library, there are various other high-quality libraries such as the Python Imaging Library
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.pyon 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:
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.
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
6) Cannot use especial symbols like !, @, #, $, % in a identifier.
eg:
myClass
, var_1,
print_this_to_screen
2)
class names start with a UPPERCASE letter while all other start with a lowercaseeg: 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
>>> 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
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.
eg: total = item_one + \
item_two + \
item_three
- The semicolon ( ; ) allows multiple statements on the single line
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