Variables in Python




Here is a simple and clear explanation of variables in Python 👇


What is a Variable in Python?

A variable in Python is like a container used to store data.
It holds a value that you can use and change later in your program.

📌 Example:

x = 10
name = "Meenu"
price = 99.50

Here:

  • x stores an integer value 10
  • name stores a string "Meenu"
  • price stores a float 99.50

Rules for Python Variables

  1. Variable names must start with:
    • a letter (a–z or A–Z) or
    • an underscore _
  2. They cannot start with numbers
  3. They can contain letters, numbers, and underscores
    my_var = 5        ✔️
    2value = 10       ❌ (invalid)
    
  4. Variable names are case-sensitive
    age = 10
    Age = 20   # different variable
    
  5. No spaces allowed
    user name = "abc"   ❌
    user_name = "abc"   ✔️
    

Assigning Values to Variables

🔹 Single assignment

a = 5
b = "Hello"

🔹 Multiple assignment

x, y, z = 10, 20, 30

🔹 Same value to multiple variables

a = b = c = 100

Types of Variables

Python automatically decides the type based on the value:

x = 10          # int
y = 10.5        # float
name = "Meenu"  # str
is_active = True  # bool

To check the type:

print(type(x))

Changing the value of a variable

Variables can be updated anytime:

x = 10
x = 20   # updated

⭐ Example Program

name = "Meenu"
age = 20
price = 56.75

print("Name:", name)
print("Age:", age)
print("Price:", price)


🔥 Python Variables – In Depth


1️⃣ What Actually Happens When You Create a Variable?

When you write:

x = 10

Python does two things:

  1. Creates an object with value 10
  2. Creates a variable x that points to that object

📌 Python variables don’t store data directly — they store references (pointers) to memory.


2️⃣ Variable Naming Styles (Best Practices)

✔ Snake Case (recommended for Python)

user_name = "Meenu"
total_amount = 500

✔ Uppercase (usually used for constants)

PI = 3.14
MAX_LIMIT = 100

❌ Not recommended:

UserName = "abc"
totalAmount = 400    # camelCase is mostly used in JavaScript, not Python

3️⃣ Dynamic Typing in Python

Python is a dynamically typed language.

Meaning:

  • You don’t need to declare a type
  • Type can change at runtime

Example:

x = 10       # x is int
x = "Hello"  # now x is str

This is valid in Python.


4️⃣ Variable Memory Concept

a = 10
b = 10

Both a and b will point to the same memory location because integers are immutable.

But:

a = 10
a = 20   # new object created

Value changes → New memory created.


5️⃣ Types of Variables in Python

📌 Global Variable

Defined outside functions → can be used anywhere

x = 100  # global variable

def show():
    print(x)

show()

📌 Local Variable

Created inside a function → only works inside that function

def fun():
    y = 50   # local variable
    print(y)

fun()

📌 Changing Global Variable inside a function

You need global keyword:

x = 10

def modify():
    global x
    x = 20

modify()
print(x)   # 20

6️⃣ Constants in Python

Python does not have true constants.
But by convention, we write constants in UPPERCASE.

PI = 3.14
GRAVITY = 9.8

7️⃣ Deleting Variables

You can delete a variable using del:

x = 10
del x
print(x)   # error → variable deleted

8️⃣ Input with Variables

name = input("Enter your name: ")
age = int(input("Enter age: "))

print("Your name is", name)
print("Your age is", age)

9️⃣ Multiple Assignments - More Examples

✔ Swap two variables

a = 5
b = 10
a, b = b, a

print(a, b)   # 10 5

✔ Unpacking a list/tuple

numbers = [1, 2, 3]
a, b, c = numbers
print(a, b, c)

🔟 Practice Questions

Try these for mastery 👇

⭐ Easy

  1. Create a variable to store your name and print it.
  2. Store two numbers in variables and print their sum.
  3. Check the type of each variable:
    a = 10
    b = "Meenu"
    c = 5.5
    

⭐ Medium

  1. Swap 3 variables without using a temporary variable.
  2. Explain the difference between global and local variables with example.
  3. Write a program that takes user input for three values and prints the largest.

⭐ Hard

  1. Show how dynamic typing works with an example.
  2. Demonstrate memory allocation using id() function.
  3. Create a program that uses both local and global variables.

If you want, I can also provide: 👉 Diagrams
👉 Notes for exam
👉 PDF version
👉 MCQs with answers
👉 Python variable exercises with solutions

Which one would you like next? 😊


Post a Comment

0 Comments