Lab 2
Lab 2
Objectives
• To understand how numbers and characters are
represented in computers
• To understand and use operators for numbers
• To understand arithmetic expression
• To understand data type and use type conversion
• To be able to read and write programs that process
numerical data.
Real number system
• Recall from your math class, real numbers consist of
rational numbers and irrational numbers.
• A rational number can be represented as i/j for integers i and j.
• Irrational numbers can be further divided into algebraic (real root of
polynomials with rational coefficients) and transcendental.
Numeric data types
• Computers “simulate” the real number system.
• Two numeric data types:
• Integer (int), e.g., 10, 0, -9999
• Floating-point number (float), e.g., 1.1, 0., -3333.33
• int and float are two different data types.
• A floating-point number can be represented by including
an exponent component, e.g., -3.33333x103 (try to type
-3.33333e3 in Python and see the output)
• Inside the computer, integers and floating point are
represented quite differently.
• Negative integer is usually represented in two's complement (to be
covered elsewhere).
EXERCISE 2.1
o Enter a very large integer in your IDLE and see
whether the returned value is the same as the entered
value.
o Repeat above with a very large floating-point number.
o Is 3.3333333333e3 or 3.33e33 larger?
Rounding
• The displayed value can be rounded (sometimes
truncated).
• Several related functions:
• round(x,n) built-in function – round to n decimal places
• math.ceil(x) math function – round up
• math.floor(x) math function – round down
Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.
Operator precedence and associativity
• The operators ** and – (negation) have higher
precedence than the four operators (+, –, *, /).
• For operators of the same precedence, the associativity
determines the order of their operations.
Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.
EXERCISE 2.7
Try the followings:
• 2**3**4
• 8+4-2
• 8-4-2
• 8*4/3
• 8/4/2
Data types
• A data type is a set of values, and a set of operators that
may be applied to those values.
• Integer, floating-point numbers and string are built-in types in
Python.
• An internal representation could have different meanings:
• 01000001bin can be interpreted as "A" (ASCII) or 65dec.
• Each literal or variable is associated with a data type (int
and float for now).
• A type(x) function returns the data type of x which
could be a literal or variable.
• Explicit type conversion
• Built-in functions int(x) and float(x).
EXERCISE 2.8
o Try out the type() function for both numeric and string
literals and variables.
o Assign 10 to x and find out the type of x, and assign
10.0 to x and find out its type.
o Try to use int() instead of eval() to get your age, and
explore with different inputs.
EXERCISE 2.9
What are the data types of the following arithmetic
expressions: 6+3, 6.0+3.0, 6.0+3, 6.00+3.00, 6*3,
6.0*3.0, 6.0*3, 6/3, 6.0/3.0, 6.0/3?
Try to answer yourself before asking Python for the
answer.
EXERCISE 2.10
• Try the following
• int(11.1) How can you get answer to
• int("11") int("11.1"), i.e., 11?
• int("11.1")
• float(11)
• float("11")
• float("11.1")
• float("1.1111111111111111")
END
References
• A Tutorial on Data Representation: Integers, Floating-
point Numbers, and Characters:
https://www3.ntu.edu.sg/home/ehchua/programming/java/
DataRepresentation.html