Values and Variables
Try Python one line at a time in the REPL, then keep values in variables: numbers for prices and totals, text for names and messages, and the answers people type in. You'll learn the math operators, f-strings for tidy output, and why the number someone types arrives as text, then write a receipt for a cinema's snack bar.
Updated
Trying Code in the REPL
- Trying Code in the REPL (Novice): The REPL is Python's interactive prompt, where you type one line at a time and see its value at once, which makes it the quickest place to check a calculation or try an idea before it goes in a file.
Variables in Python
- Variables: Giving a Value a Name (Novice): A variable gives a value a name with =, read "gets", so a program can remember it, use it in many places and change it later, and Python stops with a NameError when a line uses a name that hasn't been given a value yet.
Numbers in Python
- Numbers and Math (Novice): Python has whole numbers (int) and decimal numbers (float), seven math operators that follow the order of operations, and round() for the tiny errors decimals carry, which is how a program gets money right to the cent.
Text in Python
- Strings and f-strings (Novice): Text in Python is a string, written in quotes, joined with + and tidied with methods such as strip() and title(), and an f-string drops values into text, with :.2f to show money to two decimal places.
Input and Types
- Asking for Input, and Converting Types (Apprentice): input() asks a question in the terminal and always hands back the answer as text, so a program turns typed numbers into numbers with int() or float() (and numbers into text with str()), checks a value's type with type(), and stops with a ValueError when the text isn't a number.
Exercise: Snack Bar Receipt
- Exercise: Snack Bar Receipt (Novice): Finish a script that asks a cinema snack bar's customer for their order and prints the receipt, turning typed answers into numbers and showing the subtotal, the 7% tax and the total to the cent, while pytest types the answers in and checks every line.