Wednesday, March 16, 2016

Learning Python 1 - Introduction

A proper introduction

Python Packages
scipy
numpy
matplotlib
pandas
scikit learn

-- Enter the Python
-- About Python

Portability
Coherence
Developer productivity
An extensive library
Software quality
Software integration
Satisfaction and enjoyment

-- What are the drawbacks?
-- Setting up the environment

Python 2 versus Python 3 – the great debate

>>> import sys
>>> print(sys.version)
3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2]

-- Installing Python

$ sudo apt-get update

-- How you can run a Python program

-- How is Python code organized

A complete Python application can be made of hundreds of thousands of lines of code, so you will have to scatter it through different modules. 
It truns out that even like this it would still be impractical to work with the code. So packages allows you to group modules together. 
A package is nothing more than a folder, which must contain a spcial file, _init_.py that doesn't need to hold any code but 
whose presence is required to tell Python that the folder is not just some folder, but it's actually a package(note that as Python 3.3 
_init_.py is not strictly required any more).


$ tree -v example
$ tree -v files_only

>>> from math import factorial
>>> factorial(5)

-- Python's execution model

Names and namespaces

Names that we can use to retrieve data within our code. They need to be kept somewhere so that whenever we need to retrieve those objects, we can use their names to fetch them. We need some space to hold them, hence: namespaces!

A namespace is therefore a mapping from names to objects. Examples are the set of built-in names (containing functions that are always accessible for free in any Python program), the global names in a module, and the local names in a function. Even the set of attributes of an object can be considered a namespace.

from library.second_floor.section_x.row_three import book

We start from the library namespace, and by means of the dot (.) operator, we walk into that namespace. Within this namespace, we look for second_floor, and again we walk into it with the . operator. We then walk into section_x, and finally within the last namespace, row_tree, we find the name we were looking for: book.

a scope is a textual region of a Python program, where a namespace is directly accessible. Directly accessible means that when you're looking for an unqualified reference to a name, Python tries to find it in the namespace.

Scopes are determined statically, but actually during runtime they are used dynamically. This means that by inspecting the source code you can tell what the scope of an object is, but this doesn't prevent the software to alter that during runtime. There are four different scopes that Python makes accessible (not necessarily all of them present at the same time, of course):

The local scope, which is the innermost one and contains the local names.
The enclosing scope, that is, the scope of any enclosing function. It contains non-local names and also non-global names.
The global scope contains the global names.
The built-in scope contains the built-in names. Python comes with a set of functions that you can use in a off-the-shelf fashion, such as print, all, abs, and so on. They live in the built-in scope.

## scopes1.py
# Local versus Global

# we define a function, called local
def local():
    m = 7
    print(m)

m = 5
print(m)

# we call, or `execute` the function local
local()

$ python scopes1.py


## scopes2.py
# Local versus Global

def local():
    # m doesn't belong to the scope defined by the local function
    # so Python will keep looking into the next enclosing scope.
    # m is finally found in the global scope
    print(m, 'printing from the local scope')

m = 5
print(m, 'printing from the global scope')

local()


## scopes3.py
# Local, Enclosing and Global

def enclosing_func():
    m = 13
    def local():
        # m doesn't belong to the scope defined by the local
        # function so Python will keep looking into the next
        # enclosing scope. This time m is found in the enclosing
        # scope
        print(m, 'printing from the local scope')

    # calling the function local
    local()

m = 5
print(m, 'printing from the global scope')

enclosing_func()

Object and classes

## bike.py
# let's define the class Bike
class Bike:
    def __init__(self, colour, frame_material):
        self.colour = colour
        self.frame_material = frame_material

    def brake(self):
        print("Braking!")

# let's create a couple of instances
red_bike = Bike('Red', 'Carbon fiber')
blue_bike = Bike('Blue', 'Steel')

# let's inspect the objects we have, instances of the Bike class.
print(red_bike.colour)  # prints: Red
print(red_bike.frame_material)  # prints: Carbon fiber
print(blue_bike.colour)  # prints: Blue
print(blue_bike.frame_material)  #  prints: Steel

# let's brake!
red_bike.brake()  # prints: Braking!

-- Guidelines on how to write good code
-- The Python culture

>>> import this

-- A note on the IDEs

No comments:

Post a Comment

Blog Archive