COL100 �Introduction to Computer Science
Lecture 3 – Operators
January 2025
Semester II
2024-2025
Vireshwar Kumar, Aaditeshwar Seth
CSE@IITD
Agenda
2
Disclaimer: The contents of these slides are taken from various publicly available resources such as books, talks and lectures. They are being used for the purpose of classroom teaching and academic dissemination only. The sources are acknowledged wherever possible.
Objects
3
Scalar Objects
print(type(5))
print(type(3.6))
print(type(True))
4
Expression
print(5 + 3)
5
Arithmetic Operators
print(5+3)
print(5-3)
print(5*3)
print(5/3)
print(5//3)
print(5%3)
print(5**3)
6
Comparison Operators
print(5>3)
print(5>=3)
print(5<3)
print(5<=3)
print(5==3)
print(5!=3)
7
Logic Operators
print(True and False)
print(True or False)
print(not(True))
8
X | Y | X and Y | X or Y | not(X) | not(Y) |
True | True | True | True | False | False |
True | False | False | True | False | True |
False | True | False | True | True | False |
False | False | False | False | True | True |
Operator Precedence
print((5+3)*4**2/5)
print(5+3>2+9)
print(10!=0 and 10 + 20 != 200)
9
a + b * c – d / e 🡺 a + (b * c) – (d / e)
a * – b + d % e – f 🡺 a * (– b) + (d % e) – f
a – b + c + d 🡺 (((a – b) + c) + d)
x * y * z 🡺 ((x * y) * z)
a + b + c * d * e 🡺 (a + b) + ((c * d) * e)
Type Casting
# Implicit
print(type(5 + 3.6))
print(type(5 * 3.6))
# Explicit
print(type(5))
print(type(float(5)))
print(type(3.6))
print(type(int(3.6)))
10
Assignment Operator
a = 3
b = 2*4 – 6
c = a + b
11
Memory View
12
Contents |
11100011 |
11001101 |
|
10001011 |
00011001 |
10011100 |
Address |
00000000 |
00000001 |
|
11111101 |
11111110 |
11111111 |
Variable a
Variable b
Variable Names
13
Example: Computing Average
a = 5
b = 10
avg = (a+b)/2
print(avg)
14
Example: Computing projectile motion
import math
#this line evaluates the time of flight for projectile motion
v0 = int(input("Enter v0: "))
theta = int(input("Enter theta in degrees: "))
time_of_flight = 2 * v0 * math.sin(math.radians(theta)) / 9.8
print("Time of flight: ", time_of_flight)
print(type(time_of_flight))
print(type(v0))
v1 = float(v0) + 0.0
print(type(v1), v1)
v2 = int(v1)
print(type(v2), v2)
15
Value of Assignment Expression
a = 3.0
b = 2*4 – 6
c = a + b
print(type(c))
17
Multiple Assignment Expression
a = b = c = 5
18
Example: Swap Variable Values
# Initially
a = 10; b = 20
print(a, b)
# Target: a = 20, b = 10
# Add your code here
a = b
b = a
print(a, b)
19
Example: Swap Variable Values (Cont.)
# Initially
a = 10; b = 20
print(a, b)
# Target: a = 20, b = 10
# Add your code here
t = a # t gets the value of a
a = b # a gets the value of b
b = t # b gets the value in t
print(a, b)
20
Example: Swap Variable Values (Cont.)
# Initially
a = 10; b = 20
print(a, b)
# Target: a = 20, b = 10
# Add your code here
a = a + b
b = a - b
a = a - b
print(a, b)
21
Abstracting Expressions using Variables
# Without variables
print(3.14 * 2 ** 2)
# With variables
pi = 3.14
radius = 2
area = pi*(radius**2)
print(area)
22
Arithmetic Operations on One Variable
x=5; print(x)
x+=3; print(x)
x-=3; print(x)
x*=3; print(x)
x/=3; print(x)
x%=3; print(x)
x//=3; print(x)
x**=3; print(x)
23
Logical Expressions with Variables
height = 5.7
age = 36
good_player = ((height > 5.0) and (age <= 30))
print(good_player)
24
Programming vs. Math
x = 2
x = (x**2 + 1)/2
25
Changing Bindings
26
Contents |
11100011 |
11001101 |
|
10001011 |
00011001 |
10011100 |
Address |
00000000 |
00000001 |
|
11111101 |
11111110 |
11111111 |
radius (after first statement)
radius (after second statement)
radius = 205
radius = 25
Changing Bindings (Cont.)
radius = 205
radius = 25
27
Variables Auto-Update?
pi = 3.14
radius = 2.2
area = pi*(radius**2)
radius = radius+1
28
Variable
29
Comment Statement
# This program computes the percentage of marks in the Math course considering that the maximum mark is 50.
marks = 41 # Obtained marks
percent = marks*100/50 # Compute the percentage of marks
print(percent) # Print the percentage of marks to the terminal
30
Syntactic Error
31
Static Semantic Error
32
Semantically Wrong Statement
33
Reading Material
34
Additional examples
COL100: Introduction to Computer Science (Semester I, 2024-2025)
35