4 Python Variables

Variables are location names in memory to store data. We use variables to store data in computers memory. Those are simply containers which holds data. It consists of two parts.

  • variable name : It is the name which we provide while defining or creating variable.

  • variable value : It is the data which is being stored in variable.

Assignment operator ‘ = ’ is used for assigning to the variable name. There are following rules/conventions you need to remember while defining variables.

Variable Naming Conventions:

  1. Variable name should start with lower case letter or underscore but not with number.
    • class_name = “SSC”, marks = 90 are valid variable names.

    • 1_my_roll_number = 1 , it is not valid variable name, will give syntax error.

  2. Variable names are case sensitive.
    • first_name and First_Name are not considere to be same in Python, both are different variables.

  3. Python keywords should not be used as variable names.
    • Some keywords (True, if, not, False, while, etc.) are specially reserved for python programming purpose, so avoid them using as variables into your code.

  4. Variable can only contains alpha-numeric characters and underscores (A-Z, a-z , _).
    • first_name is valid variable name , but first&name is not valid variable.

  5. Variable name should not contain spaces. You can use underscores to separate words in variable names.
    • school_name is valid varible name, but ‘school name’ is invalid.

Variable Definition:

We have seen some important rules for defining variables, lets define variables now. We already know assignment operator (‘=’) is used to assign value(data) to variable. Below code snippet explains how to define variables.

💻 Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Valid variable names:
school_name = 'my school name'
roll_number = 123
_city = 'New York'
examFees = '$100.50'

# Invalid variable names which will cause SyntaxError: invalid syntax
school&name = 'my school name'
roll number = 123
%marks = 50
if = 'this is python keyword'
  • Example for finding variable memory location.

💻 Example:

1
2
3
4
5
# get id of a variable
a = 12
b = 13
print(id(a))
print(id(b))

✅ Output:

2171623008912
2171623008944
  • Example for finding identical variables.

💻 Example 1:

1
2
3
4
5
6
7
8
# identical variables
a = 12
b = 13
c = 12

print(id(a))
print(id(b))
print(id(c))

✅ Output:

2171623008912
2171623008944
2171623008912

💻 Example 2:

1
2
3
4
5
6
7
8
# identical variables
x = 10
y = 10.0
z = 10

print(id(x))
print(id(y))
print(id(z))

✅ Output:

2171623008848
2171699072272
2171623008848

Here are the quick notes on above topic:


Follow below video to clear the concepts practically. Comment your suggestions below the video, they are always welcome.


Be prepared with below questions before your interview !

Frequently asked Interview Questions
  1. What are python variables & How do you declare them?

  2. Does type declaration needed while defining a python variable?

  3. What rules or naming conventions needs to be followed while declaring python variables?

  4. Can you change the value of a variable after it is assigned?

  5. How to identify type of a variable?

  6. What is type() function?

  7. How to find memory address of a python variable?

  8. What are identical variables?

  9. What is the use of id() function?