Variables

Turn to Page 394. In this lesson we delve into the concept of data containers – variables.

˙⋆˖⁺‧₊☽◯☾₊‧⁺˖⋆˙

Definition

Variables are fundamental in programming as they allow us to store and manipulate data.

Variablesyntax:  variable_name = value – a named storage location in a computer's memory where data can be stored and retrieved during program execution.

name = "Hermione" # String variable

Variable Naming Rules

When naming variables, there are certain rules to follow:

Variable Assignment

To assign a value to a variable, we use the assignment operator (=). This operator assigns the value on the right-hand side to the variable on the left-hand side.

#Example: Variable assignment age = 25 print(age)

Variables can be of different types.

#Example: Variable assignment name = "Ulfric" print("My name is " + name)

Variable Reassignment

Once a variable is assigned a value, you can change its value by assigning a new value to it. This process is known as variable reassignment.

#Example: Variable reassignment age = 25 print(age) # Output: 25 age = 30 print(age) # Output: 30

Practical Application

You are tasked with brewing a potion and need to store the ingredients and their quantities in variables. Create variables for the following: "Dragon Blood" (50 ml), "Unicorn Hair" (3 strands), and "Phoenix Feather" (1 feather). Display these variables with descriptive messages.

⋆˖⁺‧₊ Use variables to store and display potion ingredients and their quantities. ₊‧⁺˖⋆

Sample Output:
Dragon Blood: 50 ml
Unicorn Hair: 3 strands
Phoenix Feather: 1 feather
#Type your code below: