A program represents information with values and chooses what to do with them. In this lesson, you will turn text into a number, classify study sessions, and repeat a calculation. Save each complete Python example in its own file and run it with the environment from lesson 1.
Names point to objects
An assignment such as minutes = 25 binds a name to an integer object. Names can later refer to objects of another type, but keeping one clear meaning per name makes programs easier to follow. Python checks whether an operation is supported when it runs; adding text to an integer raises TypeError.
The basic types are int for whole numbers, float for approximate real numbers, str for Unicode text, and bool for True or False. None represents an absent value. Numeric details and examples are in the introductory tutorial.
raw_minutes = "25"
minutes = int(raw_minutes)
hours = minutes / 60
whole_hours = minutes // 60
remaining = minutes % 60
topic = " Python Basics ".strip().lower()
print(topic)
print(f"{whole_hours} hours, {remaining} minutes")
print(f"Decimal hours: {hours:.2f}")
This prints python basics, 0 hours, 25 minutes, and Decimal hours: 0.42. The formatting rounds the displayed value; it does not change hours. / performs division, // performs floor division, and % gives the remainder. Flooring matters for negative values: -3 // 2 is -2.
Strings are immutable: operations such as strip() and lower() return strings without changing the original string. Indexes start at zero, and a slice such as topic[:6] excludes its ending position. Unicode characters are not always the same as visual symbols; an emoji or accented symbol can contain multiple code points.
Choose a branch
Use if, elif, and else to express alternatives. A colon begins each block, and indentation defines which statements belong to it. Use four spaces consistently. This program processes each value in a list, an ordered collection introduced fully in lesson 4:
sessions = [0, 15, 30, 60]
for minutes in sessions:
if minutes == 0:
label = "not started"
elif minutes < 30:
label = "short session"
else:
label = "focused session"
print(f"{minutes}: {label}")
The labels are not started, short session, focused session, and focused session. Conditions are considered from top to bottom; only the first matching branch runs. Put narrow conditions before broad ones. Swapping the last two conditions without thinking can silently misclassify a boundary value.
== compares values. is compares object identity; use value is None for absence, not is to compare numbers or text. Empty strings, empty collections, zero, and None are false in conditions. A nonempty string such as "False" is true. The built-in types reference describes these rules.
Repeat work deliberately
Use for when visiting items or a known range. Use while when repetition depends on a changing condition. The control-flow tutorial also explains break and continue.
total = 0
for day in range(1, 4):
total += day * 10
print(total)
remaining = 3
while remaining > 0:
print(remaining)
remaining -= 1
The total is 60, followed by countdown lines 3, 2, 1. range(1, 4) excludes 4. A while loop needs progress toward stopping; forgetting the decrement makes this one run forever. break exits the nearest loop, while continue skips to its next iteration.
Pitfalls and practice
input() returns text, even when somebody types digits. Convert explicitly with int; invalid numeric text raises ValueError, which lesson 8 handles. Avoid using eval to interpret input. Floating-point arithmetic is approximate: do not build exact money calculations around decimal-looking floats. Integer minor units or decimal.Decimal are more appropriate for that requirement.
Exercise: classify minutes as invalid when negative, not started at zero, short from 1 through 29, and focused from 30 onward. Test -1, 0, 29, and 30.
Solution guidance: add a negative-value branch before the zero branch. The four expected labels are invalid, not started, short, and focused. Then replace the list with different values and predict every line before executing. Checking values immediately around a boundary is more useful than checking only a comfortable middle value.
Next: Python Functions and Scope.

