Python guides
Short answers to the things that trip people up when they start with Python. Each one has an example you can run right here in the page, and a small exercise to try.
How can you run Python without installing anything?
You can run Python in a web browser, on a site that loads Python into the page, so there's nothing to download or set up first.
How do you add items to a list in Python?
Use append() to add one item to the end of a list, insert() to put one at a set position, and extend() to add several at once.
How does range() work, and why does it stop one early?
range(5) gives 0, 1, 2, 3 and 4. It stops just before the number you give it, so range(5) hands you exactly five numbers.
How to fix "TypeError: can only concatenate str (not "int") to str"
You used + to join text and a number. Turn the number into text with str() or an f-string, or turn the text into a number with int().
What does "IndentationError: expected an indented block" mean?
A line ending in a colon, like an if or a loop, has nothing indented under it. Indent the lines that belong to it, or put pass there for now.
What does "NameError: name is not defined" mean?
Python reached a name, like a variable, that nothing has been given yet. It's usually a typo, missing quotes or a variable used too early.
What is the difference between = and == in Python?
A single = stores a value in a variable. A double == asks whether two values are equal and gives True or False, which is what if and while need.
What is the difference between print and return?
print() shows a value on screen for a person to read. return hands a value back to the code that called the function, so it can be used.
Why does input() give me text instead of a number?
input() always gives back a string, even when someone types digits. Wrap it in int() or float() to get a number you can do sums with.
Why does my while loop never stop?
A while loop keeps going while its test is True. If it never stops, nothing inside the loop is changing the variable that the test checks.