Python Variables

๐Ÿ”ค Python Variables

A variable in Python is a name that stores data in memory. The data stored in a variable can be used, updated, or processed later in the program.

Python variables are created automatically when you assign a value:

name = "Vipul"
age = 25

โœ… Why Variables?

Variables help you:

  • Store values

  • Reuse values

  • Change values dynamically

  • Make programs interactive


๐Ÿง  Rules for Naming Variables

โœ” Can contain letters, numbers, and underscores
โœ” Must start with a letter or underscore
โœ” Cannot start with a number
โœ” Cannot contain spaces
โœ” Case-sensitive

Valid Names Invalid Names
student_name 123name โŒ
age1 name-age โŒ
_total my salary โŒ

Example:

name = "John"
Name = "David" # Different from name
print(name, Name)

๐ŸŽฏ Variable Assignment

โžค Single Assignment

x = 10

โžค Multiple Assignment

a, b, c = 10, 20, 30
print(a, b, c)

โžค Same Value to Multiple Variables

x = y = z = 100

๐Ÿงช Variable Data Types

Python automatically detects the data type based on value.

x = 10 # int
y = 10.5 # float
name = "Raj" # string
isActive = True # boolean

To check data type:

print(type(x))

๐Ÿงฉ Changing Variable Values (Reassignment)

x = 10
print(x)

x = 50 # value changed
print(x)


๐Ÿ–จ๏ธ Using Variables in Print

name = "Vipul"
print("My name is", name)

Or using f-string:

print(f"My name is {name}")

๐Ÿง  Variable Scope

๐Ÿ”น Global Variables

Declared outside functions; can be accessed anywhere.

x = 10

def demo():
print(x)

demo()

๐Ÿ”น Local Variables

Declared inside functions; accessible only inside the function.

def test():
a = 5
print(a)

test()
# print(a) โŒ gives error


๐ŸŒ Changing Global Variables Inside a Function

Use global keyword:

x = 10

def update():
global x
x = 20

update()
print(x) # Output: 20


๐Ÿ“Œ Constants in Python

Python does not have true constants, but we use uppercase variable names by convention:

PI = 3.14
MAX_SPEED = 120

๐Ÿงฑ Dynamic Typing

Python allows variables to change type during execution.

x = 10
print(type(x))

x = "Hello"
print(type(x))


๐Ÿ“‚ Delete Variable

x = 100
del x
# print(x) -> Error

๐Ÿ‘จโ€๐Ÿ’ป Example Program

# storing user details
name = "Vipul"
age = 25
is_student = False

print(f"Name: {name}")
print(f"Age: {age}")
print(f"Student: {is_student}")


๐Ÿ Summary Table

Feature Example
Create variable x = 10
Multiple variables a, b = 1, 2
Same value assignment x = y = 5
Check type type(x)
Change value x = "Hello"
Delete variable del x
Global variable global x

๐Ÿš€ Practice Questions

Try these:

  1. Create variables to store name, age, and city, then print them.

  2. Assign the same value to three variables.

  3. Change the type of a variable at runtime.

  4. Use f-string to print: "My name is John and I am 30."

CodeCapsule

Sanjit Sinha โ€” Web Developer | PHP โ€ข Laravel โ€ข CodeIgniter โ€ข MySQL โ€ข Bootstrap Founder, CodeCapsule โ€” Student projects & practical coding guides. Email: info@codecapsule.in โ€ข Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *