What is Python Variables?
- Python variables are values that can change, depending on conditions or on information passed to the program.
- It is a name to store a value in the memory location. Then it is very easy to retrieve data from memory.
- Every variable in Python is an object.
- You do not need to declare variables before using them, or declare their data type. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
Things to remember for variable name
- Start with a letter or underscore (a-z, A-Z, _)
- No space
- Can be any length
- Python is case sensitive. So python variables are also case sensitive
- Can not use python keywords for variable names
Invalid Variable Names | Valid Variable Name | Reason for Invalid |
NameOfTheStudent | stuName | Too long, but doesn’t show errors |
Stu Address | stuAddress | Can not use space within variable name |
5address | address5 | Can not start with a number |
_name | Valid variable name |
Assigning Values for Python Variables
>>> name=” Sithuliya”
>>> marks=78
>>> average_marks=76.0
To get the output
>>> print (name)
>>> print (marks)
>>> print (average_marks)
Output
Sithuliya
78
76.0
This is invalid syntax
78=marks
Multiple Assignments in Python Variables
python allows you to assign a single value to several variables simultaneously
a=b=c=1
You can also assign multiple objects to multiple variables
a, b, c=1, 2,”Sajith”
Here, two integer objects with values 1 and 2 are assigned to variables a and b, and one string object with the value “Sajith” is assigned to the variable c.