Page 110 - Touchpad_Plus_V3.2_Class 6
P. 110

VARIABLES IN PYTHON

                  Variables are memory reference points where we store values which can be accessed or changed
                  later. The names given to variables are known as identifiers. In Python, we do not need to
                  specify the type of variable because Python is a dynamically typed and automatically identifies
                  the variable's type. Let’s understand this with the help of the given examples.
                         num = 0.4
                         _price = 30.3
                         TOTAL = round(_price * num,2)
                         print(TOTAL)
                  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 = 20 and b = 40 will be printed. You can also assign the same value
                  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:
                    name, age, grade = "Gunjan", 15, 'VII'
                    print ("Name is", name)

                    print ("Age is", age)
                    print ("Grade is", grade)
                  On the output screen, Name is Gunjan, Age is 15 and Grade is VII will be printed.
                  You must follow the given rules while creating and naming the variables:
                      ✶ A variable name must start with a letter or an underscore character.
                      ✶ A variable name cannot start with a number.

                      ✶  A variable name can only contain alphanumeric characters (all the letters of the alphabet and
                     numbers) and underscore (_).
                      ✶ Variable names are case-sensitive.
                      ✶  Variable names cannot contain any special character or symbol except underscore.

                      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. Data types in
                  Python are:


               108      Plus (Ver. 3.2)-VI
   105   106   107   108   109   110   111   112   113   114   115