Python Variable

Variable refers to a value. A value can be a number like - 5, 12, a string like "Hello", "Good Morning" , or can be a number with decimal point, like - 10.223, 23.444.

The variable name should be meaningful.

These are the good practices to write Python variable -

  • A variable can contain letter, number & underscore (_).
  • A variable name cannot start with a number. If we write a variable like this, it returns error.
    33Str (illegal) , Syntax Error - Invalid syntax
  • A variable name should not contain special character. If we write a variable like this, it returns error.
    mega@, Syntax Error - Invalid syntax.
  • A reserved word cannot be used as a variable name.
  • All these characters are allowed - a-z A-Z 0-9 and underscore. Like - xyz, Xyz, _xyz, _1_xyz, xyz_1, XyZ.
  • A variable name must begin with a letter and underscore.
  • Variable length can be unlimited.
  • In Python, names & identifiers are case sensitive.

Python Variable Example

>>> a = 10
>>> b = 20
>>> c = "Good Morning"
>>> d = 10.333
>>> print("Hello World")
Hello World
>>> print(d)

Here, we have stored the program in '' variable and run using Python Shell.

Python variable

Output of the above code
Python variable