Assignment - 1.ipynb - Colaboratory

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

07/09/2021 19121015_Assignment_1.

ipynb - Colaboratory

1.While solving mathematical expression using python, order of precedence must be carefully
followed.

5/10
# / is division operators

0.5

10/5

2.0

5.0/10
# as one value is float and other is integer so by default the resultant value is fload by ru

0.5

10%4+8/4
# % Operator gives remainder of expression
# which here his 2 and other gives 2.0
# float + integer = float

4.0

 3**10/3
# ** is operator for power ( exponential)
# following the order of precedence which says in an expression having Operators 
# paranthesis exponential division multiplcation addition subtraction evaluated respectively

19683.0

0.2+2.3-9.7+11.2
#0.2 + 2.3 – 9.7 + 11.2 (as - is different from minus sign)

4.0

4.2-2.3+9.7-11.2
#4.2 – 2.3 + 9.7 – 11.2

0.40000000000000036

4.2-2.3+9.7/11.2

2.766071428571429

https://colab.research.google.com/drive/1jHxINZgmau2GamDsOxX4VC_CE7yg7nEC#printMode=true 1/10
07/09/2021 19121015_Assignment_1.ipynb - Colaboratory

(4.2/2.3)*(9.7/11.2)
# * is multiplicative operator

1.581521739130435

(4.2/2.3) ** (9.7/11.2)

1.6845979179508572

2. Translation of mathematical operation to python equivalent

#(3+4)(7)
(3+4)*7

49

n=5
n*(n-1)/2 
 

10.0

#sqrt(a*a+b*b)
#(a*a+b*b)**0.5
import math
a=4 
b=6
print(math.sqrt(a*a+ b*b))

7.211102550927978

print(97%3)
print(pow(97,3,27))
97**3%27

19

19

pow(3,97,23)

18

((((0.3 ** 0.5) ** 0.5) ** 0.5) ** 0.5) ** 0.5

0.9630748444263257

https://colab.research.google.com/drive/1jHxINZgmau2GamDsOxX4VC_CE7yg7nEC#printMode=true 2/10
07/09/2021 19121015_Assignment_1.ipynb - Colaboratory

 ((((3 ** 0.5) ** 0.5) ** 0.5) ** 0.5) ** 0.5

1.0349277670798647

((((0.3 ** 0.5) ** 0.5) ** 0.5) ** 0.5) ** 0.5

0.9630748444263257

3. Explain the difference between 42+2 and "42"+"2". What is the + operator doing in each
case?

42+2

44

print(42+2) # in 42+2, "+" is additive operator adding interger value 
"42"+"2"# in "42"+"2", + operator catenate string value giving another string

44

'422'

4. What is wrong with this version of the quadratic formula? r1=(-b+d)/2a


Ans : r1=(-b+d)/2a is
valid if value Of d ( discriminent ) is positve.
But if d is negative , then we encounter syntax
error .
We must define the syntax for it i.e.,complex numbers we need call import cmath library

5. Write a series of Python statements that will first ask the user to type today's exchange
rate
between US dollars and indian rupees. Next, ask the user to type a value in dollars.
Finally,
print the equivalent value in rupees.

 
print(" One US dollar equal to 72.99 Indian rupees.")
x= float (input("Enter value in US dollars:"))
y= 72.99*x
print(y,"indian rupees.")

One US dollar equal to 72.99 Indian rupees.

Enter value in US dollars:67

4890.33 indian rupees.

9. What happens if you try to divide an integer value by zero?

 # int/0 gives ZeroDivision Error
https://colab.research.google.com/drive/1jHxINZgmau2GamDsOxX4VC_CE7yg7nEC#printMode=true 3/10
07/09/2021 19121015_Assignment_1.ipynb - Colaboratory
#8/0
#ZeroDivisionError

8. Write a series of Python statements that will import the math module, read a number from
the
user that represents an angle given in radians, and then prints the sine and cosine for
the
given ang

import math
radian= float(input("Enter the angle in radians: "))
degree= (180/math.pi)*radian
print(math.cos(degree)," is cosine of the angle.")
print(math.sin(degree)," is sin of the angle.")

Enter the angle in radians: 67

0.9782398586086642 is cosine of the angle.

-0.20747717712871602 is sin of the angle.

7. Write a series of Python statements that will read a number from the user that represents
the
radius of a circle. Then use a print statement to show the circle's diameter,
circumference, and
area. You can import the math module and use the constant math.pi to
represent the contant
π.

import math
radius= float (input("Enter the radius of circle: "))
print (2*radius," is the Diameter of circle.")
print (round(2*math.pi*radius,4)," is the circumference of the circle.")
print (math.pi*((radius)**2), " is the area of the circle.")

Enter the radius of circle: 4.7

9.4 is the Diameter of circle.

29.531 is the circumference of the circle.

69.39778171779854 is the area of the circle.

11. Write a python program that will read three numeric values from the user, and then print
the
largest of the three.

x= float(input("Enter the first number: "))
y= float(input("Enter the second number: "))
z= float(input("Enter the third number: "))
if x>=y and x>=z:
  print(x, " is the largest  number.")
if y>=x and y>=z:
  print (y, "is the largest number.")
if z>=x and z>=y: 
i t( " i th l t b ")
https://colab.research.google.com/drive/1jHxINZgmau2GamDsOxX4VC_CE7yg7nEC#printMode=true 4/10
07/09/2021 19121015_Assignment_1.ipynb - Colaboratory
 print( z, " is the largest number.")

Enter the first number: 6

Enter the second number: 8

Enter the third number: 19

19.0 is the largest number.

18. Write a program to compute exponential series

n= int(input(" Enter the number of terms:"))
x = float(input(" Enter the value of x: "))
sum1=1
for i in range(2,n+1):
 sum1= sum1+ ((x**i)/i)
print(" The sum of the series is", round(sum1,2))

Enter the number of terms:3

Enter the value of x: 2

The sum of the series is 5.67

12. Write a Python program that will read three strings from the user, and then print them in
dictionary order.

a = []
while len(a) < 3 :
    a.append(str(input(" enter  string = ")))
print(sorted(a))

enter string = bhesh

enter string = ayush

enter string = kshma

['ayush', 'bhesh', 'kshma']

19. Write a program to compute cosine series.

import math
def cosine(x,n):
    cosx = 1
    sign = -1
    for i in range(2, n, 2):
        pi=22/7
        y=x*(pi/180)
        cosx = cosx + (sign*(y**i))/math.factorial(i)
        sign = -sign
    return cosx
x=int(input("Enter the value of x in degrees:"))
n=int(input("Enter the number of terms:"))
https://colab.research.google.com/drive/1jHxINZgmau2GamDsOxX4VC_CE7yg7nEC#printMode=true 5/10
07/09/2021 19121015_Assignment_1.ipynb - Colaboratory
n int(input( Enter the number of terms: ))
print(round(cosine(x,n),2))

Enter the value of x in degrees:4


Enter the number of terms:4

1.0

20. Write a program to find the roots of quadratic equation ax^2 + bx + c = 0

import cmath
a= float(input("Enter the coefficient a :"))
b= float(input("Enter the coefficent b:"))
c= float(input(" Enter the coefficent c :"))
 
d=(b**2)-(4*a*c)
 
root1= (-b+cmath.sqrt(d))/2*a
root2= (-b-cmath.sqrt(d))/2*a
print("The solutions are {0},{1}".format(root1,root2))
 
 
 
 
 

Enter the coefficient a :4

Enter the coefficent b:5

Enter the coefficent c :4

The solutions are (-10+12.489995996796797j),(-10-12.489995996796797j)

10. What happens if you press ctrl-Z on windows when the Python system is waiting for input
from a call on raw_input?

# would erase the printed text when python system is waiting for input from a call on raw_inp

13. Three numbers, , , and , are called a Pythagorean triple if . An example is


the triple 3,4, and 5,
since 9+16=25. Pythagorean triples can represent, for example, the
length of sides in a right
triangle. Write a series of Python statements that will read three
numbers into variables
named , , and and then print a message saying whether or not
they are a Pythagorean triple.

a= int(input("Enter the value a :"))
b= int(input("Enter the value b :"))
c= int(input("Enter the value c :"))
if a**2 + b**2 == c**2:
  print(a,b,c,"Are pythagorean triple.")
else:
https://colab.research.google.com/drive/1jHxINZgmau2GamDsOxX4VC_CE7yg7nEC#printMode=true 6/10
07/09/2021 19121015_Assignment_1.ipynb - Colaboratory
else:
  print(a,b,c, "Are not pythagorean triple.")

Enter the value a :4

Enter the value b :3

Enter the value c :5

4 3 5 Are pythagorean triple.

14. Using a for loop, print a table of powers of , where ranges from to . For each value , print the
quantity , , and . Use tab characters in your print statement to make the
values line up nicely.

for x in range(1,11):
  #print(f"entered value is {x}, square of {x} is {x**2}, cube of {x} is {x**3}")
  print(math.pow())

entered value is 1, square of 1 is 1, cube of 1 is 1

entered value is 2, square of 2 is 4, cube of 2 is 8

entered value is 3, square of 3 is 9, cube of 3 is 27

entered value is 4, square of 4 is 16, cube of 4 is 64

entered value is 5, square of 5 is 25, cube of 5 is 125

entered value is 6, square of 6 is 36, cube of 6 is 216

entered value is 7, square of 7 is 49, cube of 7 is 343

entered value is 8, square of 8 is 64, cube of 8 is 512

entered value is 9, square of 9 is 81, cube of 9 is 729

entered value is 10, square of 10 is 100, cube of 10 is 1000

15. Using a while loop, produce a simple table of sines, cosines, and tangents. Make thevariable x
range from 0 to 3 in steps of 0.1. For each value of x, print the vale of math.sin(x),
math.cos(x), and math.tan(x)

import math
x=0
while x<=3:
 print(f"sin({x})= {math.sin(x)}, cos({x})= {math.cos(x)}, tan({x})= {math.tan(x)}")
 x +=0.1

sin(0)= 0.0, cos(0)= 1.0, tan(0)= 0.0

sin(0.1)= 0.09983341664682815, cos(0.1)= 0.9950041652780258, tan(0.1)= 0.100334672085450


sin(0.2)= 0.19866933079506122, cos(0.2)= 0.9800665778412416, tan(0.2)= 0.202710035508672
sin(0.30000000000000004)= 0.2955202066613396, cos(0.30000000000000004)= 0.95533648912560
sin(0.4)= 0.3894183423086505, cos(0.4)= 0.9210609940028851, tan(0.4)= 0.4227932187381618
sin(0.5)= 0.479425538604203, cos(0.5)= 0.8775825618903728, tan(0.5)= 0.5463024898437905

sin(0.6)= 0.5646424733950354, cos(0.6)= 0.8253356149096783, tan(0.6)= 0.6841368083416923


sin(0.7)= 0.644217687237691, cos(0.7)= 0.7648421872844885, tan(0.7)= 0.8422883804630794

sin(0.7999999999999999)= 0.7173560908995227, cos(0.7999999999999999)= 0.6967067093471655


sin(0.8999999999999999)= 0.7833269096274833, cos(0.8999999999999999)= 0.6216099682706645
sin(0.9999999999999999)= 0.8414709848078964, cos(0.9999999999999999)= 0.5403023058681398
sin(1.0999999999999999)= 0.8912073600614353, cos(1.0999999999999999)= 0.4535961214255775
sin(1.2)= 0.9320390859672263, cos(1.2)= 0.3623577544766736, tan(1.2)= 2.5721516221263188
sin(1.3)= 0.963558185417193, cos(1.3)= 0.26749882862458735, tan(1.3)= 3.6021024479679786
https://colab.research.google.com/drive/1jHxINZgmau2GamDsOxX4VC_CE7yg7nEC#printMode=true 7/10
07/09/2021 19121015_Assignment_1.ipynb - Colaboratory

sin(1.4000000000000001)= 0.9854497299884603, cos(1.4000000000000001)= 0.1699671429002408


sin(1.5000000000000002)= 0.9974949866040544, cos(1.5000000000000002)= 0.0707372016677026
sin(1.6000000000000003)= 0.9995736030415051, cos(1.6000000000000003)= -0.029199522301289
sin(1.7000000000000004)= 0.9916648104524686, cos(1.7000000000000004)= -0.128844494295525
sin(1.8000000000000005)= 0.973847630878195, cos(1.8000000000000005)= -0.2272020946930875
sin(1.9000000000000006)= 0.9463000876874142, cos(1.9000000000000006)= -0.323289566863503
sin(2.0000000000000004)= 0.9092974268256815, cos(2.0000000000000004)= -0.416146836547142
sin(2.1000000000000005)= 0.8632093666488735, cos(2.1000000000000005)= -0.504846104599857
sin(2.2000000000000006)= 0.8084964038195899, cos(2.2000000000000006)= -0.588501117255346
sin(2.3000000000000007)= 0.7457052121767197, cos(2.3000000000000007)= -0.666276021279824
sin(2.400000000000001)= 0.6754631805511503, cos(2.400000000000001)= -0.737393715541246,
sin(2.500000000000001)= 0.5984721441039558, cos(2.500000000000001)= -0.8011436155469343,
sin(2.600000000000001)= 0.5155013718214634, cos(2.600000000000001)= -0.8568887533689478,
sin(2.700000000000001)= 0.42737988023382895, cos(2.700000000000001)= -0.9040721420170617
sin(2.800000000000001)= 0.33498815015590383, cos(2.800000000000001)= -0.9422223406686585
sin(2.9000000000000012)= 0.23924932921398112, cos(2.9000000000000012)= -0.97095816514959

16. What does the range loop do if it is given negative numbers? Experiment with nagative
values
in the first, second, or third location and try to come up with a general rule.

# Range loop has three parameters : start, stop and step. Start is an integer value that spec
 
#So, when we enter negative values in the range function, we need to make sure the stop value 
#greater than start value, when the step value is default ot positive,
#less than start value, when the step value is negative too.
 
for i in range(-10,-100,-30):  
 print(i)

-10

-40

-70

6. Write a Python statement that reads a numeric value from the user, placing it into a
variable
named x. Then write a print statement that will print the values x, x , , and .2 x3 x4 Use tab
characters to separate the four resulting values. Write a second print statement,
but use
newline characters instead ot tab stops.

x= float(input("enter the value of x:"))
print(f"x={x},\nx^2={x**2}, \nx^3={x**3}")

enter the value of x:4

x=4.0,

x^2=16.0,

x^3=64.0

x = float(input("enter the number: "))
https://colab.research.google.com/drive/1jHxINZgmau2GamDsOxX4VC_CE7yg7nEC#printMode=true 8/10
07/09/2021 19121015_Assignment_1.ipynb - Colaboratory
( p ( ))
 
print(f'''entered value is {x}
square of {x} is {x**2}
cube of {x} is {x**3}
biquadrate of {x} is {x**4}''')

enter the number: 4

entered value is 4.0

square of 4.0 is 16.0

cube of 4.0 is 64.0

biquadrate of 4.0 is 256.0

17.

# +
4+2 #legal
 
"2"+"4" # legal and concatenation
4.2+ 3 #legal
 
#-
4-2 # legal
4.0-3 #legal
#"4"-"2" #TypeError: unsupported operand type(s) for -: 'str' and 'str'
 
# *
3*4#legal
3.5*4.6 #legal
#"bhesh"*"chandra"#TypeError: can't multiply sequence by non-int of type 'str'
 
 #**
print(2**3)
type(2**3)
#8 int
print(2.6**4.33)
type(2.6**4.33) 
#62.637526099984136 float
#print("3"**"4.78")
#type("3"**"4.78") TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'str'
 
#/
print(4/5)
type(4/5)
#0.8 float
print (8.8/4.4)
type(8.8/4.4) #2.0 float
 
#print("bhesh"/"chandra")
#type("bhesh"/"chandra") #TypeError: unsupported operand type(s) for /: 'str' and 'str'

https://colab.research.google.com/drive/1jHxINZgmau2GamDsOxX4VC_CE7yg7nEC#printMode=true 9/10
07/09/2021 19121015_Assignment_1.ipynb - Colaboratory

62.637526099984136

0.8

2.0

float

https://colab.research.google.com/drive/1jHxINZgmau2GamDsOxX4VC_CE7yg7nEC#printMode=true 10/10

You might also like