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 family | What happens |
|---|---|
| Syntax error | Python cannot understand the code structure. |
| Runtime error | The program starts, then stops because an operation cannot continue. |
| Logic error | The 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 sequence | Meaning |
|---|---|
\n | Start 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.
| Type | Meaning | Examples |
|---|---|---|
int | Whole number | 7, -3, 0 |
float | Decimal number | 2.5, -0.75 |
str | Text | "Python", "42" |
bool | Logical value | True, 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
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 2 | 9 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 7 * 2 | 14 |
/ | Division | 7 / 2 | 3.5 |
// | Floor division | 7 // 2 | 3 |
% | Remainder | 7 % 2 | 1 |
** | Power | 2 ** 3 | 8 |
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:
- Input: collect the data.
- Processing: calculate or transform it.
- 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.