Ryan Singh

Exam 1 review: variables, operators, conditions and loops

Tutorial · ISA 281: Concepts in Business Programming, part 1 of 3

#python#teaching

This is the review I ran before the first exam. The slides and the practice notebook cover the same topics in the same order: open the practice notebook in Colab, make your own copy (File → Save a copy in Drive), and follow along.

A few ground rules from the session still apply if you’re reading this on your own: don’t suffer in silence, and if something doesn’t click, read it twice, run it, and change it until it does.

Pseudocode

Pseudocode is “fake code”: an informal, plain-language outline of your program’s logic. It has no syntax rules and is never run. You write it before the code, to plan.

IF HoursWorked > NormalMax THEN
    Display overtime message
ELSE
    Display regular time message
ENDIF

Comments and pseudocode are not the same thing. Comments explain specific lines of code and sit next to them. Pseudocode describes the logic of the whole program before any code exists. Think of it as structured English: explain your logic the way you’d break it down for your roommate. “If the input is X, then do Y; otherwise, do Z.”

Variables

A variable is a name for a value stored in memory. The general form is variable = expression.

price = 19.99
quantity = 3
x, y = 5, 4  # you can assign several at once

The rules for names:

print()

print() writes to the console. Its full signature shows the options you can change:

print(*objects, sep=' ', end='\n', file=None, flush=False)

print("a", "b", "c", sep="-")   # a-b-c
print("no newline", end="")     # the next print continues on this line

Operators

Python has four kinds of operators you need for this exam.

Kind Example
Arithmetic 1 + 1
Assignment x = 1
Relational x > 2
Logical True and True

Arithmetic

With a = 3 and b = 4:

Expression Meaning Result
a + b sum 7
a - b difference -1
a * b product 12
a / b division 0.75
a // b floor division (whole part of the division) 0
a % b modulo (the remainder) 3
a ** b power 81

Floor division and modulo trip people up the most:

25 / 6    # 4.1666…
25 // 6   # 4: the division, rounded down
27 % 5    # 2, because 27 = 5 * 5 + 2

Assignment

Operator Example Same as
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3

+= and friends are augmented assignment: they update a variable using its current value.

Relational

>, >=, <, <=, == (equal to) and != (not equal to). They always give True or False. A tip for remembering the spelling: the = always comes last (>=, never =>). And == compares, while = assigns.

Logical

Operator Returns True when Example
and both conditions are true x < 5 and x < 10
or at least one is true x < 5 or x < 4
not the condition is false not (x < 5 and x < 10)

if statements

An if runs its block only when its condition is True.

if hours_worked > normal_max:
    print("Overtime")
else:
    print("Regular time")

Nested if

You can put an if inside another:

if x == y:
    print("x and y are equal")
else:
    if x < y:
        print("x is less than y")
    else:
        print("x is greater than y")

Nested ifs get hard to read quickly. The same logic flattens into one level with elif, and logical operators can merge conditions. Being able to rewrite one as the other is a classic exam question:

if x == y:
    print("x and y are equal")
elif x < y:
    print("x is less than y")
else:
    print("x is greater than y")

for loops

A for loop runs once for each item in a sequence: a list, tuple, dictionary, set or string.

for fruit in ["apple", "banana", "cherry"]:
    print(fruit)

range(start, stop, step) generates numbers to loop over. It starts at 0 by default, counts up by 1 by default, and stops before stop.

for i in range(2, 10, 3):
    print(i)   # 2, 5, 8

break exits a loop immediately. Loops already have a normal way to end, so break usually sits inside an if that checks for a special case. It works in while loops too.

for n in range(100):
    if n * n > 50:
        print(n)   # 8
        break

while loops

A while loop repeats as long as its condition is True. Something inside the loop has to make the condition false eventually, or it never stops. And if the condition is false to begin with, the loop never runs at all.

balance = 1000
years = 0
while balance < 2000:
    balance *= 1.07
    years += 1
print(years)   # 11

Further reading