Variables are basic building blocks of a programming language to store the value, reuse it in code, and modify the value.
Python Variables
Variables are names, assigned with a value, then reused the value in other places of a code.
It points to the memory location for the assigned value.
Variables in Python are dynamically typed values, which means variables are assigned a value without defining a datatype.
m=20;
print(m); #20
Variables can be reassigned with new values as per give below example
m=20;
m=m+1;
print(m);#21
variables can change their type by assigning a new value type
m=20;
m="hello";
print(m);#hello
Multiple variables can be declared in a single statement using comma-separated
id, name, salary = 1, 'john', 4000
print(id);#1
print(name);#john
print(salary);#4000
How to create a variable in Python
Variables are declared using name and value with assignment operator =
.
str="name"
str is declared and assigned with a value of name.
Variable naming convention rules
- variables names either short or long
- Python recommends names are in lowercase with underscore by convention, not compulsory
- Usually, Variable names are identifier that always starts with either alphabets or underscore characters
- alphabets contain a to z or 0-9
- It does not start with a letter
- Names are case-sensitive, variable names such as str, Str, STR are three different variables
Valid variable names are
- number1
- _price
- total_value
- _amount1
Invalid variable names are
- 9amt
- 21_proice
- 111