Unit 1 Knowledge Handout: Python Foundations

Use this handout to preview and review Unit 1 Lessons 1-6: instructions, output, values, variables, expressions, input, conversion, and small interactive programs.

1. Instructions, Algorithms, and Errors

An algorithm is a precise, finite sequence of steps for completing a task. A program turns an algorithm into instructions a computer can execute.

Python normally runs statements from top to bottom. Small details matter: spelling, punctuation, capitalization, and indentation can change what the program does.

Error familyWhat happens
Syntax errorPython cannot understand the code structure.
Runtime errorThe program starts, then stops because an operation cannot continue.
Logic errorThe program runs, but its result is wrong.

Read the final line of an error message first, locate the named line, change one thing, and run again.


2. Output, Strings, and Comments

print() displays a value. Text written directly in code is a string literal and must be inside matching quotes.

print("Hello, Python!")
print("Line 1\nLine 2")
print("She said, \"Hello!\"")
Escape sequenceMeaning
\nStart a new line
\"Put a double quote inside a double-quoted string
\\Display one backslash

Comments explain code to people. Python ignores everything after # on that line.

# A comment can use its own line.
print("Ready")  # It can also follow code.

3. Values, Types, and Expressions

An expression is code that produces a value. type() reports the type of a value.

TypeMeaningExamples
intWhole number7, -3, 0
floatDecimal number2.5, -0.75
strText"Python", "42"
boolLogical valueTrue, False
print(8 + 3)          # 11
print(type(2.5))      # <class 'float'>
print("Py" + "thon") # Python

+ adds numbers but concatenates strings. A numeric-looking string is still text.


4. Variables and Assignment

A variable is a name bound to a value. Assignment evaluates the right side first, then stores that result under the name on the left.

score = 10
score = score + 5
print(score)  # 15

Variable names may contain letters, digits, and underscores, but cannot begin with a digit or use a Python keyword. Names are case-sensitive. Prefer meaningful snake_case names such as total_price.


5. Numeric Operations

OperatorMeaningExampleResult
+Addition7 + 29
-Subtraction7 - 25
*Multiplication7 * 214
/Division7 / 23.5
//Floor division7 // 23
%Remainder7 % 21
**Power2 ** 38

Python evaluates parentheses first, then powers, then multiplication/division/floor division/remainder, then addition/subtraction. Operators at the same level normally run left to right.

Use parentheses when they make the intended calculation clearer.


6. Formatting Output with f-Strings

An f-string places values inside text. Put f before the opening quote and expressions inside braces.

name = "Mina"
score = 92
print(f"{name} scored {score} points.")

The braces contain Python expressions, not printed brace characters.


7. Input and Type Conversion

input() pauses the program, displays a prompt, and returns the user's response as a str.

name = input("Name: ")
age = int(input("Age: "))
height = float(input("Height in metres: "))

Use int() for whole-number text and float() for decimal text. Conversion fails if the text is not a valid number.

Think in three stages:

  1. Input: collect the data.
  2. Processing: calculate or transform it.
  3. Output: display the result.
price = float(input("Price: "))
quantity = int(input("Quantity: "))
total = price * quantity
print(f"Total: {total}")

8. Building and Testing a Small Program

Build the smallest working version first. Add one feature at a time and run after each meaningful change.

Test with more than one input. Include an ordinary case and a boundary or unusual case when one exists. Check the prompt, calculation, output label, and data type separately.