Python Variables: Store and Use Values
What Is a Variable?
A variable is a name that stores a value, so your program can use it later. You create one with the = sign:
name = "Sunny"
age = 25Now name holds the text "Sunny", and age holds the number 25. Nothing is printed yet: creating a variable only stores the value.
Think of it like a labelled box. The label is the name (name), and inside the box is the value ("Sunny"). Whenever you say the label, Python opens the box and uses what's inside.
How = works in Python
- The name goes on the left, the value on the right:
name = "Sunny". =means "store this value in this name". It does not mean "is equal to" like in maths.- Read it out loud as "name gets Sunny".
This step is called assignment: you assign a value to a variable.
The value can be anything you've met so far
city = "Delhi"
score = 90
price = 19.99Text, whole numbers, decimal numbers: a variable can hold all of them. You'll learn about these kinds of values in the last topic of this chapter.
Common beginner doubt: "Can I write "Sunny" = name?" No. The name must always be on the left side of =.
A variable is a labelled box: name = value stores the value under that name.