Page 194 - Ai Book - 10
P. 194
Variables
Variables can act as a container for storing values. A memory location which is used to store data created in the
main memory of a computer is called a variable. These locations are created and identified by the given name
while executing a program. Using variables, values are stored and manipulated to produce the desired results.
Rules of Naming Variables
The naming convention plays an important role while creating variables. You should follow certain rules for
naming a variable in Python. These rules are as follows:
u A meaningful name should be used for a variable.
u The first character must be a letter or an underscore as it is considered as a letter by the Python compiler.
u Variable name is made up of the following characters a-z, A-Z, 0-9 and underscore(_).
u Upper and lower case letters are different.
u Do not keep the size of variables too short or too long.
u A variable name must not be a keyword of Python such as else, print, if etc.
u A variable name must not contain any special character except underscore.
Some examples of valid and invalid variable name are given below:
S. No. Valid Invalid
1 student_1 1 student
2 Myfile My.file
3 _Program1 .Program1
You should remember that Python is a case-sensitive language as it considers upper-case and lower-case
differently.
Assigning Values to Variables
Values are assigned to a variable using equal to (=) sign. For example, a = 10.
Here,10 is assigned as a value to a variable.
Python will let you assign the same value to multiple variables in one statement.
The syntax for assigning a value to the variable is:
Variable_name= variable_value
Example:
Always remember that text value can also be assigned to a variable name.
68
68