Exercises

Unless specified otherwise, use IPython sessions for each exercise.

  1. 4.1 (Discussion: else Clause) In the script of Fig4.1. , we did not include an else clause in the ifelif statement. What are the possible consequences of this choice?

  2. 4.2 (Discussion: Function-Call Stack) What happens if you keep pushing onto a stack, without enough popping?

  3. 4.3 (What’s Wrong with This Code?) What is wrong with the following cube function’s definition?

    def cube(x):
        """Calculate the cube of x."""
        x ** 3
    print('The cube of 2 is', cube(2))
    
  1. 4.4 (What’s Does This Code Do?) What does the following mystery function do? Assume you pass the list [1, 2, 3, 4, 5] as an argument.

    def mystery(x):
        y = 0
        for value in x:
            y += value ** 2
        return y
    
  1. 4.5 (Fill ...

Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.