Session 5 CPSC 231 Tu02 Table of content

Session 5 CPSC 231 Tu#02

Table of content • Data Types • Operations • Operator Preceding • Exercises 2

Data Types • A Data Type is a set of values 3

Literals • Python-code representation of a data-type • Int: A sequence of digits • 1234 or 99 • float: A sequence of digits and a decimal point • 3. 14 • bool: True of False • str: Sequence of characters enclosed in quotations 4

Operators • Python-code representation of data-type operation • +, • and, or, not 5

Identifiers • Python-code representation of a name • A name used to identify a variable, function, class, module or other object. • a sequence of letters, digits, and underscores, the first of which is not a digit • abc, Ab_, abc 123 6

Variables • A name associated with a data-type value • Counter = 3 • total = 150 • A constant variable is a variable whose value does not change during the program 7

Expression • An expression is a combination of literals, variables, and operators • An expression is a directive to perform a sequence of operations • Most of the operators that we use are binary operators that take exactly two operands • Each operand can be any expression, perhaps within parentheses • Example: 8

Operator Precedence • All operators are left associative except ** 9

Operator Precedence (Cont’d) 10

Operator Precedence (Cont’d) 11

• a. stdio. writeln(2 + 3) • b. stdio. writeln(2. 2 + 3. 3) • c. stdio. writeln('2' + '3') • d. stdio. writeln('2. 2' + '3. 3') 12

• 1. 2. 15 Suppose that x and y are two floats that represent the Cartesian coordinates of a point (x, y) in the plane. Give an expression that evaluates to the distance of the point from the origin. • 1. 2. 16 Compose a program that takes two integers a and b from the command line and writes a random integer between a and b. • 1. 2. 17 Compose a program that writes the sum of two random integers between 1 and 6 (such as you might get when rolling dice). • 1. 2. 18 Compose a program that takes a float t from the command line and writes the value of sin(2 t) + sin(3 t). • 1. 2. 19 Compose a program that takes three floats x 0, v 0, and t from the command line, evaluates x 0 + v 0 t − G t 2/ 2, and writes the result. (Note: G is the constant 9. 80665. This value is the displacement in meters after t seconds when an object is thrown straight up from initial position x 0 at velocity v 0 meters per second. ) 13
- Slides: 13