Variables in Python

A Python variable, often known as an identifier, is a container for data. We don’t need to define the type of variable in Python because the language is intelligent enough to figure it out.

A variable is a term that refers to a memory region. Variable names can contain both letters and numbers, but they must start with a letter or underscore. The variable name should be written in lowercase letters.

Python allows us to create a variable at the required time. We don’t need to declare explicitly variable in Python. When we assign any value to the variable, that variable is declared automatically. The equal (=) operator is used to assign value to a variable.

Example:

message = 'Hello, World!'
print(message)

message = 'Good Bye!'
print(message)

Code language: Python (python)

Output

Hello, World!
Good Bye!
  • In this example, message is a variable. It holds the string ‘Hello, World!’. The print() function shows the message Hello, World! to the screen.
  • The next line assigns the string ‘Good Bye!’ to the message variable and print its value to the screen.
  • The variable message can hold various values at different times. And its value can change throughout the program.