Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 145 - 2620_Birla Open Mind C-6
P. 145

    Declaring and Initializing a Variable

                     In Python, variables are declared and initialized at the same time in the following way:

                        a = 10

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

                        print ("b=", b)
                     On the output screen, a = 20 and b = 40 will be printed. You can also assign the same value to multiple
                     variables at the same 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:

                        name, age, grade="Deepak", 12, ‘VII’

                        print ("Name is", name)

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

                     You must follow the given rules while creating and naming the variables:
                        Variable names can contain letters (a-z, A-Z), digits (0-9), or an underscores (_).

                        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.

                              Double Tap

                            Circle the incorrect variable  in the following code:

                            Roll no = 201    Student Name = "Chirag"    Section = 'A'

                          DATA TYPES

                     A data type specifies the type of value a variable can contain. For example, a person’s name must be
                     stored as a string value whereas the person’s age must be stored as an integer.

                                                                                                              #Python 143

| 140 141 142 143 144 145 146 147 148 149 150 |