Thursday 2 March 2017

Python 3 While Loop

Python 3 While Loop

The below code block is an example for python while loop

'''While loops'''
a = 11
while a>10:
    a+=1
    print (a)
    break       # prints 12 and then breaks

Wednesday 1 March 2017

Python 3 Conditional Operators

Python 3 Conditional Operators

The explanation programs for conditional operators is as follows,

'''Conditional Operation'''
a = 100
if a == 100:
    print ("true")
else:
    print ("false")

# result is true
if a <50:
    print ("less than 50")
else:
    if a > 50:
        print ("greater than 50")

# result is greater than 50
if a < 50:
    print ("less than 50")
elif a>50:
    print ("greater than 50")
    
# result is greater than 50











Python 3 Relational Operators

Python 3 Relational Operators

Below code represents as an example for Relational Operators in Python

'''Relational Operation'''
test = 100
print (test == 100)  #True
print (test == 50)   #False
print (test != 100)  #False
print (test !=50)    #True
print (test < 200)   #True
print (test > 200)   #False

Python 3 Conversion between Scalar Built in Types

Python 3 Conversion between Scalar Built in Types


The type conversion in Python 3 is explained with the code below,

"Conversion between the types"
int_value = 100          # An integer assignment
float_value = 100.0       # A floating point
string_value = "name"       # A string# 
none_value = None

print (int_value)           #100
print (float(int_value))    #100.0
print (str(int_value))       #100
print (float_value)         #100.0 
print (int(float_value))    #100
print (str(float_value))    #100.0
print (string_value)        #name
# print (int(string_value)) 
#  ValueError: invalid literal for int() with base 10: 'name'# 
print (float(string_value)) 
# ValueError: could not convert string to float: 'name'

Python 3 Scalar Built in Types

Python 3 Scalar Built in Types

The below example explains about the python 3 available built in scalar types,


''' Available list of scalar built in types in python are
    1. int    
    2. float    
    3. None    
    4. bool'''
int_value = 5          # An integer assignment
float_value = 50.0       # A floating point
string_value = "name"       # A stringnone_value = None

print (int_value)
print (float_value)
print (string_value)
# print (None(none_value)) it will not print gives 
#"TypeError: 'NoneType' object is not callable"

Python 3 help Command

Python 3 help() Command

Just Type help() and hit enter or in your IDE type help() and run the file it will give help contents and help console.

Like the one given below

Welcome to Python 3.4's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.4/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help> 


Example for help command usage 


help> import
The "import" statement
**********************

   import_stmt     ::= "import" module ["as" name] ( "," module ["as" name] )*
                   | "from" relative_module "import" identifier ["as" name]
                   ( "," identifier ["as" name] )*
                   | "from" relative_module "import" "(" identifier ["as" name]
                   ( "," identifier ["as" name] )* [","] ")"
                   | "from" module "import" "*"
   module          ::= (identifier ".")* identifier
   relative_module ::= "."* module | "."+
   name            ::= identifier

The basic import statement (no "from" clause) is executed in two
steps:

1. find a module, loading and initializing it if necessary

2. define a name or names in the local namespace for the scope
   where the "import" statement occurs.

When the statement contains multiple clauses (separated by commas) the
two steps are carried out separately for each clause, just as though
the clauses had been separated out into individiual import statements.

The details of the first step, finding and loading modules are
described in greater detail in the section on the *import system*,
which also describes the various types of packages and modules that
can be imported, as well as all the hooks that can be used to
customize the import system. Note that failures in this step may
indicate either that the module could not be located, *or* that an
error occurred while initializing the module, which includes execution
of the module's code.

If the requested module is retrieved successfully, it will be made
available in the local namespace in one of three ways:

* If the module name is followed by "as", then the name following
  "as" is bound directly to the imported module.

* If no other name is specified, and the module being imported is a
  top level module, the module's name is bound in the local namespace
  as a reference to the imported module

* If the module being imported is *not* a top level module, then the
  name of the top level package that contains the module is bound in
  the local namespace as a reference to the top level package. The
  imported module must be accessed using its full qualified name
  rather than directly

The "from" form uses a slightly more complex process:

1. find the module specified in the "from" clause, loading and
   initializing it if necessary;

2. for each of the identifiers specified in the "import" clauses:

   1. check if the imported module has an attribute by that name

   2. if not, attempt to import a submodule with that name and then
      check the imported module again for that attribute

   3. if the attribute is not found, "ImportError" is raised.

   4. otherwise, a reference to that value is stored in the local
      namespace, using the name in the "as" clause if it is present,
      otherwise using the attribute name

Examples:

   import foo                 # foo imported and bound locally
   import foo.bar.baz         # foo.bar.baz imported, foo bound locally
   import foo.bar.baz as fbb  # foo.bar.baz imported and bound as fbb
   from foo.bar import baz    # foo.bar.baz imported and bound as baz
   from foo import attr       # foo imported and foo.attr bound as attr

If the list of identifiers is replaced by a star ("'*'"), all public
names defined in the module are bound in the local namespace for the
scope where the "import" statement occurs.

The *public names* defined by a module are determined by checking the
module's namespace for a variable named "__all__"; if defined, it must
be a sequence of strings which are names defined or imported by that
module.  The names given in "__all__" are all considered public and
are required to exist.  If "__all__" is not defined, the set of public
names includes all names found in the module's namespace which do not
begin with an underscore character ("'_'").  "__all__" should contain
the entire public API. It is intended to avoid accidentally exporting
items that are not part of the API (such as library modules which were
imported and used within the module).

The "from" form with "*" may only occur in a module scope.  The wild
card form of import --- "from module import *" --- is only allowed at
the module level.  Attempting to use it in class or function
definitions will raise a "SyntaxError".

When specifying what module to import you do not have to specify the
absolute name of the module. When a module or package is contained
within another package it is possible to make a relative import within
the same top package without having to mention the package name. By
using leading dots in the specified module or package after "from" you
can specify how high to traverse up the current package hierarchy
without specifying exact names. One leading dot means the current
package where the module making the import exists. Two dots means up
one package level. Three dots is up two levels, etc. So if you execute
"from . import mod" from a module in the "pkg" package then you will
end up importing "pkg.mod". If you execute "from ..subpkg2 import mod"
from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The
specification for relative imports is contained within **PEP 328**.

"importlib.import_module()" is provided to support applications that
determine dynamically the modules to be loaded.


Future statements
=================

A *future statement* is a directive to the compiler that a particular
module should be compiled using syntax or semantics that will be
available in a specified future release of Python where the feature
becomes standard.

The future statement is intended to ease migration to future versions
of Python that introduce incompatible changes to the language.  It
allows use of the new features on a per-module basis before the
release in which the feature becomes standard.

   future_statement ::= "from" "__future__" "import" feature ["as" name]
                        ("," feature ["as" name])*
                        | "from" "__future__" "import" "(" feature ["as" name]
                        ("," feature ["as" name])* [","] ")"
   feature          ::= identifier
   name             ::= identifier

A future statement must appear near the top of the module.  The only
lines that can appear before a future statement are:

* the module docstring (if any),

* comments,

* blank lines, and

* other future statements.

The features recognized by Python 3.0 are "absolute_import",
"division", "generators", "unicode_literals", "print_function",
"nested_scopes" and "with_statement".  They are all redundant because
they are always enabled, and only kept for backwards compatibility.

A future statement is recognized and treated specially at compile
time: Changes to the semantics of core constructs are often
implemented by generating different code.  It may even be the case
that a new feature introduces new incompatible syntax (such as a new
reserved word), in which case the compiler may need to parse the
module differently.  Such decisions cannot be pushed off until
runtime.

For any given release, the compiler knows which feature names have
been defined, and raises a compile-time error if a future statement
contains a feature not known to it.

The direct runtime semantics are the same as for any import statement:
there is a standard module "__future__", described later, and it will
be imported in the usual way at the time the future statement is
executed.

The interesting runtime semantics depend on the specific feature
enabled by the future statement.

Note that there is nothing special about the statement:

   import __future__ [as name]

That is not a future statement; it's an ordinary import statement with
no special semantics or syntax restrictions.

Code compiled by calls to the built-in functions "exec()" and
"compile()" that occur in a module "M" containing a future statement
will, by default, use the new syntax or semantics associated with the
future statement.  This can be controlled by optional arguments to
"compile()" --- see the documentation of that function for details.

A future statement typed at an interactive interpreter prompt will
take effect for the rest of the interpreter session.  If an
interpreter is started with the *-i* option, is passed a script name
to execute, and the script includes a future statement, it will be in
effect in the interactive session started after the script is
executed.

See also: **PEP 236** - Back to the __future__

     The original proposal for the __future__ mechanism.

Related help topics: MODULES

Learning Python 3

Learning Python 3

This will be completely code driven learning

1. Python 3 import usage
2. Python 3 help Command 
3. Python 3 Scalar Built in Types
4. Python 3 Conversion between Scalar Built in Types
5 Python 3 Relational Operators
6. Python 3 Conditional Operators
7. Python 3 While Loop
8. Python 3 Strings
9. Python 3 Collections
10. Python 3 Iterations

Python 3 Import Usage


Python 3 Import Usage

The below are some valid python import usage for which

'''Example for normal import which imports all the classes present'''
import module_name

'''Example for importing a class inside a package or module'''
from module_name import function_name

'''Example for importing with alias'''
from module_name import function_name as alias_name

id = alias_name.method.param