Sunday, 19 June 2011

Statement and Expression

A statement is a unit of code. Two types of statements are print and assignment. In interactive mode interpreter execute the statement and display the result. In script mode includes sequence of statements.
Operators are special symbols, that represent computations. The values the operator is applied to are called operands.
Operators are + (Addition), - (Subtraction), *(Multiplication), / (Division), and ** (Exponentiation).
>>> a=44
>>> a/45
0
When both the operands are integers , then the result is round downs to zero. If either the operands is a floating point number , then the python perform the floating point division.
>>> a/45.0
0.97777777777777775

An Expression is a combination of values, variables and operators.
>>> x=56
>>> x+4
60
If more than one operator appears in an expression, the order of evaluation depends on the rules pf precedence.
  • Parentheses have the highest precedence. eg. 2 * (3-1) is 4
  • Exponentiation has the next higher precedence. eg. 2**1 + 1 is 3
  • Multiplication and division have the same precedence. eg. 2*3-1 is 5
  • Addition and subtraction have the next precedence. eg. 6+4/2 is 8.  Operators with the same precedence are evaluated from left to right.
Strings are enclosed in single or double quotes. '+' operator used for concatenation.
Example:
>>> a='Hi'
>>> b='Alice'
>>> a+b # '+' is used for concatenation operation.
'HiAlice'
>>>
'*' operator is also work with string.
>>> a*3 # Disply the value of a in 3 times
'HiHiHi'
>>>

Sample exercise
Assume that we execute the following assignment statements:
width = 17
height = 12.0
delimiter = '.'
For each of the following expressions, write the value of the expression
  1. width/2
  2. width/2.0
  3. height/3
  4. 1 + 2 * 5
  5. delimiter * 5
Use the Python interpreter to check your answers
Answer
>>> width=17
>>> height=12.0
>>> delimiter = '.'
>>> width/2
8
>>> width/2.0
8.5
>>> height/3
4.0
>>> 1+2*5
11
>>> delimiter *5
'.....'
>>>

Variables

    Two basic things related to the programs are values and types. Values are like 1, 5, 2.7, and 'Hello'. Here 1 and 5 is an integer (int), 2.7 is a floating point number (float) and 'Hello' is a string (str) .
     Variable is a name that refers to a value. In python type of the variable is not defined when we create a variable. Based on the value of the variable memory is allocated for it.
Assignment statement (=) used to assign values to a variable.
Example:
>>> message = 'Hello'
>>> message
'Hello'
>>> type(message)
<type 'str'>
>>>
Here message is a variable, we assign a value 'Hello' to it. If you are not sure what type a value has, the interpreter can tell you. In this example 'Hello' is a string (str). Based on this value memory is allocated for message.

Another examples are,
>>> a=4.5
>>> type(a)
<type 'float'>
>>> b='Alice'
>>> type(b)
<type 'str'>
>>> c=684
>>> type(c)
<type 'int'>
>>> a
4.5
>>> b
'Alice'
>>> c
684
>>>
We can also use the print statement to display the value of a variable.

>>> a='Bob'
>>> print a
Bob
>>>
We can assign value of one variable in to another variable by using assignment operator.

>>> a=45
>>> b=a
>>> a
45
>>> b
45
>>>
Variable names can contain both letters and numbers. But they have to begin with a letter. The underscore character (_) can appear in a name. It is often used in names with multiple words such as
front_end, back_end. If we given an illegal name, get a syntax error.
Example:

>>> 76message='Hello'                # Begin with integer
SyntaxError: invalid syntax
>>>
>>> class = 23                              # 'class' is an python keyword
SyntaxError: invalid syntax
>>>
>>> begin@ = 3                           # '@' is an illegal character
SyntaxError: invalid syntax
>>>
python has 31 keywords.
and del from not while as elif global or with assert else if pass yield break except import print class exec in raise continue finally is return def for lambda try
The interpreter uses keywords to recognize the structure of the program, and they cannot be used as variable names.