Chapter 1: Getting Started with Python Basics#
This chapter introduces you to programming in Python and, more broadly, to thinking like a computer scientist. You'll learn how Python handles numbers and text, how to write simple calculations, and how to read Python's error messages. By the end, you'll have the vocabulary to describe what Python is doing — and why it sometimes does something unexpected.
Arithmetic Operators#
Let's start with the most familiar territory: arithmetic. An arithmetic operator is a symbol that tells Python to perform a calculation. Python supports six core arithmetic operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Addition | 30 + 12 |
42 |
- |
Subtraction | 43 - 1 |
42 |
* |
Multiplication | 6 * 7 |
42 |
/ |
Division | 84 / 2 |
42.0 |
// |
Integer (floor) division | 84 // 2 |
42 |
** |
Exponentiation | 7 ** 2 |
49 |
One thing worth pausing on: when you divide two whole numbers with /, Python gives you back a decimal result even when the answer is a whole number — 84 / 2 gives 42.0, not 42. This is because Python recognises two distinct families of numbers (we'll get to that in the "Values and Types" section).
Integer division (also called floor division) uses // and always rounds the result down toward the nearest whole number. So 85 // 2 gives 42, not 43, because 42.5 rounds down to 42. Think of it like asking "how many full groups fit?" — any leftover gets thrown away.
A common gotcha: if you're coming from another programming language, you might expect ^ to mean "to the power of." In Python, ^ is actually the XOR bitwise operator, so 7 ^ 2 gives 5, not 49. Always use ** for exponentiation in Python.
📝 Section Recap: Python's six arithmetic operators cover addition, subtraction, multiplication, regular division, floor division, and exponentiation. Regular division always returns a decimal; floor division always returns a whole number rounded down. Use
**— not^— for powers.
Expressions#
Now that you know the operators, you can combine them with numbers to form an expression — any combination of operators, values, and (later) variables that Python can evaluate down to a single result.
For example, 6 + 6 ** 2 is an expression containing two operators. Its value is 42 because Python doesn't just read left to right — it follows a priority ordering called the order of operations:
- Exponentiation (
**) first - Then multiplication and division (
*,/,//) - Then addition and subtraction (
+,-)
So in 6 + 6 ** 2, Python calculates 6 ** 2 = 36 first, then adds 6, giving 42.
Here's a useful mental model: think of operators as having "sticking power." The ones with high priority stick to their numbers first before the low-priority ones get a chance. If you want a different order, use parentheses — Python always evaluates the innermost parentheses first, just like in normal maths class.
Key point: Every expression has exactly one value — the result Python gets when it works out the calculation.
📝 Section Recap: An expression is any combination of numbers and operators. Python evaluates expressions using a fixed order of operations (exponentiation before multiplication/division before addition/subtraction). Use parentheses to override that order.
Arithmetic Functions#
Beyond operators, Python has built-in functions — named tools you can call to perform more specialised operations. Three worth knowing right away:
round(x)— rounds a floating-point number to the nearest whole number.round(42.4)gives42;round(42.6)gives43.abs(x)— returns the absolute value, meaning the positive version of any number.abs(-42)gives42;abs(42)also gives42.type(x)— tells you the kind of value something is (more on this soon).
A function call is what it's called when you use one of these tools — you write the function's name immediately followed by parentheses containing whatever you want to pass in. Those parentheses are not optional. If you write abs 42 instead of abs(42), Python will complain with a syntax error — an error that means your code isn't written in a way Python can parse at all.
Interestingly, if you type just abs without any parentheses or value, Python doesn't crash — it shows you <function abs(x, /)>, which is Python's way of saying "yes, abs is a valid function object." A bare function name is a legal expression; it just doesn't call the function.
📝 Section Recap: Functions like
round(),abs(), andtype()perform common operations. You invoke (use) a function by writing its name followed by parentheses containing the input. Missing parentheses trigger a syntax error.
Strings#
Python isn't only about numbers. It can also work with text. A string is a sequence of characters — letters, digits, spaces, punctuation — enclosed in either single quotes ('...') or double quotes ("..."). Think of the characters as beads and the quotes as the thread that holds them together.
'Hello' # valid string
"world" # also valid
"it's a small" # double quotes let you include an apostrophe insideTwo operators carry over from numbers to strings, but they behave differently:
+performs concatenation — it joins two strings end-to-end.'Well, ' + 'world.'gives'Well, world.'*repeats a string.'Spam, ' * 4gives'Spam, Spam, Spam, Spam, '
The other arithmetic operators (-, /, **) do not work on strings — trying them causes a TypeError.
The built-in len() function counts how many characters are in a string. len('Spam') gives 4. It counts only the characters between the quotes, not the quotes themselves.
One tricky point: always use straight (vertical) quotes. Backticks (`) and "curly" (smart) quotes — the ones word processors automatically produce — are not valid in Python and will cause a syntax error.
📝 Section Recap: Strings are sequences of characters wrapped in straight quotes. You can concatenate strings with
+, repeat them with*, and measure their length withlen(). The operators-,/, and**are not defined for strings.
Values and Types#
Every value in Python belongs to a type — a category that determines what kind of data it is and what you can do with it. The three types introduced in this chapter are:
| Python type | Name | Examples |
|---|---|---|
int |
Integer | 2, -7, 1000000 |
float |
Floating-point number | 42.0, 3.14, -0.5 |
str |
String | 'Hello', "42", '3.14' |
You can check any value's type with type(...). For example, type(2) returns int and type('Hello') returns str.
These type names double as conversion functions:
int(42.9)converts a float to an integer, always rounding down — you get42, not43.float(42)converts an integer to a float:42.0.int('126')converts a digit-only string to an integer.float('12.6')converts a string containing a decimal number to a float.
Here's a classic confusion: '126' looks like a number, but Python sees it as a string because of the quotes. Trying to divide it — '126' / 3 — produces a TypeError because division isn't defined between a string and an integer. The fix is int('126') / 3, which gives 42.0.
Another quirk: if you write a large number with commas — 1,000,000 — Python does not treat it as one million. Instead, it reads it as three separate integers: (1, 0, 0). To make large numbers readable without tripping Python up, use underscores instead: 1_000_000 gives 1000000.
📝 Section Recap: Python values come in types —
int,float, andstrare the three introduced here. Thetype()function identifies a value's type. You can convert between types usingint(),float(), andstr(). Watch out for strings that look like numbers — they're not numbers until you convert them.
Formal and Natural Languages#
A big part of thinking like a programmer is understanding why programming languages work the way they do. The chapter draws a contrast between the kinds of language we use every day and the kind Python is.
Natural language: Any language that evolved organically among humans — English, Spanish, Mandarin. These languages were never "designed" by a committee; they grew over time.
Formal language: A language deliberately designed for a specific purpose. Mathematics uses a formal language. All programming languages, including Python, are formal languages.
The key differences matter a lot when you're learning to code:
- Ambiguity: Natural languages are full of it — a single sentence can mean different things depending on context. Formal languages are designed to have exactly one meaning, no matter the context. Every Python statement has one and only one interpretation.
- Redundancy: Natural languages repeat themselves to help clarify meaning ("I saw the man with the telescope — the man had the telescope, I mean"). Formal languages are concise; there's no room for helpful rephrasing.
- Literalness: In English, "it's raining cats and dogs" means heavy rain, not falling animals. Formal languages mean exactly what they say — there are no idioms or metaphors. Python does precisely what you write, not what you meant to write.
The practical upshot: when you're reading or writing Python, slow down. Small details — a missing parenthesis, an extra space in the wrong place, a colon instead of a quote — can completely change what a program does, or stop it from running at all. In natural language, readers fill in the gaps; Python never does.
📝 Section Recap: Natural languages evolved and are full of ambiguity and idiom. Formal languages like Python are designed to be precise, concise, and literal. This means small errors in Python code have big consequences — details matter in a way they simply don't in everyday speech.
Debugging#
Every programmer makes mistakes. Programming errors are called bugs, and the process of finding and fixing them is called debugging. These playful terms have stuck around since the early days of computing.
Debugging can feel frustrating — maybe even embarrassing — especially when the bug turns out to be something tiny. That's completely normal. The chapter suggests a useful mental shift: think of the computer as a very fast, very literal assistant. It's brilliant at following instructions exactly as written, but it has zero common sense and no ability to figure out what you meant. Your job as the programmer is to write instructions precisely enough that even this extremely literal assistant gets it right.
A few things worth noting about error messages:
- When Python shows you a SyntaxError, it usually points a caret (
^) at the spot in your code where it got confused. Start your investigation there. - A TypeError tells you that you tried to use an operator or function on a value of the wrong type — for example, dividing a string by an integer.
- Error messages are your friend, not a sign of failure. Getting comfortable reading them is one of the most important skills you can develop as a beginner.
📝 Section Recap: Bugs are errors in code; debugging is the process of finding and fixing them. Treat the computer as a precise but literal assistant — it does exactly what you wrote, not what you meant. Error messages (SyntaxError, TypeError, etc.) point you toward the problem; learning to read them quickly is a core programming skill.
Glossary Quick Reference#
Below is a summary of the key terms introduced in this chapter.
| Term | Meaning |
|---|---|
| Arithmetic operator | A symbol (+, -, *, /, //, **) representing a calculation |
| Integer | A whole number, stored as type int |
| Floating-point number | A number with a decimal point, stored as type float |
| Floor division | Division using // that always rounds the result down |
| Expression | A combination of values, operators, and/or function calls that evaluates to a single value |
| Value | A piece of data — an integer, float, or string |
| Function | A named operation you can call by writing its name and parentheses |
| Function call | Using a function: abs(-7), round(3.7), etc. |
| Syntax error | An error caused by code that Python cannot parse |
| String | A sequence of characters, stored as type str |
| Concatenation | Joining two strings end-to-end using + |
| Type | The category of a value (int, float, or str) |
| Operand | A value that an operator acts on |
| Natural language | A human language that evolved organically (English, Spanish, etc.) |
| Formal language | A language designed for a specific purpose (mathematics, Python) |
| Bug | An error in a program |
| Debugging | The process of finding and correcting bugs |