Variables & Assignments
This page will teach you about variables and assignments in programming.
Variables & Assignments
- Introduction:
- Variables are ways to store values inside a game or code for later use. It can be numerical or strings(letters and words). It is like a box that has a name but the contents inside cannot be changed. Make sure you name your variables with descriptive, but not long words.
- Key concepts:
- Variable: A container that stores information and values
- Assignment: The value being assigned to the variable
- Value: The data inside a variable
3. Python:
age = 15 # Stores "age" as 15, INTEGER
weight = 135 # Stores "weight" as 135, INTEGER
name = "Mike" # Stores "name" as Mike, STRING
JavaScript
(Similar to python)
%%javascript // This line is needed to run JavaScript in a Jupyter notebook cell
let age = 15 // Stores "age" as 15 as well, but add let in front
let weight = 135 // Stores "weight" as 135
let name = "Mike" // Stores "name" as Mike
<IPython.core.display.Javascript object>
WARNING! Avoid doing the follwing:
%%javascript // This line is needed to run JavaScript in a Jupyter notebook cell
// bad examples
let age == 14 // Incorrect as "==" is for comparing values
age = 14 // does not create a variable in JavaScript
Popcorn Hack 1
# Finish the code
# Example: snakeSize = 10
snakeSize = ? # <-- Add the size of the snake
snakeColor = "" # <-- Add the color of the snake
# Print greeting
print("The snake is " + str(snakeSize) + " inches long and it's " + snakeColor + ".")
The snake is 3 inches long and it's green.
Popcorn Hack 2
# # Finish the code
# Define the win/lose messages first
messageWin = "" # <-- Change this to "You win!"
messageLose = "" # <-- Change this to "You lose!"
# Choose outcome
winOrLose = "" # <-- Change this to either "win" or "lose"
# Print result
if winOrLose == "win":
print(messageWin)
else:
print(messageLose)
Which one do you think is a better variable name?
- highScore vs highestScoreInTheGame
- firstName vs 1stName
- ID vs studentID
Multiple Choice!
Compete as teams and try to win!