What is a variable?#
A variable is a name that refers to a value stored in the computer's memory. Think of it as a labelled box - you put a value inside and use the label to get it back later.
student = "Priya"
marks = 87
print(student)
print(marks)
Priya 87
Here student and marks are variables. The = sign is the assignment operator - it means "store the value on the right in the name on the left". It does not mean "equal to" as in maths.
Variable को एक डिब्बे पर लगे लेबल की तरह समझें। marks = 87 का मतलब है - "87 नाम की value को memory में रखो और उस पर marks का लेबल लगा दो।" बाद में जब भी आप marks लिखेंगे, Python उस डिब्बे से 87 निकाल कर देगा।
ध्यान दें: यहाँ = का मतलब "बराबर है" नहीं, बल्कि "में रखो (store करो)" है।
Creating variables#
In Python you create a variable simply by assigning a value. There is no int x; style declaration.
Changing a variable's value#
A variable can be given a new value at any time. The old value is replaced.
score = 10
print(score)
score = 25
print(score)
score = score + 5 # take current value, add 5, store back
print(score)
10 25 30
The line score = score + 5 confuses many beginners. Python always works right side first: it calculates score + 5 (25 + 5 = 30), then stores 30 in score.
A variable can even hold a different type of value later (because Python is dynamically typed):
x = 100 # x holds a number
x = "hundred" # now x holds text - this is allowed
Naming rules#
| Rule | Valid | Invalid |
|---|---|---|
Must start with a letter or _ |
name, _temp |
2name |
Only letters, digits and _ |
total_marks2 |
total-marks, total marks, price$ |
| Cannot be a keyword | class_name |
class, for, if |
| Case-sensitive | age and Age are different |
- |
Naming conventions (good habits)#
- Use meaningful names:
total_priceis better thantporx. - Use snake_case for variables:
first_name,monthly_salary. - Use ALL CAPS for values that should not change (constants):
PI = 3.14159,MAX_USERS = 100.
Don't use built-in function names as variable names, e.g. print = 5 or list = [1, 2]. It works at first, but then print("hi") stops working in that program!
Multiple assignment#
Many values to many variables#
x, y, z = 10, 20, 30
print(x, y, z)
One value to many variables#
a = b = c = 0
print(a, b, c)
Swapping two variables#
In most languages you need a temporary third variable. Python makes it one line:
a, b = b, a में Python पहले right side की दोनों values (9, 5) एक साथ उठा लेता है, फिर उन्हें left side के variables में क्रम से रख देता है। इसलिए किसी extra (temporary) variable की ज़रूरत नहीं पड़ती।
How Python variables really work#
In Python, a variable is a reference (pointer) to an object in memory, not the box itself. Two names can refer to the same object:
a = 500
b = a
print(id(a) == id(b)) # id() gives the memory identity of an object
True
id() returns a unique number for each object. Here both a and b point to the same 500. This matters later when you learn about lists.
Deleting a variable#
temp = 42
del temp
print(temp) # NameError: name 'temp' is not defined
Practical example#
Summary#
- A variable is a name that refers to a value:
name = value. =means "assign", and the right side is evaluated first.- Names: letters/digits/underscore, can't start with a digit, can't be keywords, case-sensitive.
- Use meaningful snake_case names.
x, y = 1, 2assigns many values;a, b = b, aswaps values.