Python Lab • Foundations

The starting point for NAMSSN UI students who have never written code before. You move from “What is this?” to “I can read and write simple Python comfortably.”

This page is the slow, careful introduction. You can read it on its own or keep the main Python Lab open in another tab to run code in the live pad.

  • Module A: First contact — running code and printing values.
  • Module B: Variables, input, and basic expressions.
  • Module C: Lists, loops, and small tables.
  • Module D: Functions that mirror your formulas.

You do not need any previous programming experience. The only requirement is the willingness to try, make mistakes, and read what Python is telling you.

Module A • First contact with Python

The goal here is very modest: you learn how to run code, see output, and not be afraid of error messages. Once this feels normal, the rest becomes much easier.

Step A.1 — Running your first lines

Open the Python Lab main page and scroll to the Live Python pad. Then type or paste the following:

print(2 + 3)
print(2**5)
print("Hello NAMSSN UI")

You should see the answers appear in the output box. This is already “real coding”: you are giving precise instructions and reading the result.

  • • Change the numbers and run again.
  • • Add a new line like print(7 * 9).
  • • Replace the message with your own name or a short sentence.

Step A.2 — Errors as feedback, not as judgement

Try deliberately causing an error:

print(2 + "3")

Python will complain. That is not an insult; it is simply saying “you mixed a number and a string.” Fix it by changing the line to:

print(2 + int("3"))   # both are now numbers
# or
print(str(2) + "3")  # both are now strings

Every time something breaks, pause and read the message. Over time you will recognise patterns (“NameError”, “TypeError”, “ZeroDivisionError”) just as you recognise familiar theorems in class.

Module B • Variables, expressions, and simple input

A variable is a name you attach to a value. Once you do that, you can reuse the value many times without retyping it.

Step B.1 — Naming values

n = 5
h = 0.01
area = 2 * 3.14 * h

print("n =", n)
print("h =", h)
print("area =", area)

These names are completely your choice. The only rule is that they must be consistent: once you choose n, you keep writing n, not N or m.

  • • Change h to 0.001 and run again.
  • • Add a new variable radius and compute the area of a circle.
  • • Try using non-integer values such as 2.5 or 1.75.

Step B.2 — Simple user input (for later)

In the browser pad we keep things simple and avoid interactive input, but in a local notebook you can write:

n = int(input("Enter an integer n: "))
print("You chose n =", n)

For web-based work, you can simulate this by editing the number directly in the code instead of typing it into an input box.

Module C • Lists and loops

Many mathematical questions have the form “for n = 1, 2, 3, …, what happens?” Python handles this with lists and loops.

Step C.1 — Lists of numbers

xs = [0, 0.5, 1, 1.5, 2]
print(xs)

You can iterate over these values:

for x in xs:
    print("x =", x)

Change the list to something from your course (for example, points where you evaluate a function from calculus).

Step C.2 — Small tables with loops

for n in range(1, 6):
    print(n, n**2, n**3)

This prints a small table of powers. Change the range to range(1, 11) and observe the new values.

  • • Replace n**3 with 2**n.
  • • Print 1/n alongside n.
  • • Think of this as a quick way to explore patterns from your lecture notes.

Module D • Defining your own functions

In mathematics you write formulas like f(x) = x² + 1. In Python you formalise this as:

def f(x):
    return x**2 + 1

print(f(2))
print(f(3.5))

You can combine this with lists and loops:

def f(x):
    return x**2 + 1

xs = [0, 1, 2, 3]
for x in xs:
    print(x, f(x))

This is already a prototype for a computational version of an example from class.

  • • Define g(x) = x**3 - x and test it on a few values.
  • • Define a function that uses parameters, e.g. g(x, a, b) = a*x + b.
  • • Try translating a formula from a UI assignment into a function and test several values.

After this page

Once you are comfortable with everything above, you are ready to:

The long-term plan is that many NAMSSN UI students will treat Python as naturally as a calculator: a tool that is always available when an example or a pattern needs to be tested.