1 of 35

COL100 �Introduction to Computer Science

Lecture 3 – Operators

January 2025

Semester II

2024-2025

Vireshwar Kumar, Aaditeshwar Seth

CSE@IITD

2 of 35

Agenda

  • Scalar Data Objects

​

  • Arithmetic and Logic Operators

​

  • Operator Precedence

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.

3 of 35

Objects

  • A program manipulate objects through statements
    • Example: A “bottle” is an object which
      • can be filled with a liquid
      • can contain a cap which can be opened or closed

​

  • Data objects can be of two types:
    • Scalar object that cannot be subdivided
      • Integer: 5

​

    • Non-scalar object that has internal structure that can be accessed
      • Set of integers: [5, 2, 3]

3

4 of 35

Scalar Objects

  • int
    • Integer: 5
  • float
    • Real number: 3.6
  • bool
    • Boolean values: True and False
      • True: 1
      • False: 0

print(type(5))

print(type(3.6))

print(type(True))

4

5 of 35

Expression

  • Expression is a legal combination of primitives in a programming language.

​

  • Expression has a value which has a type.

​

  • Syntax for a simple expression:
    • <object> <operator> <object>

​

​

​

print(5 + 3)

5

6 of 35

Arithmetic Operators

  • Addition: +
  • Subtraction: –
  • Multiplication: *
  • Division: /
  • Floor division (quotient): //
  • Modulus (remainder): %
  • Exponentiation (power): **

​

print(5+3)

print(5-3)

print(5*3)

print(5/3)

print(5//3)

print(5%3)

print(5**3)

6

7 of 35

Comparison Operators

  • is greater than: >
  • is greater than or equal to: >=
  • is less than: <
  • is less than or equal to: <=
  • is equal to: ==
  • is not equal to: !=

​

print(5>3)

print(5>=3)

print(5<3)

print(5<=3)

print(5==3)

print(5!=3)

​

7

8 of 35

Logic Operators

  • All true: and
  • Either true: or
  • Inverse: not

​

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

9 of 35

Operator Precedence

  • In decreasing order of priority
    • Parentheses
    • Power
    • Multiplication, Division
    • Addition and Subtraction

​

  • For operators of the same priority, evaluation is from left to right.

​

  • When arithmetic expressions are used on either side of a comparison operator, the arithmetic expressions will be evaluated first and then the results compared.

​

​

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)

10 of 35

Type Casting

  • Implicit
    • Arithmetic operation on int and float results into a float

​

  • Explicit
    • float(): converts int to float
    • int(): truncates float to integer

​

​

  • Typically, float should not be typecast to int as an int cannot store everything a float can store.

​

​

# 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

11 of 35

Assignment Operator

  • The “=” operator makes an assignment of a value to a name called variable.

​

  • Syntax
    • variable = expression

​

  • Left of “=” is called l-value which must be a modifiable variable.

​

  • Right of “=” is called r-value which can be any expression.

a = 3

b = 2*4 – 6

c = a + b

​

11

12 of 35

Memory View

  • A value is stored in computer memory.

​

  • An assignment operator binds a variable to the value.

​

  • A variable is like a bin. The contents of the bin is the value of the variable.

​

  • A variable is mapped to an address in the memory.

​

  • We can retrieve the value associated with the variable by invoking the variable.

​

​

​

​

​

12

Contents

11100011

11001101

​

10001011

00011001

10011100

Address

00000000

00000001

​

11111101

11111110

11111111

Variable a

Variable b

13 of 35

Variable Names

  • Sequence of letters and digits,
  • No special characters other than ‘_’,
  • No blank in between
  • First character must be a letter or ‘_’
  • Names are case-sensitive (max and Max are two different names)

​

  • Examples of valid names:
    • i, rank1, MAX, max, Min, class_rank

​

  • Examples of invalid names:
    • a’s, fact rec, 2sqroot, “class,rank”

13

14 of 35

Example: Computing Average

  • Problem: Given any two numbers, compute their average

a = 5

b = 10

avg = (a+b)/2

print(avg)

14

15 of 35

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

16 of 35

17 of 35

Value of Assignment Expression

  • An assignment expression evaluates to a value same as any other expression.

​

  • Value of an assignment expression is the value assigned to the l-value

​

  • Example: value of
    • a = 3.0 is 3.0
    • b = 2*4 – 6 is 2

​

  • The type of the variable is determined implicitly.

​

a = 3.0

b = 2*4 – 6

c = a + b

print(type(c))

17

18 of 35

Multiple Assignment Expression

  • Several variables can be assigned the same value using multiple assignment operators in a single statement.

​

  • Easy to understand if you remember that the assignment expression has a value.

​

  • Multiple assignment operators are right-to-left associative.

a = b = c = 5

​

18

19 of 35

Example: Swap Variable Values

  • Problem: Given two variables, exchange the values assigned to them without using another value

​

  • What happens if we do
    • a = b
    • b = a

# 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

20 of 35

Example: Swap Variable Values (Cont.)

  • We can use a variable t to swap values assigned to two variables a and b .

​

  • Can we swap without using the temporary variable?

# 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

21 of 35

Example: Swap Variable Values (Cont.)

  • If the variables are integers or floats then we can swap in this way too

# 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

22 of 35

Abstracting Expressions using Variables

  • Variables enable us to give names to values of expressions.

​

  • We can reuse those names instead of values.

​

  • It is easier to use others values later.

​

  • Problem: Print
    • area of a circle with radius of 2
    • area of a semi-circle with radius of 2
    • area of a circle with radius of 3

​

​

​

# Without variables

​

print(3.14 * 2 ** 2)

# With variables

​

pi = 3.14

radius = 2

area = pi*(radius**2)

print(area)

22

23 of 35

Arithmetic Operations on One Variable

  • x=x+3: x+=3
  • x=x-3: x-=3
  • x=x*3: x*=3
  • x=x/3: x/=3
  • x=x%3: x%=3
  • x=x//3: x//=3
  • x=x**3: x**=3

​

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

24 of 35

Logical Expressions with Variables

  • We can write logical expressions using variables.

height = 5.7

age = 36

good_player = ((height > 5.0) and (age <= 30))

print(good_player)

​

​

24

25 of 35

Programming vs. Math

  • In programming, you do not “solve” for x.

x = 2

x = (x**2 + 1)/2

​

25

26 of 35

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

​

27 of 35

Changing Bindings (Cont.)

  • We can re-bind the variable name using a new assignment statement.

​

  • The previous value may still be stored in the memory, but we lost the handle for it.

​

  • The re-bindings make one variable change memory addresses.

​

  • At one time, there is only one memory address and one value that is referred by a variable.

​

​

​

radius = 205

radius = 25

​

27

28 of 35

Variables Auto-Update?

  • A variable is assigned the value of the expression only when the statement is executed.

​

  • Recall that the statements in a typical program are executed sequentially.

​

  • The value for the area variable does not change until we tell the computer to recompute.

​

​

pi = 3.14

radius = 2.2

area = pi*(radius**2)

radius = radius+1

​

28

29 of 35

Variable

  • A variable is an entity that has a value and is known to the program by a name.

​

  • It can be used to store any temporary result while executing a program.

​

  • It can have only one value assigned to it at any given time.

​

  • Its value can be changed during the execution of the program.

​

  • It has a type indicating what type of values the variable can hold.

​

29

30 of 35

Comment Statement

  • Comments are required to improve the readability of the program.

​

  • The comments are not executed by the computer.

​

  • The symbol # indicates that the following text is a comment.

​

  • You should use comments extensively in your programs.

# 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

31 of 35

Syntactic Error

  • Invalid use of an operator or invalid combination of objects

​

    • English
      • “cat dog boy”: not a syntactically valid statement

​

      • “cat hugs boy”: a syntactically valid statement

​

    • Programming language
      • 3 5 *: not a syntactically valid statement

​

      • 3 * 5: a syntactically valid statement

​

  • Common and easily caught

​

​

​

​

31

32 of 35

Static Semantic Error

  • A syntactically valid combination does not have a meaning

​

    • English
      • "I are hungry“ syntactically valid but static semantically invalid

​

    • Programming Language
      • 3 + “hi”: static semantic error

​

  • Some languages check for these before running program

​

  • If not caught, it can cause unpredictable behavior

​

​

32

33 of 35

Semantically Wrong Statement

  • Semantics is the meaning associated with a syntactically valid statement with no static semantic errors.

​

  • Different meaning than what programmer intended
    • program crashes, stops running
    • program runs forever
    • program gives an answer but different than expected

​

  • Handled manually through the process called debugging

33

34 of 35

Reading Material

  • Reference Book
    • Chapter 2

​

34

35 of 35

Additional examples

  • Plot a graph of a given expression

​

COL100: Introduction to Computer Science (Semester I, 2024-2025)

35