androidengineers.Book a session

Python 01 · Start programming

Expressions, names, and basic data types

articleSelf-paced

Learn the concept

An expression produces a value. Assignment binds a name to that value. Python names do not declare a fixed type, but the values they refer to do have types. Integers represent whole numbers, floats approximate real numbers, strings hold text, and Booleans represent true or false.

Use names that describe meaning, such as ticket_count, rather than abbreviations that hide the domain. Reassignment changes which value a name refers to. It does not retroactively change a previously computed result.

Conversions are explicit when input arrives as text. int("12") produces an integer, while int("twelve") raises an exception. Do not assume that all text can be converted. Floating-point arithmetic is approximate; reserve integer minor units or decimal arithmetic for quantities requiring exact decimal behavior.

Run and inspect

count = 3
price = 2.5
total = count * price
count = 4
assert total == 7.5
assert isinstance(count, int)
assert int("12") == 12

Your exercise

Create variables for a document name, page count, and whether it is active. Calculate a derived value, then reassign an input and explain why the previous result stays unchanged.

Check your understanding

You distinguish a string containing digits from a number and can predict the value of each expression before running it.

Guided workshop: trace a program before running it

You are preparing a document import. A source contains three files, and two more arrive. A variable gives a value a name; assignment does not create a permanent equation between names. Predict every line before executing this complete script in counts.py with python3 counts.py (or py counts.py on Windows).

# academy-check
received = 3
snapshot = received
received = received + 2
label = "documents"
print(received, snapshot, label)
print(type(received).__name__, type(label).__name__)
assert (received, snapshot) == (5, 3)

The output is 5 3 documents, followed by int str. Python evaluates the right-hand side before assigning the result. snapshot still refers to the integer 3. Changing a name is different from modifying a mutable object; collections later make that distinction visible.

Now replace 2 with "2". The addition fails because text is not an integer. Convert external text with int() at an input boundary rather than scattering conversions throughout the program. Converting "two" fails too; conversion does not establish that arbitrary input is valid.

Build your own script with received, rejected, and accepted. For 12 received and 3 rejected, print Accepted 9 of 12 documents. Use subtraction for the count and an f-string for the message. Do not put quotation marks around variable names inside expressions.

Worked solution: accepted = received - rejected, then print(f"Accepted {accepted} of {received} documents"). Run cases (0, 0), (12, 3), and (5, 5). The accepted counts must be 0, 9, and 0. A rejected count greater than the received count is a domain error; a later validation function will reject it.

Checkpoint: explain why snapshot stays 3, why "3" + "2" produces text 32, and why a program that runs without exceptions can still calculate the wrong result. Save the script and its output in your learning repository.

YOUR LEARNING JOURNEY

0 of 42 available lessons completed

Progress saved in this browser. No account needed.
Expressions, names, and basic data types | Python for AI Engineering | Android Engineers