Sunday, June 19, 2011

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

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):

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.

No comments:

Post a Comment

Comments with advertisement links will not be published. Thank you.