Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

{

"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter value of x: 3\n",
"Enter value of y: 3\n",
"[[0, 0, 0], [0, 0, 0], [0, 0, 0]]\n",
"[[1, 0, 0], [1, 1, 0], [1, 0, 1]]\n",
"('Index:', 0, 0)\n",
"('Index:', 1, 0)\n",
"('Index:', 1, 1)\n",
"('Index:', 2, 0)\n",
"('Index:', 2, 2)\n"
]
}
],
"source": [
"#Youtube Link : https://youtu.be/iqo0faRlgt4\n",
"#Chapter 3 Assignment\n",
"\n",
"#Vatsal Patel (186383)\n",
"\n",
"#CISC 504-90- O-2020/Late Summer - Principles of Programming Language\n",
"\n",
"# taking x value from the user\n",
"x = int(input('Enter value of x: '))\n",
"# taking y value from the user\n",
"y = int(input('Enter value of y: '))\n",
"\n",
"# Task1\n",
"# Goal of the Task1 is to return a list that contain x number of nested lists
and each nested list contain y number of 0's \n",
"\n",
"# function declaration that take two parameters\n",
"def task1_function(x, y):\n",
"\t# empty list declaration\n",
"\tnested_list = []\n",
"\t\n",
"\t# using for loop in range 0 to x\n",
"\tfor i in range(x):\n",
"\t\t# empty list declaration\n",
"\t\tinner_list = []\n",
"\t\t# using for loop in range 0 to y\n",
"\t\tfor j in range(y):\n",
"\t\t\t# appending 0 to the inner_list\n",
"\t\t\tinner_list.append(0)\n",
"\t\t# appending inner_list to the nested_list\n",
"\t\tnested_list.append(inner_list)\n",
"\t# returning nested_list\n",
"\treturn nested_list\n",
"\n",
"\n",
"# calling the task1_function with arguments x and y\n",
"# and storing the result in nested_list_1 \n",
"nested_list_1 = task1_function(x, y)\n",
"# printing the nested_list_1\n",
"print(nested_list_1)\n",
"\n",
"# Task2\n",
"\n",
"\n",
"# Goal of Task2 is to return a list that contain x number of nested lists and
each nested list contain y number of elements, elements are 1's and 0's \n",
"# depending on the given condition '''\n",
"\n",
"\n",
"# function declaration that take two parameters\n",
"def task2_function(x, y):\n",
"\t# emptylist declaration\n",
"\tnested_list_2 = []\n",
"\t# using for loop in range 0 to x\n",
"\tfor i in range(x):\n",
"\t\t# empty list declaration\n",
"\t\tinner_list_2 = []\n",
"\t\t# using for loop in range 0 to y\n",
"\t\tfor j in range(y):\n",
"\t\t\t# checking if condition\n",
"\t\t\tif (i+1) % (j+1) == 0:\n",
"\t\t\t\t# appending 1 to the inner_list_2 if above condition is true\n",
"\t\t\t\tinner_list_2.append(1)\n",
"\t\t\telse:\n",
"\t\t\t\t# appending 0 to the inner_list_2 if above condition is false\n",
"\t\t\t\tinner_list_2.append(0)\n",
"\t\t# appending inner_list_2 to the nested_list_2\n",
"\t\tnested_list_2.append(inner_list_2)\n",
"\t# returning the nested_list_2\n",
"\treturn nested_list_2\n",
"\n",
"\n",
"# calling the task2_function with arguments x and y\n",
"# and storing the result in nested_list_2\n",
"nested_list_2 = task2_function(x, y)\n",
"# printing the nested_list_2\n",
"print(nested_list_2)\n",
"\n",
"# Task3\n",
"\n",
"\n",
"# function declartion with two parameters\n",
"def task3_function(x, y):\n",
"\t# using for loop in the range 0 to x\n",
"\tfor i in range(x):\n",
"\t\t# using for loop in the range o to y\n",
"\t\tfor j in range(y):\n",
"\t\t\t# checking if condition \n",
"\t\t\tif nested_list_2[i][j] == 1:\n",
"\t\t\t\t# printing the i and j values if above condition is true\n",
"\t\t\t\tprint('Index:',i,j)\n",
"\n",
"\n",
"# calling the task3_function with arguments x and y\n",
"task3_function(x, y)\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"#2) Surface Area of sphere is\n",
"\n",
"# A = 4 π r **2\n",
"\n",
"#Convert this into function and pass any value of radius to display area of
sphere\n",
"\n",
"#Code\n",
"import math # maths library to use the value of PI\n",
"\n",
"def CalculateSurfaceArea(Radius): #function to calculate surface area of
sphere\n",
" A = 4 * math.pi * Radius * Radius # calculate the surface are using
formula\n",
" print(\"The Surface Area of the Sphere is : \",A) # print the surface
area\n",
"\n",
"\n",
"Rad = float(input(\"Enter the Radius of the Sphere : \")) # take the radius
from user\n",
"CalculateSurfaceArea(Rad)#calling of the function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

You might also like