Skip to content
Aabha AI Academy
Skip to reading

Python · Module 1

Values, names and types

Outcome: choose simple types, calculate with explicit units and convert text deliberately. Prerequisite: Module 0. Profile: Python basics. Time: 90–120 minutes.

A name refers to a value

A value is data, such as the number 3 or the text "notebook". A name gives you a way to refer to it. In quantity = 3, Python evaluates the right-hand side and binds quantity to that value. Later, quantity = quantity + 1 reads the old value, calculates 4 and rebinds the name. It is an update instruction, not an algebra equation.

Use names that carry meaning: unit_price_cents is more informative than p. Names are case-sensitive. total and Total are different. Avoid naming a variable list, str, sum or print: those names already refer to useful Python tools, and replacing them makes later code confusing.

The main types we need first are int for integers, float for approximate real-number calculations, str for text, bool for True/False and None for an explicit absence of a value. Text that looks numeric is still text: "4" + "2" joins two strings into "42"; 4 + 2 adds two integers. type(value).__name__ helps identify the difference.

Expressions and units

An expression produces a value. Multiplication uses *; division / produces a float; // gives floor division; % gives the remainder. With non-negative whole minutes, 135 // 60 is 2 hours and 135 % 60 is 15 minutes. For negative numbers floor division rounds down rather than toward zero, so do not assume that example covers negative-duration rules.

Multiplication and division are evaluated before addition and subtraction. Parentheses make intent explicit: (10 + 5) * 2 is 30, while 10 + 5 * 2 is 20. A correct expression can still answer the wrong question if units are mixed. Keep cents and currency units clearly distinguished. Our fictional prices use integer cents to avoid a floating-point rounding issue in simple totals; this does not make the example a complete accounting system.

Worked example

# Profile: Python basics
item = "notebook"
quantity = 3
unit_price_cents = 250
subtotal_cents = quantity * unit_price_cents
print(f"{quantity} {item}s: {subtotal_cents} cents")
print(type(quantity).__name__, type(item).__name__)
print("Whole hours:", 95 // 60)
print("Minutes left:", 95 % 60)
raw_quantity = "4"
print("Text joined:", raw_quantity + "2")
print("Numbers added:", int(raw_quantity) + 2)
print("Available:", quantity > 0)
# Ordinary binary floats cannot represent every decimal exactly.
print("Float equality:", 0.1 + 0.2 == 0.3)

Download this worked script

Trace the subtotal: quantity 3 multiplied by 250 cents per item gives 750 cents. It does not give 750 currency units. The f-string inserts values inside braces into a readable message. Formatting a number changes how it is displayed, not the underlying quantity.

Expected key results: 3 notebooks: 750 cents, types int str, whole hours 1, minutes left 35, text 42, number 6, availability True, float equality False.

Convert, do not guess

int("4") converts valid integer text. int("four") raises ValueError because the content does not match that conversion. int(2.9) truncates to 2; it is not a validation rule for whole-number input. Later, we will handle invalid values without losing the original record.

Strings have useful methods: " Data ".strip().lower() removes outer whitespace and lowercases text, yielding "data". It does not remove spaces from the middle or repair misspellings. Methods are operations attached to a value; the dot connects the value and method name. Most string methods return new strings rather than changing the original name's value.

Comparisons such as quantity > 0, quantity == 3 and quantity != 0 produce booleans. = assigns; == compares. Python's binary floating-point representation cannot represent all decimal fractions exactly, which is why the last line prints False. We will use math.isclose in numeric tests rather than expecting exact equality for approximate results.

Practice

Complete exercises/01_values.py; the None values are deliberate blanks.

  • 1A: calculate the cost of four items at 175 cents each. Print the total with its unit.
  • 1B: split 135 minutes into whole hours and leftover minutes. Repeat with 0 and 59.
  • 1C: convert "7" to an integer and add 3; normalize " Data " to "data". Explain why conversion and normalization are separate operations.

Exit check

What is the difference between "20", 20 and 20.0? Why does int(2.9) not prove that 2.9 is a valid count? If a price is 175 cents, which name would make your calculation easier to review? Predict 2 + 3 * 4 and (2 + 3) * 4 before running them.

Further reading

Read the separate answers and explanations

Reading progress

11 lessons remain open to guests. Marking a lesson read records reading only; it does not award assessment credit or a certificate.

Device reading marks require browser storage. Reading is always available.

Sign in or create an account to save separate account progress. Your current page is kept.