Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Prep Bowls (Variables)

A variable is a labeled container for a piece of information, like using a labeled bowl for an ingredient. This practice, known in cooking as mise en place, keeps your code organized and easy to read.

The equals sign (=) is the assignment operator. It's an action, not a question. It means: "Put the value on the right into the container on the left."

# Create a variable 'number_of_eggs' and assign the value 3 to it.
number_of_eggs = 3

# Now we can use the variable's name to get its value.
# This will display the number 3.
print(number_of_eggs)
// Create a variable 'numberOfEggs' and assign the value 3 to it.
let numberOfEggs = 3;

// Now we can use the variable's name to get its value.
// This will display the number 3.
console.log(numberOfEggs);

By using variables, you can store information and refer to it by name anywhere in your program.

A Note on Style

You'll notice Python code uses snake_case (words separated by underscores) while JavaScript uses camelCase (the first letter of each new word is capitalized). These are style conventions for each language. Following them makes your code easier for other programmers to read!

Summary of Naming Conventions

Case Style Definitions

  • camelCase: The first letter of the first word is lowercase, and the first letter of each subsequent word is capitalized. Example: numberOfUsers.
  • PascalCase: The first letter of every word is capitalized. Also known as UpperCamelCase. Example: UserAccount.
  • snake_case: All words are lowercase and separated by an underscore. Example: user_account.
  • SCREAMING_SNAKE_CASE: All words are uppercase and separated by an underscore. Used for constants. Example: MAX_LOGIN_ATTEMPTS.

A Universal Convention: PascalCase for Types

Across all the languages in this guide (Python, JavaScript, Go, C#, PHP, and Rust), the standard convention for naming classes, structs, enums, or any other custom type is PascalCase. For example: SimpleUser, HttpRequest, DatabaseConnection.

LanguageVariable & FunctionConstantsNotes
Pythonsnake_caseSCREAMING_SNAKE_CASEFollows the PEP 8 style guide.
Strongly enforced by the community.
JavaScriptcamelCaseSCREAMING_SNAKE_CASEPascalCase is used only for constructor functions/classes.
This is the universally accepted convention.
GocamelCase or PascalCasePascalCaseCase determines visibility:
- camelCase (lowercase start) is private.
- PascalCase (uppercase start) is public/exported.
C#camelCasePascalCasecamelCase for local variables/parameters.
PascalCase for public methods/properties.
Private fields are often _camelCase.
PHPcamelCaseSCREAMING_SNAKE_CASEModern PHP (e.g., Laravel, Symfony) uses camelCase.
You may see snake_case in older code or WordPress.
Rustsnake_caseSCREAMING_SNAKE_CASEFollow the Rust API naming guide.
The rustc compiler is very strict about these conventions
and will issue warnings if they are not followed.

Mini-Exercise 💡

  1. Declare a variable to store the name of your favorite dish.
  2. Print that variable to the screen.