Constants and Good Names

Some values should never change, the number of days in a week, a tax rate, a founding year. JavaScript has a keyword for that: const. Reassigning a const throws an error on purpose. It's protection, not punishment. Python has no enforced constant; the convention is to write the name in ALL_CAPS as a promise to other programmers.

Constants

PYTHON

FOUNDING_YEAR = 1957  # convention: all caps = do not change

JAVASCRIPT

// enforced: reassigning throws an error
const FOUNDING_YEAR = 1957;
Compare → Ghana's independence year shouldn't change, so we mark it constant. JavaScript enforces it. Python trusts you.
EXAMPLE
loading editor…
YOUR TURN
Your turn → Fix the program: delete the line that reassigns FOUNDING_YEAR, then run it so it prints 1957.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Naming rules (enforced): names can't start with a digit, can't contain spaces, can't be keywords like let or print.

Naming conventions (culture): Python uses snake_case, total_sales. JavaScript uses camelCase, totalSales. Above all: names should say what they hold. bookstore_revenue beats x every single time.

EXAMPLE
loading editor…
YOUR TURN
Your turn → The store sells 6 more books. Update the variable and rerun, notice you only had to change ONE line. That's why variables exist.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

◈ Graded Challenge

A bookstore sold 34 books at 15 dollars each. Create a variable named books_sold, another named price_per_book, and a third named daily_revenue holding the total. Do the multiplication in code, not in your head.

Name it exactly: daily_revenue

3 visible + 0 hidden
loading editor…

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back