Sunday, June 19, 2011

Interactive vs Script


Here I want to discuss about the differences in interactive mode and script mode. Some changes in code must be done in order to execute the same program in both modes.

If you type an expression in interactive mode, the interpreter evaluates it and displays the result:

>>> 1 + 1
2

But in a script, an expression all by itself doesn’t do anything!

We can solve our confusion by an example.

The author of Think Python has given a problem for solving this.

Type the following statements in the Python interpreter to see what they do:
5
x = 5
x + 1
Now put the same statements into a script and run it. What is the output? Modify the script by transforming each expression into a print statement and then run it again.

When I run the code in interactive mode I got the output as usual.





When I run the same in script code I got no output. So I tried the same program with print statements and I got the results.










In script mode we must use the print statement in order to display some outputs whereas in interactive mode print statement is not necessary.

Thanks

AJAY

Statements and Expressions


I have showed you an interesting behavior of python variables in my previous post. Now I want to continue with statements and expressions.

We know a statement is a unit of code that can be executed. In interactive mode we can type the statements and we can see the results after that. In script mode, the script usually contains many statements and result will be shown after executing the statements.

For example, the script
print 1
x = 2
print x
produces the output
1
2

We have seen two types of statements – print and assignment. Assignment statement will not have any outputs.

Operators are special symbols that represent computations like addition and multiplication. The values the operator is applied to are called operands. The operators +, -, *, / and ** perform addition, subtraction, multiplication, division and exponentiation. In some other languages, ^ is used for exponentiation, but in Python it is a bitwise operator called XOR.


When we divide integer values using ‘/’ operator, Python performs floor division, which means it chops off the fraction part. If anyone of the values are floating value then Python will perform floating point division.


An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions (assuming that the variable x has been assigned a value):

17
x
x + 17

When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. For mathematical operators, Python follows mathematical convention. 
  • Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1) is 4, and (1+1)**(5-2) is 8.
  • Exponentiation has the next highest precedence, so 2**1+1 is 3, not 4, and 3*1**3 is 3, not 27.
  • Multiplication and Division have the same precedence, which is higher than Addition and Subtraction, which also have the same precedence. So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.
  • Operators with the same precedence are evaluated from left to right. So in the expression degrees / 2 * pi, the division happens first and the result is multiplied by pi. To divide by 2 π, you can use parentheses or write degrees / 2 / pi.
The + operator works with strings, but it might not do what you expect: it performs concatenation, which means joining the strings by linking them end-to-end. For example:
             first = 'throat'
                   second = 'warbler'
                   print first + second

The output of this program is throatwarbler.
The * operator also works on strings; it performs repetition. For example, 'Spam'*3 is 'SpamSpamSpam'. If one of the operands is a string, the other has to be an integer.
Comments are the notes used in the code in order to provide some help to understand what is meant by a particular line/set of code.
# compute the percentage of the hour that has elapsed
percentage = (minute * 100) / 60
In this case, the comment appears on a line by itself. You can also put comments at the end of a line:
percentage = (minute * 100) / 60     # percentage of an hour
In my next post I will illustrate some problems and solutions regarding the statements and expressions.
Thanks
AJAY

Wednesday, May 25, 2011

An Interesting Variable Behavior

I have promised you that I will illustrate an interesting example in my next post. Here we go with that interesting variable behavior.

If you type an integer with a leading zero, you might get a confusing error.



Other numbers seem to work, but the results are bizarre.


Can you figure out what is going on? It’s really confusing. Isn’t it?

When we assign an integer with a leading zero, Python assumes that it’s an octal number, and stores its corresponding decimal value. Take a look at the figure below.


We know decimal values of (010)8 = 8, (0100)8 = 64 etc. Here (02132)8 has the decimal value 1114. Then what is the error in 02492? It’s not an octal number at all. We know octal number have only digits 0 to 7.  02492 have 9 in it. So an error occurred.

Just for my curiosity, I tried an integer with two leading zeros and I got the same effect. I tried another one with floating values with leading zeros in their integer part. I found it is same as the normal floating values and no octal-decimal conversion take place.



Have you enjoyed? Keep browsing TaLenCia, I will be back with more interesting facts about Python.

Thanks

AJAY