Values, Variables, and Calculations
Python is a programming language: we write instructions, and Python follows them.
print(...) means show this on the screen. The parentheses hold what we want to show. Matching quotes mark text.
Expected output: Hello, Python! — without the quotation marks. Change the message to your own greeting.
Numbers do not need quotes. Text does. Python calculates the expression before print() displays it.
The outputs are 2 + 3, 5, and Answer: 5.
The comma lets print() show more than one value, separated by a space. Try replacing 3 with 8.
A variable is a name that refers to a value. A useful name tells us what the value means.
Read pocket_money = 100 as: make the name pocket_money refer to 100. = is assignment, not an equation to solve.
Python first calculates the right side of =, then assigns the result to the name on the left. The output is 75.
Names are case-sensitive: price and Price are different. Use names such as snack_cost; do not start a name with a digit.
A name can be assigned again. The new value replaces what the name previously referred to.
Outputs: 10, then 15. In the update, Python reads the old 10, adds 5, and assigns 15.
points = points + 5 makes sense as an instruction, even though it would not make sense as an algebraic equation.
A calculated value remembers the result of that calculation. It does not automatically recalculate when an input changes.
Outputs: 20, then 24. We must run the calculation again to use the new price. Rerunning this whole cell starts again at price = 10.
Start with HK$100, receive HK$20, then spend HK$35. Finish the two updates.
Expected result: Money left: 85. Then start with 50 instead of 100; the result should be 35.
A type tells Python what kind of value it is working with. The value has the type; the variable name can be reassigned.
| Python name | Meaning | Example |
|---|---|---|
int |
Integer: a whole number | 4 |
float |
An approximate decimal number | 12.5 |
str |
String: text | "hello" |
type(value) asks Python to tell us the type.
Outputs include <class 'int'>, <class 'float'>, and <class 'str'>. Read these as “integer”, “float”, and “string”. We will practise each type separately.
int is short for integer. Integers include positive whole numbers, zero, and negative whole numbers. Use them for counts such as students or tickets.
Outputs: 28, the type int, then 1000 1000.
An underscore makes a large number easier to read: 1_000 and 1000 have the same value. Use it instead of a thousands comma in a number literal.
A float represents an approximate number, often written with a decimal point. Use examples such as prices, lengths, and rates.
Outputs start with 12.5 and 50.0. Python need not display trailing zeros.
4 is an int; 4.0 is a float. A float can represent a whole-number value too. Here multiplying an int by a float produces a float.
+ adds two numbers. - subtracts the second number from the first. A leading minus sign makes a number negative.
Expected outputs: 9, 5, -5, 3.5, -2.
Predict 10 - 4 + 2, then add a line to check it. Addition and subtraction at this level are evaluated from left to right.
Use * for multiplication and / for division. Python does not use the written symbols × and ÷ as operators.
Outputs: 14, 3.5, 4.0, and the type float.
Dividing these built-in numeric types with / produces a float, even when the answer is a whole number. Dividing by zero raises ZeroDivisionError.
A power means repeated multiplication for a positive whole-number exponent.
2 ** 3 means 2 × 2 × 2. The first number is the base; the second is the exponent.
Outputs: 8, 8, 25, 1.
Use two asterisks together: **. The symbol ^ is a different Python operator and does not mean power. Try 3 ** 2 and 2 ** 3: do they give the same result?
Suppose 7 biscuits are shared into groups of 2. We can make 3 complete groups, with 1 biscuit left.
Outputs: 3.5, 3, 1, 7.
For nonnegative whole-number counts, // gives the number of complete groups and % gives the remainder. More generally, // takes the floor: -7 // 2 is −4, not −3. Here % is not a percentage sign.
Python uses the usual arithmetic order: powers, then multiplication/division, then addition/subtraction. Parentheses ask Python to do a group first.
Outputs: 14, 20, 18, 36. Write out the steps on paper.
Use parentheses when they make your intended calculation clearer. These examples use positive bases; write (-3) ** 2 when the negative sign belongs to the base.
There are 23 items. Each box holds 5. Use // and % to calculate the number of full boxes and the items left.
Expected: 4 full boxes, 3 items left, and 23 accounted for. Test 25 items: 5 full boxes, 0 left. Test fewer items than one box can hold.
Prof. Xuhu Wan · HKUST ISOM · Introduction to Business Analytics
Comments and Reading Order
Python normally runs these lines from top to bottom.
A
#starts a comment: a note for people reading the code. Python ignores the rest of that line.Expected output:
30. Changing a comment does not change the calculation; changingpricedoes.