{"course":{"slug":"python-for-ai","title":"Python, from Zero, for AI","url":"https://addaly.com/learn/python-for-ai","level":"beginner","track":"build"},"lesson":{"slug":"variables-and-types","title":"Variables, and the fact that everything has a type","url":"https://addaly.com/learn/python-for-ai/variables-and-types","position":4,"of":89,"minutes":6,"answer":"A name is attached to a value at the moment the line runs, and the value's type decides what operators do.","keyIdea":"A name is attached to a value at the moment the line runs, and the value's type decides what operators do."},"body":{"format":"markdown","text":"## A variable is a name stuck onto a value\n\n```python\nprice = 250\ncurrency = \"rupees\"\nprint(price)\nprint(currency)\n```\n\nThe `=` here is not the `=` from school maths. It is not a claim that two things are equal. It is an instruction: work out what is on the right, then attach the name on the left to it. Read it as \"price gets 250\".\n\nBecause it is an instruction, it happens at the moment that line runs, and it can happen again:\n\n```python\nprice = 250\nprice = price + 50\nprint(price)   # 300\n```\n\nLine two would be nonsense as an equation. As an instruction it is ordinary: take the current value of `price`, add 50, and re-attach the name to the result.\n\nNames can be almost anything made of letters, digits and underscores, as long as they do not start with a digit. Use names that say what the thing is. `total_marks` beats `tm` every time you come back to the file a week later.\n\n## Everything has a type\n\nEvery value in Python is of some kind, and the kind decides what you are allowed to do with it. You can ask:\n\n```python\nprice = 250\ncurrency = \"rupees\"\nrate = 0.5\npassed = True\n\nprint(type(price))      # <class 'int'>\nprint(type(currency))   # <class 'str'>\nprint(type(rate))       # <class 'float'>\nprint(type(passed))     # <class 'bool'>\n```\n\nFour types cover most of what a beginner needs:\n\n- `int`: a whole number: `250`, `-3`, `0`\n- `float`: a number with a decimal point: `0.5`, `19.99`\n- `str`: text, in quotes: `\"rupees\"`, `'Kano'`\n- `bool`: `True` or `False`, with capital letters\n\n## The type changes what an operator means\n\nThis is the part worth slowing down for:\n\n```python\nprint(250 * 2)          # 500\nprint(\"rupees\" * 2)     # rupeesrupees\n```\n\nThe same `*` did two different jobs. With numbers it multiplied. With text it repeated. Python looked at the types and chose. The same happens with `+`: numbers add, text joins end to end.\n\n```python\nprint(3 + 4)            # 7\nprint(\"3\" + \"4\")        # 34\n```\n\n`\"3\"` is not the number three. It is a character that happens to look like three, the way the word \"three\" does.\n\nAnd when the types do not agree, Python refuses rather than guessing:\n\n```python\nprint(\"3\" + 4)\n```\n\n```\nTypeError: can only concatenate str (not \"int\") to str\n```\n\nSome languages would quietly decide you meant `\"34\"`, or `7`. Python decides that a wrong answer is worse than a stop. You will meet this error constantly, and it almost always means \"one of these is text and you thought it was a number\".\n\n## Converting on purpose\n\nWhen you do want to cross the line, say so:\n\n```python\nprint(int(\"3\") + 4)       # 7\nprint(\"3\" + str(4))       # 34\nprint(float(\"19.99\") * 2) # 39.98\n```\n\n`int(\"hello\")` fails, and it should. `int(\"3.7\")` also fails, because `\"3.7\"` is not a whole number written down; use `float` first.\n\n## Try this now\n\n```python\nsubtotal = 1200\ntax = subtotal * 0.18\ntotal = subtotal + tax\nlabel = \"Total: \"\nprint(label + str(total))\nprint(type(total))\n```\n\nThen remove the `str(...)` and read the error carefully. That error message is a friend, and lesson seven explains why."},"check":{"question":"A file contains these three lines in order: `a = 5`, then `b = a`, then `a = 9`. The next line is `print(b)`. What appears, and why?","options":[{"text":"Nothing appears, because `b = a` only borrows a value and does not give `b` a value of its own.","correct":false,"tempting":"Assignment examples nearly always show a literal on the right, so copying from another name can look like it does not really count as giving `b` a value."},{"text":"`9`, because `b = a` made `b` a second name for `a`, so `b` follows `a` wherever it goes.","correct":false,"tempting":"The line reads like a link between two names, and in a spreadsheet a cell that refers to another cell really does update when that cell changes."},{"text":"`5`, because `b = a` ran when `a` was still 5, and later changing `a` does not reach back to `b`.","correct":true},{"text":"An error, because Python reads the whole file first and finds `a` assigned twice.","correct":false,"tempting":"Treating `=` as a mathematical claim makes two different values for `a` look like a contradiction the language ought to reject."}]},"prev":{"slug":"shell-and-script","title":"The interactive shell and the saved file, and when to use each","url":"https://addaly.com/learn/python-for-ai/shell-and-script"},"next":{"slug":"numbers-and-arithmetic","title":"Numbers: whole, decimal, and the one that surprises everybody","url":"https://addaly.com/learn/python-for-ai/numbers-and-arithmetic"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Exam keys checked blind by a second model: 3,665 of 3,666 agreed. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Variables, and the fact that everything has a type', in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/variables-and-types (Accessed: 31 October 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Variables, and the fact that everything has a type","courseTitle":"Python, from Zero, for AI","module":{"index":1,"slug":"getting-python-running","title":"Getting Python running, and your first working code"},"answer":null,"keyIdea":"A name is attached to a value at the moment the line runs, and the value’s type decides what operators do.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"a-variable-is-a-name-stuck-onto-a-value","runs":[{"text":"A variable is a name stuck onto a value"}]},{"t":"code","lang":"python","label":"Python","text":"price = 250\ncurrency = \"rupees\"\nprint(price)\nprint(currency)"},{"t":"p","runs":[{"text":"The "},{"text":"=","code":true},{"text":" here is not the "},{"text":"=","code":true},{"text":" from school maths. It is not a claim that two things are equal. It is an instruction: work out what is on the right, then attach the name on the left to it. Read it as “price gets 250”."}]},{"t":"p","runs":[{"text":"Because it is an instruction, it happens at the moment that line runs, and it can happen again:"}]},{"t":"code","lang":"python","label":"Python","text":"price = 250\nprice = price + 50\nprint(price)   # 300"},{"t":"p","runs":[{"text":"Line two would be nonsense as an equation. As an instruction it is ordinary: take the current value of "},{"text":"price","code":true},{"text":", add 50, and re-attach the name to the result."}]},{"t":"p","runs":[{"text":"Names can be almost anything made of letters, digits and underscores, as long as they do not start with a digit. Use names that say what the thing is. "},{"text":"total_marks","code":true},{"text":" beats "},{"text":"tm","code":true},{"text":" every time you come back to the file a week later."}]},{"t":"h2","id":"everything-has-a-type","runs":[{"text":"Everything has a type"}]},{"t":"p","runs":[{"text":"Every value in Python is of some kind, and the kind decides what you are allowed to do with it. You can ask:"}]},{"t":"code","lang":"python","label":"Python","text":"price = 250\ncurrency = \"rupees\"\nrate = 0.5\npassed = True\n\nprint(type(price))      # <class 'int'>\nprint(type(currency))   # <class 'str'>\nprint(type(rate))       # <class 'float'>\nprint(type(passed))     # <class 'bool'>"},{"t":"p","runs":[{"text":"Four types cover most of what a beginner needs:"}]},{"t":"ul","items":[[{"text":"int","code":true},{"text":": a whole number: "},{"text":"250","code":true},{"text":", "},{"text":"-3","code":true},{"text":", "},{"text":"0","code":true}],[{"text":"float","code":true},{"text":": a number with a decimal point: "},{"text":"0.5","code":true},{"text":", "},{"text":"19.99","code":true}],[{"text":"str","code":true},{"text":": text, in quotes: "},{"text":"\"rupees\"","code":true},{"text":", "},{"text":"'Kano'","code":true}],[{"text":"bool","code":true},{"text":": "},{"text":"True","code":true},{"text":" or "},{"text":"False","code":true},{"text":", with capital letters"}]]},{"t":"h2","id":"the-type-changes-what-an-operator-means","runs":[{"text":"The type changes what an operator means"}]},{"t":"p","runs":[{"text":"This is the part worth slowing down for:"}]},{"t":"code","lang":"python","label":"Python","text":"print(250 * 2)          # 500\nprint(\"rupees\" * 2)     # rupeesrupees"},{"t":"p","runs":[{"text":"The same "},{"text":"*","code":true},{"text":" did two different jobs. With numbers it multiplied. With text it repeated. Python looked at the types and chose. The same happens with "},{"text":"+","code":true},{"text":": numbers add, text joins end to end."}]},{"t":"code","lang":"python","label":"Python","text":"print(3 + 4)            # 7\nprint(\"3\" + \"4\")        # 34"},{"t":"p","runs":[{"text":"\"3\"","code":true},{"text":" is not the number three. It is a character that happens to look like three, the way the word “three” does."}]},{"t":"p","runs":[{"text":"And when the types do not agree, Python refuses rather than guessing:"}]},{"t":"code","lang":"python","label":"Python","text":"print(\"3\" + 4)"},{"t":"code","lang":"","label":"Text","text":"TypeError: can only concatenate str (not \"int\") to str"},{"t":"p","runs":[{"text":"Some languages would quietly decide you meant "},{"text":"\"34\"","code":true},{"text":", or "},{"text":"7","code":true},{"text":". Python decides that a wrong answer is worse than a stop. You will meet this error constantly, and it almost always means “one of these is text and you thought it was a number”."}]},{"t":"h2","id":"converting-on-purpose","runs":[{"text":"Converting on purpose"}]},{"t":"p","runs":[{"text":"When you do want to cross the line, say so:"}]},{"t":"code","lang":"python","label":"Python","text":"print(int(\"3\") + 4)       # 7\nprint(\"3\" + str(4))       # 34\nprint(float(\"19.99\") * 2) # 39.98"},{"t":"p","runs":[{"text":"int(\"hello\")","code":true},{"text":" fails, and it should. "},{"text":"int(\"3.7\")","code":true},{"text":" also fails, because "},{"text":"\"3.7\"","code":true},{"text":" is not a whole number written down; use "},{"text":"float","code":true},{"text":" first."}]},{"t":"h2","id":"try-this-now","runs":[{"text":"Try this now"}],"role":"try"},{"t":"code","lang":"python","label":"Python","text":"subtotal = 1200\ntax = subtotal * 0.18\ntotal = subtotal + tax\nlabel = \"Total: \"\nprint(label + str(total))\nprint(type(total))"},{"t":"p","runs":[{"text":"Then remove the "},{"text":"str(...)","code":true},{"text":" and read the error carefully. That error message is a friend, and lesson seven explains why."}]}],"check":{"question":[{"text":"A file contains these three lines in order: "},{"text":"a = 5","code":true},{"text":", then "},{"text":"b = a","code":true},{"text":", then "},{"text":"a = 9","code":true},{"text":". The next line is "},{"text":"print(b)","code":true},{"text":". What appears, and why?"}],"options":[{"text":[{"text":"Nothing appears, because "},{"text":"b = a","code":true},{"text":" only borrows a value and does not give "},{"text":"b","code":true},{"text":" a value of its own."}],"correct":false,"tempting":[{"text":"Assignment examples nearly always show a literal on the right, so copying from another name can look like it does not really count as giving "},{"text":"b","code":true},{"text":" a value."}]},{"text":[{"text":"9","code":true},{"text":", because "},{"text":"b = a","code":true},{"text":" made "},{"text":"b","code":true},{"text":" a second name for "},{"text":"a","code":true},{"text":", so "},{"text":"b","code":true},{"text":" follows "},{"text":"a","code":true},{"text":" wherever it goes."}],"correct":false,"tempting":[{"text":"The line reads like a link between two names, and in a spreadsheet a cell that refers to another cell really does update when that cell changes."}]},{"text":[{"text":"5","code":true},{"text":", because "},{"text":"b = a","code":true},{"text":" ran when "},{"text":"a","code":true},{"text":" was still 5, and later changing "},{"text":"a","code":true},{"text":" does not reach back to "},{"text":"b","code":true},{"text":"."}],"correct":true,"tempting":null},{"text":[{"text":"An error, because Python reads the whole file first and finds "},{"text":"a","code":true},{"text":" assigned twice."}],"correct":false,"tempting":[{"text":"Treating "},{"text":"=","code":true},{"text":" as a mathematical claim makes two different values for "},{"text":"a","code":true},{"text":" look like a contradiction the language ought to reject."}]}],"order":[0,1,2,3],"why":null},"prev":{"slug":"shell-and-script","title":"The interactive shell and the saved file, and when to use each","minutes":7},"next":{"slug":"numbers-and-arithmetic","title":"Numbers: whole, decimal, and the one that surprises everybody","minutes":8}}}