Question

In: Computer Science

Python 1) Show the result of evaluating each expression. Be sure that the value is in...

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

Solutions

Expert Solution

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.


Related Solutions

Find is the final result of evaluating the following postfix expression using a stack. Show each...
Find is the final result of evaluating the following postfix expression using a stack. Show each push and pop operation. 85 5 / 4 * 5   6 +   10    5 -   * +
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW...
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW STACK CONFIGURATION DETAILS 20 3 18 8 - * 10 + +10 12 - /
Python Programming 4th Edition: Write Boolean expression: 1)A currently have a different value than B 2)...
Python Programming 4th Edition: Write Boolean expression: 1)A currently have a different value than B 2) B less than the sum of the current values of A and C 3)C is no more than 15. 4) 75 is between the values of integers A and B 5) Number N is divisible by 2 or it is divisible by 3. 6) X is positive and Y is positive
Add the result envidented by the python compiler. use python language. 1(a) A Give the output...
Add the result envidented by the python compiler. use python language. 1(a) A Give the output for the array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]) a. a[:6:2] = -1000 (ii) a[ : :-1] ) b. Display the values of 1D array using for loop?
 c. Write a code to print a 3D array?
 d. How to print the transpose of a 2D array?
 e Write a function to sort the array row and coloumn wise ?...
For each situation, perform a hypothesis test for the population mean. Be sure to show the...
For each situation, perform a hypothesis test for the population mean. Be sure to show the null hypothesis H0, the alternative hypothesis H1, the P-level you get from TTest, the result of the test (i.e. reject / do not reject H0) and the conclusion (interpret the result in English). A Jedi sage would like proof that Jedi trainees have a higher level of midichlorians than non-Jedis. Non-Jedis have a mean midichlorian level of 2500. A random sample of 25 Jedi...
Directions: Complete each of the following problems. Be sure to show your work in order to...
Directions: Complete each of the following problems. Be sure to show your work in order to receive full or partial credit. Calculators are allowed along with 1 page (1 side) of notes any any necessary statistical tables. 1. (True / False) Estimating parameters and testing hypotheses are two important aspects of descriptive statistics. 2. (True / False) A statistic is calculated from a population and a parameter is calculated from a sample.   3. (True / False) Descriptive statistics include visual...
Regular Expressions Assignment Write a regular expression for each of the following. Can you show output...
Regular Expressions Assignment Write a regular expression for each of the following. Can you show output please. A blank line (may contain spaces) Postal abbreviation (2 letters) for State followed by a space and then the 5-digit zip code A KU student username (Ex. lpork247) A “valid” email address (also explain how you defined “valid”) A SSN pattern (ddd-dd-dddd)
Derive the expression of density of states for quantum wells and based on the result of...
Derive the expression of density of states for quantum wells and based on the result of your derivation make comment about the advantages of quantum-well lasers as compared to bulk lasers.
Derive the expression of density of states for quantum wells and based on the result of...
Derive the expression of density of states for quantum wells and based on the result of your derivation make comment about the advantages of quantum-well lasers as compared to bulk lasers.
3. Show the string that would result from each of the following string formatting operations. If...
3. Show the string that would result from each of the following string formatting operations. If the operation is not legal, explain why. Use Python shell to solve this question (a) "Looks like {1} and {0} for breakfast".format("eggs", "spam") => (b) "There is {0} {1} {2} {3}".format(1,"spam", 4, "you") => (c) "Hello {0}".format("Susan", "Computewell") => (d) "{0:0.2f} {0:0.2f}".format(2.3, 2.3468) => (e) "{7.5f} {7.5f}".format(2.3, 2.3468) => (f) "Time left {0:02}:{1:05.2f}".format(1, 37.374) => (g) "{1:3}".format("14") =>
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT