Skip to main content

Posts

Showing posts from December, 2016

Python - Learning - Important point - p4 - Modules

Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a  module ; definitions from a module can be  imported  into other modules or into the  main  module (the collection of variables that you have access to in a script executed at the top level and in calculator mode). A module is a file containing Python definitions and statements. The file name is the module name with the suffix  .py  appended. Within a module, the module’s name (as a string) is available as the value of the global variable  __name__ .  A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the  first  time the module name is encountered in an import statement.  [1]  (They are also run if the file is executed as a script.) Each module has its own private symbol table,...

Python - Learning - Important point - p3

List Functions - list. append ( x ) - list. extend ( L ) - list. insert ( i ,  x ) - list. remove ( x ) - list. pop ( [ i ] ) - list. clear ( ) - list. index ( x ) - list. count ( x ) - list. sort ( key=None ,  reverse=False ) - list. reverse ( ) - list. copy ( ) It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one). To implement a queue, use  collections.deque  which was designed to have fast appends and pops from both ends. There is a way to remove an item from a list given its index instead of its value: the  del  statement. This differs from the  pop()  method which returns a value. The  del  statement can also be used to remove slices...

Python - Learning - Important point - p2

IF Perhaps the most well-known statement type is the  if  statement. There can be zero or more  elif  parts, and the  else  part is optional. The keyword ‘ elif ‘ is short for ‘else if’, and is useful to avoid excessive indentation. An  if  ...  elif  ...  elif  ... sequence is a substitute for the  switch  or  case  statements found in other languages. For Python’s  for  statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.  If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is recommended that you first make a copy. Iterating over a sequence does not implicitly make a copy. The slice notation makes this especially convenient. If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is r...

Python - Learning - Important point.

Abstract from  https://docs.python.org/3/tutorial/introduction.html Numbers Comments in Python start with the hash character,  # , and extend to the end of the physical line. A comment may appear at the start of a line or following whitespace or code, but not within a string literal.  The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators  + ,  - ,  *  and  /  work just like in most other languages (for example, Pascal or C); parentheses ( () ) can be used for grouping. The integer numbers (e.g.  2 ,  4 ,  20 ) have type  int , the ones with a fractional part (e.g.  5.0 ,  1.6 ) have type  float . We will see more about numeric types later in the tutorial. Division ( / ) always returns a float. To do  floor division  and get an integer result (discarding any fractional result) you can use the...