CIT 101 Practical 2
CIT 101 Practical 2
PRELUDE
You are an under graduate of the SLTC research university reading for a prestigious
Degree in Applied Information Technology.
This is a unique program being introduced with the Ministry of Sports and Youth
Affairs. National Youth Council and National Youth Corps are collaborating with
SLTC on delivery.
This is your degree that exposes the password for the future. Be respectful for your
degree.
"Rome wasn't built in a day", you will need a lot of patience to face the upcoming
challenges in the life. Fail +Fail + Fail + 1 = Success (6+6+6 +1=19)
If you have failed so far in your life, it is due to dependence. Always try to be
independent.
The backbone of any IT degree is Programming. You will need a lot patience to
understand a lesson and the courage will carry you there.
"Slow and steady wins the race". Patient work will eventually conquer any problem.
Page 1 of 9
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming
N.B. I assume that you have already installed Python 3.11.1 (64-bit) or any other version
above 3.4. Python 2.x versions are obsolete
Expressions:
An expression may consist of one or more operands, and zero or more operators.
Expression => 2 + 3
Operand
Operator
Operand
Page 2 of 9
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming
Examples:
1. Arithmetic Expression 3x4/(3+4)
2. Algebraic Expression ab +cd/(a-d)
3. String expression ‘abc’*3
4. Trigonometric expression 2cos2 + sin
5. Boolean (Logical) expression 20 > 15
2 2
Note: 2x + 3x – 1 is an expression but 2x + 3x – 1= 0 is an equation.
Rule 1: When two operators share an operand the operator with a higher precedence
will be evaluated first.
Note: Python follows the same precedence rules just as you learnt in mathematics.
Note: You can remember the precedence of order by the word ‘PEMDAS’
Rule 3: Associativity
When two Operators share an operand and operators have the same
precedence then the expression will be evaluated according to the Associativity
of the operators.
There are two types: Left Associativity (L->R) and Right Associativity (R->L)
Page 3 of 9
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming
1. Evaluate 6-4+5
E 6-4+5 Since - and + have the same precedence we use Left Associativity.
Ans: 6-4+5 = 7
>>> 6-4+5
7
2. Evaluate 48/3x8
E 48/3*8 Since / and * have the same precedence we use Left Associativity.
But if you first evaluate 3*8 = 24 and then 48/24 = 2.0 which is wrong
>>> 48/3*8
128.0
3. Evaluate 12/3/2
E 12/3/2 Since '/' and * have the same precedence we use Left Associativity.
>>> 12/3/2
2.0
Page 4 of 9
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming
4. Evaluate 2 + (3-4)x5
E 2 + (3-4)x5 Since the parentheses has the highest precedence we first evaluate
(3-4) = -1 and next precedence is for * so multiply -1* 5 = -5 and then
2+ (-5) = -3.
Ans: 2 + (3-4)x5 = -3
>>> 2 + (3-4)*5
-3
5. Evaluate 2^3^4
E 2^3^4 Since ^ and ^ have the same precedence we use Right Associativity.
>>> 2**3**4
2417851639229258349412352
6. Evaluate (2^3)^4
E (2^3)^4 Since the parentheses has the highest precedence we first evaluate
2^3 = 8 and then 8^4 = 8*8*8*8 = 64*64 = 4096
>>> (2**3)**4
4096
Python Variables
Page 5 of 9
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming
p, q and r are the names. We call them identifiers. i.e. The names given to objects that we
use.
Let us assume that we have three objects 10, 20 and 30 respectively. To use them first we
have to create the objects in the memory and bind the variable names to those values.
>>> p = 10
Bind the value 10
>>> q = 20 to variable p
>>> r = 30
Now Python knows which variable should be used to refer to the corresponding object.
For example if I ask from Python what p, q and r refer to, then Python can answer. See how
it is done.
Also you can do the same using print()
>>> p function in Python.
10 >>print(p)
We ask Python what is p?
10
Python says p is 10
>>> q
20 >>>print(q)
20
>>> r
30 >>>print(r)
30
>>> p+q
30
>>> p-q+r
20
Page 6 of 9
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming
Note: Python identifiers are case sensitive. That means P and p are two different identifiers.
For example to represent the length, breadth and height of a box, although we use l,
b and h variables respectively, professional programmers refuse to use non
meaningful identifiers saying they are ugly to use in a code.
Avoid using use the letters o, l (capital I) especially as we confuse o with 0 and 1
with l.
Example: The length, breadth and height of a box are 10, 5 and 3 units respectively.
Write single python interactive statements to find the volume of the box in cubic
units.
length
>>> length = 10 breadth
>>> breadth = 5
height
>>> height = 3
>>> volume = length*breadth*height
>>> print("The volume of the box is ", volume, 'cu.units')
The volume of the box is 150 cu.units
Use of
We cannot calculate exact value of as it is an irrational number. So we use only an
approximate value for . Since we cannot write the value of in simple editors we write it
as ‘pi’.
is a universal constant. It’s available in math module of the Python Class Library.
Therefore we import it from the math module whenever we need it as follows.
Page 7 of 9
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming
Practical 2
You have to submit all your workings to LMS clearly in a .pdf file by putting file name as
follows. (Delete all error messages)
i) 3 + 4x6/3
ii) (3 + 4)x5/7
iii) 8/3x5.7+16%3
iv) 2+1.2e10
v) -12 .003E-2
vi) 6.7e-032.8001E-4
Page 8 of 9
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming
2. Write the order of evaluation of the following arithmetic expressions and write single Python
statements for each step to verify the answer.
v) 3e3^1.6^2.8
3. If a, b, c and d are Python variables that represent 1, 2, 3 and 4 objects respectively, write
Python commands to evaluate the following expressions.
Example:
>>> p, q = 10,12
>>> print (p + q)
22
OR
>>> print("p+q =", p+q)
p+q = 22
Page 9 of 9