Friday 1 September 2017

Python Q & A

negative index in Python?
Python sequences can be index in positive and negative numbers.   For positive index, 0 is the first index, 1 is the second index and so forth.  For negative index, (-1) is the last index and (-2) is the second last index and so forth.
 module and package in Python?
In Python, module is the way to structure program. Each Python program file is a module, which imports other modules like objects and attributes.
The folder of Python program is a package of modules.  A package can have modules or subfolders.
 File maanger handle in Python
def main():
   #Open the file for write the contents
   f= open("guru99.txt","w+")
   for i in range(10):
       f.write("This is line %d\r\n" % (i+1))
   f.close()
   #Open the file back and read the contents
   f=open("guru99.txt", "r")
   if f.mode == 'r':
     contents =f.read()
     print contents
   #or, readlines reads the individual line into a list
   fl =f.readlines()
   for x in fl:
      print x

if __name__== "__main__":
  main()

No comments:

Post a Comment