Page 51 - Computer - 7
P. 51

Lab Activity 2                                                                         Experiential Learning
           Program to accept two numeric variables from the user and display their sum and product.

           1. Type the code as shown in the image in the Python Editor.



















           2. Press the F5 key to execute the code. Sample output of the program is given.

          DECLARING AND INITIALISING A VARIABLE

          A variable in Python needs to be declared and initialised before it can be used. Declaring a variable
          refers to the process of naming the variable and assigning a data type to it. Initialising a variable refers
          to the process of assigning a value to the variable. In Python, variables are dynamically identified by the
          value they store. Thus, declaring a variable and initializing is done in the same step.

          For example, the statement  Age = 15  declares a variable named ‘Age’ and initialises it with the value
          15. As 15 is an integer value, the data type of the variable Age is integer.
          OPERATORS IN PYTHON
          Operators are special symbols that are used to perform a mathematical operation or logical computation.
          For example, the difference operator ‘ – ’, denotes the process of subtraction of two numbers. The
          values on which the operator performs the task are called Operands. Different operators work with one
          or more operands. For example, the product operator ‘ * ’, requires at least two operands to carry out
          the process of multiplication.

          Types of Operators
          The different operators available in Python can be categorised as Arithmetic, Assignment, Relational,
          Logical, and String operators.

          Arithmetic Operators
          The  Arithmetic  operators  are  used  to  perform  mathematical  operations  on  numeric  values.  The
          Arithmetic operators available in Python are described in the table given below.

                                                                                          Example
            Operator         Name                        Description                                       Output
                                                                                       (X = 20, Y = 3)

                +          Addition       Adds two numeric values.                          X + Y             23

                                          Subtracts  the  second  number  from  the
                –         Subtraction                                                       X – Y             17
                                          first.


                                                                                                              49
   46   47   48   49   50   51   52   53   54   55   56