Page 135 - TP_Prime_v2.2_Class_6
P. 135

Declaring and Initialising a Variable

                 In Python, variables are declared and initialised at the same time in the following way:
                        a = 10

                        b = 20
                        print ("a=", a)

                        print ("b=", b)
                 On the output screen, a = 10 and b = 20 will be printed. You can also assign the same value                 INTRODUCTION TO PYTHON

                 to multiple variables at the time in the following way:
                        a = b = 20
                        print ("a=", a)

                        print ("b=", b)

                 On the output screen, a = 20 and b = 20 will be printed. You can also assign multiple values
                 to multiple variables in the same line in the following way:                                             133
                        name, age, grade="Deepak", 12, ‘VII’

                        print ("Name is", name)
                        print ("Age is", age)

                        print ("Grade is", grade)
                 On the output screen, Name is Deepak, Age is 12 and Grade is VII will be printed.


                  Naming Conventions for Variables

                 Before we specify any variable name, we must follow the Python rules:
                 •   Variable names can contain letters (a-z, A-Z), digits (0-9), or an underscore (_).

                 •   Variable names must begin with a letter or an underscore.
                 •   Variable names are case-sensitive (Age and age are different variables).

                 •   Python  keywords  cannot  be  used  as  variable  names  (reserved  words  like  if,  else,
                   for, etc.).

                 •   Variable names cannot contain spaces.
                 Examples of valid variables are:

                                 Employee_num       Answer_value          Variable1        Cz67ys

                 Examples of invalid variables are:

                                    1Variable                (Cannot start with a digit)

                                    Employee Num             (Space in the middle)
                                    BookPrice$               (‘$’ sign  is not accepted)

                                    break                    (Reserved keyword)
   130   131   132   133   134   135   136   137   138   139   140