DoAssignment study guide
A1.3 · Compare non-numeric data
Learn to compare non-numeric data through clear examples and targeted practice.
Ontario Grade 12 Computer Science
Programming Concepts and Skills
ICS4U A1.3 · How programs can compare words and labels
In ICS3U, you may have compared numbers with operators such as less than and equal to. A program can also compare non-numeric data, such as names, labels, or words. For text, the result depends on the comparison rule being used. This lesson focuses on a simple, explicit rule for comparing text: examine characters from left to right, and let the first differing character decide the order. Before comparing, decide whether uppercase and lowercase letters should count as the same. That decision matters because a program's comparison may not match a reader's idea of alphabetical order.
What you will learn
- Explain how two text values can be compared without treating them as numbers.
- Compare words character by character using an agreed ordering rule.
- Recognize how case and prefixes can affect a text comparison.
- Trace a short Python 3 comparison example and state its expected result.
1. From numeric comparisons to text comparisons
A comparison asks how two values relate. For numbers, the values have a familiar magnitude: for example, 12 is greater than 7. Words do not have numerical magnitude, so a program needs a rule for deciding which text value comes first. A common rule is lexicographic order, which compares text from left to right, one character at a time.
The process is similar to alphabetizing words in a dictionary. Compare the first characters. If they differ, their order decides the result. If they match, compare the next characters. Continue until a difference is found or one word ends. This is a rule for ordering text, not a claim that a word is numerically larger than another.
For example, under a simple alphabetic comparison, “maple” comes before “marble”: the first two characters match, and the third characters, p and r, differ. The result is settled there; later characters do not change it. If all characters in the shorter word match, the shorter word comes first. So “net” comes before “network”.
- Text comparisons need a defined ordering rule.
- The first differing character determines the order.
- If one text value is a matching prefix of the other, the shorter value comes first.
2. Case and the comparison rule
Before comparing text, decide how to handle uppercase and lowercase letters. A strict character comparison can treat “Mira” and “mira” as different because their first characters are not the same character. This can produce an ordering that does not match ordinary, case-insensitive alphabetizing.
For a case-insensitive comparison, one straightforward approach is to convert both values to lowercase before comparing. This makes “Mira” and “mira” equal under that comparison rule. If the converted values are equal, the program can report that they match for this purpose, even though the original spellings are not identical.
Lowercasing is a practical classroom rule for basic English text, not a complete rule for every language or every kind of text. The important habit is to state the rule and apply it consistently. Python's ordinary string comparison follows the character values used by the language; it should not be assumed to produce every reader's preferred dictionary order.
- Choose whether case matters before comparing.
- Applying the same lowercase conversion to both values gives a simple case-insensitive comparison.
- A program's text order follows its comparison rule, which may differ from a human sorting convention.
3. A small comparison algorithm
The following Python 3 function compares two text values using a simple rule: lowercase both values, then use Python's string ordering to decide whether the first comes before, after, or matches the second. Its precondition is that both inputs are strings. The input is a pair of text values. The algorithm converts each to lowercase and compares the results. The expected output is one of the three labels shown by the return statements.
For a class activity, trace the function by writing down the two lowercase values and checking the comparison between them. Do not rely on how the original capitalization looks. The code is included for reading and tracing; it has not been run.
- State the precondition and comparison rule before interpreting a result.
- Normalize both inputs in the same way.
- The result describes order under the chosen rule.
4. Interpreting comparisons carefully
A comparison result is useful only when its rule fits the task. A program that sorts usernames might need a consistent order for display; a program that checks whether a typed answer matches a stored answer may need to ignore capitalization. Those are different purposes, even though both involve comparing text.
Do not confuse “comes before” with “is the correct answer,” “is more important,” or “has a smaller value.” The comparison only reports a relationship between the two text values according to the rule. Likewise, two values can compare as equal after lowercasing while differing in their original capitalization.
A reliable trace identifies the inputs, applies the same rule to both, and states what the result means. When reviewing someone else's program, look for the comparison rule rather than assuming that text is ordered exactly as it appears on a keyboard or in a dictionary.
- The meaning of a comparison depends on the task and the rule.
- Case-insensitive equality does not mean the original strings are identical.
- Explain a result in terms of the selected ordering rule.
Worked example
Compare two labels without considering case
Use the stated rule to compare “Birch” and “beech”. Predict the function's result and explain which character settles the order.
Python 3
def compare_text(first, second):
a = first.lower()
b = second.lower()
if a < b:
return "before"
if a > b:
return "after"
return "same"- Apply the ruleThe precondition is that both inputs are strings. Convert both to lowercase: “birch” and “beech”. Their first characters match. The second characters differ: i comes before e? It does not; e comes before i. Therefore “beech” comes before “birch”, so the first argument comes after the second.
- State the expected resultThe function returns “after” because its first argument follows its second under this comparison rule.
Answer: The expected result is “after”. At the second character, e in “beech” comes before i in “birch”.
Check: The conclusion follows from the first differing character, not from comparing the words as numbers.
Worked example
A matching prefix
Using the same rule, compare “orbit” and “orbital”. Decide which comes first and why.
Python 3
first = "orbit"
second = "orbital"
# Under the lesson's rule, first is before second.- Compare the shared charactersThe inputs are strings, so the function's precondition is met. After lowercasing, both begin with “orbit”. No differing character appears in the shorter value.
- Use the prefix ruleBecause “orbit” ends while “orbital” continues, the shorter matching value comes first. The expected function result for the pair in the stated order is “before”.
Answer: “Orbit” comes before “orbital” because it is the shorter value and all its characters match the beginning of “orbital”.
Check: A prefix comparison is decided when one value ends after all of its characters have matched.
Worked example
Case-insensitive match
A user enters “LANTERN” while the stored label is “Lantern”. Under the lesson's rule, do the values compare as before, after, or the same?
Python 3
first = "LANTERN"
second = "Lantern"
# Lowercase both before comparing.- Normalize both inputsBoth inputs are strings. Lowercasing produces “lantern” for each one. Apply the same conversion to both values before deciding whether they match.
- Interpret the resultThe converted strings are equal, so the function returns “same”. This is equality under the case-insensitive rule; the original strings still have different capitalization.
Answer: They compare as “same” under the lesson's rule, although the original strings are not identical in capitalization.
Check: The comparison is about the lowercased values, not exact character-for-character equality of the original inputs.
Common mistakes and how to avoid them
Assuming words are compared by their length or by a hidden numerical value.
Correction: Use the stated text-ordering rule. In a left-to-right comparison, the first differing character settles the order.
Applying lowercase conversion to only one value.
Correction: Apply the same conversion to both values so the comparison is consistent.
Treating case-insensitive equality as proof that the original strings are identical.
Correction: Say that the values match after the chosen normalization; preserve the distinction between that result and exact spelling.
Lesson summary
- Non-numeric text can be compared by following an agreed ordering rule.
- In a left-to-right comparison, the first differing character decides the order; a shorter matching prefix comes first.
- Decide whether case matters. If it does not, convert both text values consistently before comparing.
- Describe the result in relation to the rule rather than treating it as a numerical or value judgment.
Check your understanding
Question 1
Using lowercase conversion and the lesson's comparison rule, which comes first: “Cedar” or “Cave”?
- Cave, because the second characters differ and a comes before e
- Cedar, because it is longer
- They compare as the same because both begin with C
- The result cannot be determined unless the words are converted to numbers
Show answer and explanation
Cave, because the second characters differ and a comes before e
After lowercasing, the first characters match. At the second character, a in “cave” comes before e in “cedar”, so “cave” comes first.
Question 2
Under the case-insensitive rule, what is the result of comparing “Pond” with “POND”?
- The first value comes before the second
- The first value comes after the second
- They compare as the same
- They cannot be compared because capitalization differs
Show answer and explanation
They compare as the same
Both values become “pond” after lowercasing, so they compare as the same under this rule.
Key terms
- Non-numeric data
- Information that is not represented as a number, such as a word, name, or label.
- Lexicographic order
- An ordering of text based on comparing characters from left to right, with the first difference deciding the order.
- Prefix
- Text that matches the beginning of a longer text value.
- Case-insensitive comparison
- A comparison that treats uppercase and lowercase forms as equivalent according to a chosen normalization rule.
Continue through ICS4U
View the complete ICS4U Ontario Grade 12 Computer Science curriculum and lessons
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation A1.3. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.