{"base":{"id":"cs10-03","subject":"Programming","title":"Input, variables and output","scene":"py10","steps":[{"mode":"vars","eyebrow":"Variables","title":"Named boxes for values","body":"A variable is a named place in memory that holds a value. The = sign means 'store', not 'equals'. Step through the program and watch the trace table fill in.","actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"input","eyebrow":"Input and output","title":"Talk to the user","body":"input() waits for the user to type, and always gives back text. int() turns that text into a whole number so you can do maths with it. Choose Mia's age and run it.","controls":[{"id":"age","label":"Age typed in","min":5,"max":20,"step":1,"value":14}],"actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"types","eyebrow":"Data types","title":"Every value has a type","body":"Python's main types are integer (int), float, string (str) and Boolean (bool). The type decides what you can do with a value. Slide through all four.","controls":[{"id":"i","label":"Data type","min":1,"max":4,"step":1,"value":1,"hideValue":true}]},{"mode":"concat","eyebrow":"Types matter","title":"Same sign, different job","body":"With numbers, + adds. With strings, + joins them together. Mixing the two causes an error. Predict each output before you tap.","actions":[{"id":"a0","label":"\"5\" + \"3\""},{"id":"a1","label":"5 + 3"},{"id":"a2","label":"\"5\" * 3"},{"id":"a3","label":"\"5\" + 3"}]},{"mode":"area","eyebrow":"A whole program","title":"Input, process, output","body":"This program asks for a room's length and width, works out the area and perimeter, and prints them with an f-string. Change the inputs and run it.","controls":[{"id":"l","label":"Length (m)","min":1,"max":10,"step":0.5,"value":4},{"id":"w","label":"Width (m)","min":1,"max":10,"step":0.5,"value":2.5}],"actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"quiz","look":"types","eyebrow":"Check yourself","title":"Predict the output","body":"age = input(\"Age? \")\nThe user types 12. Then the program runs print(age * 2).","question":"What is printed?","options":["24","An error","1212"],"answer":2,"right":"Correct. input() gives the string \"12\", and a string times 2 repeats it: \"1212\". Use int(input()) to get 24.","wrong":"Not quite. What type does input() always return? What does * do to a string?"}],"more":[{"type":"cards","id":"worked","eyebrow":"Worked examples","title":"Programs to read","intro":"Read each program and predict the output before reading the answer underneath.","items":[{"tag":"Python","title":"Swap two variables","body":"a = 5\nb = 9\ntemp = a      # keep a safe\na = b\nb = temp\nprint(a, b)   # 9 5\n\n# Python shortcut: a, b = b, a","code":true},{"tag":"Python","title":"Ask, calculate, reply","body":"price = float(input(\"Price? \"))\nqty = int(input(\"How many? \"))\ntotal = price * qty\nprint(f\"Total: ${total:.2f}\")\n\n# typing 2.5 and 4 prints: Total: $10.00","code":true},{"tag":"Pseudocode","title":"The same program in exam-style pseudocode","body":"DECLARE Price : REAL\nDECLARE Qty : INTEGER\nOUTPUT \"Price? \"\nINPUT Price\nOUTPUT \"How many? \"\nINPUT Qty\nOUTPUT \"Total: \", Price * Qty","code":true},{"tag":"Constants","title":"A value that never changes","body":"VAT_RATE = 0.2        # capitals = constant\nnet = 50\ngross = net * (1 + VAT_RATE)\nprint(gross)          # 60.0\n\nChange the rate once at the top,\nand the whole program updates.","code":true}]},{"type":"cards","id":"mistakes","eyebrow":"Common mistakes","title":"Watch out for these","items":[{"tag":"Mistake","title":"Forgetting int() or float()","body":"input() gives text. \"5\" + 1 crashes, and \"5\" * 2 gives \"55\". Convert first."},{"tag":"Mistake","title":"Reading = as 'equals'","body":"x = x + 1 makes no sense in maths, but in code it means 'work out x + 1, then store it in x'."},{"tag":"Mistake","title":"Quotes around a variable name","body":"print(\"name\") prints the word name. print(name) prints what the variable holds."},{"tag":"Mistake","title":"Bad variable names","body":"Names can't contain spaces or start with a digit, and Python is case-sensitive: Score and score are different. Use clear names like total_cost."}]},{"type":"cards","id":"method","eyebrow":"Set out your answer","title":"Writing good code in tests","items":[{"tag":"1 · Meaningful names","title":"say what it stores","body":"price_each and total_cost are better than p and t. Examiners and teammates can follow them."},{"tag":"2 · Prompt the user","title":"Tell them what to type","body":"input(\"Enter your age: \") is better than input(). Good programs are easy to use."},{"tag":"3 · Comment the tricky bits","title":"Use #","body":"A short comment explains WHY: # convert to cm for the label printer. Don't comment the obvious."}]},{"type":"bars","id":"types","eyebrow":"Data","title":"How much memory does each type use?","intro":"Typical sizes in languages such as C or Java (Python objects are bigger because they store extra information).","items":[{"label":"Boolean","value":8,"display":"1 byte (only 1 bit needed)","color":"#7c3aed"},{"label":"Character","value":15,"display":"1–4 bytes","color":"#db2777"},{"label":"Integer","value":32,"display":"4 bytes (32 bits)","color":"#2f6fdb"},{"label":"Float (real)","value":64,"display":"8 bytes (64 bits)","color":"#0e9aa7"},{"label":"String \"Nairobi\"","value":56,"display":"7 bytes + length","color":"#f08c2e"}],"facts":[{"title":"Why not use float for everything?","body":"Floats can't store most decimals exactly. In Python, 0.1 + 0.2 gives 0.30000000000000004. That is why banks store money as whole cents (integers)."},{"title":"Character vs string","body":"Some languages have a char type for one letter. Python uses a string of length 1 instead."}]},{"type":"cards","id":"deeper","eyebrow":"Go deeper","title":"More than the lesson","items":[{"tag":"How it works","title":"Where variables live","body":"When a program runs, each variable is a labelled location in RAM. The name is for you; the computer uses a memory address, such as 0x7ffe45a2. When the program ends, that memory is freed for other programs."},{"tag":"History","title":"Ada Lovelace's variables","body":"In 1843 Ada Lovelace wrote what many call the first computer program, for Charles Babbage's Analytical Engine. Her table used named variables (V1, V2, V3…) and showed how their values changed at each step: an early trace table."},{"tag":"Real world","title":"Bad input breaks programs","body":"Many real software failures came from unexpected input. Good programs check what users type (validation), which you will learn later in the course."},{"tag":"Coming next","title":"Where this leads","body":"Next: making decisions with if, elif and else, so your program can react to what the user types."}]},{"type":"cards","id":"challenge","eyebrow":"Challenge yourself","title":"Stretch your brain","intro":"Try each one before opening the card.","items":[{"tag":"Challenge","title":"Write a program that turns a number of seconds into minutes and seconds","body":"total = int(input(\"Seconds? \"))\nminutes = total // 60   # whole minutes (DIV)\nseconds = total % 60    # what is left (MOD)\nprint(minutes, \"min\", seconds, \"s\")\n\n# 135 → 2 min 15 s","code":true},{"tag":"Challenge","title":"What does this print?  x = 3;  x = x * x;  x = x - 1;  print(x * 2)","body":"x = 3\nx = 3 × 3 = 9\nx = 9 − 1 = 8\nprint 8 × 2 = 16","code":true}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Code it yourself","items":[{"title":"A greeting card program","time":"20 minutes","need":["A computer with Python, or a free online Python editor"],"steps":["Ask the user for their name and their favourite food.","Ask for their age and convert it with int().","Print a sentence using all three, with an f-string.","Print their age in 10 years' time."],"why":"This uses every idea from the lesson: input, variables, types, processing and output.","safety":"Use editors your school or a parent trusts. Don't share personal details online."},{"title":"Unit converter","time":"20 minutes","need":["Python or an online editor"],"steps":["Ask for a distance in kilometres (use float()).","Store 0.621 as a constant MILES_PER_KM.","Print the distance in miles, rounded to 2 decimal places with round().","Add a second conversion of your choice."],"why":"Constants make programs easier to change and read, and float() lets users type decimals.","safety":"No safety risks."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Eight more questions","questions":[{"question":"What does x = x + 4 do?","options":["Adds 4 to x and stores the result in x","Checks if x equals x + 4","Causes an error"],"answer":0,"right":"Correct. The right side is worked out, then stored.","wrong":"Not quite. In code, = means 'store'."},{"question":"What type is the value 3.0?","options":["int","float","str"],"answer":1,"right":"Correct. It has a decimal point.","wrong":"Not quite. Look for the decimal point."},{"question":"What does input() always return?","options":["An integer","A float","A string"],"answer":2,"right":"Correct. Convert it with int() or float() to do maths.","wrong":"Not quite. Think about why we need int(input())."},{"question":"What is printed by print(\"Hi\" + \"Leo\")?","options":["HiLeo","Hi Leo","An error"],"answer":0,"right":"Correct. Joining strings adds no space.","wrong":"Not quite. + joins strings exactly as they are."},{"question":"Which is a valid variable name?","options":["2nd_place","total_cost","my score"],"answer":1,"right":"Correct. Letters, digits and underscores, not starting with a digit.","wrong":"Not quite. No spaces, and don't start with a digit."},{"question":"a = 7, b = 2. What is a // b?","options":["3.5","1","3"],"answer":2,"right":"Correct. // is whole-number division (DIV).","wrong":"Not quite. // gives the whole-number part only."},{"question":"a = 7, b = 2. What is a % b?","options":["3","1","0.5"],"answer":1,"right":"Correct. % gives the remainder (MOD): 7 = 3 × 2 + 1.","wrong":"Not quite. % gives what is left after dividing."},{"question":"Which type best stores whether a door is locked?","options":["Boolean","Integer","String"],"answer":0,"right":"Correct. Locked or not: True or False.","wrong":"Not quite. There are only two possible values."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can…","items":["explain what a variable is and choose a good name","read = as 'store' and trace assignments","use input() with int() or float()","name the data types int, float, str and bool, with examples","predict what + and * do with numbers and strings","use print() and f-strings to show results","use // (DIV) and % (MOD)","explain the difference between a variable and a constant"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"variable","meaning":"A named location in memory that holds a value that can change."},{"word":"constant","meaning":"A named value that does not change while the program runs."},{"word":"assignment","meaning":"Storing a value in a variable with =."},{"word":"input","meaning":"Data given to a program, often typed by the user."},{"word":"output","meaning":"Data a program gives back, like printed text."},{"word":"data type","meaning":"The kind of data: integer, float, string or Boolean."},{"word":"integer","meaning":"A whole number."},{"word":"float (real)","meaning":"A number with a decimal part."},{"word":"string","meaning":"Text, written in quotes."},{"word":"Boolean","meaning":"True or False."},{"word":"casting","meaning":"Changing a value's type, like int(\"5\")."},{"word":"concatenation","meaning":"Joining strings together."}]},{"type":"sources","id":"sources","title":"Sources","items":["Python documentation: Built-in types; input(); f-strings","BBC Bitesize, GCSE Computer Science: Variables and data types","Computer History Museum: Ada Lovelace","Isaac Computer Science: Programming fundamentals"]}]},"overlay":null}