官术网_书友最值得收藏!

Iterative Functions

In the For Loops section in Chapter 1, Vital Python – Math, Strings, Conditionals, and Loops, you were introduced to the syntax for looping over objects in Python. As a refresher, here is an example where you perform five iterations and print the i variable in each loop:

for i in range(5):

    print(i)

You should get the following output:

0

1

2

3

4

For loops can also be placed within functions.

Exercise 48: A Simple Function with a for Loop

In this exercise, you create a sum_first_n function that sums up the first n integers. For example, if you pass the n=3 function, it should return 1 + 2 + 3 = 6:

  1. In a Python shell, enter the function definition. Note that the tab spacing needs to match the following output:

    def sum_first_n(n):

        result = 0

        for i in range(n):

            result += i + 1

        return result

       

  2. Test the sum_first_n function on an example:

    sum_first_n(100)

    You should get the following output:

    5050

In this exercise, you successfully implemented a simple sum_first_n function with a for loop to find the total sum of n numbers.

Exiting Early

You can exit the function at any point during the iterations. For instance, you might want the function to return a value once a certain condition is met.

Exercise 49: Exiting the Function During the for Loop

In this exercise, you will create a function that (inefficiently) checks whether a certain number x is a prime. The function does this by looping through all the numbers from 2 to x and checks whether x is divisible by it. If it finds a number that x is divisible by, the iteration will stop and return False, as it has ascertained that x is not prime:

  1. In a Python shell, enter the function definition. Note that the tab spacing needs to match the following output:

    def is_prime(x):

        for i in range(2, x):

            if (x % i) == 0:

            return False

        return True

  2. Test the function on a couple of examples:

    is_prime(7)

    You should get the following output:

    True

    Now, find out if 1000 is a prime number or not.

    is_prime(1000)

    You should get the following output:

    False

In this exercise, you successfully implemented a code that checks whether the x variable is prime by looping through numbers. In the case that it is divisible, it will exit the loop and provide the output as False.

Activity 10: The Fibonacci Function with an Iteration

You work in an IT firm, and your colleague has realized that being able to quickly compute elements of the Fibonacci sequence will reduce the time taken to execute the testing suite on one of your internal applications. You will use an iterative approach to create a fibonacci_iterative function that returns the nth value in the Fibonacci sequence.

The steps are as follows:

  1. Create a fibonacci.py file.
  2. Define a fibonacci_iterative function that takes a single positional argument representing which number term in the sequence you want to return.
  3. Run the following code:

    from fibonacci import fibonacci_iterative

    fibonacci_iterative(3)

    You should get the following output:

    2

    Another example to test your code can be as mentioned in the following code snippet:

    fibonacci_iterative(10)

    You should get the following output:

    55

    Note

    The solution for this activity is available on page 530.

主站蜘蛛池模板: 武宁县| 佛山市| 通城县| 秦安县| 苏尼特左旗| 岚皋县| 无锡市| 应城市| 泰安市| 达孜县| 金华市| 西峡县| 迁安市| 玉树县| 通江县| 张掖市| 静宁县| 鹤壁市| 兴海县| 固始县| 花莲县| 论坛| 兰考县| 镇巴县| 张北县| 临海市| 齐齐哈尔市| 竹北市| 平和县| 从江县| 定边县| 来宾市| 金溪县| 桃园市| 铜梁县| 平果县| 襄城县| 翁牛特旗| 阿克苏市| 汉源县| 延庆县|