0% found this document useful (0 votes)
13 views

Data and Expessions

The document discusses various topics related to data and expressions including literals, variables, identifiers, operators, and data types. It provides examples and explanations of numeric, string, and boolean literals as well as implicit and explicit conversions between different data types in expressions.

Uploaded by

isidro ylanan
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)
13 views

Data and Expessions

The document discusses various topics related to data and expressions including literals, variables, identifiers, operators, and data types. It provides examples and explanations of numeric, string, and boolean literals as well as implicit and explicit conversions between different data types in expressions.

Uploaded by

isidro ylanan
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/ 30

Data and

Expressions
SY 2020-2021 3rd Term
CS001L Computer Fundamentals and Programming 1
Laboratory
Objectives
This lesson aims to explain topics about Data
and Expressions composed of literals, variables,
identifiers, operators, expressions, and data
types.
Literals
A literal is a sequence of
one or more characters Types of Literals
that stands for itself. It is
a succinct and visible integers
way to write a value. Floating point
booleans
strings
Literals:

Numeric Literals
A numeric literal is a literal containing only the
digits 0–9, a sign character (+ or -) and a
possible decimal point.
Literals:
Limits of Range in Floating-Point
Representation
Arithmetic overflow occurs when a calculated
result is too large in magnitude to be represented.
ex. >>> 1.5e200 * 2.0e210
>>> inf
Arithmetic underflow occurs when a calculated
result is too small in magnitude to be represented.
ex. >>>1.0e2300 / 1.0e100
0.0
Literals:
String Literals
A string literal, or string, is a sequence of
characters denoted by a pair of matching
single or double (and sometimes triple) quotes
in Python.
Ex. 'Hello’
'Smith, John’
"Baltimore, Maryland 21210"
A string consisting of only a pair of matching quotes is called the
empty string.
Literals:
Control Characters

Control characters are nonprinting characters


used to control the display of output (among
other things).
An escape sequence is a string of one or more
characters used to denote control characters.

ex. print('Hello\nJennifer Smith')


Literals:
String Formatting

Built-in function format can be used to control


how strings are displayed.
format(value, format_specifier)
Left Justified ex. format('Hello', ' < 20') ➝ 'Hello ‘

Right Justified format('Hello', ' > 20') ➝ ‘ Hello’


Literals:
Implicit and Explicit Line Joining

print('Name:', student_name, 'Address:', student_address, 'Number of Credits:',


total_credits, 'GPA:', current_gpa)

numsecs_1900_dob = ((year_birth - 1900) * avg_numsecs_year) + \


((month_birth - 1) * avg_numsecs_month) + \
(day_birth * numsecs_day)
Variables and Identifiers:
What is a variable?

A variable is a name that is associated with a


value.
The assignment operator, =, is used to assign
values to variables.
>>>id(num) >>>id(k)
505494040 505494040

An immutable value is a value that cannot be


changed.
Ex.
var = 12 integer
var = 12.45 float
var = 'Hello’ string
Variables and Identifiers:
Variable Assignment and Keyboard Input

All input is returned by the input function as a


string type. Built-in functions int() and float()
can be used to convert a string to a numeric
type.

num_credits = int(input('How many credits do you have? ‘))

gpa = float(input('What is your grade point average? '))


Variables and Identifiers:
What is an identifier?

An identifier is a sequence of one or more


characters used to name a given program
element. It may contain letters and digits, but
cannot begin with a digit. The special
underscore character can also be used.
Variables and Identifiers:
Keywords and Other Predefine Identifiers in Python

A keyword is an identifier that has predefined


meaning in a programming language and
therefore cannot be used as a “regular” identifier.
Doing so will result in a syntax error.
>>> and = 10
SyntaxError: invalid syntax
Operators

An operator is a symbol that represents an


operation that may be performed on one or
more operands.
Operators that take one operand are called
unary operators.
Operators that take two operands are called
binary operators.
20 - 5 ➝ 15 ( - as binary operator)
-10 * 2 ➝ - 20 ( - as unary operator)
Operators:
Arithmetic Operators

The division operator, /, produces “true division”


regardless of its operand types.

The truncating division operator, //, produces either


an integer or float truncated result based on the
type of operands applied to.
The modulus operator (%) gives the remainder of
the division of its operands.
Expressions and Data Types

An expression is a combination of symbols (or


single symbol) that evaluates to a value.
A subexpression is any expression that is part
of a larger expression.
ex. 4 + (3 * k)
Expressions and Data Types:
Operator Precedence

The way we commonly represent expressions,


in which operators appear between their
operands, is referred to as infix notation.
4+3
Operator precedence is the relative order that
operators are applied in the evaluation of
expressions, defined by a given operator
precedence table.
Expressions and Data Types:
Operator Precedence
Expressions and Data Types:
Operator Precedence

ex. 1
4 + 3 * 5 ➝ 4 + 15 ➝ 19
ex. 2
(4 + 3) * 5 ➝ 7 * 5 ➝ 35
ex. 3
4 + 2 ** 5 // 10 ➝ 4 + 32 // 10 ➝ 4 + 3 ➝ 7
Expressions and Data Types:
Operator Associativity

Operator associativity is the order that


operators are applied when having the same
level of precedence, specific to each operator.
ex. (2 + 3) + 4 ➝ 9 2 + (3 + 4) ➝ 9

(a) (8 - 4) - 2 ➝ 4 - 2 ➝ 2 8 - (4 - 2) ➝ 8 - 2 ➝ 6
(b) (8 / 4) / 2 ➝ 2 / 2 ➝ 1 8 / (4 / 2) ➝ 8 / 2 ➝ 4
(c) 2 ** (3 ** 2) ➝ 512 (2 ** 3) ** 2 ➝ 64
Expressions and Data Types:
Data Types

A data type is a set of values, and a set of


operators that may be applied to those values.

Predefine Data Types

integers floats strings


Expressions and Data Types:
Data Types

Data Type
Approaches

Static Typing

Dynamic
Typing
Expressions and Data Types:
Mixed-Type Expressions

A mixed-type expression is an expression with


operands of different type.

Common Type of Conversion


implicit
explicit
End

Thanks!

You might also like