diff --git a/Lab3_Notebook.ipynb b/Lab3_Notebook.ipynb new file mode 100644 index 0000000..51b0b98 --- /dev/null +++ b/Lab3_Notebook.ipynb @@ -0,0 +1,808 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Student Name:Chaitanya Kovid Tanikella \n", + "# Enrollment Number: E18ECE012\n", + "# Lab assignemnt number:3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Lab 3: Arithmetic Expressions, Operators" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Compulasory Questions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1 Write a program in Python that takes a length of one side of square (floating-point value) as input and prints the area and perimeter of the square." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter length of side :5\n", + "perimeter of square is : 20.0\n", + "area of square is : 25.0\n" + ] + } + ], + "source": [ + "#write your code here\n", + "n=float(input(\"Enter length of side :\"))\n", + "print(\"perimeter of square is :\",4*n)\n", + "print(\"area of square is :\",n*n)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2\tWrite a program in Python to take input length and breadth of a rectangle (floating-point values) and print the area and perimeter of the rectangle." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter length of rectangle :3\n", + "Enter breadth of rectangle :6\n", + "perimeter of rectangle is : 18.0\n", + "area of rectangle is : 18.0\n" + ] + } + ], + "source": [ + "#write your code here\n", + "l=float(input(\"Enter length of rectangle :\"))\n", + "b=float(input(\"Enter breadth of rectangle :\"))\n", + "print(\"perimeter of rectangle is :\",2*(l+b))\n", + "print(\"area of rectangle is :\",l*b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3 Write a program in Python which converts 8 hours and 32 minutes into seconds." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30720\n" + ] + } + ], + "source": [ + "hours=8\n", + "minutes=32\n", + "seconds= (hours*60*60)+(minutes*60)\n", + "print(seconds)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 4 Write a program in Python which inputs time in hours and minutes and converts it into seconds." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hours :7\n", + "Minutes :44\n", + "Number of seconds in 7 Hours and 44 minutes is : 27840\n" + ] + } + ], + "source": [ + "#write your code here\n", + "h=int(input(\"Hours :\"))\n", + "m=int(input(\"Minutes :\"))\n", + "print(\"Number of seconds in \",h,\" Hours and \",m,\" minutes is :\",(h*60*60)+(m*60))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 5\tWrite a program in Python to convert temperature in degrees Celcius to degrees Fahrenheit. The formula is F = (temp * 9/5) +32 . For temp=40 ᵒC, the output should be 104 ᵒF . Try for other different values of temp." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "temperature in ᵒC 40\n", + "40.0 ᵒC = 104.0 ᵒF\n" + ] + } + ], + "source": [ + "temp_in_deg=float(input('temperature in ᵒC '))\n", + "#write your code here\n", + "temp=(temp_in_deg * 9/5) +32\n", + "print(temp_in_deg,'ᵒC = ',temp,'ᵒF')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 6 Execute the following program in python to perform assignment operations and write comments on each line as per your understanding" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "21\n", + "11\n", + "44\n" + ] + } + ], + "source": [ + "x=20\n", + "#x=x+1\n", + "x+=1\n", + "print(x)\n", + "#x=x-10\n", + "x-=10\n", + "print(x)\n", + "#x=x*4\n", + "x*=4\n", + "print(x)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 7 Execute the following program in python to perform assignment operations and write comments on each line as per your understanding." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6.666666666666667\n", + "0.666666666666667\n", + "0.0\n", + "4\n", + "4\n" + ] + } + ], + "source": [ + "x=20\n", + "y=3\n", + "#x=x/y\n", + "x/=y\n", + "print(x)\n", + "#x=x%y\n", + "x%=y\n", + "print(x)\n", + "#x=x//y\n", + "x//=y\n", + "print(x)\n", + "x=6\n", + "y=4\n", + "#x=x&y\n", + "x&=y\n", + "print(x)\n", + "#x=x|y\n", + "x|=y\n", + "print(x)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 8 Execute the following program in python to perform following relational operations and write comments on each line as per your understanding. " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n", + "False\n", + "True\n" + ] + } + ], + "source": [ + "x=\"happy\"\n", + "y=\"had\"\n", + "print(xy) #compares the two values and returns true when initial one is greater than the latter\n", + "print(x==y) #compares the two values and returns true when initial one is equal to the latter\n", + "print(x!=y) #compares the two values and returns true when initial one is not equal to the latter" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 9 Execute the following program in python to perform following relational operations and write comments on each line as per your understanding." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "False\n", + "False\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "print(1<2) # 1 is smaller and hence statement true\n", + "print(4>10) # 10 is greater than 4 and hence statement false\n", + "print(6==8) # 6 and 8 are not equal and henc statement false\n", + "print(2>10) # 10 is greater than 2 and hence statement false\n", + "print(6<8) #8 is greater than 6 and hence statement true\n", + "print(6>8) #8 is greater than 6 and hence statement false" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 10 Execute the following program in python to perform following logical AND OR NOT operations and write comments on each line as per your understanding." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "False\n", + "False\n", + "False\n", + "True\n", + "True\n", + "True\n" + ] + } + ], + "source": [ + "print(1<2 and 4>10) #Only one statement is true, hence output is false \n", + "print(6==8 or 2>10) #Both stements are false, hence output is false \n", + "print(6<8 and 6>8) #Only one statement is true, hence output is false \n", + "print(not(1)) #Not true is false, hence output is false \n", + "print(not(1==2)) #result of comparison is false and not false is true, hence output is true\n", + "print(not(4>7 and 7>8)) #Both stements are false and not false is true, hence output is true\n", + "print(not(4>7 or 7>8)) #Both stements are false and not false is true, hence output is true" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 11\tWrite a program in Python that inputs data (class, age, marks in mathematics, science) about two persons- Ram and Shyam. The program uses relational operators to check whether they study in same class? Are Ram Shyam of same age? Is Ram’s marks in mathematics more than Shyam? Is Ram’s marks in science less than Ram? The answer by the program is either True or False. The operators to be used are: <, >, ==, !=. " + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ram's classX\n", + "Shyam's classX\n", + "Ram's age12\n", + "Shyam's age12\n", + "Ram's maths marks28\n", + "Shyam's maths marks99\n", + "Ram's science marks100\n", + "Shyam's science marks0\n", + "Both are in same class ? : True\n", + "Both are of same age ? : True\n", + "Ram’s marks in mathematics are more than Shyam : False\n", + "Ram’s marks in science are less than Shyam : False\n" + ] + } + ], + "source": [ + "R_class=str(input(\"Ram's class\"))\n", + "S_class=str(input(\"Shyam's class\"))\n", + "\n", + "R_age=int(input(\"Ram's age\"))\n", + "S_age=int(input(\"Shyam's age\"))\n", + "\n", + "R_math=float(input(\"Ram's maths marks\"))\n", + "S_math=float(input(\"Shyam's maths marks\"))\n", + "\n", + "R_sci=float(input(\"Ram's science marks\"))\n", + "S_sci=float(input(\"Shyam's science marks\"))\n", + "\n", + "print('Both are in same class ? :',R_class==S_class)\n", + "#write your code here\n", + "print('Both are of same age ? :',R_age==S_age)\n", + "print(\"Ram’s marks in mathematics are more than Shyam :\",R_math>S_math)\n", + "print(\"Ram’s marks in science are less than Shyam :\",R_sci>1 will give 100 i.e 4 after shifting bits by 1 place to right. \n", + " n<<1 will give 10000 i.e. 16 after shifting bits by 1 place to left." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8 binary 1000\n", + "8 * 2= 16\n", + "8 / 2= 4\n", + "for n1=4\n", + "4 binary 1000\n", + "4 * 2= 16\n", + "4 / 2= 4\n" + ] + } + ], + "source": [ + "n=8\n", + "print(n,'binary',format(n,'b'))\n", + "print(n,'* 2=',n<<1)\n", + "print(n,'/ 2=',n>>1)\n", + "n1=4\n", + "print(\"for n1=4\")\n", + "#write your code here\n", + "print(n1,'binary',format(n,'b'))\n", + "print(n1,'* 2=',n<<1)\n", + "print(n1,'/ 2=',n>>1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 16\tWrite a program in Python to multiply and divide by multiple powers of two using bitwise shift operators. Observe the given example for n=8,k=3. Write code for m=4, k1=2 and execute. " + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Input\n", + " n=8\n", + " k=3\n", + "Output\n", + " Value of 8 after multiplying by 2 3 times = 8*8=64\n", + " Value of 8 after dividing by 2 3 times = 8/8=1" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Value of 8 after multiplying by 2 3 times = 64\n", + "Value of 8 after dividing by 2 3 times = 1\n", + "Value of 8 after multiplying by 2 3 times = 16\n", + "Value of 8 after dividing by 2 3 times = 1\n" + ] + } + ], + "source": [ + "n=8\n", + "k=3\n", + "#write your code here\n", + "n1= n<