Example of Variable in Python
An Example of a Variable in Python is a representational name that serves as a pointer to an object. Once an object is assigned to a variable, it can be referred to by that name. In layman’s terms, we can say that Variable in Python is containers that store values.
Here we have stored “w3wiki” in a variable var, and when we call its name the stored information will get printed.
Python3
Var = "w3wiki" print (Var) |
Output:
w3wiki
Notes:
- The value stored in a variable can be changed during program execution.
- A Variables in Python is only a name given to a memory location, all the operations done on the variable effects that memory location.
Rules for Python variables
- A Python variable name must start with a letter or the underscore character.
- A Python variable name cannot start with a number.
- A Python variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
- Variable in Python names are case-sensitive (name, Name, and NAME are three different variables).
- The reserved words(keywords) in Python cannot be used to name the variable in Python.
Example
Python3
# valid variable name geeks = 1 Geeks = 2 Ge_e_ks = 5 _geeks = 6 geeks_ = 7 _GEEKS_ = 8 print (geeks, Geeks, Ge_e_ks) print (_geeks, geeks_, _GEEKS_) |
Output:
1 2 5
6 7 8
Python Variables
Python Variable is containers that store values. Python is not “statically typed”. We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it. A Python variable is a name given to a memory location. It is the basic unit of storage in a program. In this article, we will see how to define a variable in Python.