VARIABLES IN PYTHON

Python Free Tutorial

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
  1. A variable name must start with a letter or the underscore character
  2. A variable name cannot start with a number
  3. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  4. 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.

About Diwas Pandey

Highly motivated, strong drive with excellent interpersonal, communication, and team-building skills. Motivated to learn, grow and excel in Data Science, Artificial Intelligence, SEO & Digital Marketing

View all posts by Diwas Pandey →

4 Comments on “VARIABLES IN PYTHON”

  1. 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!

Leave a Reply

Your email address will not be published.