0% found this document useful (0 votes)
42 views7 pages

ITE3711 Lab1.3 Operators

This document provides exercises on operators and comparisons in Python. It includes multiple examples of writing the output of code snippets using various arithmetic, assignment, comparison and logical operators.

Uploaded by

zainoff445
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

ITE3711 Lab1.3 Operators

This document provides exercises on operators and comparisons in Python. It includes multiple examples of writing the output of code snippets using various arithmetic, assignment, comparison and logical operators.

Uploaded by

zainoff445
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Hong Kong Institute of Vocational Education

FS113002N Diploma of Foundation Studies – Information Technology


ITE3711 Programming Concept and Applications
Topic 1
Lab 1.3 – Operators

Exercise 1
Write down the output of the program fragments below.

Example:
Program fragments Output
print(5 + 5) 10
print(5 - 5) 0
print(5 * 5) 25

Program fragments Output


(a) print(3 - 2 + 1)
(b print(10 * 2 - 3)
)
(c) print(20 / 4 * 2)

(d print(7 % 3)
)
(e) print(2 ** 3)

(f) print(6 + 3 - 2)

(g print(5 * 4 + 2)
)
(h print(7 - 4 * 2)
)
(i) print(12 / 3 + 2 * 4)

(j) print(2 ** 3 + 1 * 2 + 4)
(k print(5 % 2 + 3 ** 2)
)
(l) print(10 * 2 // 4 - 1)

Exercise 1 (cont’)
Write down the output of the program fragments below.

Program fragments Output


(m num1 = 76
)
num2 = 345

num3 = 85

print(num1 + num2)

print(num1 * 2 - num3)

print(num2 % 2 + num2)

(n) num1 = 7.0

num2 = 49

num3 = 7

print(num2 + num1)

print(num2 / num3)

print(num2 // num3)

(o) num1 = 67

num2 = 98

num3 = 23

print((num1 + num2) * num3)

print((num1 // num3) ** 2)
print((num2 % 2 + 5) * num3)

Exercise 2
Write down the program code format of the following formulas.

Example:
Formula Python program code format

√ a2 +ab (a ** 2 + a * b) ** 0.5

Formula Python program code format

2 r∗3.14

base∗height
2

y 2− y 1
x 2−x 1

Celsius∗9
+ 32
5

( b 1+b 2 ) h
2

( )
nt
r
P 1+
n
−b+ √ b 2−4 ac
2a
Exercise 3
Write down the output of the program fragments below.

Example:
Program fragments Output
(a) x = 5 6
x += 1
print(x)

Program fragments Output


(a) x = 5
x += 3
print(x)

(b x = 10
) x -= 4
print(x)

(c) x = 3
x *= 4
print(x)

(d x = 20
) x /= 5
print(x)

(e) x = 35
x %= 6

print(x)

(f) x = 65
x %= 9
x *= 5
print(x)
Exercise 4
Write down the output of the program fragments below.

Example:
Program fragments Output
num1 = 5 False
num2 = 25

print(num1 > num2)

Program fragments Output


(a) num1 = 5
num2 = 7

print(num1 > num2)


print(num1 < num2)
print(num1 >= num2)
print(num1 <= num2)
print(num1 == num2)
print(num1 != num2)

(b num1 = 6
) num2 = 8
num3 = 48

print(num2 > num3)


print(num3 < num1)
print(num1 >= num2 - 2)
print(num3 <= num1 * num2)
print(num1 * num2 == num3)
print(num1 != num2)

(c) msg1 = "Hello"


msg2 = "hello"

print(msg1 == msg2)
print(msg2 != msg1)
Exercise 4 (cont’)
Write down the output of the program fragments below.

Program fragments Output


(d num1 = 78
) num2 = 45
msg1 = "X"
msg2 = "x"

print(num1 > num2 and 45 <= num2)


print(num1 >= 78 or num1 < num2)
print(num1 + num2 < 150 or msg1 == "X")
print(msg1 == "Y" or msg1 == "x")
print(not(msg1 == "X"))
print(num1 %2 == 0 and num1 != num2)

(e) num1 = 10
num2 = 5
msg1 = "hello"
msg2 = "world"

print(num1 > num2 and len(msg1) < len(msg2))


print(num1 != 10 or num2 == 5)
print(len(msg1) + len(msg2) >= 10)
print(msg1 == "Hello" or msg2 == "WORLD")
print(not(msg1 == "hello"))
print(num1 % 2 == 0 or num2 % 2 == 0)

You might also like