CBSE class 8 Python list loop functions solved exercise question answer worksheet





1





CBSE class 8 Python list loop functions solved exercise question answer worksheet

Select the correct option. 



Items in a list with five elements will have index from _______. 

i. 1 to 4         ii. 1 to 5           iii. 0 to 5          iv. 0 to 4



Ans:

0 to 4



The method used to remove an item from a list is _________. 

i. pop()          ii. remove()   iii. both i) and ii)     iv. none of the above


Ans:

remove()   



Each execution of a loop is called _________. 

i. cycle         ii. iteration        iii. phase             iv. none of the above


Ans:

iteration



The _________ statement is used to go ahead to the next iteration without executing the remaining statements in the loop 

i. continue     ii. break              iii. Else              iv. stop


Ans:

continue




2.


What will be the output of the following code? 




Ans:

96








Ans:

1

4

9









Ans:

>




3.


Answer the following


a.

What is the difference between a ‘while’ and a ‘for’ loop? 


Ans:

  • The ‘for’ loop in Python is used to iterate over a sequence, such as a list or a string.

  • A ‘for’ loop in Python can also have an optional ‘else’ block. The ‘else’ block is executed when the sequence of items used in the ‘for’ loop gets over. A break statement can be used to exit a ‘for’ before it ends. In such a case, the program ignores the ‘else’ part.

  • The ‘while’ loop in Python is used to execute a block of code as long as the test expression (condition) is true. 

  • We generally use this loop when we do not know how many times the loop will execute in advance.

  • You can use an optional ‘else’ block inside a while loop as well. The ‘else’ part will be executed when the condition in the ‘while’ loop becomes False. Similar to the ‘for’ loop, to exit a ‘while’ loop you can use a break statement. In that case, the ‘else’ part is ignored.





b.

What are parameters? What is the purpose of using a parameter? 


Ans:

  • A function can accept input data to work on. These are called parameters. For example, int() takes a string value as a parameter. 

  • Parameters are specified after the function name, inside the brackets. You can add more than one parameter by separating them with commas.

  • When a function is called, you need to pass the values of the parameters to the function. For example, if you define a function to add two integers as follows.





c.

Ishaan wants to print the multiplication table using a loop. Which loop will he select for the same and why? 


Ans:

The for loop will repeat for a fixed number of times decided by a counter variable. This variable is incremented or decremented after each iteration of the loop and when it reaches a certain value, it will stop.





d.

What is the use of functions in a program? 


Ans:

  • Functions help in dividing your program into smaller modules. As a program grows bigger and bigger, functions help to make it more organised and easy to manage. 

  • Functions also make it easy to identify and correct errors in a program. 

  • It helps avoid repetition of the same code and also makes the code reusable. 




>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


Comments

Popular posts from this blog