Numbers, Strings & Operators
You'll learn to
- -Perform arithmetic, including integer division, modulo, and exponentiation
- -Create and concatenate strings, and measure their length
- -Index into a string from the front or the back
- -Write a basic f-string to embed a value inside text
With variables and types covered, it's time to actually do something with them. Numbers and strings are the two types you'll manipulate the most, and Python gives each a set of operators worth knowing cold.
Arithmetic Operators
The two operators new programmers usually haven't seen before are // and %. Floor division (//) divides and rounds down to the nearest whole number - useful whenever you need a count of "how many whole groups fit," like splitting items evenly across pages. Modulo (%) gives you what's left over after that division - useful for anything cyclical, like checking if a number is even (n % 2 == 0) or wrapping an index around the length of a list.
Notice that / always returns a float, even when the division comes out even (8 / 2 is 4.0, not 4). // is what gives you a whole-number-style result, and even then, it returns an int only if both operands were ints.
Strings: Text as a Value
Strings can be written with either single or double quotes - Python treats them identically, so pick whichever avoids escaping (a string containing an apostrophe is easier to write with double quotes, and vice versa). The + operator between two strings concatenates them rather than adding numbers. len() works on strings too - it returns the character count, including spaces.
Indexing and Negative Indexing
Indexing starts at 0, not 1 - word[0] is the first character. Negative indices count backward from the end, so word[-1] is always the last character no matter how long the string is, which is far more common in real Python code than computing len(word) - 1 yourself. Indexing past either end (word[100] on a 6-character string) raises an IndexError - Python won't silently return something wrong.
A First Taste of f-strings
An f-string is a string literal prefixed with f, where anything inside curly braces gets evaluated and dropped into the text. It's the cleanest way to build a string out of variables - no manual + concatenation needed. This is just a preview; the next chapter covers f-string formatting in real depth.