Tuesday, June 19, 2018

Sequences: Strings, Lists, and Tuples

                              Sequences: Strings, Lists, and Tuples 

we have looked at variables that hold a single value. A  sequence  is a variable that holds multiple values as an array. Each element can be addressed by its position in the sequence as an offset from the first element. The three types of sequence are as follows:

     Strings   : A sequence of characters that together form a text string. 

•     Lists   : A sequence of values where each value can be accessed using an offset from the first entry in the list. 

•     Tuples   : A sequence of values, very much like a list, but the entries in a tuple are immutable; they cannot be changed
.   
 We’ll look at the Python features that are common to all sequences and then look at the three types separately. 

Sunday, June 17, 2018

Python Objects

             Python Objects 

 Every variable in Python is actually an object. You don’t have to write object-oriented (OO) code to use Python, but the way Python is constructed encourages an OO approach. 1

     Object Types 

 All variables have a type that can be seen by using the built-in  type()  function.     



>>> type(23)
 <type 'int'>
 >>> type('some more text')
 <type 'str'>
 >>> c=[1,2,'some more text']
 >>> type(c)
 <type 'list'>

   Other types are  'class' ,  'module' ,  'function' ,  'file' ,  'bool' ,  'NoneType' , and  'long' .  The special constants  True  and  False  are  'bool'  types. The special constant  None  is a  'NoneType' .  To see a textual definition of any object, you can use the  str()  function; for example, the variable  c  can be represented as a string:   




Monday, June 4, 2018

Python Keywords

                                                        Python Keywords 

Like all programming languages, in Python, some words have defined meanings and are reserved for the Python interpreter. You must not use these words as variable names. Note that they are all lowercase:-

 and ,   as, assert ,   break  ,  class ,   continue  ,  def   , del   , elif   , else  ,  except ,   exec ,   finally ,   for  ,  from,    global  ,  if   , import ,   in   , is ,   lambda   , not,    or ,   pass ,   print   , raise,    return    ,try ,   while   , with   , yield  

 There are a large number of built-in names that you must not use, except for their intended purpose. The cases of  True ,  False , and  None  are important. The most common ones are listed here:-

.
  True  ,  False  ,  None    ,abs,    all   , any ,   chr,    dict,    dir ,   eval    ,exit  ,  file ,   float  ,  format    input    ,int   , max ,   min,    next  ,  object ,   open  ,  print ,   quit,    range ,   round ,   set  ,  str   , sum    tuple  ,  type  ,  vars  ,  zip .



 READ ALSO:-   (1)   Comments, Code Blocks, and Indentation ,variable in Python
                 
                            (2)   How to Downloading Python


Sunday, June 3, 2018

Comments, Code Blocks, and Indentation ,variable in Python

Python, like all programming languages has conventions that we must follow.
Some programming languages use punctuation such as braces ({}) and semicolons (;) to structure code blocks. Python is somewhat different (and easier on the eye) because it uses white space and indentation to define code structure. 3  Sometimes code needs a little explanation, so we use comments to help readers of the code (including you) understand it.  We introduce  indentation   and  comments   with some examples.

  # some text after hashes   
 #    brdr = 2 # thick border  

 Any text that appears after a hash character (#) is ignored by the interpreter and treated as a comment. We use comments to provide documentation. 

 Variables :

 A   variable    is a named location in the program’s memory that can be used to store some data. There are some rules for naming variables:- 

•    The first character must be a letter or underscore ( _ ). 
•   Additional characters may be alphanumeric or underscore. 
•   Names are case-sensitive.

 Common Assignment Operations 

 When you store data in a variable it is called  assignment.  An  assignment   statement places a value or the result of an expression into variable(s). The general format of an assignment is:

                                           " var = expression "

 An   expression    could be a literal, a calculation, a call to a function, or a combination of all three. Some expressions generate a list of values; for example:

Here are some more examples: 

>>> # 3 into integer myint 
  >>> myint = 3              
 >>> 
>>> # a string of characters into a string variable 
  >>> text = 'Some text' 
 Thanks for reading................
   

Thursday, May 31, 2018

Command-Line Mode(python)

                                                       Command-Line Mode 

 In command-line  mode  , the Python program is still run at the command line but you add the name of a file (that contains your program) to the command: 

                           " python myprogram.py "

   The interpreter reads the contents of the file ( myprogram.py  in this case), scans and validates the code, compiles the program, and then executes the program. If the interpreter encounters a fault in the syntax of your program, it will report a compilation error. If the program fails during execution, you will see a runtime error. If the program executes successfully, you will see the output(s) of the program.  You don’t need to worry about how the interpreter does what it does, but you do need to be familiar with the types of error messages it produces.  We use command-line mode to execute our programs in files.

  Coding, Testing and Debugging Python Programs

  The normal sequence of steps when creating a new program is as follows:

   1.    Create a new  .py  file that will contain the Python program (sometimes called source code). 

  2.    Edit your  .py  file to create new code (or amend existing code) and save the file.
   
3.    Run your program at the command prompt to test it, and interpret the outcome.
 
  4.    If the program does not work as required, or you need to add more features, figure out what changes are required and go to Step 2.

It’s usually a good idea to document your code with comments.

This is part of the editing process, Step 2. If you need to make changes to a working program, again, you start at Step 2.  Writing new programs is often called   coding   . When your programs don’t work properly, getting programs to do exactly what you want them to do is often called   debugging .  .      

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

Tuesday, May 29, 2018

How to Downloading Python

                                             How to Downloading Python

All  Python downloads   can be found at    https://www.python.org/downloads/    
 .
  You need to choose a Python version before you download.

 There are currently two versions:

 •    Version 2 is coming to the end of its life but is still widely used. 

 •   Version 3 has been around for some time; people have been slow to convert but it is gaining a following. 
 python



setup






 Editing Your Python Code 

 I recommend using either a language-sensitive editor or the editor that comes with your Python installation.

 •    On Windows, use the IDLE Integrated Development Environment ( IDE  ) or perhaps

       Notepad++.

•   On Linux, there is a selection of editors—  vi, vim, emacs, gedit , and so on; I use  gedit . 

 •   On OS X, TextMate works fine, but there are other options. 

 When you are more experienced, you might upgrade to using an IDE.

 There is a list of Python-compatible IDEs available at   

 https://wiki.python.org/moin/ IntegratedDevelopmentEnvironments     .   
  ←←