In: Computer Science
Python
1) Show the result of evaluating each expression. Be sure that the value is in the proper form to indicate its type (int or float). If the expression is illegal, explain why.
a) 4.0/10.0 + 3.5 *2
b)10%4 + 6/2
c)abs(4-20//3)**3
d) sqrt(4.5-5.0) + 7*3
e) 3*10//3 + 10%3
f) 3**3
2) Show the sequence of numbers that would be generated by each of the following range experssions.
a) range(5)
b) range(3,10)
c)range (4, 13, 3)
d)range (15, 5, -2)
e)range(5,3)
3) What do you think will happen if you use a negative number as the second parameter in the round function? E.g. what should be the result of round (314. 159265, -1)?
Previous Next
Answer: hey! kindly find your solution. If you have any query, feel free to ask me. Thanks
import math
num1 = 4.0/10.0 + 3.5 *2 #It is valid expression and output is 7.4. this is the correct calculation-divide/multiplication/addition
num2 = 10%4 + 6/2 #it is also valid expression and output is 5.0. It is the correct. 10%4 it returns remainder only =2, divide returns quotient 6/2=3
num3 = abs(4-20//3)**3 #it is valid expression. First it will calculate absolute value by abs() and return a value (**means power), then calculate power of this number and output is- 8
#num4 = (math.sqrt(4.5-5.0)) + 7*3 # this is invalid expression because sqrt() does not take negative values. After subtraction, a negtaive value will be return that's why it is raising an error.
num4 = 3*10//3 + 10%3 #this is valid. output is-11
num5 = 3**3 #it will return power of 3*3*3
print("Expression 1-:",num1)
print("Expression 2-:",num2)
print("Expression 3-:",num3)
print("Expression 4-:",num4)
print("Expression 5-:",num5)
print("A- using range function")
for i in range(5): #this will return 0 to 4 values
print(i, end=', ')
print("\n B- range(3,10) function")
for i in range(3,10): #it will return 3 to 9
print(i, end=', ')
print("\n C- range(4,13,3) function")
for i in range(4,13,3): # it will return 4 to 12 with 3 step like 4,7,10 (+3 in every digit)
print(i, end=', ')
print("\n D- range(15,5,-2) function")
for i in range(15,5,-2): #it will return 15 to 7 with -2 step(it will return backword numbers with step -2) 15,13,11,9,7
print(i, end=', ')
print("\n E- range(5,3) function")
for i in range(5,3): #it will return nothing because first value is greater than step values .If you want to a series like 5 to 10 then you will give parameters (5,10) if you want to back step then you will give(10,5,-1) one should be true tor eturn values.
print(i, end=', ')
I think this is applying round() on the left part of the decimal that's why it is rounding to (314) returns 310 because 4 is less than 5 if 4 will be greater than 315, 316 or equal to it will return 320.