Professional Documents
Culture Documents
Assignment - 1.ipynb - Colaboratory
Assignment - 1.ipynb - Colaboratory
Assignment - 1.ipynb - Colaboratory
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
#(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'
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.")
# 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.")
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.")
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.")
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))
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))
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))
1.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))
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
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.")
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())
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
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}")
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}''')
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