Chapter 1. Binary Numbers and Your First Python FREE
1.1 Welcome to the year of real code
In Grade 7 you learnt to read line-based code next to blocks, to plan with pseudocode and to follow programs with trace tables. This year you write the real thing. Grade 8 Coding and Robotics keeps the same four strands, but the coding strand grows: you will work in the Python shell and the IDE, make decisions with IF and ELIF, command servos by their exact angle, and build towards a project with a serious purpose, a smart food garden that senses the soil and the sun. Before any of that, we start where the computer itself starts: with 1 and 0.
1.2 Binary: how computers count
Everything inside a computer, numbers, words, photos, code, is stored as a pattern of 1s and 0s. Each 1 is a tiny switch that is on, and each 0 is a switch that is off. There is no third option, so computers count in the binary number system: a number system with only two digits.
Binary works exactly like the decimal numbers you know, except that each place is worth double the place to its right instead of ten times as much. Reading from the right, the places are worth 1, 2, 4 and 8. Worked example: the binary number 1011 has a 1 in the eights place, a 0 in the fours place, a 1 in the twos place and a 1 in the ones place. That gives 8 + 0 + 2 + 1 = 11. And it works in reverse: to write the decimal number 13 in binary, take an 8 (13 - 8 leaves 5), take a 4 (5 - 4 leaves 1), skip the 2, take the 1: 13 is 1101 in binary.
Here is the catch: the same bits can mean different things. The eight bits 01000001 are the number 65, but they are also the letter A, because the ASCII table, an agreed list that pairs numbers with characters, says 65 stands for A. The computer cannot tell which one you mean from the bits alone. It needs to know what TYPE of data it is storing, exactly the data types you met in Grade 7: strings, integers, floats and Booleans. This chapter ends with that idea in action.
1.3 The shell: a conversation with Python
Python is the line-based language from Grade 7, and the shell is the window where you talk to it directly: you type one line, press Enter, and Python answers immediately. Two rules first. One: Python is case sensitive. The instruction print works; Print and PRINT do not, because Python treats capital and small letters as different. Two: Python does mathematics with the operators + for addition, - for subtraction, * for multiplication and / for division.
Integers and floats behave just as they did in Grade 7, with one Python habit to learn: division always answers with a float. Type 7 / 2 and the shell answers 3,5, as you would expect, but even 10 / 2 answers 5,0, not 5. The order of operations is the one from Mathematics: brackets first, then multiplication and division, then addition and subtraction. So 2 + 10 / 2 gives 7,0 (the division happens first), while (2 + 10) / 2 gives 6,0.
1.4 When the shell complains: syntax errors
Break the grammar rules of the language and Python refuses to work: instead of an answer, the shell shows an error message. Type 2 +* 3, or leave out a bracket in print("Hello", or misspell print, in every case the line does not run, and the message points at the place where Python got confused. These are the syntax errors from Grade 7 Chapter 5, now live on your own screen. The habit to build from day one: read the message, find the spot, fix it, press Enter again. Every programmer in the world does this daily.
1.5 Variables, input and print
A variable is still the labelled box you met in Grade 7, the shell just makes it real. Type apples = 4 and the box exists; type apples * 3 and the shell answers 12. Two functions complete the conversation. The function print() shows a value on the screen: print(apples) shows 4. The function input() does the opposite: it pauses, waits for the user to type something, and hands over whatever was typed. A two-line program: name = input() stores what the user types, and print("Hello " + name) greets them with it.
1.6 Typecasting: "13" is not 13
One trap hides inside input(): it ALWAYS hands back a string, even when the user types digits. If the user types 7, the variable holds the string "7", text, not a number. And the + operator does different work for different types: for numbers it adds, but for strings it joins. So "7" + "5" gives 75, the two texts glued together, not 12.
The fix is typecasting: converting a value from one type to another. The function int() converts a string of digits into an integer: int("7") + int("5") gives 12. Trace this program, assuming the user types 7 and then 5:
first = input()
second = input()
total = int(first) + int(second)
print(total)
| Line | first | second | total |
|---|---|---|---|
| first = input() | "7" | - | - |
| second = input() | "7" | "5" | - |
| total = int(first) + int(second) | "7" | "5" | 12 |
| print(total) | the screen shows 12 | ||
Summary
- Computers store everything as 1s and 0s: binary, the number system with two digits, where each place doubles (1, 2, 4, 8).
- 1011 in binary is 8 + 0 + 2 + 1 = 11; the decimal number 13 is 1101 in binary.
- The same bits need a data type: 01000001 is the number 65 or, via the ASCII table, the letter A.
- The shell answers one line at a time; Python is case sensitive; division always gives a float; order of operations applies (2 + 10 / 2 is 7,0).
- Syntax errors stop a line from running; read the message, fix the spot, try again.
- input() always hands back a string; "7" + "5" is 75, but int("7") + int("5") is 12, typecasting converts the type.