Introduction To Python: A Readable, Dynamic, Pleasant, Flexible, Fast and Powerful Language
Introduction To Python: A Readable, Dynamic, Pleasant, Flexible, Fast and Powerful Language
Introduction To Python: A Readable, Dynamic, Pleasant, Flexible, Fast and Powerful Language
8/22/201
7
Python Overview
By Ripal 8/22/201
Ranpara 7
By Ripal 8/22/201
Ranpara 7
Job Trend
Per the indeed.com, percentage growth of Python is 500 times more than it’s peer
Languages.
http://www.indeed.com/jobtrends?q=Perl%2C+.Net%2C+Python%2Cjava&l=&relative=1
8/22/2017
Job In Big Data space
Source: http://www.forbes.com/sites/louiscolumbus/2014/12/29/where-big-data-jobs-will-be-in-
2015/
8/22/2017
What is Scripting Language?
8/22/201
7
Interpreters Versus
Compilers
8/22/201
7
• Create source file using text edit
• Use compiler to syntax check and convert source file into binary
• Use linker to turn binary files into executable format
• Run the resulting executable format file in the operating system.
By Ripal 8/22/201
Ranpara 7
• The biggest difference between interpreted code and compiled code is
that
an interpreted application need not be “complete.”
• You can test it in bits and pieces until you are satisfied with the results
and put them all together later for the end user to use.
By Ripal 8/22/201
Ranpara 7
Python Features
8/22/201
7
More Features ..
8/22/201
7
Why
Python
Easy to read Python scripts have clear syntax, simple structure and very few protocols
to remember before programming.
Easy to Maintain Python code is easily to write and debug. Python's success is that its source code is
fairly
easy-to-maintain.
Portable Python can run on a wide variety of Operating systems and platforms and providing the
similar interface on all platforms.
Broad Standard Libraries Python comes with many prebuilt libraries apx. 21K
High Level programming Python is intended to make complex programming simpler. Python deals with
memory addresses, garbage collection etc internally.
Interactive Python provide an interactive shell to test the things before implementation. It
provide the user the direct interface with Python.
Database Interfaces Python provides interfaces to all major commercial databases. These interfaces
are pretty easy to use.
GUI programming Python supports GUI applications and has framework for Web. Interface to
tkinter, WXPython, DJango in Python make it . 8/22/2017
History of Python
8/22/201
7
Python time line
By Ripal 8/22/201
Ranpara 7
Key Changes in Python 3.0
Python 2's print statement has been replaced by the print() function.
Old: New:
Old: New:
Old: New:
The division of two integers returns a float instead of an integer. "//" can be
used to have the "old" behavior.
8/22/201
7
Python Syntax
By Ripal 8/22/201
Ranpara 7
8/22/201
7
Python Data Types
By Ripal 8/22/201
Ranpara 7
Numbers
Numbers are Immutable objects in Python that cannot change their values.
There are three built-in data types for numbers in Python3:
• Integer (int)
• Floating-point numbers (float)
• Complex numbers: <real part> + <imaginary part>j (not used much in Python programming)
String indexes starting at 0 in the beginning of the string and working their way from -1
at the end.
8/22/201
7
Strings
String Formatting
8/22/201
7
Strings
Common String Methods
Method Description
str.count(sub, beg= Counts how many times sub occurs in string or in a substring of string if
0,end=len(str)) starting index beg and ending index end are given.
str.isalpha() Returns True if string has at least 1 character and all characters are
alphanumeric and False otherwise.
str.isdigit() Returns True if string contains only digits and False otherwise.
str.lower() Converts all uppercase letters in string to lowercase.
str.upper() Converts lowercase letters in string to uppercase.
str.replace(old, new) Replaces all occurrences of old in string with new.
access
slice
update
8/22/201
delete 7
Lists
Lists can have sublists as elements and these sublists may contain other sublists
as well.
8/22/201
7
Lists
Common List Methods
Method Description
list.append(obj) Appends object obj to list
list.insert(index, obj) Inserts object obj into list at offset index
list.count(obj) Returns count of how many times obj occurs in list
list.index(obj) Returns the lowest index in list that obj appears
list.remove(obj) Removes object obj from list
list.reverse() Reverses objects of list in place
list.sort() Sorts objects of list in place
List Comprehensions
Each list comprehension consists of an expression followed by a for clause.
List comprehension
8/22/201
7
Python Reserved Words
A keyword is one that means something to the language. In other words, you can’t
use a reserved word as the name of a variable, a function, a class, or a module. All
the Python keywords contain lowercase letters only.
Python Tuples are Immutable objects that cannot be changed once they have
been
created.
A tuple contains items separated by commas and enclosed in parentheses instead of square
access
brackets.
No update
Python's dictionaries are kind of hash table type which consist of key-value pairs
of unordered elements.
• Keys : must be immutable data types ,usually numbers or strings.
• Values : can be any arbitrary Python object.
Python Dictionaries are mutable objects that can change their values.
A dictionary is enclosed by curly braces ({ }), the items are separated by commas,
and each key is separated from its value by a colon (:).
Dictionary’s values can be assigned and accessed using square braces ([]) with a
key to obtain its value.
8/22/201
7
Dictionary
This example shows how to access, update and delete dictionary elements:
The output:
8/22/201
7
Dictionary
By Ripal 8/22/201
Ranpara 7
In Python, True and False are Boolean objects of class 'bool' and they are immutable.
Python assumes any non-zero and non-null values as True, otherwise it is False value.
Python does not provide switch or case statements as in other languages.
Syntax:
if Statement if..else Statement if..elif..else Statement
Example:
8/22/201
7
Conditionals
8/22/201
7
Loops
8/22/201
7
Loops
Loop Control Statements
break :Terminates the statement and transfers execution to the statement
loop
immediately following the loop.
continue :Causes the loop to skip the remainder of its body and immediately retest
its
condition prior to reiterating.
pass :Used when a statement is required syntactically but you do not want
any command or code to execute.
8/22/201
7
Python Functions
By Ripal 8/22/201
Ranpara 7
A function is a block of organized, reusable code that is used to perform a single, related action.
Functions provide better modularity for your application and a high degree of code reusing.
Defining a Function
• Function blocks begin with the keyword def followed by the function name and parentheses
( ( ) ).
• Any input parameters or arguments should be placed within these parentheses. You can also
define parameters inside these parentheses.
• The first statement of a function can be an optional statement - the documentation string of
the function or docstring.
• The code block within every function starts with a colon (:) and is indented.
• The statement return [expression] exits a function, optionally passing back an expression to
the caller. A return statement with no arguments is the same as return None.
8/22/201
7
Functions
Function Syntax
Function Arguments
You can call a function by using any of the following types of arguments:
• Required arguments: the arguments passed to the function in correct
positional order.
• Keyword arguments: the function call identifies the arguments by the
parameter names.
• Default arguments: the argument has a default value in the function
declaration used when the value is not provided in the function call.
8/22/201
7
Functions
• Variable-length arguments: This used when you need to process unspecified additional
arguments. An asterisk (*) is placed before the variable name in the function declaration.
8/22/201
7
Python File Handling
By Ripal 8/22/201
Ranpara 7
8/22/201
7
File Handling
8/22/201
7
Python Exception Handling
By Ripal 8/22/201
Ranpara 7
8/22/20 1
7
By Ripal
EXCEPTION NAME DESCRIPTION
Exception Base class for all exceptions
Raised when the next() method of an
StopIteration
iterator does not point to any object.
SystemExit Raised by the sys.exit() function.
Base class for all built-in exceptions except
StandardError
StopIteration and SystemExit.
Base class for all errors that occur for
ArithmeticError
numeric
calculation.
Raised when a calculation exceeds
OverflowError
maximum limit for a numeric type.
FloatingPointError Raised when a floating point calculation fails.
Raised when division or modulo by zero
ZeroDivisionError
takes place for all numeric types.
Raised when there is no input from either the raw_input() or input() function and
EOFError
the end of file is reached.
ImportError Raised when an import statement fails.
KeyboardInter
Raised when the user interrupts program execution, usually by pressing Ctrl+c.
rupt
LookupError Base class for all lookup errors.
IndexErro Raised when an index is not found in a sequence.
r Raised when the specified key is not found in the dictionary.
KeyError
NameError Raised when an identifier is not found in the local or global namespace.
UnboundLoc
Raised when trying to access a local variable in a function or method but no value
al Error
has
EnvironmentE
been assigned to it.
rror
Base class for all exceptions that occur outside the Python environment.
By Ripal 8/22/201
Ranpara 7
Raised when an input/ output operation fails, such as the print statement or the
IOError
open() function when trying to open a file that does not exist.
IOError
Raised for operating system-related errors.
SyntaxError Raised when there is an error in Python syntax.
IndentationError Raised when indentation is not specified properly.
Raised when the interpreter finds an internal problem, but when this
SystemError
error is encountered the Python interpreter does not exit.
Raised when Python interpreter is quit by using the sys.exit() function. If not
SystemExit
handled in the code, causes the interpreter to exit.
Raised when an operation or function is attempted that is invalid for the specified
TypeError
data type.
Raised when the built-in function for a data type has the valid type of arguments,
ValueError
but the arguments have invalid values specified.
RuntimeError Raised when a generated error does not fall into any category.
By Ripal 8/22/201
Ranpara 7
A module is a file consisting of Python code that can define functions, classes and
variables.
A module allows you to organize your code by grouping related code which makes the
code easier to understand and use.
You can use any Python source file as a module by executing an import statement
Python's from statement lets you import specific attributes from a module into the
current namespace.
import * statement can be used to import all names from a module into the current
namespace
8/22/201
7
By Ripal
Python Object Oriented
By Ripal 8/22/201
Ranpara 7
Class variable
Class constructor
Output
8/22/201
7
Python Classes
Data Hiding You need to name attributes with a double underscore prefix, and those
attributes then are not be directly visible to outsiders.
8/22/201
7
Class Inheritance
8/22/201
7
Python vs. Java
Code Examples
By Ripal 8/22/201
Ranpara 7
Hello World
Java
Python
String Operations
Java
Python
8/22/201
7
Python vs. Java
Collections
Java
Python
8/22/201
7
Python vs. Java
Class and Inheritance
Java
Python
8/22/201
7
Python Useful Tools
By Ripal 8/22/201
Ranpara 7
Python IDEs
• Vim
• Eclipse with PyDev
• Sublime Text
• Emacs
• Komodo Edit
• PyCharm
8/22/201
7
Useful Tools
8/22/201
7
Who Uses Python?
By Ripal 8/22/201
Ranpara 7
8/22/201
7
References
1 Python-course.eu, 'Python3 Tutorial: Python Online Course', 2015. [Online]. Available:
http://www.python-course.eu/python3_course.php.
5Sebastianraschka.com, 'Python 2.7.x and Python 3.x key differences', 2015. [Online]. Available:
http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html.
6Programcreek.com, 'Java vs. Python: Why Python can be more productive?', 2015. [Online].
Available:
http://www.programcreek.com/2012/04/java-vs-python-why-python-can-be-more- productive
/
.
References
7Stsdas.stsci.edu, 'A Quick Tour of Python', 2015. [Online]. Available:
http://stsdas.stsci.edu/pyraf/python_quick_tour.html.
9Pymbook.readthedocs.org, 'Welcome to Python for you and me — Python for you and me
0.3.alpha1 documentation', 2015. [Online]. Available:
http://pymbook.readthedocs.org/en/latest/index.html.
10Code Geekz, '10 Best Python IDE for Developers | Code Geekz', 2014. [Online]. Available:
https://codegeekz.com/best-python-ide-for-developers/.
11K. Radhakrishnan, 'Top 10 Python Powered Web Frameworks For Developers', Toppersworld.com,
2014. [Online]. Available:
http://toppersworld.com/top-10-python-powered-web- frameworks-for-developers/.
8/22/201
7