ITE 260 SAS Day 04 Data and Expressions
ITE 260 SAS Day 04 Data and Expressions
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
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
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!
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
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
(*).
Example:
list = [ 'snip', 529 , 1.15, 'iya', 80.3 ]
tinylist = [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')
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"
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
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 ):
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.
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.
Example:
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c=0
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
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:
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
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
2. These words are reserved and cannot be used as names for variables, constants, or
identifiers.
4. A hash sign (#) that is placed outside of a string literal denotes the start of a
_________.
6. The amount of bits specified by the right operand is used to shift the value of the left
operand to the left.
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.
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.