A variable is a label for a location in memory that can be used to hold a value. Unlike other programming languages, python is not strictly typed, i.e. there is no need to declare variables or declare their type before using them.
Rules for creating variables in Python:
- The variable must start with a letter or underscore character.
- The variable name cannot start with a number.
- Only alpha-numeric characters and underscores (A-z, 0-9, and _ ) must be in a variable name.
- Variable names are case-sensitive (name, Name, and NAME are three different variables).
- The reserved words(keywords) cannot be used to name the variable.
a = 5 b = 'PYTHON' c = 3.09 print(a) print(b) print(c)
5 PYTHON 3.09
A variable once declared can be re-declared in Python. But the previous data is lost.
a = 5 print("Before redeclaring: ", a) a = 15.43 print("After redeclaring: ", a)
Before redeclaring: 5 After redeclaring: 15.43