Page 47 - PYTHON-11
P. 47

53.  What is iteration construct?
                Ans.  Iteration means repetition of a set of statements until the test condition remains true depending upon
                    the loop. In Python, iteration constructs are for, while.
                 54.  Define the body of the loop.
                Ans.  The body of the loop comprises sets of statements that get executed based on the condition being
                    evaluated as true.
                 55.  What is the purpose of using for loop?
                Ans.  for loop is used for iterating a specific block of code for a definite or fixed number of times.
                 56.  What is the purpose of else in a loop?
                Ans.  Looping statements may include an optional  else block. The  else part executes if the loop completes
                    without encountering a break statement or if the items in the sequence do not match the condition.
                 57.  What is a nested loop?
                Ans.  A loop inside another loop is termed as a nested loop.
                      For example,
                     for i in range(3):
                          for j in range(2):
                              print("i =" ,i, "j =" , j)
                 58.  What are the supported data types in Python?
                Ans.  Python has five standard data types—
                      (a)  Numbers                      (b)  String
                      (c)  List                         (d)  Tuple
                      (e)  Dictionary
                 59.  Is Python an object-oriented programming language?
                Ans.  Yes, Python is an object-oriented programming language.
                 60.  State the difference between a runtime error and a logical error.
                Ans.  A runtime error typically generates an exception or terminates a program when a program is syntactically
                    correct but encounters an error during execution. For example, dividing a number by zero—the program
                    is doing something that is undefined.
                      A logical error is caused by a mistake in the program’s logic. For example, using integer division instead of
                    floating-point division.
                 61.  What is the purpose of using pass statement in Python?
                Ans.  A pass statement in Python does nothing. It is ignored by the interpreter. Thus, it is a dummy statement. It
                    is often used to leave a slot for the code to be filled in later. Sometimes, it is also used to simplify program
                    logic when no action is required for a particular condition.
                 62.  What is an empty string?
                Ans.  An empty string contains no characters and has a length of 0.
                 63.  Is string a sequence data type?
                Ans.  Yes, string is a sequence data type in Python.
                 64.  What is slicing in Python?
                Ans.  A mechanism to select a range of items from sequence types like list, tuple, strings, etc. is known as slicing.
                      For example,
                     >>>Str="Message"
                     >>>Str[2:5]
                         'ssa'
                 65.  What is negative index in Python?
                Ans.  Python sequences can be indexed in positive and negative numbers. For positive index (from left to right),
                    0 is the first index, 1 is the second index, and so on. For negative index (from right to left), –1 is the last
                    index, –2 is the second last index, and so on.
                 66.  State the purpose of range() method in Python.
                Ans.  The range() method is used to generate sequence of values within the specified start and stop value, where
                    start value (by default as 1) is included but stop value (by default till last) is excluded with step value by
                    default as 1 other than specified by the user. It is always used with for loop for generating an iterable
                    sequence for looping.



                                                           V.5
   42   43   44   45   46   47   48   49   50   51   52