This document provides an overview of the Python programming language, including its history, key features, syntax examples, and common uses. It also discusses how Python can be used under Linux and some potential issues.
19. has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in C or C++.
20. usable as an extension language for applications that need a programmable interface.
21. What is Python ??? supports multiple programming paradigms (primarily object oriented, imperative, and functional)
22. portable: runs on many Unix variants, on the Mac, and on PCs under MS-DOS, Windows, Windows NT, OS/2, FreeBSD Solaris, OS/2, Amiga,AROS, AS/400, BeOS, OS/390, z/OS, Palm OS, QNX, VMS, Psion, Acorn RISC OS, VxWorks, PlayStation, Sharp Zaurus, Windows CE and even PocketPC !
23. What is Python ??? Developed and supported by a large team of volunteers - Python Software Foundation
37. Free and open source Implemented under an open source license. Freely usable and distributable, even for commercial use. Simplicity , Great first language
63. E.g. Projects with Python Websites: Google, YouTube, Yahoo Groups & Maps, CIA.gov Appengine: http://code.google.com/appengine/
64. ” Google: Python has been an important part of Google since the beginning.”, Peter Norvig.
65. Python application servers and Python scripting to create the web UI for BigTable (their database project) Systems: NASA, LALN, CERN, Rackspace Nasa Nebula http://nebula.nasa.gov/about Games: Civilization 4, Quark (Quake Army Knife)
87. Strings: format() >>>age = 25 >>>name = 'Swaroop' >>> print ( '{0} is {1} years old' .format(name, age)) Swaroop is 25 years old >>> '{0:.3}' .format( 1 / 3 ) '0.333' >>> '{0:_^11}' .format( 'hello' ) '___hello___' >>> '{name} wrote {book}' .format(name= 'Swaroop' , book= 'A Byte of Python' ) 'Swaroop wrote A Byte of Python'
88. Variables Naming identifiers: The first character be a letter of the alphabet (uppercase ASCII or lowercase ASCII or Unicode character) or an underscore ('_').
89. The rest of the identifier name can consist of letters (uppercase ASCII or lowercase ASCII or Unicode character), underscores ('_') or digits (0-9).
90. Identifier names are case-sensitive. For example, myname and myName are not the same.
92. Indentation Python uses whitespace to determine blocks of code def greet (person): if person == “Tim”: print (“Hello Master”) else : print (“Hello {name}”.format(name=person))
93. Control Flow if guess == number: #do something elif guess < number: #do something else else : #do something else while True : #do something #break when done break else : #do something when the loop ends for i in range( 1 , 5 ): print (i) else : print ( 'The for loop is over' ) #1,2,3,4 for i in range( 1 , 5 , 2 ): print (i) else : print ( 'The for loop is over' ) #1,3
100. Functions Variable length args acceptable as a list or dict def total (initial= 5 , *numbers, **keywords): count = initial for number in numbers: count += number for key in keywords: count += keywords[key] return count print (total( 10 , 1 , 2 , 3 , vegetables= 50 , fruits= 100 ))
101. Functions def printMax (x, y): '''Prints the maximum of two numbers. The two values must be integers.''' x = int(x) # convert to integers, if possible y = int(y) if x > y: r eturn x else : r eturn y printMax( 3 , 5 )
103. Modules can be imported or run by themselves if __name__ == '__main__' : print ( 'This program is being run by itself' ) else : print ( 'I am being imported from another module' )
104. Modules #!/usr/bin/python # Filename: mymodule_demo.py import mymodule mymodule.sayhi() print ( 'Version' , mymodule.__version__) #!/usr/bin/python # Filename: mymodule.py def sayhi (): print ( 'Hi, this is mymodule speaking.' ) __version__ = '0.1' # End of mymodule.py
105. OOP class MyClass : """This is a docstring.""" name = "Eric" def say (self): return ( 'My name is {0}' .format(name)) instance = MyClass() print instance.say()
106. OOP class Person : def __init__ (self, name): self.name = name def __del__ (self): print ( 'deleting this person' ,self.name) def sayHi (self): print ( 'Hello, my name is' , self.name) p = Person( 'Swaroop' ) p.sayHi() del p
107. OOP All class members (including the data members) are public and all the methods are virtual in Python.
111. Files myString = ”This is a test string” f = open( 'test.txt' , 'w' ) # open for 'w'riting f.write(myString) # write text to file f.close() # close the file f = open( 'test.txt' ) #read mode while True : line = f.readline() if len(line) == 0 : # Zero length indicates EOF break print (line, end= '' ) f.close() # close the file
112. Pickle import pickle shoplistfile = 'shoplist.data' shoplist = [ 'apple' , 'mango' , 'carrot' ] f = open(shoplistfile, 'wb' ) pickle.dump(shoplist, f) # dump the object to a file f.close() del shoplist # destroy the shoplist variable f = open(shoplistfile, 'rb' ) storedlist = pickle.load(f) # load the object from the file print (storedlist)
122. Various editors Text editors, IDLE , plugins for eclipse & Netbeans Embeddable in many applications as scripting interface Rhythmbox, Blender, OpenOffice, BitTorrent, ...
123. Linux and Python ” Talk is cheap. Show me the code.” Linus Torvalds