Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Lab Session 3 - Python Operators (1) exercise

Uploaded by

zoyashad3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab Session 3 - Python Operators (1) exercise

Uploaded by

zoyashad3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB Session: 3 Exercise

1. What is the output of the following code:


9//2

2. Assumes the variables w, x, y and z are all integers, and that w=5, x=4, y=8 and z=2. What value will
be stored in result after each of the following statements execute?
1. x += y
2. z *= 2
3. y /= z
4. w %= z

3. What happens when Python evaluates the statement x += x - x when x has the value 3?

4. Write the shortest way to express each of the following statements.


1. x = x + 1
2. x = x / 2
3. x = x - 1
4. x=x+y
5. x = x - (y + 7)
6. x = 2*x
7. number_of_closed_cases = number_of_closed_cases + 2*ncc

Answer:
1. x += 1
2. x /= 2
3. x -= 1
4. x += y
5. x -= (y + 7)
6. x* = 2
7. number_of_closed_cases = number_of_closed_cases + 2*ncc
5. Write a Python program to solve (x + y) * (x + y) / 2*x.
Test Data: x = 4, y = 3

6. Read two integers from the user and print three lines where:
1. The first line contains the sum of the two numbers.
2. The second line contains the difference of the two numbers (first - second).
3. The third line contains the product of the two numbers.

7. Write a Python program to get the volume of a sphere with radius 6.


8. Write a Python program which accepts temperature in Fahrenheit from the user and convert it into
Celsius.

9. Write a Python program to add two positive integers without using the '+' operator.
Note: Use bit wise operations to add two numbers.

10. Consider the following program that attempts to compute the circumference of a circle given the
radius entered by the user. Given a circle’s radius, r, the circle’s circumference, C is given by the
formula: C = 2pr

Program:
r=0
PI = 3.14159
# Formula for the area of a circle given its radius
C = 2*PI*r
# Get the radius from the user
r = float(input("Please enter the circle's radius: "))
# Print the circumference
print("Circumference is", C)
1. The program does not produce the intended result. Why?

Answer: because we write circumference(c) statement before the input statement.


2. How can it be repaired so that it works correctly?

Answer: correct the statement sequence.

You might also like