Create your own
Lesson illustration

Python Values and Operators: Numbers, Strings, Booleans, and None

Welcome back. In the previous lesson, you practiced the core tracing habit: Python executes statements in order, assignments update variable state, and print() shows a value without changing it. We also distinguished notebook state from a fresh script run.

Now we will make those variables more useful. Python values have types, and an operator such as + means different things depending on the types involved. By the end of this lesson, you should be able to recognize the core values you will repeatedly encounter in data work—numbers, text, Boolean values, and None—and choose operators that produce the result you intend.


Values have types, and types give operators meaning

Start with a small set of values representing an analytics task:

order_count = 25
conversion_rate = 0.083
customer_segment = "returning"
is_active = True
last_contact_date = None

These variables hold five important kinds of Python values:

Value typeExampleMeaning
int25An integer: a whole number
float0.083A floating-point number: a number with a decimal point
str"returning"A string: text enclosed in quotes
boolTrueA Boolean value: True or False
NoneTypeNoneA deliberate “no value” or “not available” marker

Use the built-in type() function when you need to inspect a value:

print(type(order_count))
print(type(conversion_rate))
print(type(customer_segment))
print(type(is_active))
print(type(last_contact_date))

The output will identify each value’s type. You will not normally print types in finished analysis code, but type() is a valuable diagnostic tool when a calculation behaves unexpectedly.

Three details matter immediately:

  1. 25 is a number, while "25" is text that happens to contain digits.
  2. True is a Boolean value, while "True" is a nonempty string.
  3. None has no quotation marks. "None" is just text.

Python will not automatically treat text that looks numeric as a number. This is especially important when working with CSV files, APIs, and user inputs, where values often arrive as strings.

raw_orders = "12"

print(raw_orders + raw_orders)

This produces:

1212

Python joins the two strings; it does not add twelve plus twelve. Convert deliberately when the text represents a valid integer:

raw_orders = "12"
order_count = int(raw_orders)

print(order_count + order_count)

Now the output is:

24

A compact trace makes the change clear:

Lineraw_ordersorder_countType of order_count
raw_orders = "12""12"
order_count = int(raw_orders)"12"12int
print(order_count + order_count)"12"12int

The conversion functions you will use most often at this stage are:

int("12")       # 12
float("0.083")  # 0.083
str(25)         # "25"
bool(1)         # True

Conversion is not cosmetic. It determines whether Python performs arithmetic, joins text, or raises an error because an operation does not make sense for those types.

Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Data

Watch “Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Data” by Corey Schafer to see type inspection, arithmetic, precedence, and a realistic string-to-number conversion demonstrated in code.

Watch numeric types for the distinction between integers and floats. Continue with arithmetic operators, paying particular attention to division, floor division, and modulo. Watch precedence for parentheses and update shorthand. Finally, skip to string conversion and notice why "100" + "200" differs from integer addition.


Numeric operators: calculate deliberately

Python supports the arithmetic operators you would expect:

OperatorPurposeExampleResult
+Addition20 + 525
-Subtraction20 - 515
*Multiplication20 * 5100
/Division20 / 54.0
//Floor division23 // 54
%Remainder after division23 % 53
**Exponentiation2 ** 38

For most data calculations, ordinary division (/) is what you want:

total_revenue = 1250
order_count = 25

average_order_value = total_revenue / order_count
print(average_order_value)

The result is 50.0, a float. Python’s / produces a float even when the mathematical answer is a whole number. This is useful because many quantities in analysis—rates, averages, model predictions, probabilities—are naturally fractional.

Use floor division (//) only when you specifically need the number of complete groups. For example, 23 // 5 tells you that 23 observations contain 4 complete groups of 5. The remainder operator (%) tells you what is left:

records = 23
batch_size = 5

complete_batches = records // batch_size
remaining_records = records % batch_size

print(complete_batches)
print(remaining_records)

Parentheses make both Python’s evaluation order and your intent clearer:

revenue = 1200
cost = 750
orders = 30

profit_per_order = (revenue - cost) / orders
print(profit_per_order)

Without the parentheses, multiplication and division happen before subtraction. You do not need to memorize every precedence rule if you use parentheses to group a meaningful intermediate calculation.

An assignment such as this should look familiar from the previous lesson:

order_count = 25
order_count = order_count + 1

After the second line, order_count is 26. Python first calculates the right side, then rebinds the name on the left. The shorthand form is:

order_count += 1

For now, prefer the longer form whenever it helps you trace the update confidently.


Strings: text values, joining, and conversion

A string is text in either single or double quotes:

dataset_name = "customer_churn"
region = 'west'

Choose one quote style consistently. Use the other style when it saves you from escaping a quote inside the text:

message = "Customer's record is complete"

The + and * operators have a different meaning for strings:

first_name = "Amina"
last_name = "Patel"

full_name = first_name + " " + last_name
divider = "-" * 20

print(full_name)
print(divider)

Output:

Amina Patel
--------------------

With strings:

  • + concatenates text.
  • * repeats text a whole-number number of times.

Python does not allow direct concatenation of a string and a number:

order_count = 25

# "orders: " + order_count

Instead, convert the number explicitly:

label = "orders: " + str(order_count)
print(label)

Or, when simply displaying values, let print() separate them:

print("orders:", order_count)

The latter is often easier to read while you are exploring data.

3. An Informal Introduction to Python

Read the relevant portions of the official Python documentation to reinforce the exact behavior of core arithmetic and string operations. Official documentation is worth becoming comfortable with early: later, you will use it to verify a function or operator rather than relying on memory or generated code.

In Section 3.1, “Using Python as a Calculator,” read subsection 3.1.1, “Numbers,” beginning with the arithmetic introduction. Focus on the distinction between /, //, %, and **. Then read subsection 3.1.2, “Text,” beginning with the string introduction. Continue to concatenation rules, noting that Python gives the same symbols different meanings for different value types.


Comparisons produce Boolean values

A Boolean has exactly one of two values:

True
False

The capitalization is required. Python treats true and false as different, undefined names.

Most Booleans arise from a comparison. For example:

actual_orders = 28
target_orders = 25

target_met = actual_orders >= target_orders
print(target_met)

The expression actual_orders >= target_orders evaluates to True, and that resulting Boolean is assigned to target_met.

Use these comparison operators:

OperatorMeaningExample
==equal tostatus == "active"
!=not equal toregion != "unknown"
>greater thanrevenue > 0
<less thanerror_rate < 0.05
>=greater than or equal toscore >= 80
<=less than or equal toage <= 18

Do not confuse = with ==:

status = "active"       # assignment: give status a value
is_active = status == "active"  # comparison: produce True or False

The first line changes program state. The second line asks a question about the current state.

An illustration of a Python comparison machine: numeric inputs are evaluated with operators such as less than, greater than, and equality, producing Boolean outputs. Python spells these values `True` and `False` in code.

Comparisons can involve strings too, particularly equality comparisons:

source_system = "crm"
is_crm_data = source_system == "crm"

print(is_crm_data)

String equality is case-sensitive:

"CRM" == "crm"   # False

For business labels such as country, segment, or status, avoid treating alphabetical ordering as a meaningful data comparison. "premium" > "basic" is technically valid Python, but it does not represent an analytical claim about customer value.

A number and a numeric-looking string are not equal:

25 == "25"  # False

This is a useful signal that a type conversion may be needed before a calculation or comparison.

Python Tutorial for Beginners 6: Conditionals and Booleans - If, Else, and Elif Statements

Watch the selected portions of “Python Tutorial for Beginners 6: Conditionals and Booleans - If, Else, and Elif Statements” by Corey Schafer. The video introduces Boolean comparisons and logical operators visually; the conditional-statement syntax it shows will be the focus of the next lesson.

Begin with comparisons, focusing on why == checks equality while = assigns. Then watch logical operators for and, or, and not. Finish with truthy values, noting that None, zero, and empty text behave differently from values that are present.


Combine Boolean conditions with and, or, and not

A single comparison often is not enough. A data-quality rule might require that a value is present and within an allowed range. Python’s logical operators combine Boolean values.

has_email = True
has_consent = True

can_send_email = has_email and has_consent
print(can_send_email)

Use the operators as follows:

OperatorResult
a and bTrue only when both a and b are True
a or bTrue when at least one of a or b is True
not aReverses the Boolean value of a

For example:

is_employee = False
is_contractor = True

has_internal_access = is_employee or is_contractor
is_not_employee = not is_employee

print(has_internal_access)
print(is_not_employee)

Both values printed are True.

Use parentheses when a compound condition would otherwise be hard to scan:

age = 31
has_consent = True

is_eligible = (age >= 18) and has_consent

Python also supports readable chained comparisons:

score = 82
is_valid_score = 0 <= score <= 100

This checks that score is at least 0 and at most 100.

Short-circuit evaluation

Python evaluates and and or from left to right, stopping when it already knows the outcome. This is called short-circuit evaluation.

denominator = 0
numerator = 10

is_positive_ratio = (
    denominator != 0
    and numerator / denominator > 0.5
)

print(is_positive_ratio)

The result is False, but Python does not attempt the division. Once denominator != 0 is False, an and expression cannot become True, so Python stops. This pattern is useful for writing safe validation logic.

At this stage, use and, or, and not primarily to combine Boolean expressions. Python can also apply them to numbers and strings, returning one of the original values rather than always returning True or False. That behavior is useful later, but it is easy to misuse when you are still building a type-and-state mental model.


None means “no value”; it does not mean zero or empty text

Use None to represent an absent, unknown, or not-yet-computed value:

discount_rate = None

This differs from each of these:

discount_rate = 0       # A known rate of zero
discount_rate = 0.0     # Also a known numeric zero
discount_rate = ""      # An empty text value
discount_rate = "None"  # The literal text None

This distinction matters in data work. A customer may have made zero purchases, which is meaningful. That is different from not knowing their purchase count at all.

When you specifically need to check whether a variable is None, use is None:

last_contact_date = None

is_missing_contact_date = last_contact_date is None
print(is_missing_contact_date)

Likewise:

units_sold = 0

has_known_units_sold = units_sold is not None
print(has_known_units_sold)

This correctly prints True: zero is still a known value.

Some values are considered falsy in a Boolean context:

bool(False)  # False
bool(None)   # False
bool(0)      # False
bool(0.0)    # False
bool("")     # False

Most nonempty strings and nonzero numbers are truthy:

bool("0")    # True: it is nonempty text
bool(3)      # True
bool("False")  # True: it is nonempty text

This is why “is there a value?” and “is the value truthy?” are different questions. For a numeric field where zero is valid, do not use truthiness to decide whether the value is missing. Use is None.


Build one small value-and-operator scratchpad

Spend about 10 minutes creating a notebook named core_values_practice.ipynb. Before running the code, make a short prediction in a markdown cell: record each variable’s final value, its type, and the two printed lines.

raw_orders = "18"

new_orders = int(raw_orders) + 2
is_target_met = new_orders >= 20
report = "orders=" + str(new_orders)

print(report)
print(is_target_met)

Then change only this line:

raw_orders = "17"

Run the cell again and inspect how new_orders, is_target_met, and report change together. This is the same trace discipline from the previous lesson, now applied to type conversion, arithmetic, comparison, and string construction.

Finally, add this separate check:

returned_orders = 0

print(returned_orders is None)
print(bool(returned_orders))

The two outputs should differ. Keep this example in the notebook: it captures a distinction that prevents many data-cleaning mistakes.


Key takeaways

Python’s core values include integers (int), decimal numbers (float), text (str), Boolean values (bool), and the absence marker None. The type of a value determines what an operator means: + adds numbers but joins strings, while == compares values and produces a Boolean.

Use arithmetic operators intentionally, add parentheses where they make calculations clearer, and convert numeric text with int() or float() before doing numerical work. Comparisons such as >= and == produce True or False, and and, or, and not let you combine those conditions safely.

Finally, distinguish None from zero and empty text. In data work, zero is often a valid observed value, while None indicates that no value is available.

Next, you will use these Boolean expressions to write conditional statements that select exactly one action among mutually exclusive cases.

Can't find a good explanation? Sign up and we'll make it for you

Sign up