python workshop. lug maniapl
DESCRIPTION
Slides for Python Workshop, LUG ManipalTRANSCRIPT
![Page 2: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/2.jpg)
![Page 3: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/3.jpg)
What We Will Learn● History, Features and Basic Details● Language Basics● Control flow● Functions● Modules● Data Structures● File I/O
![Page 4: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/4.jpg)
![Page 5: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/5.jpg)
What is Python?● Python is a general purpose, object oriented, high level, interpreted language
● Developed in early 90's by Guido Van Rossum
● Its Simple, Portable, Open Source and Powerful
![Page 6: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/6.jpg)
Where is it used?● Google uses it for its web crawler and search engine
● Youtube is based on Python● Disney uses it in Panda3d● Bittorrent is implemented in Python● Used extensively from Space Shuttles to Mobile Phones
![Page 7: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/7.jpg)
Diff from C/C++/Java● No Pointers (similar to java)● No prior Compilation to ByteCode(?), directly Interpreted
● Includes Garbage Collector(?)● Can be used in Procedure(?)/ Object(?) Oriented approach/style
● Very good for scripting
![Page 8: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/8.jpg)
Versions Of Python● What Do You mean By Versions??
Major/Important Versions Currently● Python 2.5● Python 2.6● Python 3.0
So what will we use??
![Page 9: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/9.jpg)
Why 2.5/2.6 and !3.0???● Available on a Large Number of Systems
● Major Library's and Extensions available for 2.5/2.6 not 3.0
● <<<<MORE>>>>
![Page 10: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/10.jpg)
Python InterpreterInteractive session
● What is it?● Their Importance● How to exit an Interactive session quit() or
Ctrl + D on Unix-like Systems
Ctrl + Z on windows
![Page 11: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/11.jpg)
Language Basics
![Page 12: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/12.jpg)
Indentation● Indentation is very important● There are no begin/end delimiters● Physical lines & Logical lines● Joining physical lines by /● Use # for comments
![Page 13: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/13.jpg)
Data Types● Integer Numbers –> (dec) 1, 44; (oct) 01, 022;(hex) 0x1, 0x23; (long) 1L, 344343L
● Floating Point -> 0.,0.0,323.23,2e3,23e32
● Complex numbers –> j = (-1)1/2
10+10j, -1+5j, 5-6j
![Page 14: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/14.jpg)
Strings● Strings can be single (',”) or triple (''',”””) quoted
● Character escaping by \● Escape sequence - \\, \', \”, \n, \t
![Page 15: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/15.jpg)
Tuple● It is an Immutable(?) ordered sequence of items(?)
● Assigned-> a = (1122,1212,1212)● Using tuples –> can be used as a constant array (but are much more)
● Data can be accessed similar to an array -> a=(132,3232,323)
a[1] or a[3]
![Page 16: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/16.jpg)
Lists● List is a mutable ordered sequence of items (similar to tuple)
● Assigned-> a = [121,121212,34367]● Using Lists -> simplest use is a arrays (but again are much more)
● Data can be accessed similar to an array -> a=[132,3232,323]
a[1] or a[3]
![Page 17: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/17.jpg)
Dictionaries● Dictionaries are containers, which store items in a key/value pair(?)
● Assigned -> d ={'x':24,'y':33}● Using Dict -> They are used at a lot of places (covered later)
● Data can be accessed by using the key ->d['x']
![Page 18: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/18.jpg)
Variables● There is no prior declaration needed● Variables are the references(?) to the allocated memory(?)
● Variables can refer to any data type (like Tuple, List,Dictionary, Int, String, Complex)
● References are share● List,Dict etc are always shared
![Page 19: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/19.jpg)
Index and slices● String, List, Tuple, etc can be sliced to get a part of them
● Index -> similar to array index, it refers to 1 position of data
● Slices-> gives the data in the range● Example ->
a=”LUG Manipal”a[:3] a[4:11] a[4:] a[-7:] a[:-8] a[:11:2]
![Page 20: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/20.jpg)
Control Flow
![Page 21: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/21.jpg)
print● Print is a simple statement for giving output similar to C's printf function
● Can be used to output to Console or a file
● Use -> print “Hello World”
![Page 22: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/22.jpg)
input● Use raw_input() to take a string input from the user
● Used as
<var> = raw_input(“Enter a String: “)● Input() in used to take a input without specifying the type
![Page 23: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/23.jpg)
if● If is a conditional statement, for simple “If then else“ clause in English
● Header lines(?) are always concluded with a “ : “ followed by intended block of statements
● Optionally it can be followed by an “else if” clause known as “elif” in python
![Page 24: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/24.jpg)
If (con)● Example->● if <condition>:
Statement 1Statement 2
elif <condition>:Statements
else:statements
![Page 25: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/25.jpg)
while● While statement is used for repeatedly executing a block of code till the condition is true, also has an optional else clause
● Use wildly for infinite loop
![Page 26: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/26.jpg)
While (con)● Example ->● While <condition>:
statements
else:statements
![Page 27: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/27.jpg)
for● It is a sequence iterator(?)● It works on Strings, lists, tuples, etc
● Example->● For <target> in <iterable>:
statements
![Page 28: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/28.jpg)
range/xrange● They are used to generate and return integer sequence(?)
● Range(5) -> [0,1,2,3,4]● Range(1,5) -> [1,2,3,4]● Range(0,8,2) -> [0,2,4,6]● Xrange is used as a memory efficient(?) alternative for range
![Page 29: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/29.jpg)
break● Used to terminate a loop● If nested(?) it terminates the inner most loop
● Practically used for conditional loop termination with an if statement
![Page 30: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/30.jpg)
continue● Terminates the current iteration and executes next
● Practically used for conditional statements termination with an if statement
![Page 31: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/31.jpg)
Some Helpful Functions● Dir()● Help()
![Page 32: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/32.jpg)
Functions
![Page 33: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/33.jpg)
What are functions?● A Function is a group of statements that execute on request
● In Python Functions are Objects● Defining a function ->
def name(parameters):statement(s)
![Page 34: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/34.jpg)
Parameters● Types of parameters
Mandatory ParametersOptional parameters
● Defaults values● Be careful when default value is a mutable object
![Page 35: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/35.jpg)
● Def a(x,y=[]):y.append(x)print y
print a(12)
print a(34)● What just happened here?
Example
![Page 36: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/36.jpg)
Namespace● A namespace is an abstract container or environment created to hold a logical grouping of unique(!) identifiers or symbols
● <<MORE>>
![Page 37: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/37.jpg)
Nested Functions● Def statement inside a function defines a nested function
● Useful sometimes● Use Of Class preffered
![Page 38: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/38.jpg)
![Page 39: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/39.jpg)
Functions (con)● Return ● Return Type● Some Examples
![Page 40: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/40.jpg)
Modules
![Page 41: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/41.jpg)
Modules● What are modules?● How to load modules● Effect on namespace● Important modules
OS– SYS
![Page 42: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/42.jpg)
How To Make Modules?
![Page 43: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/43.jpg)
Y is this Important
![Page 44: Python Workshop. LUG Maniapl](https://reader034.vdocuments.site/reader034/viewer/2022051815/5406de368d7f7288088b483a/html5/thumbnails/44.jpg)