Example 5.1. Using Expressions
#!/usr/bin/python # Filename : expression.py length = 5 breadth = 2 area = length * breadth print 'Area is', area print 'Perimeter is', 2 * (length + breadth)
Here, we are given the length and breadth of a rectangle. We then calculate the area and perimeter of the rectangle using expressions. We store the result of the expression length * breadth in the variable area and then print it using the print statement. In the second case, we directly use the value of the expression 2 * (length + breadth) in the print statement.
Also, notice how Python "pretty-prints" the output. Even though we have not specified a space between 'Area is' and the variable area, Python puts it for us so that we get a clean nice output and the program is much more readable this way. This is an example of how Python makes life easy for the programmer.