Variables are like containers in the world of programming. Imagine you have a bunch of boxes, each with a label on it. These labels help you remember what’s inside the box. In programming, variables work the same way — they store different kinds of information and help you keep track of it all.
Let’s start with a joke! 🤡
Why weren’t the two math variables speaking to each other?
Because they were x’s.
What Exactly are Variables?
Variables are like placeholders for information. They can hold numbers, words, or even more complex stuff like lists of things. When you want to use some information in your program, you put it into a variable. Later, you can look at the variable and see what’s inside.
Declaring and Using Variables
Think of variables like sticky notes you use to remember things. When you declare a variable, you’re sticking a label on a piece of information so you can use it later. Here’s how it works:
name = "Alice"
In this example, name
is our sticky note, and "Alice"
is what we’re writing on it. Whenever we want to remember "Alice"
, we just look at our name
sticky note.
Manipulating Variables
Now, let’s talk about changing our sticky notes. You can add new information, erase old stuff, or even combine different notes. Here’s a simple example:
age = 25
age = age + 1
Here, we start with the age
sticky note saying 25
. Then, we add 1
to it and update the age
sticky note to 26
. It’s like changing the number on a sticky note to keep track of someone’s age getting older.
Types of Information: Data Types
Variables can hold different types of information. Think of them as different kinds of boxes for different things. Here are some common types:
- Numbers: These are for storing numbers, like
1
,5.6
, or-10
. - Text: For storing words or sentences, like
"Hello, world!"
or"How are you?"
. - True/False: These are for storing whether something is true or false, like
True
orFalse
.
Understanding Variable Scope
Variable scope determines where in your code you can access a variable. Here’s a simple breakdown:
Local Scope: Inside Functions
Variables defined inside a function are only accessible within that function. They’re like secrets hidden inside a room. Once you leave the room (finish the function), you can’t access them anymore.
def my_function():
x = 10
print(x)
my_function()
print(x) # This will cause an error because x is not defined here
Global Scope: Everywhere
Variables defined outside of any function, usually at the top of your code, are accessible from anywhere. It’s like putting something in the main room of your house that everyone can see and use.
y = 20
def another_function():
print(y)
another_function()
print(y) # This will print 20 because y is accessible here as well
Why Scope Matters
Understanding scope helps you organise and manage your code better. Local variables keep things tidy inside functions, while global variables can be accessed from anywhere but may lead to confusion if overused. By mastering scope, you write cleaner, more organised code that’s easier to work with.