Thursday, 21 February 2013



Encapsulating a series of operations

First, we will define a not-too-useful function that calculates y give a slope of 5, a y-intercept of -3, and an x-value of 9 (exactly as above). We do this by wrapping a function around the sequence of operations above. The return value of this function is the computed y value:
    def y():
        m = 5
        x = 9
        b = -3
        return m * x + b
The keyword def indicates that a function definition is occurring. The name of this particular function is y. The names of the things being sent to the function are given between the parentheses; since there is nothing between the parentheses, we don't need to send any information to this function when we use it. Together, the first line is known as the function signature, which tells you the name of the function and how many values it expects to be sent when it is used.
The stuff indented from the first line of the function definition is called the function body and is the code that will be evaluated (or executed) when the function is used.The use of a function is known as a function call. Most function calls look similar: the name of the function to be called followed by a parenthesized list of information to send the function so that it can do its job. Thus, the parentheses after the y indicate that we wish to call the y function and get its value. Because we designed the function to take no information when called, we do not place any information between the parentheses.

Passing arguments

A hallmark of a good function is that it lets you compute more than one thing. We can modify our y function to take in the value of x in which we are interested. In this way, we can compute more than one value of y. We do this by passing in some information. This information that is passed to a function in a function call is known as an argument, in this case, the value of x.
    def y(x):
        slope = 5
        intercept = -3
        return slope * x + intercept
Note that we have moved the variable x from the body of the function to between the parentheses. We have also refrained from giving it a value since its value is to be sent to the function when the function is called. What we have done is to parameterize the function to make it more general and more useful. The variable x is now called a formal parameter since it sits between the parentheses in the first line of the function definition.
    >>> y(0)
    >>> y(-2)
What if we wish to compute a y-value for a given x for a different line? One approach would be to pass in the slope and intercept as well as x:
    def y(x,m,b):
        return m * x + b
>>> y(0,5,-3)

Creating functions on the fly (ADVANCED)

Since creating functions is hard work (lots of typing) and Computer Scientists avoid unnecessary work like the plague, somebody early on got the idea of writing a function that itself creates functions! Brilliant! We can do this for our line problem. We will tell our creative function to create a y function for a particular slope and intercept! While we are at it, let's change the variable names m and b to slope and intercept, respectively:
    def makeLine(slope,intercept):
        def y(x):
            return slope * x + intercept
        return y  # the value of y is returned, y is NOT CALLED!
           >>> a = makeLine(5,-3)
    >>> b = makeLine(6,2)
    >>> a(9)

The Function and Procedure Patterns

When a function calculates (or obtains) a value and returns it, we say that it implements the function pattern. If a function does not have a return value, we say it implements the procedure pattern.
Here is an example of the function pattern:
    def square(x):
        return x * x
This function takes a value, stores it in x, computes the square of x and returns the result of the computation.
Here is an example of the procedure pattern:
    def greeting(name):
        return "done"
Another common mistake is to inadvertently implement a procedure pattern when a function pattern is called for. This happens when the return keyword is omitted.
    def psquare(x):
        x * x
Calling this kind of function yields a surprising result:
    >>> x = psquare(4)
When you do not specify a return value, Python will supply one for you anyway. This supplied value is the special symbol None. Since psquare did not explicitly return a value, None was returned and assigned to the variable x

Wednesday, 20 February 2013


Assignment Patterns

The art of writing programs lies in the ability to recognize and use patterns that have appeared since the very first programs were written.

The Transfer Pattern

The transfer pattern is used to change the value of a variable based upon the value of another variable. Suppose we have a variable named alpha which is initialized to 3 and a variable beta which is initialized to 10:
    alpha = 3
    beta = 10
Now consider the statement:
    alpha = beta
This statement is read like this: make the new value of alpha equal to the value of beta, throwing away the old value of alpha. What is the value of alpha after that statement is executed? Highlight the following line to see the answer:
The new value of alpha is 10.
The transfer pattern tells us that value of beta is imprinted on alpha at the moment of assignment but in no case are alpha and beta conjoined in anyway in the future. 
A graphical way to see this is to use the idea of a pointer. When a variable is assigned a value, we will say that variable points to the value. For the variables:
   alpha = 3
   beta = 10
Now when we assign alpha the value of beta:
    alpha = beta
we cross out alpha's pointer and replace it with a pointer to whatever beta points:
Note that we never point a variable to another variable; we only point variables to values.
    alpha = 4
    beta = 13
    alpha = beta
    beta = 5
What are the final values of alpha and beta? Highlight the following line to see the answer:
The value of alpha is 3 and the value of beta is 5.

Lets check what happens if the following code is executed:
    alpha = 4
    beta = 13
    alpha = beta
    alpha = 42
The final values of alpha and beta  are
The value of alpha is 42and the value of beta is 13.

The Update Pattern

The update pattern is used to change the value of a variable based upon the original value of the variable. Suppose we have a variable named counter which is initialized to zero:
    counter = 0
Now consider the statement:
    counter = counter + 1
This statement is read like this: make the new value of counter equal to the old value of counter plus one. Since the old value is zero, the new value is one. Consider this sequence:
    counter = 0
    counter = counter + 1
    counter = counter + 1
    counter = counter + 1
    counter = counter + 1
    counter = counter + 1
The value of counter after the following code is executed:
The value of counter is 5.
There is another form of this update:
    counter = 0
    counter += 5
The operator += says to update the variable on the left by adding in the value on the right to the current value of the variable.
The update pattern can be used to sum a number of variables. Suppose we wish to compute the sum of the variables abcd, and e. The obvious way to do this is with one statement:
    sum = a + b + c + d + e
However, we can use the update pattern as well:
    sum = 0
    sum = sum + a
    sum = sum + b
    sum = sum + c
    sum = sum + d
    sum = sum + e
If a is 1, b is 2, c is 3, d is 4, and e is 5, then the value of sum in both cases is 15.  we use the update pattern for computing a sum when the first version is so much more compact and readable because we'll have to wait until we cover a programming concept called a loop. With loops, the update pattern is almost always used to compute sums, products, etc.

The Throw-away Pattern

The throw-away pattern is a mistaken attempt to use the update pattern. In the update pattern, we use the original value of the variable to compute the new value of the variable. Here again is the classic example of incrementing a counter:
    counter = counter + 1
In the throw-away pattern, the new value is computed but it the variable is not reassigned, nor is the new value stored anywhere. Many novice programmers attempt to update a counter simply by computing the new value:
    count + 1       # throw-away!
Python does all the work to compute the new value, but since the new value is not assigned to any variable, the new value is thrown away.

The Throw-away Pattern and Functions

The throw-away pattern applies to function calls as well. We haven't discussed functions much, but the following example is easy enough to understand. First we define a function that computes some value:
    def inc(x):
        return x + 1
This function returns a value one greater than the value given to it, but what the function actually does is irrelevant to this discussion. That said, we want to start indoctrinating you on the use of functions. Repeat this ten times:
We always do four things with functions: define themcall themreturn something, and save the return value.
To call the function, we use the function name followed by a set of parentheses. Inside the parentheses, we place the value we wish to send to the function. Consider this code, which includes a call to the function inc:
    y = 4
    y = inc(y)
    print("y is",y)
If we were to run this code, we would see the following output:
    y is 5
The value of y, 4, is sent to the function which adds one to the given value and returns this new value. This new value, 5, is assigned back to y. Thus we see thaty has a new value of 5.
Suppose, we run the following code instead:
    y = 4
    print("y is",y)
Note that the return value of the function inc is not assigned to any variable. Therefore, the return value is thrown away and the output becomes:
    y is 4
The variable y is unchanged because it was never reassigned.

An Introduction to Python


Python is a general purpose programming language that can be used effectively to build any kind of program. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.
Click to download python


Python works by figuring out the meaning or value of some code. This is true for the tiniest pieces of code to the largest programs. The process of finding out the meaning of code is known as evaluation.
The things whose values are the things themselves are known as literals. The literals of Python can be categorized by the following types: integersrealnumbersstringsBooleans, and arrays.
Python (or more correctly, the Python interpreter) responds to literals by echoing back the literal itself. Here are examples of each of the types:
    >>> 3
    >>> -4.9
    >>> "hello"

    >>>[3, -4.9, "hello"]
    [3, -4.9, 'hello']


Integers are numbers without any fractional parts. Examples of integers are:
    >>> 3

Real Numbers

Reals are numbers that do have a fractional part (even if that fractional part is zero!). Examples of real numbers are:
    >>> 3.2


Strings are sequences of characters delineated by double quotation marks:
    >>> "hello, world!"
    'hello, world!'
Python accepts both double quotes and single quotes to delineate strings.Characters in a string can be escaped (or quoted) with the backslash character, which changes the meaning of some characters. For example, the character n, in a string refers to the letter n while the character sequence \n refers to the newline character. A backslash also changes the meaning of the letter t, converting it into a tab character. You can also quote single and double quotes with backslashes. When other characters are escaped, it is assumed the backslash is a character of the string and it is escaped (with a backslash) in the result:
    >>> "\z"
A string with no characters between the double quotes is known as an empty string.
Unlike some languages, there is no character type in Python. A single character a, for example, is entered as the string "a" or 'a'.

True, False, and None

There are two special literals, True and False. These literals are known as the Boolean values and are used to guide the flow of a program. The so-called Boolean logic or Boolean algebra is concerned with the rules of combining truth values (i.e., true or false).
Another special literal is None. This literal is used to indicate the end of lists; it also is used to indicate something that has not yet been created. 

Collections of literals

If you read any other text on Python, the basic way of grouping literals together (rather like throwing a bunch of groceries in a shopping bag) is called a list.
Arrays are just collections of values. One creates an array by enclosing a comma-separated listing of values in square brackets. The simplest array is empty:
Arrays can contain any values:
    >>>[2, "help", len]
    [2, 'help', <built-in function len>]
The first value is an integer, the second a string, and the third is something known as a function. We will learn more about functions later, but the len function is used to tell us how many items are in an array:
    >>> len([2, "help", len])
Arrays can even contain arrays!
    >>>[0, [3, 2, 1] 4]
    [0, [3, 2, 1] 4]
An array is something known as a data structure; data structures are extremely important in writing sophisticated programs.

Indexing into Arrays

You can pull out an item from an array by using bracket notation. With bracket notation, you specify exactly which element (or elements) you wish to extract from the array. This specification is called an index. The first element of the array has index 0, the second index 1, and so on. Here is some code that extracts the first element of an array. Note that the first interaction creates a variable named items that points to an array of three elements.
    >>> items = ['a', True, 7]

    >>> items
    ['a', True, 7]

    >>> items[0]

    >>> items[1]
Extracting an item from an array leaves the array unchanged. What happens if our index is too large?
    >>> items[3]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      IndexError: list index out of range
There is a special notation for extracting more than one element of an array. This notation is known as a slice. Here is a slice that extracts all but the first element of an array:
    >>> items[1:]
    [True, 7]
This particular slice (you can slice an array many different ways) says, start extracting at the second item (which has index one) and go to the end. This operation is so common, it has a special name, taking the tail of an array.
Here is a slice that says, start extracting at the first element (which has index 0) and go up to, but do not include, the element with index 2:
    >>> items[0:2]
    ['a', True]


The operands of the other basic operators have special names too. For addition, the left operand is known as the augend and the right operand is known as theaddend. The result is known as the sum. For subtraction, the left operand is the minuend, the right the subtrahend, and the result as the difference. For division (and I think this is still taught), the left operand is the dividend, the right operand is the divisor, and the result is the quotient. Finally, for exponentiation, which is shorthand for repeated multiplication:
    >>> 3 ** 4
    >>> 3 * 3 * 3 * 3
the left operand is the base and the right operand is the exponent
If it makes sense to add two things together, you can probably do it in Python using the + operator. For example:
    >>> 2 + 3
Adding an string to an integer (with an augend integer) yields an error; the types are not "close" enough, like they are with integers and reals:
    >>> 2 + "hello"
    TypeError: unsupported operand types(s) for +: 'int' and 'str'
In general, when adding two things, the types must match or nearly match.
>>> 1.9 + 3.1 5.0
One can see that if one adds two integers, the result is an integer.
You can even add strings with strings and arrays with arrays:
    >>> "hello" + "world"

    >>> [1, 3, 5] + [2, 4, 6]
    [1, 3, 5, 2, 4, 6]
You can multiply strings and arrays with numbers:
    >>> "hello" * 3

    >>> [1, 2] * 3
    [1, 2, 1, 2, 1, 2]
 The division operator with respect to integer operands. Consider evaluating the following expression:
    15 / 2
If one asked the Python interpreter to perform this task, the result would be 7.5, as expected. However, often we wish for just the quotient without the remainder. In this case, the quotient is 7 and the remainder is 0.5. The double forward slash operator is Python's quotient operator; if we ask the interpreter to evaluate
    14 // 5
the result would be 2, not 2.8. Use of the quotient operator is known as integer division.
The complement to integer division is the modulus operator %. While the result of integer division is the quotient, the result of the modulus operator is the remainder. Thus
    14 % 5
evaluates to 4 since 4 is left over when 5 is divided into 14. To check if this is true, one can ask the interpreter to evaluate:
    (14 // 5 * 5) + (14 % 5) == 14
This complicated expression asks the question "is it true that the quotient times the divisor plus the remainder is equal to the original dividend?". The Python interpreter will respond that, indeed, it is true. 

Comparing things

The Boolean literals, True and False can be used in the Boolean comparison operators to generate such values. For example, we can ask if 3 is less than 4:
    >>> 3 < 4
Besides integers, we can compare reals with reals, strings with strings, and arrays with arrays using the comparison operators:
    >>> "apple" < "banana"
    >>> [1, 2, 3] < [1, 2, 4]
In general, it is illegal to compare integers or reals with strings.
>>> not(3 < 4 and 4 < 5)
    >>> not(3 < 4 or 4 < 5)


+ and - have higher precedence than <The lowest precedence operator in Python is the assignment operator Next come the Boolean connectives and and or.  At the next higher level are the Boolean comparatives, <<=>>===, and !=.After that come the additive arithmetic operators + and-. Next comes the multiplicative operators */ and %. Higher still is the exponentiation operator **. Finally, at the highest level of precedence is the selection, or dot, operator (the dot operator is a period or full-stop). Higher precedence operations are performed before lower precedence operations. Functions which are called with operator syntax have the same precedence level as the mathematical operators.

Assignment and Arrays

You can change a particular element of a array by assigning a new value to the index of that element by using bracket notation:
    >>> items = ['a', True, 7]

    >>> items[0] = 'b'

Tuesday, 4 December 2012

|t$ Me@N L0vE

Am all alone
With the stars above
You are the one my love
I think of in this blue night.

And all I can say is te amo
You mean a lot to me
And you belong to me
Its the love that makes me firm.

And so lets dive deep
In the pool of love
And you see its not my fantasy
Its the feeling that drives me crazy.

And with times
Rooting my love strong
And making my desires prolong
To sing a new song.

And blue are the clouds
Grasses are all green
And precious you are to me
More than anything, its love and it can be mean.

Saturday, 20 October 2012

Android-Getting Started

This Android Getting Started tutorial will walk you through the process of setting up all the basic tools you will need to start creating your very own Android applications.  We will be installing Eclipse (the IDE), downloading the Android SDK, installing both the Android AVD and the SDK, and setting up different Android Platforms.  After that, you can go to the next Android getting started tutorial and learn to create your own app from scratch!

[If you don't have the JAVA JRE click ]

Before you can get started developing your own Android apps, you will need to download and install the tools necessary to make it happen.  This Android getting started tutorial will start by downloading the Eclipse IDE.
Eclipse is an IDE, which stands for Integrated Development Environment, and if you are a new programmer, you are probably like WTF is that??!  Well, what an IDE does is make development a lot easier, such as recommending methods to use on a certain object, and if you are like what is a method, and what is an object, don’t worry about that right now.  All that is important is that installing Eclipse will help us create apps much quicker and easier.
Now, that we know (somewhat) what an IDE is, we will install the Android SDK, and setup the Android AVD. The Android SDK (Software Development Kit) will allow us to use methods and structures specifically designed for Android, such as using methods that we can use to send text messages, set the background of the user’s phone, and many, many more.
The Android AVD (Android Virtual Device) will allows us to test our Android apps on an emulator (or a virtual device) which comes in handy if we don’t have an actually android device to do our testing on.  You will be able to set up many different Android AVDs so you can test your application on “different” android devices, such as different android tablets and different phones.  If you want to eventually test your Android applications on your own device, you may want to check out this walkthrough.

So here you are in the magic world of apps and smart phones.

Friday, 19 October 2012

The Last Tear

Your words thrust my heart,
and I cried tears of a pain "Get out" I yelled
"These are the last tear I´ll ever cry for you".
So you left.
I waited hours, but you did n ´t return.
I cried tears of frustration.
I waited days, but you had nothing to say.
Remembering our days, I cried tears of solitude.
I waited months but you left no sign for me.

In the depths of my heart, I cried tears of despair.

How strange that all these tears could not wash away the wound!
The one thought of ecstasy pierced my bitterness.
I remembered you in the dawn,
I remembered you in the dusk
with a smile as sweet as May wine.
The tear of gratitude shimmered, and miraculously, you were back.
Soft fingers touched my cheek, and you bent over for a kiss.
"Why have you come?" I whispered
"To wipe away your last tear", you replied.
"It was the one you saved for me".