Python Variables

Variables are containers that store information that can be manipulated and referenced later by the programmer within the code.

In python, the programmer does not need to declare the variable type explicitly, we just need to assign the value to the variable.

Example:

name = "Abhishek"   #type str
age = 20            #type int
passed = True       #type bool

It is always advisable to keep variable names descriptive and to follow a set of conventions while creating variables:

  • Variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable name must start with a letter or the underscore character.
  • Variables are case sensitive.
  • Variable name cannot start with a number.
Color = "yellow"    #valid variable name
cOlor = "red"       #valid variable name
_color = "blue"     #valid variable name

5color = "green"    #invalid variable name
$color = "orange"   #invalid variable name

Sometimes, a multi-word variable name can be difficult to read by the reader. To make it more readable, the programmer can do the following:

Example:

NameOfCity = "Mumbai"       #Pascal case
nameOfCity = "Berlin"       #Camel case
name_of_city = "Moscow"     #snake case