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

ITE 260 SAS Day 04 Data and Expressions

Uploaded by

mAikle
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)
41 views

ITE 260 SAS Day 04 Data and Expressions

Uploaded by

mAikle
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/ 12

ITE 260: Computer Programming 1

Module #4Student Activity Sheet


Class number: _______
Name:_________________________________________________________ Date: _______________
Section: ____________ Schedule: __________________________________

Lesson Title: Data and Expressions Materials:


Lesson Targets: SAS
At the end of this module, students will be able to: References:
1. Discuss the basic data structures and the correct naming and ● https://www.tutorialspoint.com/
declaration of variables and constants. python/python_basic_syntax.ht
2. Analyze the Python program that makes use of variables and m
operators. ● https://www.tutorialspoint.com/
python/python_variable_types.
htm

1. LESSON PREVIEW/REVIEW
Introduction
Good day, everyone!
You have learned a concise overview of the current generation of programming languages, compilers, and the
fundamentals of Python programming. We'll go over fundamental data structures as well as proper variable
and constant naming and declaration. Variables are only reserved memory spaces for the storage of values.
This implies that you set aside some memory when you create a variable. The interpreter allots memory and
determines what can be placed in the reserved memory based on the data type of a variable. Therefore, you
can store integers, decimals, or characters in these variables by giving them alternative data types.
So, let's get started!

B.MAIN LESSON

Content and Skill-Building

Python identifier naming conventions:


Uppercase letters are used to begin class names.
● Every other identifier begins with a lowercase letter.
● An identity that begins with a single leading underscore indicates that it is private.
● Starting an identifier with two leading underscores indicates that it is highly private.
● If the identifier is followed by two trailing underscores, it is a language-defined special name.

What is keyword?
These are reserved words that cannot be used as constant, variable, or identifier names. All Python keywords
use only lowercase letters.
and exec not del else
assert finally or import is
break for pass try with
class from print elif except

This document is the property of PHINMA EDUCATION


ITE 260: Computer Programming 1
Module #4Student Activity Sheet
Class number: _______
Name:_________________________________________________________ Date: _______________
Section: ____________ Schedule: __________________________________

continue global raise in lambda


def if return while yield

Lines and indention


Python does not use brackets to mark code blocks for class and function definitions or flow control. Line
indentation, which is strictly enforced, is used to designate code blocks. The number of spaces used for
indentation varies, but all statements within a block must be indented the same amount.
As an example, However, the following block generates an error −
if True: ifTrue:
print "True" print"Answer"
else: print"True"
print "False" else:
print"Answer"
print"False"

Thus, in Python all the continuous lines indented with same number of spaces would form a block.

In Python, statements are often terminated with a new line. Python does, however, allow the use of the line
continuation character (\) to signify that the line should continue.
Example: total = item_one + \
item_two + \
item_three
The line continuation character is not required for statements enclosed by the [], or () brackets.
Example: days = ['Monday', 'Tuesday', 'Wednesday','Thursday', 'Friday']

Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same
type of quote starts and ends the string. To span the string across many lines, use triple quotes.
Example: word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""

A comment is introduced by a hash sign (#) that is outside of a string literal. The Python interpreter ignores all
characters that come after the # and before the end of the actual line since they are considered to be part of
the comment.
Example: # First comment
print"Welcome to Computer Programming 1!"# second comment
Output: Welcome to Computer Programming 1!

Assigning values to variables

To set aside memory, Python variables don't require an explicit declaration. When you provide a variable a
value, the declaration occurs automatically. For assigning values to variables, use the equal sign (=). The
name of the variable is the operand to the left of the = operator, and the value that is placed in the variable is
the operand to the right of the = operator.
Example: Output:
counter=100# An integer assignment 100

This document is the property of PHINMA EDUCATION


ITE 260: Computer Programming 1
Module #4Student Activity Sheet
Class number: _______
Name:_________________________________________________________ Date: _______________
Section: ____________ Schedule: __________________________________

miles=1000.0# A floating point 1000.0


name="Prince"# A string Prince
print counter```
print miles
print name

a=b=c=1 #a=1, b=1, c=1


a,b,c = 1,2,"john" #a=1, b=2, c=”john”

Standard Data types


1. Numbers - Numeric values are stored in number data types. When you assign a value to a number
object, it is generated.
Example: var1 = 1
var2 = 10
Four different numerical types:
1) int (signed integers)
2) long (long integers, they can also be represented in octal and hexadecimal)
3) float (floating point real values)
4) complex (complex numbers)
Examples:
int long float complex
10 51924361L 0.0 3.14j
100 -0x19323L 15.20 45.j
-786 0122L -21.9 9.322e-36j
080 0xDEFABCECBDAECBFBAEl 32.3+e18 .876j
-0490 535633629843L -90. -.6545+0J
-0x260 -052318172735L -32.54e100 3e+26J
0x69 -4721885298529L 70.2-E12 4.53e-7j
2. Strings –In Python, a string is defined as a contiguous group of characters denoted by quote marks.
Python supports either single or double quotes. Subsets of strings can be obtained by employing the
slice operators ([] and [:]), with indexes beginning at 0 at the beginning and working their way to -1 at
the end. The string concatenation operator is the plus (+) sign, and the repetition operator is the
asterisk (*).
Example:
str = ‘Welcome to Programming!'

printstr # Prints complete string


printstr[0] # Prints first character of the string
printstr[2:7] # Prints characters starting from 3rd to 5th
printstr[2:] # Prints string starting from 3rd character
printstr * 2 # Prints string two times
printstr + "Dear" # Prints concatenated string

3. Lists - Lists are the most versatile compound data type in Python. Items in a list are separated by
commas and enclosed in square brackets ([]). Lists are similar to arrays in C in certain ways. One
distinction is that all of the entries in a list can be of different data types. The values in a list can be
retrieved using the slice operator ([] and [:]), with indexes beginning at 0 and working their way to the
end -1. The list concatenation operator is the plus (+) sign, while the repetition operator is the asterisk
(*).

This document is the property of PHINMA EDUCATION


ITE 260: Computer Programming 1
Module #4Student Activity Sheet
Class number: _______
Name:_________________________________________________________ Date: _______________
Section: ____________ Schedule: __________________________________

Example:
list = [ 'snip', 529 , 1.15, 'iya', 80.3 ]
tinylist = [127, 'prince']

print list # Prints complete list


print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd till 3rd
print list[2:] # Prints elements starting from 3rd element
printtinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists
OUTPUT:
[ 'snip', 529 , 1.15, 'iya', 80.3 ]
snip
[529 , 1.15]
[1.15, 'iya', 80.3 ]
[127, 'prince', 127, 'prince']
['snip', 529, 1.15, 'iya', 80.3, 127, 'prince']

4. Tuples - A tuple is a form of sequence data that is comparable to a list. A tuple is a collection of values
separated by commas. Tuples, unlike lists, are wrapped in parentheses. The major distinction between
lists and tuples is that lists are contained in brackets ([]), and their elements and size can be altered,
but tuples are encased in parentheses (()), and their elements and size cannot be changed.Tuples are
similar to read-only lists.
Example:
tuple = ( 'snip', 529 , 1.15, 'iya', 80.3 )
tinytuple = (127, 'prince')

print tuple # Prints the complete tuple


print tuple[0] # Prints first element of the tuple
print tuple[1:3] # Prints elements of the tuple starting from 2nd till 3rd
print tuple[2:] # Prints elements of the tuple starting from 3rd element
printtinytuple * 2 # Prints the contents of the tuple twice
print tuple + tinytuple # Prints concatenated tuples

OUTPUT:
('snip', 529, 1.15, 'iya', 80.3)
snip
(529, 1.15)
(1.15, 'iya', 80.3)
(127, 'prince', 127, 'prince')
('snip', 529, 1.15, 'iya', 80.3, 127, 'prince')

5. Dictionary -Dictionaries in Python are a form of hash table. They function similarly to associative
arrays or hashes found in Perl and are made up of key-value pairs. A dictionary key can be practically
any Python type, however they are most commonly numbers or strings. In contrast, values can be any
arbitrary Python object. Curly braces ({ }) surround dictionaries, whereas square braces ([ ]) allow
values to be assigned and accessed.
Example:
dict = {}
dict['one'] = "This is sam"

This document is the property of PHINMA EDUCATION


ITE 260: Computer Programming 1
Module #4Student Activity Sheet
Class number: _______
Name:_________________________________________________________ Date: _______________
Section: ____________ Schedule: __________________________________

dict[2] = "This is iya"

tinydict = {'name': 'prince','code':1527, 'dept': 'sales'}

printdict['one'] # Prints value for 'one' key


printdict[2] # Prints value for 2 key
printtinydict # Prints complete dictionary
printtinydict.keys() # Prints all the keys
printtinydict.values() # Prints all the values

Operator
Operators are constructs that can change the values of operands. Consider the formula 11 + 5 = 16.
11 and 5 are termed operands, while + is called the operator.

Types of Operator
1. Arithmetic Operators
Operator Description Example
+ Addition Adds values on either side of the operator. a + b = 30
Subtracts right hand operand from left hand
- Subtraction a – b = -10
operand.
*
Multiplies values on either side of the operator a * b = 200
Multiplication
Divides left hand operand by right hand
/ Division b/a=2
operand
Divides left hand operand by right hand
% Modulus b%a=0
operand and returns remainder
Performs exponential (power) calculation on
** Exponent a**b =10 to the power 20
operators
Floor Division - The division of operands where
the result is the quotient in which the digits after
the decimal point are removed. But if one of the 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4,
//
operands is negative, the result is floored, i.e., -11.0//3 = -4.0
rounded away from zero (towards negative
infinity) −
Example:
a = 11
b = 12
c = 27

c=a+b
print "Line 1 - Value of c is ", c

c=a-b
print "Line 2 - Value of c is ", c

c=a*b
print "Line 3 - Value of c is ", c

c=a/b

This document is the property of PHINMA EDUCATION


ITE 260: Computer Programming 1
Module #4Student Activity Sheet
Class number: _______
Name:_________________________________________________________ Date: _______________
Section: ____________ Schedule: __________________________________

print "Line 4 - Value of c is ", c

c=a%b
print "Line 5 - Value of c is ", c

a=8
b=3
c = a**b
print "Line 6 - Value of c is ", c

a = 29
b=5
c = a//b
print "Line 7 - Value of c is ", c
2. Comparison (Relational) Operators- These operations compare the values on both sides and determine the
relationship between them. Relational operators are another name for them.
Operator Description Example
The condition becomes true if the values of two
== (a == b) is not true.
operands are equal.
If the values of two operands differ, the
!= (a != b) is true.
condition becomes true.
If the values of two operands differ, the
<> (a <> b) is true. This is similar to != operator.
condition becomes true.
The condition becomes true if the left operand's
> (a > b) is not true.
value exceeds the right operand's value.
If the left operand's value is lower than the right
< (a < b) is true.
operand's, the condition becomes true.
The condition becomes true if the value of the
>= left operand is larger than or equal to the value (a >= b) is not true.
of the right operand.
If the left operand's value is less than or equal
<= to the right operand's value, the condition (a <= b) is true.
becomes true.
Example:
a = 12
b = 27
c=0

if ( a == b ):
print "Line 1 - a is equal to b"
else:
print "Line 1 - a is not equal to b"

if ( a != b ):
print "Line 2 - a is not equal to b"
else:
print "Line 2 - a is equal to b"

if ( a <> b ):

This document is the property of PHINMA EDUCATION


ITE 260: Computer Programming 1
Module #4Student Activity Sheet
Class number: _______
Name:_________________________________________________________ Date: _______________
Section: ____________ Schedule: __________________________________

print "Line 3 - a is not equal to b"


else:
print "Line 3 - a is equal to b"

if ( a < b ):
print "Line 4 - a is less than b"
else:
print "Line 4 - a is not less than b"

if ( a > b ):
print "Line 5 - a is greater than b"
else:
print "Line 5 - a is not greater than b"

a = 5;
b = 29;
if ( a <= b ):
print "Line 6 - a is either less than or equal to b"
else:
print "Line 6 - a is neither less than nor equal to b"

if ( b >= a ):
print "Line 7 - b is either greater than or equal to b"
else:
print "Line 7 - b is neither greater than nor equal to b"

3. Assignment Operators
Operator Description Example
Values from the right side operands are
= c = a + b assigns value of a + b into c
assigned to the left side operand.
+= Add It assigns the result to the left operand after
c += a is equivalent to c = c + a
AND adding the right operand to the left operand.
-= The right operand is subtracted from the left
Subtract operand, and the result is assigned to the left c -= a is equivalent to c = c - a
AND operand.
The right operand and left operand are
*= Multiply
multiplied, and the output is assigned to the left c *= a is equivalent to c = c * a
AND
operand.
/= Divide The result is assigned to the left operand after
c /= a is equivalent to c = c / a
AND the left operand and right operand are divided.
%=
Two operands are used to take the modulus,
Modulus c %= a is equivalent to c = c % a
and the result is assigned to the left operand.
AND
**=
It calculates operators' exponential (power)
Exponent c **= a is equivalent to c = c ** a
values and assigns them to the left operand.
AND
//= Floor It divides operators by floor and gives the left
c //= a is equivalent to c = c // a
Division operand a value.

This document is the property of PHINMA EDUCATION


ITE 260: Computer Programming 1
Module #4Student Activity Sheet
Class number: _______
Name:_________________________________________________________ Date: _______________
Section: ____________ Schedule: __________________________________

Example:
a = 27
b = 12
c=0

c=a+b
print "Line 1 - Value of c is ", c

c += a
print "Line 2 - Value of c is ", c

c *= a
print "Line 3 - Value of c is ", c

c /= a
print "Line 4 - Value of c is ", c

c =2
c %= a
print "Line 5 - Value of c is ", c

c **= a
print "Line 6 - Value of c is ", c

c //= a
print "Line 7 - Value of c is ", c
4. Logical Operators
Operator Description Example
and Logical AND The condition is true if both operands are true. (a and b) is true.
The condition is true if either of the two operands
or Logical OR (a or b) is true.
is non-zero.
It is used to change the operand's operand's
not Logical NOT Not(a and b) is false.
logical state.

5. Bitwise Operators
Operator Description Example
If a bit exists in both operands, the operator
& Binary AND (a & b) (means 0000 1100)
transfers it to the result.
| Binary OR If a bit exists in either operand, it is copied. (a | b) = 61 (means 0011 1101)
If the bit is set in one operand but not both, it
^ Binary XOR (a ^ b) = 49 (means 0011 0001)
is copied.
(~a ) = -61 (means 1100 0011 in 2's
~ Binary Ones
It has the effect of 'flipping' bits and is unary. complement form due to a signed binary
Complement
number.
<< Binary Left The value of the left operand is shifted left by
a << 2 = 240 (means 1111 0000)
Shift the number of bits given by the right operand.

This document is the property of PHINMA EDUCATION


ITE 260: Computer Programming 1
Module #4Student Activity Sheet
Class number: _______
Name:_________________________________________________________ Date: _______________
Section: ____________ Schedule: __________________________________

The value of the left operand is shifted right


>> Binary
by the number of bits given by the right a >> 2 = 15 (means 0000 1111)
Right Shift
operand.

Example:
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c=0

c = a & b; # 12 = 0000 1100


print "Line 1 - Value of c is ", c

c = a | b; # 61 = 0011 1101
print "Line 2 - Value of c is ", c

c = a ^ b; # 49 = 0011 0001
print "Line 3 - Value of c is ", c

c = ~a; # -61 = 1100 0011


print "Line 4 - Value of c is ", c

c = a << 2; # 240 = 1111 0000


print "Line 5 - Value of c is ", c

c = a >> 2; # 15 = 0000 1111


print "Line 6 - Value of c is ", c

6. Membership Operators - Python’s membership operators test for membership in a sequence, such as
strings, lists, or tuples.
Operator Description Example
If a variable is found in the prescribed sequence, x in y, here in results in a 1 if x is a member of
in
the evaluation is true; otherwise, it is false. sequence y.
If a variable is not found in the specified
x not in y, here not in results in a 1 if x is not a
not in sequence, the evaluation returns true; otherwise,
member of sequence y.
it returns false.
Example:
a = 12
b = 27
list = [12, 5, 3, 8, 29 ];

if ( a in list ):
print "Line 1 - a is available in the given list"
else:
print "Line 1 - a is not available in the given list"

if ( b not in list ):
print "Line 2 - b is not available in the given list"
else:

This document is the property of PHINMA EDUCATION


ITE 260: Computer Programming 1
Module #4Student Activity Sheet
Class number: _______
Name:_________________________________________________________ Date: _______________
Section: ____________ Schedule: __________________________________

print "Line 2 - b is available in the given list"

a=2
if ( a in list ):
print "Line 3 - a is available in the given list"
else:
print "Line 3 - a is not available in the given list"

7. Identity Operators - Identity operators compare the memory locations of two objects.
Operator Description Example
Evaluates to true if the variables on either side of
is the operator point to the same object and false x is y, here is results in 1 if id(x) equals id(y).
otherwise.
Evaluates to false if the variables on either side
x is not y, here is not results in 1 if id(x) is not
is not of the operator point to the same object and true
equal to id(y)
otherwise.
Example:
a = 27
b = 27

if ( a is b ):
print "Line 1 - a and b have same identity"
else:
print "Line 1 - a and b do not have same identity"

if ( id(a) == id(b) ):
print "Line 2 - a and b have same identity"
else:
print "Line 2 - a and b do not have same identity"

b = 29
if ( a is b ):
print "Line 3 - a and b have same identity"
else:
print "Line 3 - a and b do not have same identity"

if ( a is not b ):
print "Line 4 - a and b do not have same identity"
else:
print "Line 4 - a and b have same identity"

Skill-building Activities
Let us practice! After completing each exercise, you may refer to the Key to Corrections for feedback. Try to
complete each exercise before looking at the feedback.
What is the output of the code shown below?
a = 27
b = 11
c = 12
d=5

This document is the property of PHINMA EDUCATION


ITE 260: Computer Programming 1
Module #4Student Activity Sheet
Class number: _______
Name:_________________________________________________________ Date: _______________
Section: ____________ Schedule: __________________________________

e=3

e = (a + b) * c / d
print "Value of (a + b) * c / d is ", e

e = ((a + b) * c) / d
print "Value of ((a + b) * c) / d is ", e

e = (a + b) * (c / d);
print "Value of (a + b) * (c / d) is ", e

e = a + (b * c) / d;
print "Value of a + (b * c) / d is ", e

Check for Understanding


Identify the following statements.
1. A name used to identify a variable, function, class, module, or other object is called an
___________.

2. These words are reserved and cannot be used as names for variables, constants, or
identifiers.

3. To distinguish code chunks, __________is employed, and it is carefully enforced.

4. A hash sign (#) that is placed outside of a string literal denotes the start of a
_________.

5. This data type is used to hold numeric values.

6. The amount of bits specified by the right operand is used to shift the value of the left
operand to the left.

7. If any of the two operands is not zero, the condition is true.

8. The modulus is calculated using two operands, and the result is assigned to the left
operand.

9. The evaluation returns true if a variable is not found in the supplied sequence;
otherwise, it returns false.

This document is the property of PHINMA EDUCATION


ITE 260: Computer Programming 1
Module #4Student Activity Sheet
Class number: _______
Name:_________________________________________________________ Date: _______________
Section: ____________ Schedule: __________________________________

10. It divides operators by the floor and assigns a value to the left operand.

C. LESSON WRAP-UP
FAQs
1. Is it acceptable to use lower case L with long?
● Python permits you to use a lowercase l with long, however it is best to use an uppercase L to avoid
confusion with the number 1. Long integers are denoted by an uppercase L in Python.
2. What is the difference between tuples and lists in Python?
● The main differences between lists and tuples are − Lists are enclosed in brackets ( [ ] ) and their
elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be
updated. Tuples can be thought of as read-only lists.

Thinking about Learning


Tell us about your learning experience today. Which parts of the lesson still feel confusing to you even after
checking the answer keys and reviewing the content? List them below and tell your teacher.
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________

This document is the property of PHINMA EDUCATION

You might also like