Page 430 - ComputerScience_Class_11
P. 430

12.10 VARIABLES IN PYTHON
              In Python, variables are names that act as containers for storing data or values in a program. They allow programmers
              to save information such as numbers, text or more complex data and use it throughout the program whenever needed.
              Variables make it possible to perform calculations, manipulate data and control the flow of a program efficiently.

              For example, if we write age = 15, the variable age holds the number 15 and we can use it later to do calculations
              or show it on the screen. Similarly, name = "Rahul" stores the text "Rahul" in the variable name. Variables are very
              useful because they make programs easier to write, read and change, since you can update the values in the variables
              without changing the whole program.

              12.10.1 Characteristics of Variables in Python
              Here are some characteristics of variables in Python:
              •  Name Storage: Each variable has a name that points to a value stored in memory.

              •  Changeable Values: The value stored in a variable can be modified at any time during the program.
              •  Data Types: Variables can hold different types of data, such as numbers, text (strings), Boolean values and more.
              •  Memory Reference: A variable stores a reference to the memory location where the actual data is kept.
              •  Case-Sensitivity: Variable names are case-sensitive, so age, Age and AGE are treated as separate variables.

              •  Dynamic Typing: The type of data a variable contains can change while the program is running.
              •  Reusability: Variables can be reused multiple times to store new values or update existing ones.

              12.10.2 Rules for Naming Variables in Python
              In Python, certain rules must be followed while naming variables to ensure that the program runs correctly. If these
              rules are not followed, the program may give an error. The rules for naming variables are given below:
              •  A variable name must start with a letter (A–Z or a–z) or an underscore (_).

              •  A variable name cannot start with a number.
              •  It can contain letters, numbers and underscores (_) only.
              •  Spaces are not allowed in variable names.

              •  Special characters like @, #, $, %, etc., or space are not allowed.
              •  A variable name cannot be a keyword (such as if, for, while, class, etc.).
              •  Variable names are case-sensitive, meaning name, Name and NAME are different variables.


              12.10.3 L-value and R-value in Python
              In Python, the terms L-value and R-value are used to describe how variables and values are used in an assignment
              statement.

              •  L-value  (Left  value):  The  L-value  is  the  variable  that  appears  on  the  left  side  of  the  assignment  operator  (=).
                 It  represents  a  memory  location  where  a  value  will  be  stored.  In  simple  words,  it  is  the  name  that  receives
                 the value.

              •  R-value (Right value): The R-value is the value or expression that appears on the right side of the assignment
                 operator (=). It provides the data that will be assigned to the variable.

                For example,
                 x = 10

                •  x → L-value (the variable that stores the value)
                •  10 → R-value (the value being assigned)



                  428  Touchpad Computer Science (Ver. 3.0)-XI
   425   426   427   428   429   430   431   432   433   434   435