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
Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off, saved just for you on this device.
With variables and types covered, it is time to actually do something with them. Numbers and strings are the two types you will manipulate the most, and Python gives each a set of operators worth knowing cold.
Arithmetic Operators
The two operators new programmers usually have not 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 is 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, like word[100] on a 6-character string, raises an IndexError. Python will not 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 is the cleanest way to build a string out of variables, with no manual + concatenation needed. This is just a preview. The next chapter covers f-string formatting in real depth.
You need to check if a number is even, and separately, wrap an index so it never goes out of bounds. Which operator covers both, and why?
"I'd write a couple of if-statements to check each case manually."
"Modulo (%). n % 2 == 0 checks evenness directly, and index % length wraps any index back into a valid range no matter how large it gets, since the remainder after dividing by the length can never reach the length itself. Both problems are really the same shape underneath, cyclical behavior, which is exactly what modulo is built for, so reaching for manual if-statements to replicate it is usually a sign the operator itself got overlooked."
What does 7 // 3 evaluate to, and what does the // operator do?