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.
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 !
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.
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.
91.
92.
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
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 )
102.
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
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)