Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 155 - 2607_Traclpad_Pro_5.1_flipbook_C8
P. 155

    total += num
                      print(total)

                      Now, answer the following questions:
                      a. What is the value of the total after the loop finishes?
                      b. What would happen if the list numbers contained negative values?
                      c. Modify the code so that it prints the average of the numbers in the list.

                  2.  Study the given code:
                      num = 10
                      while num > 0:
                          print(num)
                          num -= 2
                      Now, answer the following questions:

                      a. What will be the first and last numbers printed by this code?
                      b. How many times will the loop execute?
                      c. Modify the code to count down by 1 instead of 2.
                  3.  Write the output of the following Python code:

                      for i in “Computer Education”:
                          print(i, “$”)
                  4.  In a program  that  checks  for even numbers,  if an odd  number  is  encountered,  you  want to
                      immediately skip to the next number without further checks. Which jump statement would you
                      use, and how would you structure the program?
                  5.  What is the output of the following code?

                      while 3 >= 3:
                          print 3
                  6.  Rewrite the following code snippet using the for loop:
                      i = 100

                      while (i > 0):
                          print i
                          i -= 3

                  Chapter 6: Functions, String and List in Python

                  1.  You have a list of numbers: [10, 20, 30, 40, 50].

                      Write a Python program that adds the number 60 to the list using the append() function. How
                      does the append() function work?
                  2.  Imagine you have a list of numbers [5, 10, 15].

                      Write a Python program to add the numbers [20, 25] to this list using the extend() function. What
                      is the difference between append() and extend()?

                  3.  The list contains the words “apple”, “banana”, and “mango”.
                      Write a Python program to delete the fruit “banana” from the list using the del() function. How
                      does the del() function help in managing the list?

                      154   Premium Edition-VIII

| 150 151 152 153 154 155 156 157 158 159 160 |