Understanding Variables
What is a Variable?
A variable is like a container that holds information you want to use in your program. You can give it a name and store a value inside it.
Think of it as a labeled box where you can keep things like numbers, words, or other data.
Creating Variables
To create a variable in Python, follow these steps:
Choose a name for your variable.
Use the = sign to assign a value to the variable.
Examples:
# Creating variables
name = "Alice" # A string
age = 12 # An integer
height = 5.4 # A float (decimal number)
print(name) # Output: Alice
print(age) # Output: 12
print(height) # Output: 5.4
Rules for Naming Variables
Variable names must start with a letter or an underscore _.
They cannot start with a number.
They can only contain letters, numbers, and underscores.
Variable names are case-sensitive (Age and age are different).
Using Variables
Once you create a variable, you can use it in your program. You can print it, perform calculations with it, or combine it with other variables.
Examples:
# Using variables
x = 5
y = 10
sum = x + y
print(sum)
# Output: 15
greeting = "Hello"
name = "Alice"
message = greeting + ", " + name + "!"
print(message)
# Output: Hello, Alice!
Updating Variables
You can change the value stored in a variable at any time by assigning a new value.
Examples:
# Updating variables
score = 0
print(score)
# Output: 0
# Updating the value
score = score + 10
print(score)
# Output: 10
Practice Exercises
Create and Print Variables
Create a variable favorite_color and set it to your favorite color.
Create a variable age and set it to your age.
Print both variables.
Perform Calculations with Variables
Create two variables x and y with any numbers.
Calculate and print their sum, difference, and product.
Update Variables
Create a variable points and set it to 0.
Add 5 to points and print the result.
Subtract 2 from points and print the result.
Combine Strings
Create a variable greeting with the value "Hi".
Create a variable name with your name.
Combine these two variables to print a personalized greeting.
Challenge Exercise
Create a variable hours_worked and set it to the number of hours you worked on a project.
Create a variable hourly_rate and set it to the amount of money you earn per hour.
Calculate and print your total earnings (hours_worked * hourly_rate).
Why are Variables Important?
Variables make your code reusable and flexible. Instead of writing the same value multiple times, you can just use the variable name. This makes your code easier to read and update.
Challenge Yourself!
Try creating a program where you:
Store your name, age, and favorite hobby in variables.
Print a short introduction using these variables.
Example: "Hi, my name is [name]. I am [age] years old and I love [hobby]."
Example Code:
name = "Alice"
age = 12
hobby = "painting"
print(f"Hi, my name is {name}. I am {age} years old and I love {hobby}.")
Notice how in the example above, we have used an f-string inside a print( ) method to print a string.
We use curly braces to use a variable value inside f-strings.
Conclusion
Variables are the building blocks of programming. Practice creating, using, and updating variables to become a pro!