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

CIT 101 Practical 2

This document discusses fundamentals of programming using Python expressions. It covers precedence rules and associativity of arithmetic operators in Python, and provides examples of evaluating different types of expressions and using variables.

Uploaded by

Yasara Madana
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)
52 views

CIT 101 Practical 2

This document discusses fundamentals of programming using Python expressions. It covers precedence rules and associativity of arithmetic operators in Python, and provides examples of evaluating different types of expressions and using variables.

Uploaded by

Yasara Madana
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/ 9

Bachelor of Applied Information Technology

Module Code CIT101


Module Title: Fundamentals of Programming

Date: 13th February 2023

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

Practical 2 – Python Expressions


The objective of this practical is to learn how to evaluate Python expressions.

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

Before you start I suggest you to study the following.

1. Supplement 1 Numeric Data


2. CIT 101 Py Lsn 2 Final
3. CIT 101 Py Lsn 3 Final
4. CIT 101 Py Lsn 4 Final

What you learn?

We will learn the following:

 Precedence of order and Associativity of arithmetic operators in Python.


 How to evaluate arithmetic expressions correctly?
 How to use python variables?

Expressions:

 An expression can be regarded as a representation of a computation that evaluates


to a single value.

 An expression may consist of one or more operands, and zero or more operators.

Expression => 2 + 3
Operand
Operator
Operand

An expression can be a simple Arithmetic expression, an Algebraic expression, a


String expression, a Trigonometric expression, a Boolean (Logical) expression
etc.

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.

1. Precedence of Operator of Arithmetical Operators


Precedence and Associativity are two main characteristics of operators that determine
the order of evaluation of the operands in the absence of Parentheses.

Note: Python considers () as parentheses, [ ] as brackets and { } as braces

Rule 1: When two operators share an operand the operator with a higher precedence
will be evaluated first.

Rule 2: Order of precedence can be changed by using parentheses.

Operator Operation Associativity


() Parentheses L -> R
** Exponentiation R -> L
* % / // Multiplication, Modulus Division, F/P Division L -> R
+- Addition, Subtraction L -> R

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

Consider the following examples.

1. Evaluate 6-4+5

E  6-4+5 Since - and + have the same precedence we use Left Associativity.

So first evaluate 6-4 = 2 Then add 2 + 5 = 7

Ans: 6-4+5 = 7

But if you first evaluate 4+5 = 9 Then 6 -9 = -3 which is wrong

Verify with Python as follows

>>> 6-4+5
7

2. Evaluate 48/3x8

E  48/3*8 Since / and * have the same precedence we use Left Associativity.

So first evaluate 48/3 = 16 and then 16*8 = 128.0

Ans: 48/3x8 = 128.0

But if you first evaluate 3*8 = 24 and then 48/24 = 2.0 which is wrong

Verify with Python as follows

>>> 48/3*8
128.0

3. Evaluate 12/3/2

E  12/3/2 Since '/' and * have the same precedence we use Left Associativity.

So first evaluate 12/3 = 4 Then 4/2 = 2.0

Ans: 12/3/2 = 2.0

Verify with Python as follows

>>> 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

Verify with Python as follows

>>> 2 + (3-4)*5
-3

5. Evaluate 2^3^4

E  2^3^4 Since ^ and ^ have the same precedence we use Right Associativity.

So first evaluate 3^4 = 81 and then 2^ 81= 2417851639229258349412352

Ans: 2^3^4 = 2417851639229258349412352

Verify with Python as follows

>>> 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

Verify with Python as follows

>>> (2**3)**4
4096

Python Variables

There is a comprehensive description about Python variables in lesson 4. Please study


Lesson 4 several times before you do the Practical 2. After that go through the
examples that I have given on next page (Page 6).

Page 5 of 9
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming

Consider p, q and r are three Python variables.

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.

We do it in the following manner:

>>> 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

Now you should be able to evaluate expression in the following way.

>>> p+q
30

>>> p-q+r
20

>>> 2*p +3*r -0.5*q


100.0

Page 6 of 9
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming

Best Practices in using Identifiers

Note: Python identifiers are case sensitive. That means P and p are two different identifiers.

 Use meaningful identifiers in Programming.

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’.

Do not use  = 22/7. It's wrong. At least use   355/133

 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.

>>> import math


>>> math.pi
3.141592653589793

Page 7 of 9
Bachelor of Applied Information Technology
Module Code CIT101
Module Title: Fundamentals of Programming

Example: If the radius of a circle is 7 units find its circumference.

>>> import math # import math module


>>> radius = 7
>>> circumference = 2*math.pi*radius # 2 pi x r is the perimeter
>>> print("The circumference of the circle is", circumference, 'units')
The circumference of the circle is 43.982297150257104 units

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)

Module Code Registration number Pra2.pdf

Eg. CIT101- 22UG3 xxxx Pra2.pdf


You may discuss things with your friends, but do not copy. Copying will be a
disqualification and you will get 0 marks if you get caught. For late submissions 10%
of the marks will be deducted for each day.

Time Allocated: As notified in the LMS

Practical 2: [Marks will be given]


Study the given examples and perform the following. Do it yourself – Be independent.

1. Evaluate the following expressions by using single Python statements.

Example: >>> print(10 + 12*7/3)


38.0

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-032.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.

Example: Write the order of evaluation of the expression 12/3*4/8%5

Step 1: >>> 12/3


4.0
Step 2: >>> 4.0*4
16.0
Step 3: >>> 16.0/8
2.0
Step 4: >>> 2.0%5
2.0
Verification of the final answer:
>>> 12/3*4/8%5
2.0

i) 34-0.03+11-6.01-101.47 ii) 546x2124

iii) 13.6+2.1^3.3%1.2 iv) 3.4%1.66//4.53e-1

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

i) a+b–c+d ii) (a + b)  (c –d)

iii) 4a - 5b x 12c - d iv) (25d) / (bd)1/3

v) ab + bc – a%d – c^c vi) ‘a + b + c//d =’ <get the answer here>

vii) (25d/4a) viii) 2d

Page 9 of 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy