Python Bibliotheca
Python resources for teachers and students.

Data Types and Variables


Complete this worksheet as you read through Chapter 2 of How to Think Like a Computer Scientist: Learning with Python. Use the python shell to help you answer these questions:

Part I

    1. What is the difference between a string and an integer/float?
    2. What makes a float different from an integer?
    3. Given a variable, x, what is the command to get python to print out the type of x?
  1. What is a keyword?
  2. What is a statement? Give 2 examples.
  3. What is an expression? Give 2 examples.
  4. What is the difference between a statement and an expression?
  5. The following statement produces no output when not run in the shell.
        3.14 * 6 * 6
    Modify it to produce output.
  6. What is an operator?
  7. What problem arises in integer devision?
    • I want to divide 1 by 3 and get a result like: 0.333333333333. What is the exact command to do this?
    • HINT: you must use floats! What do floats have that integers do not have?
  8. Integer division always rounds which way? (up or down)
  9. What is concatenation, and on what type of variables (integers/floats/strings) does it operate on?
  10. Why do we use comments? (what is their purpose)
  11. Enter the following code:
          >>> 1 == 1
          >>> "1" == "1"
          >>> 1 == "1"
    Why is the third line false, while the first 2 lines are true?

Part II

What exactly is a float?


Try entering the following at the python prompt:


which will give you:


Why do you get this?

Now try entering:

65487513241654687642534657498643214654.0 == 65487513241654687642534657498643214654.0


65487513241654687642534657498643214654.0 == 65487513241654687642534657498643214654.0 + 10000

Write down the interpreter's response in your notes, then complete the following:

  1. Explain the danger when using floating point numbers.
  2. Show how the number 2.0014 is represented using the base β and precision p.
  3. Give and example (pick an x and y > 0) that shows x + y = x, for the x and y that you choose.

[ Copyright 2007, Dan Schellenberg]

Comments, questions, or suggestions? Email the webmaster