CS1010A, Semester I, 2024/2025—Recitation 1 1
National University of Singapore
School of Computing
CS1010A: Programming Methodology
Semester I, 2024/2025
Recitation 1
Introduction to CS1010A, Python & Functional Abstraction
Overview
1. Fun with IDLE
2. Assignment & Operators
3. Boolean operations
4. Conditional Statements
5. Functions
Python
1. if - elif - else:
if expression:
statement(s)
elif expression:
statement(s)
else:
statement(s)
Consider each pre-condition in sequence, if the value of the any expression is
not False, evaluate the corresponding statement(s). Otherwise evaluate the state-
ment(s) under else.
2. Ternary Operator Form [To read only. Not expected to write code like that.]
[on_true] if [expression] else [on_false]
is equivalent to
if [ expression ]:
[ on_true ]
else :
[ on_false ]
Problems
1. Python supports a large number of different binary operators. Experiment with
each of these, using arguments that are both integer, both floating point, and both
string. Not all operators work with each argument type. In the following table,
put a cross in the appropriate boxes corresponding to the argument and operator
combinations that result in error.
CS1010A, Semester I, 2024/2025—Recitation 1 2
Operator Integer Floating point String
+
-
*
/
a // b = int(a / b) **
unless a/b < 0 //
%
floor division
<
>
<=
>=
==
!=
Some of these operators were not discussed in lecture. Find out what they do. You
might be asked to explain them to the rest of the class in recitation.
2. Evaluate the following expressions assuming x is bound to 3, y is bound to 5 and z
is bound to 2:
x + y / z
x ** y % x
y <= z
x > z * y
y // x
x + z != z + x
CS1010A, Semester I, 2024/2025—Recitation 1 3
if True :
1 + 1
else :
17
2
if False :
False
else :
42
42
if ( x > 0 ):
x
else : 3
(-x)
if 0 :
0 = False
1
else : 2 ‘ ‘ = False
2
if x :
7
else :
what - happened - here error
if True :
1
elif (y > 1 ):
False
else :
1
wake - up
3. Suppose we’re designing an point-of-sale and order-tracking system for a new
burger joint. It is a small joint and it only sells 4 options for combos: Classic
Single Combo (hamburger with one patty), Classic Double With Cheese Combo (2
patties), and Classic Triple with Cheese Combo (3 patties), Avant-Garde Quadru-
ple with Guacamole Combo (4 patties). We shall encode these combos as 1, 2, 3,
and 4 respectively. Each meal can be biggie_sized to acquire a larger box of fries
and drink. A biggie_sized combo is represented by 5, 6, 7, and 8 respectively, for
combos 1, 2, 3, and 4 respectively.
(a) Write a function called biggie_size which when given a regular combo returns
a biggie_sized version.
def biggie_size(combo) :
return combo + 4
def unbiggie_sie(combo) :
return combo - 4
def is_bigger_size(combo):
if combo > 4:
return True
else:
return False
CS1010A, Semester I, 2024/2025—Recitation 1 4
(b) Write a function called unbiggie_size which when given a biggie_sized combo
returns a non-biggie_sized version.
(c) Write a function called is_biggie_size which when given a combo, returns True
if the combo has been biggie_sized and False otherwise.
(d) Write a function called combo_price which takes a combo and returns the price
of the combo. Each patty costs $1.17, and a biggie_sized version costs $.50
extra overall.
def combo_price(combo):
per_patty = 1.17
2: 2 X 1.17
if is_bigger_size(combo):
6: (6-4) x 1.17 + 0.5
return (combo - 4) * per_patty +0.5
else:
return combo * per_patty
(e) An order is a collection of combos. We’ll encode an order as each digit rep-
resenting a combo. For example, the order 237 represents a Double, Triple,
and biggie_sized Triple. Write a function called empty_order which takes no
arguments and returns an empty order which is represented by 0.
(f) Write a function called add_to_order which takes an order and a combo and
returns a new order which contains the contents of the old order and the new
combo. For example, add_to_order(1,2) -> 12.
def empty_order():
return 0
def add_to_order(order, combo):
return int(str(order) + str(combo))