Recommended resources
The freely available resources this lesson will direct you to.
Hello! Last time, you ran Python cells and saw that an assignment such as score = 8 stores a value, while an expression such as score + 2 can display a result. This lesson looks more closely at the values inside those expressions.
You’ll work with numbers, strings, and booleans, then use comparison operators to ask questions about values. These are small building blocks, but they matter throughout machine learning: a dataset may contain numeric measurements, text labels, and flags, while comparisons help express rules about those values. Allow about 35–40 minutes to watch the short excerpts and run the code in your notebook.
Values have types
Python distinguishes a whole number such as 12 from a number with a decimal point such as 12.0. Their types are called int and float. Text enclosed in quotes is a string, or str. A boolean, or bool, has one of two values: True or False. Capitalization matters for those boolean values.
Watch the brief introductions to these types in Python Variables – Python Tutorial for Beginners with Examples from Programming with Mosh. You already know how to store a value under a name, so focus on what kind of value each name holds.
Python Variables - Python Tutorial for Beginners with Examples | Mosh
Programming with Mosh introduces integers, floats, booleans, and strings. Watch for the difference between a number and quoted text, and for the capitalization of True and False.
Watch number types, then boolean values, and finish with quoted strings. The examples use assignments, but the key question here is what type of value appears on the right-hand side.
You can check types in your notebook with type(...). Run this cell:
print(type(12))
print(type(12.0))
print(type("12"))
print(type(True))
The results are int, float, str, and bool, in that order. Notice that "12" contains digits but is not a number: the quotes make it text. That distinction determines which operations Python can perform.
Calculate with numbers
An expression is code that produces a value. Arithmetic expressions use familiar operators, though Python writes multiplication as * and powers as **. Parentheses let you make the intended grouping explicit: (4 + 2) * 3 gives 18, whereas 4 + 2 * 3 gives 10.
Watch the arithmetic excerpt from Dave Gray’s Python Operators for Beginners. The examples run in an interactive shell rather than a notebook; you can enter the same expressions in notebook cells.
Python Operators for Beginners | Python tutorial
Dave Gray demonstrates Python’s arithmetic operators, including the three different questions you can ask about division: the quotient, the whole-number quotient, and the remainder.
Watch arithmetic operators. Pay particular attention to /, //, and %: they give different results for the same pair of numbers.
Try the following cell:
print(17 / 3)
print(17 // 3)
print(17 % 3)
print(2 ** 3)
The first result is approximately 5.6667: / performs division and produces a float. The next two results are 5 and 2. For these positive numbers, // gives the whole-number quotient and % gives the remainder; three fits into seventeen five times, with two left over. Finally, ** raises a number to a power, so 2 ** 3 gives 8.
One detail is worth keeping straight: // rounds down, rather than simply removing decimal digits. Thus -17 // 3 is -6. Ordinary / also returns a float when the division comes out evenly: 10 / 5 gives 2.0.
You can put arithmetic results into variables just as you did last lesson:
total = 9 + 3 + 6
count = 3
average = total / count
average
The final expression displays 6.0. Neither total nor average updates itself if you later edit count; rerun the relevant cells to calculate with the new value.
Work with strings
A string holds text, whether it looks like a word, a category label, or digits. Single and double quotes both work: 'ready' and "ready" are the same string. Quotes are part of how you write the value in code, not part of the text that print(...) displays.
The + operator behaves differently with strings than with numbers. Watch the short string example from Dave Gray’s video.
Python Operators for Beginners | Python tutorial
Return to Python Operators for Beginners for its demonstration of joining two strings with +. Notice that a space must be included in one of the strings if you want one in the result.
Watch joining strings, then try the same operator with your own short text values.
For example:
print("sensor " + "reading")
print("2" + "3")
print(2 + 3)
These print sensor reading, 23, and 5. In the middle line, + joins text; it does not interpret the digits as quantities. You can also repeat a string: "go" * 3 produces "gogogo".
An expression such as "2" + 3 does not have a useful meaning to Python and raises a TypeError. When working with data later, this distinction will help you recognize why a column of numeric-looking text cannot automatically be treated as numbers.
Ask questions with comparisons
A comparison expression asks whether a relationship holds. Its value is always the boolean True or False. Watch Dave Gray’s examples, especially the distinction between assignment and equality.
Python Operators for Beginners | Python tutorial
In the comparison portion of Python Operators for Beginners, Dave Gray tests equality and ordering. Focus on the boolean result of each expression and on why equality requires two equals signs.
Start at comparison operators. Ignore the brief terminal-cleanup instructions at the start and concentrate on the six operators being demonstrated.
| Operator | Question it asks | Example that is True |
|---|---|---|
== |
Are the values equal? | 8 == 8 |
!= |
Are they different? | 8 != 9 |
< |
Is the left value smaller? | 8 < 9 |
<= |
Is it smaller or equal? | 8 <= 8 |
> |
Is it larger? | 9 > 8 |
>= |
Is it larger or equal? | 8 >= 8 |
The distinction between = and == is fundamental: limit = 10 stores a value, while limit == 10 asks a question and produces a boolean. Comparisons can use the result of arithmetic:
measurement = 18.5
limit = 20
within_limit = measurement <= limit
print(within_limit)
print(measurement * 2 > limit)
Both printed results are True. within_limit is now a name for a boolean value, not for the measurement itself.
Strings can be compared for equality too. "approved" == "approved" is True, while "Approved" == "approved" is False: capitalization matters. Types matter as well: 2 == "2" is False because a number is not the same value as a string. An ordering comparison such as 2 < "2" raises a TypeError; Python does not define that ordering between a number and text.
A numeric caution: floats are approximations for some decimal values. For example, 0.1 + 0.2 == 0.3 evaluates to False in Python. Use exact equality when exact identity is what you mean; be cautious about using it to test the result of decimal arithmetic.
Combine boolean expressions
Comparisons become more useful when a rule has more than one part. With boolean operands, and is True when both parts are True; or is True when at least one part is True; not reverses a boolean value.
Watch the next portion of Dave Gray’s video. For now, focus on examples whose inputs are booleans—either True and False or the results of comparisons.
Python Operators for Beginners | Python tutorial
The video now combines boolean values with not, and, and or. Watch how each operator changes the result; you do not need to master the discussion of which operand Python checks first yet.
Watch boolean operators. In particular, distinguish “both conditions” for and from “at least one condition” for or.
Here is a complete expression using a numeric measurement and a boolean flag:
measurement = 18.5
is_valid = True
usable = is_valid and measurement >= 0
print(usable)
print(not usable)
The results are True and False. The first expression asks whether the flag is true and the measurement meets the numeric rule. Parentheses can make longer expressions easier to read:
is_valid and (measurement >= 0) and (measurement <= 20)
This also evaluates to True. It is still just an expression that produces a value; it does not yet make Python choose which code to run. You’ll use boolean results for that purpose when you reach conditional statements.
Takeaways
Numbers support arithmetic; strings hold quoted text and can be joined; booleans are True or False. Comparisons such as >= and == produce booleans, and and, or, and not let you combine boolean conditions. Above all, check the type of a value when an expression behaves unexpectedly: "2" and 2 may look similar, but they play different roles in Python.
Next, you’ll store and retrieve several values together using lists and dictionaries—useful when one observation has more than one measurement or label.