KEMBAR78
python beginner talk slide | PPTX
ABU ZAHED JONY
“Quotes”

 Perl is worse than Python because people wanted it worse
 - Larry Wall, Creator of Perl
 Python fits your brain
 - Bruce Eckel, Author: Thinking in Java
 Python is an excellent language & makes sensible
  compromises.
 - Peter Norvig. AI

 Life is better without brackets
About Python
 very clear, readable syntax
 portable
 intuitive object orientation
 natural expression of procedural code
 full modularity, supporting hierarchical packages
 very high level dynamic data types
 extensive standard libraries and third party modules for
  virtually every task
 extensions and modules easily written in C, C++ (or Java for
  Jython, or .NET languages for IronPython)
Hello World
print “Hello World”
 Hello World
a=6
print a;

 6


a=‘Hello’
a=a+ 6


 Errors               a=a + str(6)
*.py
Interpreter Output
Interpreter
Strings
a=‘Hello’
a=“Hello”
a=“I Can’t do this”
a=“I ”Love” Python”
a=‘I “Love” Python’
Hello
Hello
I Can’t do this
I “Love” Python
I “Love” Python
a=“Hello”
print a[0]
print len(a)
print a[0:2]
print a[2:]

H
5
He
llo
a=“Hello”


print a[-1]
print a[-2:]

 o
lo
More About str Class
print dir(str)
 ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__',
'__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__',
'__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__',
'__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith',
'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title',
'translate', 'upper', 'zfill']
Help Class
print help(str.find)
find(...)
   S.find(sub [,start [,end]]) -> int

  Return the lowest index in S where substring sub is found,
  such that sub is contained within s[start,end]. Optional
  arguments start and end are interpreted as in slice notation.

  Return -1 on failure.
Some Basic Syntax(1)




n Odd
n is Odd and greater than 5
n divided by 2 or 5
Some Basic Syntax(1)
               //Array Declaration
               a=[]




2
1
5
Using Range:
2
1
5
True
List(1)
a=[3,1,5]
b=[4,2]
c=a+b
print c
del c[2]
print c
print len(c)
[3,1,5,4,2]
[3,1,4,2]
4
List(2)
(a,b)=([3,1,5],[4,2])
c=a+b
                                       c= sorted(c)
print sorted(c)
print a==b
print sorted(c,reverse=True)
print c
print help(sorted)
[1,2,3,4,5]
False
[5,4,3,2,1]
[3, 1, 5, 4, 2]
Help on built-in function sorted in module __builtin__:
sorted(...)
   sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
List and Sort
a=['ax','aae','aac']
                       return 1,”Hello World”

def mFun(s):
                       a,b=mFun(“Hi”)
  return s[-1]

print sorted(a,key=mFun)
print sorted(a,key=mFun,reverse=True)
print sorted(a,key=len)
['aac', 'aae', 'ax']
['ax', 'aae', 'aac']
['ax', 'aae', 'aac']
Tuples
a=(1,2,1)
print a[0]
print len(a)
b=[(1,2,3),(1,2,1),(1,4,1)]
print a in b

1
3
True
Tuples and sort
a=[(1,"b"),(2,"a"),(1,"e")]
print a
print sorted(a)
def myTSort(d):
  return d[0]
print sorted(a,key=myTSort)
k=(1,”e”)
print k in a

[(1, 'b'), (2, 'a'), (1, 'e')]
[(1, 'b'), (1, 'e'), (2, 'a')]
[(2, 'a'), (1, 'b'), (1, 'e')]
True
Dictionary


 d={}

 d[‘a’]=‘alpha’
 d[‘o’]=‘omega’
 d[‘g’]=‘gamma’

 print d[‘a’]
  alpha
Dictionary
d={}
(d['a'],d['o'],d['g'])=("alpha","omega","gamma")
print d
print len(d)
{'a': 'alpha', 'g': 'gamma', 'o': 'omega'}
3


Check a value is in dictionary ???

‘a’ in d
Dictionary
d={}
(d['a'],d['o'],d['g'])=("alpha","omega","gamma")
print d[‘a’]
print d[‘x’] //???
print d.get(‘x’) // return None
print d.get(‘a’)

 if(d.get(‘a’)){
      print ‘Yes’
 }
Dictionary
d={}
(d['a'],d['o'],d['g'])=("alpha","omega","gamma")
print d.keys()
print d.values()
print d.items()
['a', 'g', 'o']
['alpha', 'gamma', 'omega']
[('a', 'alpha'), ('g', 'gamma'), ('o', 'omega')]
What needs for sorted dictionary data(key order) ???
All data returns random order

for k in sorted( d.keys() ):
  print d[k]

print sorted(d.keys())
Dictionary
print dir(dict)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__',
'__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__gt__',
'__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__setitem__', '__str__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items',
'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update',
'values']

print help(dict)


print help(dict.items)
File
def readFile(fileName):   // File write
  f=open(fileName,'r')    f=open(fileName,‘w')
                          f.write(data)
  for line in f:
                          f.close()
     print line,
   f.close()

def readFile(fileName):
  f=open(fileName,'r')
  text=f.read()
  print text
  f.close()


 f.seek(5)
Regular expression
import re

match=re.search('od',"God oder")
print match.group()
match=re.search(r'od',"God oder")
print match.group()

od
od
Regular expression
import re

print re.findall(r'ar+','a ar arr')
print re.findall(r'ar*','a ar arr')

['ar', 'arr']
['a', 'ar', 'arr']


+ for 1 or more
* for 0 or more

print dir(re)
Utility: OS
import os
print dir(os)
['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL',
'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR',
'O_SEQUENTIAL', 'UserDict', 'W_OK', 'X_OK', '_Environ', '__all__',
'__builtins__', '__doc__', '__file__', '__name__', '_copy_reg',
'_pickle_statvfs_result', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close',
'curdir', 'defpath', 'isatty', 'linesep', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir',
'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'popen2', 'popen3',
'popen4', 'putenv', 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir',
'sep', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'startfile', 'stat',
'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'sys', 'system',
'tempnam', 'times', 'tmpfile', 'tmpnam', 'umask', 'unlink', 'unsetenv',
'urandom', 'utime', 'waitpid', 'walk', 'write']
Utility: OS
import os
print help(os.unlink)
unlink(...)
  unlink(path)
  Remove a file (same as remove(path)).

print help(os.rmdir)


rmdir(...)
  rmdir(path)
  Remove a directory.
Utility: HTTP request, URL




try:
    pass
except:
    pass
finally:
    print “ok”
Some OOP




 class MyClass(AnotherClass):
 import myClass.py
Some OOP (Thread)
Access Shared Resource
Database(Sqlite)
Database(MySql)
Reserved Word
   and           finally      pass
   assert        for          print
   break         from         raise
   class         global       return
   continue      if           try
   def           import       while
   del           in
   elif          is
   else          lambda
   except        not
   exec          or
Code Source


      http://jpython.blogspot.com/
Resource

 http://www.learnpython.org/
 http://learnpythonthehardway.org/book/
 http://code.google.com/edu/languages/google-
 python-class/

 http://love-python.blogspot.com
 http://jpython.blogspot.com
THANK YOU

python beginner talk slide

  • 1.
  • 2.
    “Quotes”  Perl isworse than Python because people wanted it worse - Larry Wall, Creator of Perl  Python fits your brain - Bruce Eckel, Author: Thinking in Java  Python is an excellent language & makes sensible compromises. - Peter Norvig. AI Life is better without brackets
  • 3.
    About Python  veryclear, readable syntax  portable  intuitive object orientation  natural expression of procedural code  full modularity, supporting hierarchical packages  very high level dynamic data types  extensive standard libraries and third party modules for virtually every task  extensions and modules easily written in C, C++ (or Java for Jython, or .NET languages for IronPython)
  • 4.
    Hello World print “HelloWorld” Hello World a=6 print a; 6 a=‘Hello’ a=a+ 6 Errors a=a + str(6)
  • 5.
  • 6.
  • 7.
  • 8.
    Strings a=‘Hello’ a=“Hello” a=“I Can’t dothis” a=“I ”Love” Python” a=‘I “Love” Python’ Hello Hello I Can’t do this I “Love” Python I “Love” Python
  • 9.
    a=“Hello” print a[0] print len(a) printa[0:2] print a[2:] H 5 He llo
  • 10.
  • 11.
    More About strClass print dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
  • 12.
    Help Class print help(str.find) find(...) S.find(sub [,start [,end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within s[start,end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.
  • 13.
    Some Basic Syntax(1) nOdd n is Odd and greater than 5 n divided by 2 or 5
  • 14.
    Some Basic Syntax(1) //Array Declaration a=[] 2 1 5 Using Range: 2 1 5 True
  • 15.
    List(1) a=[3,1,5] b=[4,2] c=a+b print c del c[2] printc print len(c) [3,1,5,4,2] [3,1,4,2] 4
  • 16.
    List(2) (a,b)=([3,1,5],[4,2]) c=a+b c= sorted(c) print sorted(c) print a==b print sorted(c,reverse=True) print c print help(sorted) [1,2,3,4,5] False [5,4,3,2,1] [3, 1, 5, 4, 2] Help on built-in function sorted in module __builtin__: sorted(...) sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
  • 17.
    List and Sort a=['ax','aae','aac'] return 1,”Hello World” def mFun(s): a,b=mFun(“Hi”) return s[-1] print sorted(a,key=mFun) print sorted(a,key=mFun,reverse=True) print sorted(a,key=len) ['aac', 'aae', 'ax'] ['ax', 'aae', 'aac'] ['ax', 'aae', 'aac']
  • 18.
  • 19.
    Tuples and sort a=[(1,"b"),(2,"a"),(1,"e")] printa print sorted(a) def myTSort(d): return d[0] print sorted(a,key=myTSort) k=(1,”e”) print k in a [(1, 'b'), (2, 'a'), (1, 'e')] [(1, 'b'), (1, 'e'), (2, 'a')] [(2, 'a'), (1, 'b'), (1, 'e')] True
  • 20.
    Dictionary d={} d[‘a’]=‘alpha’ d[‘o’]=‘omega’ d[‘g’]=‘gamma’ print d[‘a’] alpha
  • 21.
    Dictionary d={} (d['a'],d['o'],d['g'])=("alpha","omega","gamma") print d print len(d) {'a':'alpha', 'g': 'gamma', 'o': 'omega'} 3 Check a value is in dictionary ??? ‘a’ in d
  • 22.
    Dictionary d={} (d['a'],d['o'],d['g'])=("alpha","omega","gamma") print d[‘a’] print d[‘x’]//??? print d.get(‘x’) // return None print d.get(‘a’) if(d.get(‘a’)){ print ‘Yes’ }
  • 23.
    Dictionary d={} (d['a'],d['o'],d['g'])=("alpha","omega","gamma") print d.keys() print d.values() printd.items() ['a', 'g', 'o'] ['alpha', 'gamma', 'omega'] [('a', 'alpha'), ('g', 'gamma'), ('o', 'omega')] What needs for sorted dictionary data(key order) ??? All data returns random order for k in sorted( d.keys() ): print d[k] print sorted(d.keys())
  • 24.
    Dictionary print dir(dict) ['__class__', '__cmp__','__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__str__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] print help(dict) print help(dict.items)
  • 25.
    File def readFile(fileName): // File write f=open(fileName,'r') f=open(fileName,‘w') f.write(data) for line in f: f.close() print line, f.close() def readFile(fileName): f=open(fileName,'r') text=f.read() print text f.close() f.seek(5)
  • 26.
    Regular expression import re match=re.search('od',"Gododer") print match.group() match=re.search(r'od',"God oder") print match.group() od od
  • 27.
    Regular expression import re printre.findall(r'ar+','a ar arr') print re.findall(r'ar*','a ar arr') ['ar', 'arr'] ['a', 'ar', 'arr'] + for 1 or more * for 0 or more print dir(re)
  • 28.
    Utility: OS import os printdir(os) ['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'UserDict', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '_copy_reg', '_pickle_statvfs_result', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'curdir', 'defpath', 'isatty', 'linesep', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'popen2', 'popen3', 'popen4', 'putenv', 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'sys', 'system', 'tempnam', 'times', 'tmpfile', 'tmpnam', 'umask', 'unlink', 'unsetenv', 'urandom', 'utime', 'waitpid', 'walk', 'write']
  • 29.
    Utility: OS import os printhelp(os.unlink) unlink(...) unlink(path) Remove a file (same as remove(path)). print help(os.rmdir) rmdir(...) rmdir(path) Remove a directory.
  • 30.
    Utility: HTTP request,URL try: pass except: pass finally: print “ok”
  • 31.
    Some OOP classMyClass(AnotherClass): import myClass.py
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
    Reserved Word  and  finally  pass  assert  for  print  break  from  raise  class  global  return  continue  if  try  def  import  while  del  in  elif  is  else  lambda  except  not  exec  or
  • 37.
    Code Source http://jpython.blogspot.com/
  • 38.
    Resource  http://www.learnpython.org/  http://learnpythonthehardway.org/book/ http://code.google.com/edu/languages/google- python-class/  http://love-python.blogspot.com  http://jpython.blogspot.com
  • 39.