What is a Variable?
Variable, in simple language, means a container for storing values. We can store any piece of information in a variable.
Actually, a variable is an identifier that denotes a storage location used to store a data value. This value can vary during the execution of a program.
Each variable is given an appropriate name so that it can be identified easily.
Variable Name Rules in Python
For naming a variable in Python, there are some basic rules that we must follow. These rules are given below:
- Variable names must not begin with a digit.
- Variable name always start with an underscore('_') or a letter(lowercase or uppercase)
- The rest of variable name can be underscore('_'), letter(uppercase or lowercase), or digits(0-9)
- It should not be a keyword.
- White space is not allowed.
- Variable names are case sensitive. For example, roll and ROLL are different variable names.
- Punctuation characters such as @, #, $, and % are not included within a variable name.
Python Variable Examples
In Python, there is no command for declaring a variable. The declaration is actually made automatically when we assign a value to the variable using the equal sign(=). The operand on the left side of the equal sign is the variable's name, and the operand on the right side of the equal sign is the value to be stored in that variable.
In Python, we can reassign variables as often as we want to change the values stored in them.
Actually, Python variables do not have any specific types, so we can assign an integer to a variable and later assign a string to the same variable.
Now we are giving an example to display different types using variable and literal constants
a=90 b=123.456 c='B' pi=3.1415926536 text="Hello students" print("\nNumber="+str(a)) print("\nSalary="+str(b)) print("\nCode="+str(c)) print("\nPi value="+str(pi)) print("\nMessage="+str(text))
Output
Number=90
Code=B
Pi value=3.1415926536
Message=Hello students
Here we are giving another example to reassign values to a variable
Salary_amount=90000 print(Salary_amount) Salary_amount=56000.400 print(Salary_amount) Salary_amount="Welcome" print(Salary_amount)
Output:
90000 56000.4 Welcome
Leave a comment