{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Python-Quiz-1.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyMOS4djbDANTtS9pwz7pBQW",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "A1rBu0k2lZqV",
"colab_type": "text"
},
"source": [
"# **Python Basics - Quiz-1**\n",
"\n",
"## **Use this to enhance to your python skill**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zIhaMRYnfoTW",
"colab_type": "text"
},
"source": [
"### **1) What is the output of the below code**\n",
"1. 22.5\n",
"2. 23\n",
"3. 26\n",
"4. 26.5"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ChFMY92Wfl_v",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
},
"outputId": "6eb80564-fe01-41c8-ac8e-08bdf8e0a5ac"
},
"source": [
"'''\n",
"Program: 1\n",
"Description: Calling the addition function multiple times within the same function.\n",
" The function add just takes two inputs from the user, adds them and returns it.\n",
"Author: Tanu Nanda Prabhu\n",
"'''\n",
"\n",
"def add(a, b): # Function declaration to add to numbers\n",
" c = a + b # Adds two number and stores them in variable c\n",
" return c # Returns the result \n",
"\n",
"# Calling the function by passing the input parameters.\n",
"result = add(add(add(5, add(5, 6.5)), 5), 5)\n",
"print(\"---------------------------------------------\")\n",
"print(\"The final result is:\", result)\n",
"print(\"---------------------------------------------\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"---------------------------------------------\n",
"The final result is: 26.5\n",
"---------------------------------------------\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "57caIUqLiU7x",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n",
"\n",
"\n",
"\n",
"### **2) What is the output of the below code**\n",
"1. 16\n",
"2. `List index out of range`\n",
"3. 4\n",
"4. 20"
]
},
{
"cell_type": "code",
"metadata": {
"id": "K-7290uNfvgp",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
},
"outputId": "23f46944-f038-4375-d501-21eccd092ec7"
},
"source": [
"'''\n",
"Program: 2\n",
"Description: Lambdas defined in a loop with different values all return the same result.\n",
"\n",
"Author: Tanu Nanda Prabhu\n",
"'''\n",
"\n",
"squares = [] # Creating an empty list \"squares\"\n",
"for x in range(5):\n",
" squares.append(lambda n=x: n**2) # Lambda function, surf about it on the internet.\n",
"\n",
"print(\"---------------------------------------------\")\n",
"print(squares[4]())\n",
"print(\"---------------------------------------------\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"---------------------------------------------\n",
"16\n",
"---------------------------------------------\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4v_BZuq2mK2f",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n",
"\n",
"\n",
"### **3) What is the output of the below code**\n",
"1. (1, 2, 3)\n",
"2. [1, 2, 3]\n",
"3. 123\n",
"4. 1, 2, 3"
]
},
{
"cell_type": "code",
"metadata": {
"id": "rVrE-rqWhIqj",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
},
"outputId": "ae0023fd-7189-4f0b-c373-387f9535341c"
},
"source": [
"'''\n",
"Program: 3\n",
"Description: String format() is the primary API method. \n",
" It takes a string and an arbitrary set of positional and keyword arguments. \n",
"\n",
"Author: Tanu Nanda Prabhu\n",
"'''\n",
"\n",
"print(\"---------------------------------------------\")\n",
"print(\"[{0},{1},{2}]\".format(1, 2, 3)) # Using the format() to format the string \n",
"print(\"---------------------------------------------\")\n"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"---------------------------------------------\n",
"[1,2,3]\n",
"---------------------------------------------\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Znxr4-xwAuJe",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n",
"\n",
"\n",
"### **4) What would be type of `updated_name` in this example**\n",
"1. string\n",
"2. list\n",
"3. TypeError \n",
"4. tuple"
]
},
{
"cell_type": "code",
"metadata": {
"id": "khjbedjk_4Nf",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
},
"outputId": "404c760a-3219-4a9f-edac-a7762d6028af"
},
"source": [
"'''\n",
"Program: 4\n",
"Description: String split() is the primary API method. \n",
" The split() method splits a string.\n",
" You can specify the separator, default separator is any whitespace \n",
"\n",
"Author: Tanu Nanda Prabhu\n",
"'''\n",
"\n",
"name = 'P Y T H O N'\n",
"print(\"---------------------------------------------\")\n",
"updated_name = name.split() # Splitting the string 'P Y T H O N'\n",
"print(type(updated_name))\n",
"print(\"---------------------------------------------\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"---------------------------------------------\n",
"\n",
"---------------------------------------------\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IC0WwbSqWMUN",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n",
"\n",
"\n",
"### **5) What is the output of the following code:**\n",
"1. P Y T H O N\n",
"2. N O H T Y P\n",
"3. P Y T H O \n",
"4. Invalid Syntax"
]
},
{
"cell_type": "code",
"metadata": {
"id": "1qql8iWFWA9U",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
},
"outputId": "cc5e3a36-e19d-4c80-b9c5-2181ec64a12c"
},
"source": [
"'''\n",
"Program: 5\n",
"Description: String slice is the primary API. \n",
" The string slice actually slices the string.\n",
" Usually represented in square brackets like []\n",
"Author: Tanu Nanda Prabhu\n",
"'''\n",
"\n",
"name = 'P Y T H O N'\n",
"print(\"---------------------------------------------\")\n",
"updated_name = name[::-1] # Slicing the string 'P Y T H O N'\n",
"print(updated_name)\n",
"print(\"---------------------------------------------\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"---------------------------------------------\n",
"N O H T Y P\n",
"---------------------------------------------\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "JKC6bmNOWc9x",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n",
"\n",
"### **6) What is the output of the following code:**\n",
"1. 15, 16, 17, 18, 19, \n",
"2. 15, 16, 17, 18, 19, 20\n",
"3. 15, 16, 17, 18, 19\n",
"4. 15, 16, 17, 18, \n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Fq8Ep6s1WQw4",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
},
"outputId": "bd520af2-3b83-4acd-abc2-8ff0ef647c1b"
},
"source": [
"'''\n",
"Program: 6\n",
"Description: Just playing with 'for' loops usign 'end'\n",
"Author: Tanu Nanda Prabhu\n",
"'''\n",
"\n",
"print(\"---------------------------------------------\")\n",
"for i in range(15, 20, 1):\n",
" print( i, end = ', ') # Playing with the 'for' loop with 'end'\n",
"\n",
"print(\"\\n---------------------------------------------\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"---------------------------------------------\n",
"15, 16, 17, 18, 19, \n",
"---------------------------------------------\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KR2EHAKt4SFa",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n",
"### **7) What is the output of the following code:**\n",
"1. [25, 10] \n",
"2. [25, 15, 10, 5]\n",
"3. [ ]\n",
"4. `ValueError` "
]
},
{
"cell_type": "code",
"metadata": {
"id": "kFpcwcfG4Qh_",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
},
"outputId": "e09fc168-e172-4091-d714-feefb40c627c"
},
"source": [
"'''\n",
"Program: 7\n",
"Description: List slicing: Used to slice the list based on the value,\n",
" provided inside the square brackets\n",
"Author: Tanu Nanda Prabhu\n",
"'''\n",
"\n",
"print(\"---------------------------------------------\")\n",
"new_list = [5, 10, 15, 25]\n",
"print(new_list[::-2]) # Using the concept of list slicing\n",
"print(\"---------------------------------------------\")\n"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"---------------------------------------------\n",
"[25, 10]\n",
"---------------------------------------------\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QFQQXyMl2POE",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n",
"### **8) What is the output of the following code:**\n",
"1. ['Hello Dear', 'Hello Bye', 'GoodDear', 'GoodBye'] \n",
"2. ['Hello Dear', 'Hello Bye', 'GoodDear']\n",
"3. [ ]\n",
"4. ['Hello Good', Dear Bye'] "
]
},
{
"cell_type": "code",
"metadata": {
"id": "yVriPDtj2h3H",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
},
"outputId": "27a0bdff-2a3b-4b06-ed90-2ab4a49451a6"
},
"source": [
"'''\n",
"Program: 8\n",
"Description: Concatenating values in a list using '+'\n",
"Author: Tanu Nanda Prabhu\n",
"'''\n",
"\n",
"print(\"---------------------------------------------\")\n",
"List = [x + y for x in ['Hello ', 'Good'] for y in ['Dear', 'Bye']]\n",
"print(List)\n",
"print(\"---------------------------------------------\")"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"---------------------------------------------\n",
"['Hello Dear', 'Hello Bye', 'GoodDear', 'GoodBye']\n",
"---------------------------------------------\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CJN8EPlWDF6E",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n",
"### **9) What is the output of the following code:**\n",
" 1. PythonProgramming-\n",
" 2. Python-Programming\n",
" 3. -PythonProgramming\n",
" 4. PythonProgramming"
]
},
{
"cell_type": "code",
"metadata": {
"id": "TheQClU1DP9C",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
},
"outputId": "ca2a71dd-e3b9-4810-88ac-e443d84d5d95"
},
"source": [
"'''\n",
"Program: 9\n",
"Description: Joining the value of the list using 'join()'\n",
"Author: Tanu Nanda Prabhu\n",
"'''\n",
"\n",
"List = [\"Python\", \"Programming\"]\n",
"print(\"---------------------------------------------\")\n",
"print(\"-\".join(List)) # Using join() to join the values in the list\n",
"print(\"---------------------------------------------\")\n"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"---------------------------------------------\n",
"Python-Programming\n",
"---------------------------------------------\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TGIa7sXNqDqx",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n",
"### **10) What is the output of the following code:**\n",
" 1. True\n",
" 2. False\n",
" 3. Unknown\n",
" 4. `TypeError`"
]
},
{
"cell_type": "code",
"metadata": {
"id": "OWRBxmsaoosf",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "878d1753-e68a-4b81-d1b0-7287e440b29e"
},
"source": [
"'''\n",
"Program: 10\n",
"Description: Converting values into 'integer' and 'round' at the same time\n",
"Author: Tanu Nanda Prabhu\n",
"'''\n",
"\n",
"a = 5 # Integer values\n",
"b = 3\n",
"\n",
"c1 = a/b\n",
"c1 = int(c1) # Type casting to integer\n",
"\n",
"c2 = a/b\n",
"c2 = round(c2) # Rounding the value\n",
"\n",
"if c1 == c2:\n",
" print(\"True\")\n",
"else:\n",
" print(\"False\")"
],
"execution_count": 18,
"outputs": [
{
"output_type": "stream",
"text": [
"False\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BPqBRMiTq7ZV",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n",
"# **Thankyou**\n",
"\n",
"I hope you solved all the 10 questions given above. Stay tuned to solve more. \n",
"\n",
"\n"
]
}
]
}