Thursday, May 31, 2018

Getting Started Python


                                          Getting Started  Python 


The Python Interpreter:

  The  Python interpreter   is a program that reads Python program statements and executes them immediately (see [8] for full documentation). To use the interpreter, you need to open a terminal window or command prompt on your workstation. The interpreter operates in two modes.

Interactive Mode 

You can use the interpreter as an interactive tool. In interactive mode, you run the Python program and you will see a new prompt,  >>> , and you can then enter Python statements one by one. In Microsoft Windows, you might see something like this:

C:\Users\Paul>python 

 Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 

Type "help", "copyright", "credits" or "license" for more information. 

 >>> _ 

 shell
python shell

   The interpreter executes program statements immediately. Interactive mode is really useful when you want to experiment or try things out. For example, sometimes you need to see how a particular function (that you haven’t used before) behaves. On other occasions, you might need to see exactly what a piece of failing code does in isolation.

 The  >>>  prompt can be used to enter one-line commands or code blocks that define classes or functions (discussed later). Some example commands are shown here:




1   >>> dir(print)

 2    ['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__','__name__','__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__text_signature__']


 3   >>>
python shell
python IDE


 4   >>> 123.456 + 987.654


 5   1111.11


 6   >>>


 7   >>> 'This'+'is'+'a'+'joined'+'up'+'string.'


 8   'Thisisajoinedupstring.'


 9   >>>


 10  >>> len('Thisisajoinedupstring.')


 11  22

   The   dir()    command on line 1 lists all the attributes of an object, helpful if you need to know what you can do with an object type.  dir()  run without an argument tells you what modules you have available.  dir(print)  shows a list of all the built-in methods for
 print() , most of which you’ll never need.  If you type an expression value, as on line 4,  123.456 + 987.654  the interpreter will execute the calculation and provide the result. The expression on line 7 joins the strings of characters into one long string. The   len()    function on line 10 gives you the length of a string in characters.  If you define a new function 2  in interactive mode, the interpreter prompts you to complete the definition and will treat a blank line as the end of the function.
 example:



1   >>> def addTwoNumbers(a,b): 


 2   ...     result = a + b 


 3   ...     return result 


 4   ... 


 5   >>> addTwoNumbers(3,6) 


 6   9 


 7   >>> 

   Note:-  that when the interpreter expects more code to be supplied in a function, for example, it prints the ellipsis prompt ( ... ). In the case of function definitions, a blank line (see line 4 above) completes the function definition.  We define the function in lines 1 through 3 (note the  indentation  ), and the blank line 4 ends the definition. We call the function on line 5 and add 6 + 3, and the result is (correctly) 9.
 One other feature of the interactive interpreter is the  help()  function. You can use this to see the documentation of built-in keywords and functions. For example:     
 >>> 


 help(open) 


 Help on built-in function open in module io: 





open(...) 

      open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) -> file object 

   
 ■  Note   The Python interactive interpreter is really handy to try things out and explore features of the language


Read also : How to Download python

No comments:

Post a Comment