Hi everyone, I’m Ikanke.
I'm on my coding journey, and it’s been interesting so far.
I’m going to be sharing some stuff I’ve learned.
So I’ll be starting with Python variables.
Mastering Python Variables
In Python, variables are like magical containers that hold values. Think of them as little storage boxes with names on them, and you can store anything you want inside—numbers, strings, lists... the sky’s the limit!
Here’s the fun part: you get to name these containers however you want (well, almost). Let’s dive in and explore how we use these super-powered containers in Python.
What’s a Variable, Anyway? 🤔
In simple terms, a variable is just a name that holds a value. It’s like a label on a box where you store your favorite things. But the trick is, you can store any kind of value you like!
Example:
n = 120
My_lucky_number = 7
print(n + My_lucky_number) # Output: 127
- `n` holds the value `120`.
- `My_lucky_number` holds the value `7`.
- When you add them up, Python tells you `127`!
In Python, you don’t need to stick with the same value forever! You can reassign a variable at any time.
Example:
python
Fav_num = 23
Fav_num = 24
Total_num = Fav_num * 2
print(Total_num) # Output: 48
- Initially, `Fav_num` is `23`, but we decide to change it to `24`.
- Then we multiply it by 2 to get `Total_num`, which is `48`. Easy, right?
---
Variable Naming Rules: Don't Break Them!
Now, here comes the important part: While you can name your variables almost anything, Python has a few rules to keep things sane.
1.Start with a Letter or Underscore
You can’t start a variable name with a number. Python is very picky about that.
Do not try this:
python
5numbers = 10 ❌ Nope! Can't start with a number.
But you can start with an underscore or a letter:
python
_numbers = 10 ✅ This is fine!
Totalnum = 20 ✅ This is also fine!
2.Use Letters, Numbers, or Underscores
After the first character, you can mix it up with letters, numbers, or underscores. But no spaces or funky symbols! Keep it clean.
Here’s what works:
python
numbers_5 = 15 ✅ Totally cool.
totalSum123 = 30 ✅ Also good.
3.Case Sensitivity is Real
Python is case-sensitive, meaning `myvariable`, and `MYVARIABLE` are both different!
---
These are a few important things to note when naming variables in Python.
I hope this helped!
Top comments (0)