Variable is defined as the reserved memory location to store the value. Various data types like Numbers, List, Tuple, Strings, Dictionary, etc are used in python and hence these values are stored in the variables. We start with an identifier which is the name of our variable then equal sign and finally a value.
>>> a = 5 # a is variable
>>> print(a)
output :5
We can re-declare the variable even though it is declared above. It stores the newly assigned value replacing the old value assigned to it.
>>> a= 10
>>> a =56 # reassigning the value to variable
>>> print (a)
output: 56
Let’s try something different:
>>> a = 6
>>> del a
>>> print (a)
output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
Here, we delete variable using the command del . In the above example, we deleted variable a, and when we proceed to print it, we get error “name ‘a’ is not defined” which means you have deleted the variable.
Rules of Declaring variables
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive (age, Age and AGE are three different variables)
ADD TWO NUMBERS
SWAP TWO VARIABLES
This page is contributed by Diwas & Sunil . If you like AIHUB and would like to contribute, you can also write an article & mail your article to itsaihub@gmail.com . See your articles appearing on AI HUB platform and help other AI Enthusiast.
Since the admin of this site is working, no hesitation very rapidly it will be well-known, due to its quality contents.
adreamoftrains best web hosting company
Thanks for sharing your thoughts on website hosting companies.
Regards
Thanks for a marvelous posting! I quite enjoyed reading it, you could be a great
author. I will be sure to bookmark your blog and
will eventually come back later on. I want to encourage one to continue
your great posts, have a nice evening!