美文网首页
Python-31~35

Python-31~35

作者: AoEliauk | 来源:发表于2020-09-21 14:51 被阅读0次

    ---31---

    Question:

    >Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys.

    Hints:

    >Use dict[key]=value pattern to put entry into a dictionary.Use ** operator to get power of a number.Use range() for loops.

    Solution:

    解法一

    解法二

    输出结果


    ---32---

    Question:

    >Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only.

    Hints:

    >Use dict[key]=value pattern to put entry into a dictionary.Use ** operator to get power of a number.Use range() for loops.Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.

    Solution:


    ---33---

     Question:

    >Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).

    Solution:


    ---34---

    Question:

    >Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.

    Solution:


    ---35---

    Question:

    >Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list.

    Solution:

    相关文章

      网友评论

          本文标题:Python-31~35

          本文链接:https://www.haomeiwen.com/subject/swndyktx.html