Data Types and Variables
Directions:
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
-
- What is the difference between a string and an integer/float?
- What makes a float different from an integer?
- Given a variable, x, what is the command to get python to print out
the type of x?
- What is a keyword?
- What is a statement? Give 2 examples.
- What is an expression? Give 2 examples.
- What is the difference between a statement and an expression?
- The following statement produces no output when not run in the shell.
3.14 * 6 * 6
Modify it to produce output.
- What is an operator?
- 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?
- Integer division always rounds which way? (up or down)
- What is concatenation, and on what type of variables
(integers/floats/strings) does it operate on?
- Why do we use comments? (what is their purpose)
- 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?
Read:
Try entering the following at the python prompt:
65487513241654687642534657498643214654.0
which will give you:
6.548751324165469e+37
Why do you get this?
Now try entering:
65487513241654687642534657498643214654.0 == 65487513241654687642534657498643214654.0
and
65487513241654687642534657498643214654.0 == 65487513241654687642534657498643214654.0 + 10000
Write down the interpreter's response in your notes, then complete
the following:
- Explain the danger when using floating point numbers.
- Show how the number 2.0014 is represented using the base
β and precision p.
- Give and example (pick an x and y > 0) that shows x + y = x,
for the x and y that you choose.
|