# Higher Python: learn by making it work

An interactive practice course for Python programming concepts associated with Higher Computing Science Software Design & Development. This resource is not a complete Higher Computing Science course; the qualification also includes other areas such as data and information, web design, and computer systems.

Official course information: https://www.sqa.org.uk/sqa/56924.html  
Course specification: https://www.sqa.org.uk/sqa/files_ccc/h-course-spec-computing-science.pdf

## 1. Variables, types and input

Variables give values meaningful names. Common Python types include `str` (text), `int` (whole number), `float` (decimal), and `bool` (True or False). `input()` returns text, so convert it with `int()` or `float()` before calculations.

```python
name = "Ari"
score = 72
average = 72.5
is_ready = True
print(name, score)
```

**Practise:** Create a temperature variable, convert Celsius to Fahrenheit with `(temperature * 9 / 5) + 32`, and print it.

## 2. Selection

Use `if`, `elif`, and `else` to make a decision. Conditions use comparison operators such as `==`, `!=`, `<`, `>=`. Indentation defines each block. `=` assigns; `==` compares.

```python
score = 64
if score >= 70:
    grade = "A"
elif score >= 50:
    grade = "Pass"
else:
    grade = "Try again"
print(grade)
```

**Practise:** A ride requires a height of at least 140 cm. Print an appropriate message for someone who can ride or is not tall enough.

## 3. Boolean logic

Combine conditions with `and`, `or`, and `not`. `and` requires both conditions to be true; `or` requires at least one. Use parentheses to make mixed conditions clear.

```python
age = 16
has_ticket = True
if age >= 12 and has_ticket:
    print("Entry allowed")
```

**Practise:** Approve a laptop loan only when the student is in S4 or above and has returned a previous loan. Check boundary cases.

## 4. Iteration with for

A `for` loop processes items in a sequence. `range(1, 6)` produces 1 through 5 because the stop value is excluded.

```python
for number in range(1, 6):
    print(number)
```

**Practise:** Print the 7 times table from 7 × 1 to 7 × 10.

## 5. Iteration with while

A `while` loop repeats while its condition remains true. Update the condition's data within the loop to ensure it can end.

```python
count = 3
while count > 0:
    print(count)
    count -= 1
print("Go!")
```

**Practise:** Start at 1 and double a value while it is below 100. Print each value.

## 6. Validation

Validation checks that data is acceptable before the program uses it. A range check tests limits; a presence check checks that something was supplied. Try normal, boundary, and invalid values.

```python
mark = 68
if 0 <= mark <= 100:
    print("Mark accepted")
else:
    print("Enter a mark from 0 to 100")
```

**Practise:** Check whether an age is between 0 and 120 inclusive. Print a useful response for invalid data.

## 7. Strings and lists

String and list indexes start at zero. A slice `word[1:4]` includes positions 1, 2, and 3. Lists support operations such as `append`, `len`, and indexing.

```python
word = "computer"
print(word[0], word[1:4])
scores = [62, 71, 85]
scores.append(90)
print(len(scores))
```

**Practise:** Loop through a list of scores to calculate the total and average. Check that the list is not empty before dividing by its length.

## 8. Functions

Functions give programs reusable, named parts. Parameters are names in the definition; arguments are values supplied when the function is called. `return` sends a result back.

```python
def calculate_area(width, height):
    return width * height

print(calculate_area(4, 6))
```

**Practise:** Write `is_even(number)` which returns True for even values and False otherwise.

## 9. Reading and writing files

Use `with open(...)` to work with a file; Python closes it when the block ends. Common modes are `"r"` for reading, `"w"` for writing, and `"a"` for appending.

```python
with open("notes.txt", "w") as file:
    file.write("Practise each day.\n")

with open("notes.txt", "r") as file:
    print(file.read())
```

**Practise:** Save a short message and read it back. The website's isolated browser runner does not preserve files between runs; try this example with local Python.

## 10. Algorithms, testing and evaluation

An algorithm is a finite sequence of clear steps. Decompose a problem, plan before coding, and use pseudocode or trace tables to reason about it. Test normal, boundary, and erroneous data. Compare actual output with expected output, fix defects, and evaluate the result against requirements.

```python
def is_valid_mark(mark):
    return 0 <= mark <= 100

for mark in [-1, 0, 57, 100, 101]:
    print(mark, is_valid_mark(mark))
```

**Practise:** Test the function with a typical mark, both boundaries, and an invalid value. Record the expected and actual result for each.

---

## Using this pack in NotebookLM

On the website, download `higher-python-course.md`, then add the file as a source in NotebookLM. You can also add the public website URL with NotebookLM's Website source option. NotebookLM features and accepted source formats can change; if a URL import is unavailable, use the downloadable Markdown file.
