{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Question 31\n", "\n", "### **Question:**\n", "\n", "> **_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._**\n", "\n", "---\n", "\n", "### Hints:\n", "\n", "\n", "Use dict[key]=value pattern to put entry into a dictionary.Use ** operator to get power of a number.Use range() for loops.\n", "\n", "\n", "---\n", "\n", "\n", "\n", "**Solutions:**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def printDict():\n", " dict = {i: i ** 2 for i in range(1, 21)} # Using comprehension method and\n", " print(dict)\n", "\n", "\n", "printDict()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "# Question 32\n", "\n", "### **Question:**\n", "\n", "> **_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._**\n", "\n", "---\n", "\n", "### Hints:\n", "\n", "\n", "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.\n", "\n", "\n", "---\n", "\n", "\n", "\n", "**Solutions:**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def printDict():\n", " dict = {i: i ** 2 for i in range(1, 21)}\n", " print(dict.keys()) # print keys of a dictionary\n", "\n", "\n", "printDict()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "# Question 33\n", "\n", "### **Question:**\n", "\n", "> **_Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included)._**\n", "\n", "---\n", "\n", "### Hints:\n", "\n", "\n", "Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.\n", "\n", "\n", "---\n", "\n", "\n", "\n", "**Solutions:**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def printList():\n", " lst = [i ** 2 for i in range(1, 21)]\n", " print(lst)\n", "\n", "\n", "printList()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "# Question 34\n", "\n", "### **Question:**\n", "\n", "> **_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._**\n", "\n", "---\n", "\n", "### Hints:\n", "\n", "\n", "Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use [n1:n2] to slice a list\n", "\n", "\n", "---\n", "\n", "\n", "\n", "**Solutions:**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def printList():\n", " lst = [i ** 2 for i in range(1, 21)]\n", "\n", " for i in range(5):\n", " print(lst[i])\n", "\n", "\n", "printList()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n", "16\n", "25\n" ] } ], "source": [ "\"\"\"Solution by: yuan1z && edited by: apurvmishra99\"\"\"\n", "func = lambda: ([i ** 2 for i in range(1, 21)][:5])\n", "print(*(func()), sep=\"\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "# Question 35\n", "\n", "### **Question:**\n", "\n", "> **_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._**\n", "\n", "---\n", "\n", "### Hints:\n", "\n", "\n", "Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use [n1:n2] to slice a list\n", "\n", "\n", "---\n", "\n", "\n", "\n", "**Solutions:**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def printList():\n", " lst = [i ** 2 for i in range(1, 21)]\n", " for i in range(19, 14, -1):\n", " print(lst[i])\n", "\n", "\n", "printList()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "# Question 36\n", "\n", "### **Question:**\n", "\n", "> **_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 all values except the first 5 elements in the list._**\n", "\n", "---\n", "\n", "\n", "Hints: Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use [n1:n2] to slice a list\n", "\n", "\n", "---\n", "\n", "\n", "\n", "**Solutions:**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def printList():\n", " lst = [i ** 2 for i in range(1, 21)]\n", " for i in range(5, 20):\n", " print(lst[i])\n", "\n", "\n", "printList()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "# Question 37\n", "\n", "### **Question:**\n", "\n", "> **_Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included)._**\n", "\n", "---\n", "\n", "### Hints:\n", "\n", "\n", "Use ** operator to get power of a number.Use range() for loops.Use list.append() to add values into a list.Use tuple() to get a tuple from a list.\n", "\n", "\n", "---\n", "\n", "\n", "\n", "**Solutions:**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def printTupple():\n", " lst = [i ** 2 for i in range(1, 21)]\n", " print(tuple(lst))\n", "\n", "\n", "printTupple()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", "Solution by: Seawolf159\n", "\"\"\"\n", "\n", "\n", "def square_of_numbers():\n", " return tuple(i ** 2 for i in range(1, 21))\n", "\n", "\n", "print(square_of_numbers())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "### Comment\n", "\n", "**_Problems of this section is very much easy and all of those are of a modification of same type problem which mainly focused on using some commonly used function works with list,dictionary, tupple.In my entire solutions, I havn't tried to solve problems in efficient way.Rather I tried to solve in a different way that I can.This will help a beginner to know how simplest problems can be solved in different ways._**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }