PYTHON
BY Kannan Moudgalya
Presented By Priya Bisht
Course BCA (B)
Roll no. 1021639 (29)
Course Overview
Python is a general-purpose, high-level, remarkably powerful dynamic programming
language that is used in a wide variety of application domains. Python supports multiple
programming paradigms, including object-oriented, imperative and functional
programming styles.
FOSSEE Project, IIT Bombay promotes Python for scientific computing through various
activities like Python Textbook Companion, creation of spoken tutorials & courses like
SDES.
About the instructor
Prof Kannan Moudgalya
is a professor at the
Chemical Engg. Dept. of
IIT Bombay. He is a
LaTeXenthusiast and a
Kannan Moudgalya free and open source
software promoter. He
is the PI of the Spoken
Tutorial Project, IIT
Bombay.
Introduction to Python
A high-level programming language Open source and community driven
Standard Distribution includes many modules
Dynamic typed; Automatic Memory management
Basically interpreted, but source can be compiled
Easy to learn, yet powerful
Robust support for OO programming
Can integrate well with other languages
Most popular for Data Analytics
Compiling and Interpreting
Python Scripts
Code is interpreted
You see the result immediately on the shell window.
If a statement is in error, execution stops and error message is displayed.
The interpretation does not guarantee display of all errors. Errors are reported only when encountered,
it stops on the first error
A Code Sample (in IDLE)
x = 34 - 23 # A comment.
y = "Hello" # Another one.
z = 3.45
if z == 3.45 or y == "Hello":
x = x + 1 y = y + " World “ # String concat
print (x)
print (y)
Data in Python
Everything in Python is an object.
All objects in Python can be either mutable or immutable.
A mutable object can be changed after it is created, and an immutable object can’t.
Objects of built-in types like (int, float, bool, str, tuple, unicode) are immutable. Objects of
built-in types like (list, set, dict) are mutable
Basic Datatypes
Numbers
Strings
List
Tuple
Data structures
Set
Dictionary
Mutable / Immutable
Mutable: meaning you can change their content without
changing their identity
Immutable: are objects that can not be changed
Modules & Packages
Similar to libraries, called Modules
It can be used (referenced) in standard python code. Include the modules in the code.
Usually a File containing python code
Can define functions, classes, variables
Multiple modules make up a Package, usually a directory of python files.
Official packages are listed in a directory (PyPI) from where one can download / install
pip is the tool to download and install packages
Basic Datatypes
Integers (default for numbers) z = 5 / 2 # Answer 2.5, real division
Floats x = 3.456
Strings
Can use " " or ' ' to specify with "abc" == 'abc'
Unmatched can occur within the string: "matt's"
Use triple quotes for multi-line strings or strings than contain both ‘ and “ inside of them:
"""a'b"c"""
Data Structures
List : An unordered collection of data items
[35,23.15,"Hello", [2,3,'a','b']]
Can be used as a stack, queue
mutable
Tuple : An unordered collection of data items used to capture an entity’s data
(25, 5.5, "My Data", ['a','b'], (1,2,3)) immutable
Set : An unordered set of values ( no duplicates allowed)
{2,3,"hello"}
Dictionary : unordered collection of data items, each data item is a <key, value> pair
{'name': 'John', 1: [2, 4, 3]}
Standard Operators
Control Structures
if ..
if ..else
if .. elif
while
while .. else
for
break
continue
Functions & Classes
def fname (abc) : … … return(rval)
class cname(pclass): …
EXAMPLE:
def fact(x): """Returns the factorial of its argument, assumed to be a posint"""
if x == 0: return 1 return x * fact(x - 1)
print
print ("N fact(N)")
print ("---------")
for n in range(10):
print (n, " factorial is : ", fact(n))
Class definitions
Class ClassName :
< statement - 1 > ...
< statement - N >
must be executed
can be executed conditionally
creates new namespace
Class objects
[Link] references (plus module!):
class MyClass:
"A simple example class" i = 123
def f(self): return 'hello world‘
>>> MyClass.i
123
MyClass.f is method object
Files
Files are manipulated by creating a file object
f = open("[Link]", "r")
The file object then has new methods
print [Link]() # prints line from file
Files can be accessed to read or write
f = open("[Link]", "w")
[Link]("Important Output!")
Files are iterable objects, like lists
Error Capture
Check for type assignment errors, items not in a list, etc.
Try & Except try: a block of code that might have an error except: code to execute if an
error occurs in "try"
Allows for graceful failure – important for applications which use system resources
Popular Modules
Cryptograpgy
M2Crypto, OpenSSL
GUI
PyGTK, TKInter, PyQt
Networking
RPyC, HTTPLib2
Plotting
Matplotlib, plotly, SciPy
Scientific
Numpy, SciPy,
Threading
Threadpool
Web development
Django, web2py
Thankyou