Python Variables (with Examples)
๐ Python Variables: The Cool Kidโs Guide ๐ฉโ
Welcome to the world of Python variables โ where data gets stored, changed, juggled, and sometimes even misplaced (oops). Think of a variable as your pet jar that holds jellybeans (or maybe integers, strings, lists...).
In this epic journey, youโll learn the essentials of variables, naming rituals, the mystery of scopes (local vs. global), and a few sorcerer-level tricks like chained assignments. Buckle up!
๐ท๏ธ 1. Python Variable Naming Conventionโ
Before you name your variable GandalfTheGray
, there are a few sacred scrolls (rules) you should honor:
- ๐ฅ Must start with a letter (a-z, A-Z) or an underscore (_).
- ๐ข Can include letters, numbers, and underscores (party mix).
- ๐ Case-sensitive!
myVariable
โmyvariable
. - ๐ซ Avoid using Pythonโs holy reserved words like
if
,for
,while
, etc. - ๐ Unicode is welcome! You can name a variable
ฯ
orๅๅญ
if thatโs your jam.
๐งช 2. Declaring a Variable in Pythonโ
Python is chill โ you donโt have to scream the data type like in other languages. Just throw a value at a name, and boom ๐ฅ โ youโve got a variable.
๐ฏ 2.1 Simple Variable Assignmentsโ
# Assigning an integer value to a variable
age = 25
# Assigning a string value to a variable
name = "Alice"
# Assigning a list of numbers to a variable
scores = [90, 85, 88, 92]
Wanna be fancy?
author = 'Sujit'
blogName = "FOSS GURU"
Single or double quotes โ your choice. Python isnโt picky like some people on dating apps.
๐ 2.2 Chained Assignmentsโ
Want to clone a value across multiple variables like a boss? Do this:
i = j = k = 20
print(i) # prints 20
print(j) # prints 20
print(k) # prints 20
Now all your variables are twins. Or triplets. Or quadruplets. You get the idea.
๐งโโ๏ธ 2.3 Declaring Multiple Variables in One Lineโ
Why write three lines when one will do?
x, y, z = 10, 20, 30
print("x:", x)
print("y:", y)
print("z:", z)
Perfect for when you're unpacking a treasure chest (aka list/tuple) ๐งณ
๐ 2.4 Variable Re-declarationโ
Pythonโs flexible. Today index
is a number, tomorrow itโs a string. Who are we to judge?
index = 10
index = 20
index = "NA" # index is now a string variable
print(index) # prints NA
You can change its type faster than a cat changes its mind.
๐ง 3. Local vs Global Variablesโ
A tale of two kingdoms: Localandia and Globalopolis. ๐ญ
๐ 3.1 Local Variableโ
Born and raised inside a function. They donโt know what the outside world looks like.
def my_function():
x = 10
print("Inside the function, x =", x)
my_function()
Outside the function? x
is a ghost. ๐ป
๐ 3.2 Global Variableโ
This is your loud neighbor who shows up everywhere.
y = 20
def my_function():
global y
print("Inside the function, y =", y)
y = 30 # changing the neighborhood!
my_function()
print("Outside the function, y =", y)
Output:
Inside the function, y = 20
Outside the function, y = 30
โ ๏ธ Best Practice Tip: Avoid modifying globals like itโs a buffet. Keep them sacred unless you really must.
๐ฅ 4. Variable Examples Youโll Actually Useโ
Letโs do some real-life wizardry ๐ง
๐งฎ 4.1 Calculating Area of a Rectangleโ
length = 10
width = 5
area = length * width
print("The area of the rectangle is:", area)
Yup, weโre all architects now ๐๏ธ
๐งฉ 4.2 Concatenating Stringsโ
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print("Full Name:", full_name)
Now go build your very own name generator ๐ก
๐ 4.3 Working with Listsโ
fruits = ["apple", "banana", "cherry"]
print("Second fruit:", fruits[1])
fruits.append("orange")
print("Updated list of fruits:", fruits)
Python lists: Your digital fruit basket ๐งบ
โ Drop Your Variable Questionsโ
Got doubts? Confusions? Existential crises related to variables? Hit me up!
Happy Learning & May Your Variables Always Be Bug-Free! ๐๐