First programs: values, names and operators
Statements evaluate expressions into values, names bind to values, and every operator has a type it works on.
Type 2 + 3 into a Python prompt and something answers back. That reply is a
value, and the single line you typed is a statement that produced it.
Almost every program, no matter how large, is a sequence of such statements:
evaluate an expression, produce a value, and either print it, store it under a
name, or pass it on.
Programs are pipelines of values
Python evaluates expressions to values, and names are just labels that point
at whatever value is currently on the right-hand side of =. Reading x = 7
as "store 7 in box x" is close enough to start, but the label reading will
keep you out of trouble later when two names point at the same list.
The workbench below is a live expression evaluator. Move the operands, pick an operator, and watch both the value and its type change.
Set the operands and operator — the value, its type and the output update live
expression
x = 7 // 2
value
3
type(x) = int
print(f"Hello, {name}! x is {x}")
Hello, Ada! x is 3
// floors and % follows the divisor, so -3 // 2 is -2 and -3 % 2 is 1 — unlike JavaScript. Choosing an operator is choosing the value, the type and often the sign of the result.
Notice that the same pair of numbers gives an int with one operator and a
float with another. The operator, not the operands, decides the result type.
Names are bindings, not boxes
Assignment evaluates the right side first, then binds the name on the left:
x = 7 # x now refers to the int 7
y = x + 1 # the right side is evaluated to 8, then bound to y
x = "seven" # rebinding x does not change yPython is dynamically typed: a name can be rebound to a value of a different type at any time. The interpreter checks whether an operation makes sense when the operation runs, not when the program is written — which is why a type error can surface only when a particular branch executes.
Operators carry a type
The arithmetic operators are not interchangeable across types:
+on numbers adds; on strings it concatenates. Mixing them raisesTypeError./always produces afloat, even when it divides evenly:6 / 3is2.0.//is floor division and%is the remainder; both round toward minus infinity, so negative operands can surprise you.**is exponentiation, and*on a string repeats it.
Division and modulo are not symmetric for negatives
In Python, -3 // 2 is -2 and -3 % 2 is 1 — the quotient is floored
toward minus infinity and the remainder keeps the sign of the divisor. In C
and JavaScript these same expressions give -1 and -1. If you translate an
index calculation between languages, this is a bug waiting to happen.
Input and output are calls
Writing output is a function call: print(...) converts its arguments to text
and writes them to standard output separated by spaces. Reading input is also a
call, and it always returns a string:
name = input("your name? ") # name is always a str
count = int(name) # convert explicitly when you need a number
print(f"hello, {name}") # f-strings interpolate expressionsThe conversion step matters: input never returns a number, so "7" + 1 fails
with a TypeError where 7 + 1 succeeds. Parsing text into the type you want is
the programmer's job, and it is where a lot of beginner bugs live.
Illustrative vs real
The workbench restricts operands to small integers and shows four decimal
places so the results fit the panel. Real Python has arbitrary-precision
integers, the decimal and fractions modules, and a full set of bitwise
and comparison operators. The rule that the operator decides the result type
does not change with scale.
Check yourself
Eduspheria wiki · Programming & Data Structures, Python foundations
0 / 5 answered
From the assignment paper
Modeled on NITJ AI-503, Assignment/Quiz
0 / 5 answered
Where next: booleans and branches — making a program choose one path or another.