Variables in Python

Variables in Python

Reserving a space in memory

·

2 min read

Any value can be assigned to a name in python. The name is called variable. There is no need to declare the type of value is assigned to the name. Example:

msg = 'An article about variable.'

likes = 100

A string is assigned to the variable name msg. An integer is assigned to the variable name likes.

The variable name will be chosen based on its usage in a meaningful manner. The variable name can contain letters and numbers. But it cannot be started with number.

name = 'ahamed'
1phone = 512-345-4000

When the code is executed, the following message will be shown.

File "<ipython-input-16-8392a4213cf4>", line 2
    1phone = 512-345-4000
     ^
SyntaxError: invalid syntax

The underscore can be considered for variable name, even at first position. But the special characters cannot be considered.

_name = 'ahamed'
_1phone = 512-345-4000

The above both variable names are valid. The python language has several keywords which are reserved for its usage. Those keywords cannot be used as variable names. The reserved keywords are

and as assert async await break class continue def del elif else except False finally for from global if in import is lambda None nonlocal not or pass raise return True try while with yield Python has the following types to be assigned to the variable.

  • Numbers
  • Strings
  • List
  • Tuples
  • Dictionary

The assignment of a value to a variable name doesn't produce an output. The variable name shall be printed to see the value of it.

print(msg)

The output shall be

An article about variable.

Did you find this article valuable?

Support K Ahamed by becoming a sponsor. Any amount is appreciated!