Skip to main content

Python Variables (with Examples)

banner-python

๐Ÿ 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! ๐Ÿš€๐Ÿ