Computer Science Principles
Course Progress
0/0
Objectives in LxD
3.4 Strings
3.4 Strings
3.3 Mathematical Expressions
3.3 Math Expressions
3.2 Data Abstractions
3.2 Data Abstractions
3.1 Variables & Assignments
3.1 Variables and Assignments
3.1 Variables and Assignments (Sample)
Intro to Python
Intro to Javascript
Variables and HTML DOM (Sample)
3.5 Boolean Expressions (PY)
3.5 Boolean Expressions (JS)
3.8 Iterations
3.7 Nested Conditionals
3.6 Conditionals
3.8 Iterations
3.7 Nested Conditionals
3.6 Conditionals
3.13 Developing Procedures
3.12 Calling Procedures
3.10 Lists
3.13 Developing Procedures
3.10 Lists
3.9 Developing Algorithms
3.17 Algorithmic Efficiency
3.9 Algorithms
3.17 Algorithmic Efficiency
3.15 Random Numbers (pseudocode)
3.15 Random Numbers (js)
3.15 Random Numbers (py)
BI 3 Review
Sprint View
Week 4
3.1 Variables & Assignments
3.1 Variables & Assignments
2 min read
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 = 3 # <-- Add the size of the snake
snakeColor = "green" # <-- 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 = "You win!" # <-- Change this to "You win!"
messageLose = "You lose!" # <-- Change this to "You lose!"
# Choose outcome
winOrLose = "win" or "lose" # <-- 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!