Python Language: Introdction
Python Language: Introdction
INTRODCTION
What is Scripting Language?
• The “program” has an executable form that the computer can use directly to
execute the instructions.
• The same program in its human-readable source code form, from which
executable
programs are derived (e.g., compiled)
▪ Interactive: You can use a Python prompt and interact with the
interpreter
directly to write your programs.
• 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.
Python Features
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 .
History of Python
Old New
: :
Syntax
Basic Syntax
▪ The declarationhappens
automatically when you assign
a value to a variable.
▪ Python accepts single ('), double (") and triple (''' or """) quotes to denote
string literals.
▪ String indexes starting at 0 in the beginning of the string and working their way
from -1
at the end.
Strings
▪ String Formatting
▪ Python Lists are mutable objects that can change their values.
▪ A list contains items separated by commas and enclosed within square
brackets.
▪ List indexes like strings starting at 0 in the beginning of the list and working their
way from -1 at the end.
□ delete
Lists
▪ Lists can have sublists as elements and these sublists may contain other
sublists
as well.
▪ Common List
Functions Function Description
cmp(list1, list2) Compares elements of both lists.
len(list) Gives the total length of the list.
max(list) Returns item from the list with max value.
min(list) Returns item from the list with min value.
list(tuple) Converts a tuple into list.
Lists
Method Description
▪ Common List list.append(obj) Appends object obj to list
Methods
list.insert(index, Inserts object obj into list at offset index
obj)
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
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 changed once they
be have been
created.
▪ Tuples are faster than lists and protect your data against accidental changes to
these data.
▪ The rules for tuple indices are the same as for lists and they have the same
operations,
functions as well.
Hash Table
▪ The
output:
Dictionary
□
Loops
▪ The For
Loop
▪ The while
Loop
Loops
Loop Control
S break
tatements
:Terminates statement and execution to the
▪ transfers
the
iomompediately following
lloop. statement
the
▪ continue:Causes the loop to skip the remainder of its body and immediately
retest its
condition prior to
reiterating.
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.
Functions
▪ Function
Syntax
▪ Function
Argume
nts
You can call a function by using any of the following types of
arguments:
• “a” opens a file for appending. If the file does not exist, it creates a
new file
for writing.
▪ Closing a file fileObject.close()
The close() method flushes any unwritten information and closes the file
object.
File Handling
fileObject.read([count])
▪ Reading a file
• The read() method reads the whole file at once.
• The readline() method reads one line each time from
the file.
Raised when there is no input from either the raw_input() or input() function
EOFError
and 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
Environment
E rror been assigned to it.
Base class for all exceptions that occur outside the Python environment.
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.
Raised for operating system-related errors.
IOError
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
TypeError
specified data type.
Raised when the built-in function for a data type has the valid type of
ValueError arguments, but the arguments have invalid values specified.
RuntimeError Raised when a generated error does not fall into any category.
▪ 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 Pyt hon source file as a module by ecuting an import
statement ex
▪ 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
Python Object
Oriented
Python Classes
□ Class variable
□Class
constructor
Output □
Python Classes
▪ Hello
World
Ja Python
va
▪ String
Operations
Python
Java
Python vs. Java
▪ Collectio
ns
Java Python
Python vs.
▪ Class and Java
Inheritance
Java
Pytho
n
□
Python Useful
Tools
Useful Tools
▪ Python IDEs
•Vim
•Eclipse with
PyDev
•Sublime
Text
•Emacs
•Komodo
Edit
Useful Tools
▪ Python Web
Frameworks
•Django
•Flask
•Pylons
•Pyrami
d
•Turbo
Who Uses
Python?
Organizations Use Python
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-
Referenc
7Stsdas.stsci.edu,
'A Quick Tour of
Available: http://stsdas.stsci.edu/pyraf/python_quick_tour.html.
Pytheons,' 2015.
8Lynda.com - A LinkedIn Company, 'Python 3 Essential Training | Lynda.com Training',
2015. [Online]. Available: [Online].
http://www.lynda.com/Python-3-tutorials/essential-training/62226-2.html.
10Code Geekz, '10 Best Python IDE for Developers | Code Geekz', 2014. [Online].
Available: https://codegeekz.com/best-python-ide-for-developers/.