An assignment computes whatever sits on the right of the equals sign, then binds the name on the left to the result. It is an instruction, not a statement of equality.
A name is built from letters, digits and underscores, and it cannot start with a digit. Case matters, so total and Total are two separate names.
| Name | Status | Why |
|---|---|---|
| total_price | ✓ accepted | Words joined by underscores, the usual style in Python. |
| n2 | ✓ accepted | Digits are fine anywhere except in first position. |
| 2nd_try | ✕ rejected | A name cannot open with a digit. |
| total price | ✕ rejected | A space splits it into two names, and neither one parses. |
| class | ✕ rejected | Reserved by the language for its own syntax. |
Press Step to run one line at a time. The table holds every name Python knows at that moment, together with the type of the value behind it. Watch line 4 reuse price on both sides: the old value is read, and the new one takes its place.
| Type | Holds | Written as | Watch for |
|---|---|---|---|
| int | Integer numbers, positive or negative, with no limit on size. | 42 0 -7 |
Division with / never gives back an int. |
| float | Numbers with a decimal part. | 3.14 2.0 -0.5 |
Tiny rounding error is normal: 0.1 + 0.2 shows as 0.30000000000000004. |
| str | Text of any length, including the empty text. | "hello" 'a' "" |
"42" is text. It cannot be added to a number. |
| bool | Logical value, either true or false. | True False |
Capital letter required, and in arithmetic they count as 1 and 0. |
The name has no type of its own. It points at whichever value was assigned last, so type(x) can answer differently on two lines of the same program.
Python calls this dynamic typing. It saves you from writing the type on every line, and it moves the responsibility onto you: nothing warns you when a name that held a number now holds text, until an operation fails.
Pick an expression. Python evaluates it, and only then does the result have a type.
Pick one to see what comes back.
The functions int(), float(), str() and bool() build a new value of the type you ask for. The value they read stays as it was, so the result has to be stored or used right away.
Both lines print the same character on screen. Only the second value can take part in arithmetic, which is why print is a poor way to check a type and type() is a reliable one.
| Expression | Status | What comes back |
|---|---|---|
| int("42") | ✓ works | Gives 42. Text made only of digits converts cleanly. |
| int(3.9) | ✓ works | Gives 3. The decimal part is cut off, never rounded. Use round() for that. |
| str(3.5) | ✓ works | Gives "3.5". Any value at all can become text. |
| int("3.5") | ✕ fails | ValueError. int() reads integer numbers only, so go through int(float("3.5")). |
| int("hello") | ✕ fails | ValueError. There is no number to read. |
| "5" + 3 | ✕ fails | TypeError. Convert one of the two sides before the operation. |
bool() is the one conversion that never fails. It returns False for 0, 0.0 and "", and True for everything else, including the text "0".
Choose a value and a function, then run the pair. Errors are shown as Python reports them, since reading the error name is half of fixing it.
Calling input() pauses the program, waits for a line typed by the user, and returns that line as a str. The optional argument is printed first, as a prompt.
"30", never the number.input() with no argument waits without printing anything.Wrapping the call keeps the rest of the program working with real numbers, so no later line has to remember that the value arrived as text.
input().The program stops on the input() line and waits for you to type into the console, the way a terminal does. Answer once with a number, then run it again and answer with letters, and compare what each version does with the same reply.
Press Run, and the program will stop and wait for your answer.
Each character sits at a numbered position. Counting runs from 0 at the left, and from -1 at the right, so the last character is reachable without knowing the length.
| Expression | Status | What comes back |
|---|---|---|
| s[0] | ✓ works | Gives 'P'. The first character, always. |
| s[-1] | ✓ works | Gives 'N'. The last one, whatever the length. |
| s[len(s)] | ✕ fails | IndexError. Positions run to len(s) - 1, so the last one here is s[5]. |
A slice returns a new string built from the characters between two positions, and every part of s[start:stop:step] can be left out.
s[0:3] takes the characters at positions 0, 1 and 2, and stops just before position 3.
Move the three controls and watch which characters survive. The row above each character shows the position counted from the left, the row below shows the same position counted from the right.
Negative values are allowed on the sliders, and they count from the right.
A string cannot be edited in place. Every operation that looks like editing builds a new string and leaves the old one alone.
The second call is discarded because no name catches it. Storing the result, either back under the same name or under a new one, is what makes the change stick.
An f before the quotes lets you drop names straight into the text, with the conversion handled for you.
"Bar" + "celona" gives 'Barcelona'"ab" * 3 gives 'ababab'Each one builds a new value and leaves the original string as it was.
Five lines of work, using everything on this page. The first line is written for you.
int().Press Run. When the console asks something, type your answer into it and press Enter.
int, float, str and bool apart, and check any value with type().input() and convert it before doing arithmetic.+, methods and f-strings.