Sunday, June 19, 2011

Exercises on Statements and Expressions

The Think Python author has given 2 more sample problems in order to understand the various statement and expression behaviours.

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 and the type (of 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.

I’m trying to guess the answers first.
           1.      8
           2.     8.5
           3.     4.0
           4.     11
           5.     …..

Now I’m trying the same using interactive mode and the snapshot is given below.

Next problem is
Practice using the Python interpreter as a calculator:
The volume of a sphere with radius r is 4/3 π r3. What is the volume of a sphere with radius 5? Hint: 392.6 is wrong!

We should carefully use the integers and floating point values in division.



No comments:

Post a Comment

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