Wiki
Intro10 min read

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 y

Python 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 raises TypeError.
  • / always produces a float, even when it divides evenly: 6 / 3 is 2.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 expressions

The 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

  1. 1In Python, what is the value of 7 // 2?
    Numeric answer
  2. 2What does 6 / 3 evaluate to in Python?
    Multiple choice
  3. 3A Python name is permanently locked to one type after it is first assigned.
    True / false
  4. 4Which built-in function always returns a string, requiring an explicit conversion before arithmetic?
    Short answer
  5. 5Evaluate (2 + 3) * 4 - 5 using Python operator precedence.
    Numeric answer

From the assignment paper

Modeled on NITJ AI-503, Assignment/Quiz

0 / 5 answered

  1. 1A string holds the text WELCOME, which is 7 characters long. What index is its final character stored at?
    Numeric answer
  2. 2A call to the string method find() searches for a substring that is not present. What does it return?
    Multiple choice
  3. 3Which two string operations together turn a padded piece of text into its uppercase form with the surrounding spaces removed?
    Multiple choice
  4. 4What integer does the built-in ord() return for the single character z?
    Numeric answer
  5. 5Given s = "WELCOME", what does the slice s[4:] evaluate to?
    Multiple choice

Where next: booleans and branches — making a program choose one path or another.