Variables in Python- Declaration & Naming Rules

Programming is all about dealing with data as input and processing or manipulation it to get output. In all programming languages that you have worked with, you must be aware of term Variable. If you are starting your programming skills with Python then you must know what a variable is. It can be defined as a placeholder in memory that is given a name and may contain some value. This value can be a number, a string of characters, a date or anything you consider data. So before moving ahead, spend some time to understand variables in Python and variables in general.

Variables in Python- Declaration

Variables in Python are actually names defined by the programmer on command line or script that store single value. There is no specific declaration statement. Whenever you assign a value to a variables in Python using the ‘=’ assignment operator, you are creating a variable.

variable examples.PNG

In the above example three variables are declared with assignment operator ‘=’. myName, myAge and mySalary.

When we  pass these three variables to Python print function, for printing, their assigned values are printed on the screen.

As you can observe that the data types of the variables are not defined. It means that with each assignment statement, the variables automatically gets their data types. Python assigns data type to variables  according to the value assigned to it. The data type of a variable can be checked with Python type function. Type function takes a variable as an argument and returns its data type. Here MyName is of string type (collection of characters), myAge is of float type (numbers with decimal part) and  mySalary is of Integer type( No decimals please!)

The most amazing thing about Variables is their dynamic behavior. Any variable changes its datatype as soon as new value is assigned to it.

In this example you will notice that we have assigned a new value 45 to Variable myAge. When we pass myAge to type function it returns Int not Long or Float. So, as we changed its value the data type of myAge variable changes with the new assignment statement.

Note: Variable declared and used in one session of IDLE shell persists only till you have not closed the window. If you close the command prompt (DOS) or Shell Prompt all variables will be cleared. You have to declare them again when you open the shell next time.

Rules for Python Variable Naming

Python variables can

  • Be of any length
  • Must not contain any spaces (‘my Name’ is incorrect Python variable name)
  • Contain digits and alphabets, but always start with an alphabet
  • Include uppercase alphabets. Python is case sensitive ( myname and myName are two different variables)
  • Can include underscore(_) to separate parts of variable name(my_name, my_salary). No other special characters allowed in variable names
  • Not be Python Keywords as displayed in the image below
pyhton Keywords.png

One Comment

  1. Great content! Super high-quality! Keep it up! 🙂

    February 25, 2020

Leave a Reply

Your email address will not be published.