Page 439 - AI Ver 3.0 class 10_Flipbook
P. 439

[5]:   N1=10
                        N2=30
                        print("The sum of two numbers is ", N1+N2, end="**********")
                        print("The difference of two numbers is ",N1–N2)
                        The sum of two numbers is  40**********The difference of two
                        numbers is -20
                 The input() Function

                 The input() function is used to take an input from the user. When this statement executes then the flow of
                 the program stops and waits with the cursor blinking for the user to enter a value.  The input given by the user
                 is always accepted as a string value. In case a user enters an integer or float and we need this for mathematical
                 calculation then we have to convert them explicitly to number data type by using int() or float(). Syntax for
                 the input() function:
                           input(prompt)
                 Where prompt is optional. It is a string that works as a message for the user at the time of input of data.

                 [1]:   N1=input("Enter first number: ")
                        N2=input ("Enter second number: ")
                        Sum = N1+N2
                        print("Sum= ",Sum)

                        Enter first number: 23
                        Enter second number: 61
                        Sum= 2361

                 If N1=23 and N2=61, then the output is Sum=2361.
                 The reason for the above is that by default the input statement always takes input as a string value. If a “+” is
                 used with string, it concatenates the 2 string. In order to evaluate the expression mathematically, we must use
                 int() with an input statement to convert it into integer. For example:

                 [1]:   N1=int(input("Enter first number: "))
                        N2=int(input("Enter second number: "))
                        Sum = N1+N2
                        print("Sum= ",Sum)
                        Enter first number: 23
                        Enter second number: 61
                        Sum= 84
                 Conditional Statements


                 Conditional statements are used for selecting the block of statements to be executed based on the condition. We
                 specify the condition in the program which evaluates to either True or False. If condition is True then the block
                 of statements written for True will be executed and in case the condition is False then the block of statements
                 for False will be executed. It is also called branching as a program decides which statement to execute based on
                 the result of the evaluated condition. In Python:
                    • A block is identified by using indentation (minimum 1 space). Ensure that all statements in one block are
                   indented at the same level.

                    • We use if to do conditional programming.




                                                                                    Advance Python (Practical)  437
   434   435   436   437   438   439   440   441   442   443   444