Variables in Python
| Site: | Dr. B.R. Ambedkar Open University Online Learning Portal |
| Course: | Basics of Programming with Python |
| Book: | Variables in Python |
| Printed by: | Guest user |
| Date: | Saturday, 30 May 2026, 1:56 AM |
Description
This book resource provides information about variable definition, types, rules to create variables, assign values etc.
Note : This is mandatory to complete. After completion, you can manually mark the activity as completed
1. Definition of a variable
A variable is a logical name given to physical memory location in the RAM or HDD. When a variable is declared or created (assigning value to a variable), Python interpreter reserves some space to that variable and the space is named as the name of the variable. How much space to be reserved will be depended on the data type of the value assigned to the variable.
Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
Other DefinitionA variable is something that holds a value that may change. In simplest terms, a variable is just a box that you can put stuff in. You can use variables to store all kinds of stuff, but for now, we are just going to look at storing numbers in variables.
lucky = 7
print (lucky)
7
This code creates a variable
called lucky
and assigns to it the integer number 7.
When we ask Python to tell us what is stored in the variable lucky, it returns that number again.
2. Variable creation in Python
There is no special keyword,
command or syntax to create variables in Python. The variable is created the
moment you type some name and assign a value to it.
To create a variable we use an assignment operator ( = ) to assign a value to it.
Example 1: x=3, var=10
In the above example, the variable names are x and var. the symbol = is used to assign a value to a variable as shown in the above example.
Creating String Variables:
To create a string variable we need to place the value inside in quotes (either single or double quotes)
Example: name="Dr.B.R.A.O.U", person='KCR'
In the above example, name and person are two different variables with two different string values Dr.B.R.A.O.U & KCR and they have quoted using double and single quotes.
3. Keywords in Python
It turns out that class is one of the Python keywords. Keywords define the language’s syntax rules and structure, and they cannot be used as variable names. Python has thirty-something keywords (and every now and again improvements to Python introduce or eliminate one or two):
|
and |
as |
assert |
break |
class |
continue |
|
def |
del |
elif |
else |
except |
exec |
|
finally |
for |
from |
global |
if |
import |
|
in |
is |
lambda |
nonlocal |
not |
or |
|
pass |
raise |
return |
try |
while |
with |
|
yield |
True |
False |
None |
"Variable Names and Keywords" by Dan Schellenberg is licensed under CC BY-SA 4.0
4. Rules to name a Variable
A variable can have a short name (like a and b) or a more descriptive name (name, salary, occupation). There are some rules to name variables in python:
- A variable name must start with a letter or the underscore character
Example: var, Var, VAR, _variable (Note: Python variable are case sensitive. i.e., var & VAR are two different variables.
2. A variable name cannot start with a number.
Example: 9var, 3_var, 4a, etc. are invalid variable names.
3. A variable name can only contain alpha-numeric characters and underscores (A- z, 0-9, and _ )
Example: a3b, a_b, var_name, _ab # valid variable names
2ab, $var, @34b # invalid variable names
4. Variable names are case-sensitive (age, Age and AGE are three different variables)
5. A variable name cannot be any of the keywords of python.
Example: if, elif, for, class, etc are can not be used as variable names
Video: The following video provides you the knowledge to how to name a variables in python.
"Chapter 2: Naming Variables" by J David Eisenberg is licensed under CC BY-SA 4.0
5. Types of Variables in Python
Variables are categorized into two. They are : 1. Local Variables 2. Global Variables6. Assign Value to Multiple Variables
7. Modify the Data Type of the Python Variables
The Python variables are not declared with any particular data type. We can give variable data type even after variables have been assigned values.
Example: var=100
var="BRAOU"
if we print the value inside "var", we will get BRAOU as output.