0% found this document useful (0 votes)
33 views26 pages

Lab 2

Uploaded by

ngalva0825
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views26 pages

Lab 2

Uploaded by

ngalva0825
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

PYTHON: BASIC DATA TYPES

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

• To make use of math.*** functions, you need to


• import math
EXERCISE 2.2
o Try round(0.45,1), round(1.45,1), round(2.45,1),
round(3.45,1), …, round(9.45,1). Do you observe
any patterns?
o Try math.ceil(5.45) and math.floor(5.45).
o Try math.ceil(-5.45) and math.floor(-5.45).
o Try int(5.45), int(-5.45) and float(5).
String
• Strings in Python can be expressed inside double quotes
or single quotes.
• A string can be empty.
• Strings in Python are represented by UTF-8 using 8 to 32
bits to represent a character.
• The 8-bit representation is the same as the ASCII (American
Standard Code for Information Interchange).
• The ord function returns the numeric (ordinal) code of a
single character, e.g., ord('A') is 65.
• The chr function converts a numeric code to the
corresponding character, e.g., chr(65) is 'A'.
ASCII table
EXERCISE 2.3
Try the followings:
• print('') # two single quotes without space
• print("") # two double quotes without space
• print(" ' ")
• print(' " ')
• print("') # double quote + single quote
• ord('') # two single quotes without space
• ord(' ') # space inside
• ord("") # two double quotes without space
• ord(" ") # space inside
Control characters
• Control characters are special characters that are not displayed on
the screen, and they control the display of output (among other
things).
• An escape sequence begins with an escape character (\) that causes
the sequence of characters following it to “escape” their normal
meaning.
• Escape sequences are strings.
• Some useful ones:
• \’ single quote
• \” double quote
• \t tab
• \n give a newline
• \\ give the backslash itself
• \ooo gives the ASCII character represented by ooooct, e.g. “\063” = “3”.
• \xhh gives the ASCII character represented by hhhex, e.g. “\x41” = “A”.
• \uhhhh gives Unicode character represented by hhhhhex, e.g. “\u2190” = “”,
“\u5927” = “⼤”, “\u3042” = “あ”, “\u3184” = “ㆄ”.
EXERCISE 2.4
Try
• print("1\t2\t3")
• print("1\n2\n3")
• print("\"")
• print("\\")
• print("\")
• print("\u5927")
Assignment statements
• Simple assignment: <variable> = <expr>
variable is an identifier, expr is an expression
• The expression on the RHS (right hand side) is evaluated
to produce a value which is then associated with the
variable named on the LHS (left hand side).
EXERCISE 2.5
Ask users to input two numbers and print out
the two numbers in a reversed order.
Simultaneous Assignment
• Several values can be calculated at the same time.
• <var>, <var>, … = <expr>, <expr>, …
• Evaluate the expressions in the RHS and assign them to
the variables on the LHS.
• E.g., x, y = y, x
• E.g., sum, diff = x+y, x-y
• E.g., x, y = eval(input("Input the first and
second numbers separated by a comma: "))
EXERCISE 2.6
Simplify your codes in exercise 2.5 using
simultaneous assignment statements.
Expressions
• The fragments of code that produce or calculate new data
values are called expressions.
• A (numeric/string) literal, which is the simplest kind of
expression, is used to represent a specific value, e.g. 10
or “Mickey”.
• A simple identifier can also be an expression.
• Simpler expressions can be combined using operators +,
-, *, /, and ** (special operator // for integer division).
• The normal mathematical precedence applies.
• Only round parentheses can be used to change the precedence,
e.g., ((x1 – x2) / 2*n) + (spam / k**3).
• Try print("I" + "love" + "Mickey").
• Try print("I","love","Mickey").
Python built-in numeric operations

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

You might also like