Question

Write, save, and run a Python program that will do the following when run: Let’s consider...

Write, save, and run a Python program that will do the following when run:

  1. Let’s consider the following assignment:

a = 1 * 2 + 38 / 8

You want to change the precedence of the operators so that the addition operations are executed first.

How would you re-write the assignment? Write a print statement to show the output.

  1. Let’s consider the following assignments:

x = 'z'

y= ['x', 'z', 'q']

Now, apparently x is a member of y.

Write a print statement that should display True- what operator will you use to check if x is a member of y?

  1. Consider the following lines of code. Write the assignment (shown in bold) in the shortcut format and print the result:

tax = 15.2

tax = tax - 2.5

  1. Let’s consider this assignment:

salary = 55000

vacation = 250

  • Write a print statement that checks if salary is equal to 55000 and vacation is equal to 200. (What does your print statement return?) -do not use if statement.

2 mark

  1. Let’s consider the following assignments:

points = 14

Write a print statement that returns the remainder of points divided by 3 using a special operator (not the division one).

Write another print statement that returns the integer part of points divided by 3 using a special operator (not the division one)

  1. Round the following number to the precision of 3 digits:

5.2967

  1. Assign values (any- NOT a user input) to the following multiple variables all in one line of code x):

The hourly pay rate, number of hours worked and the amount of the tax.

  • Then calculate the net pay by multiplying the rate with hours and subtracting the tax from the result.

  • Output the net pay using the % parameter in the print statement in a right justified with 10 spaces followed by |          (use end parameter for the I)

#Sample run

5

True

12.7

--For Q.4 sample can not be provided –

2

4

5.297

234       |

Please provide code and screenshot of the results.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a) Since Python uses a set of precedence of operators we use () to make + get executed first

# using () so that "+" gets precedence 
a = 1 * (2 + 38) / 8
print(a)

abhijeet@abhijeet:-/python_prog File Edit View Search Terminal Help abhijeet@abhijeet:-/python_prog$ python3 as1.py 5.0 abhij

b) The statement "x in y" asks that if x is a member of the list y

x = 'z'
y= ['x', 'z', 'q']
print(x in y)

abhijeet @abhijeet: -/python_prog File Edit View Search Terminal Help abhijeet@abhijeet:-/python_prog$ python3 as1.py 5.0 abh

c) We can use" tax -= 2.5" operator instead of using "tax = tax - 2.5" . They are the same in python.

tax = 15.2
tax -= 2.5
print(tax)

abhijeet@abhijeet: -/python_prog File Edit View Search Terminal Help abhijeet@abhijeet:-/python_prog$ python3 as1.py 15.0 abh

d) Using == operator in python means we are checking if both sides of the operator are equal or not. It gives True or False as answer.

salary = 55000
vacation = 250
print(salary == 55000)
print(vacation == 200)

abhijeet@abhijeet:-/python_prog File Edit View Search Terminal Help abhijeet@abhijeet:-/python_prog$ python3 as1.py True Fals

e) We can use %(mod) operator to find the remainder and // operator for the quotient

points = 14
print(points % 3)
print(points // 3)

abhijeet@abhijeet:-/python_prog File Edit View Search Terminal Help abhijeet@abhijeet:-/python_prog$ python3 as1.py 2 4 abhij

f) We can use round(value,precision) to round off to the needed digits.

print(round(5.2967,3))

O abhijeet@abhijeet: -/python_prog File Edit View Search Terminal Help abhijeet@abhijeet:-/python_prog$ python3 as1.py 12 4 a

g) We use a list to take all inputs in one single line

x = [50,5,16]
print("%d          "%(x[0] * x[1] - x[2]),end = '|')

abhijeet@abhijeet: -/python_prog File Edit View Search Terminal Help abhijeet@abhijeet:-/python_prog$ python3 as1.py 234 | ab

Add a comment
Know the answer?
Add Answer to:
Write, save, and run a Python program that will do the following when run: Let’s consider...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Python The variables x and y refer to numbers. Write a code segment that prompts...

    Using Python The variables x and y refer to numbers. Write a code segment that prompts the user for an arithmetic operator and prints the value obtained by applying that operator to x and y. Using the following information run an example in Python Idle op = input("Enter an arithmetic operator: ") if op == "+":     print(x + y) elif op == "-":     print(x – y) elif op == "*":     print(x * y) elif op == "/":...

  • 1. Using logical operators modify the following Python "if" statement to print ("AND operator condition worked")?...

    1. Using logical operators modify the following Python "if" statement to print ("AND operator condition worked")? x=2 y=3 if (x<=2 or y>=3) and (x>2 or y<2): print("AND operator condition worked") else: print("it doesn't work") 2. Write a program that asks user for five numbers and returns the largest of them. Do not use max function built in python.

  • Write a Python Program What should the values of X and Y be, so that the...

    Write a Python Program What should the values of X and Y be, so that the following code prints the 'd' found inside one of the elements of alist? alist = [['47a','47b'],'47c','47d',[['47e','47f'],'47g']] print(alist[X][Y]) Then write another print(...) statement that prints the the 'b' inside of alist, by indexing again into to its specific location within alist.

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • . Use what you learned from our First Python Program to write a Python program that...

    . Use what you learned from our First Python Program to write a Python program that will carry out the following tasks. Ask user provide the following information: unit price (for example: 2.5) quantity (for example: 4) Use the following formula to calculate the revenue: revenue = unit price x quantity Print the result in the following format on the console: The revenue is $___. (for example: The revenue is $10.0.) . . Use the following formula to calculate the...

  • Please answer this python questions.Thank you! Question Two      Write a program that calculates the amount...

    Please answer this python questions.Thank you! Question Two      Write a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what the salary was for each day, and then show the total pay at the end...

  • Write a program that does the following in Python Code: Write a new Class called Famous_Day_Born...

    Write a program that does the following in Python Code: Write a new Class called Famous_Day_Born which is a subclass of Famous_Person. Add a method to calculate the day of the week the person was born and print out all of the corresponding information using the overridden print method. Use the following code below as a starting point. ////////////////////////////////////////////////////// from datetime import datetime class Famous_Person(object): def __init__(self, last, first, month,day, year): self.last = last self.first = first self.month = month...

  • 5. (12 points) Consider the following code fragment (part of a program): int index 3; String...

    5. (12 points) Consider the following code fragment (part of a program): int index 3; String dna "ACTGTCA char nucleotide dna.charAt (index); Matching: For each term below, write the letter of the ONE choice that best matches the term. Data types Variables Literals Object Method ーParameter nde x a. coumt, nucleotide, and d b. 3 and "ACTGTCA c. int, char, and String d. index e. charAt f. dna na 6. (10 points) a. Wrte a statement that assigns true to...

  • Java Write a Simple Program This question is about following the directions below. Failure to do...

    Java Write a Simple Program This question is about following the directions below. Failure to do so, results in lost credit. Write the definition of a method that takes in an integer number, and returns true if the number is prime, otherwise, returns false. A prime number is only divisible by itself and 1. Do not write the code for main here. The code must follow these steps in the order shown: 1. Name the method isPrime, and make it...

  • Hi, Looking for some help with this Python question as follows: Consider the following Python program:...

    Hi, Looking for some help with this Python question as follows: Consider the following Python program: def fun(x, y): return x * y a = fun(2, 3) b = fun("2", 3) print(a, b) What does the function evaluate to? What would happen if we replace the last statement print a, b with print a + b? Thanks for any help. John

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT