[PriP 1.2] Revision Tour-1 – Practical 1.2

|

This PriP session is aimed at revising various concepts we learnt in Class XI for those of you in PCM. Sumita Arora python class 12 practical book solutions.

Table of Contents

PriP 1.2 – Python: Data Handling

1. In each of the following Python expressions or statements, indicate what data type belongs in the indicated place by choosing one of these data types:
int float bool str list

Answer:

ExpressionCorrect Datatype
(a)s = ___ + 17int
(b)t = ___ + 'pie'str
(c)x[___] = 'catfood'int
(d)___.sort()list
(e)if ___:bool

2. What does Python print as it executes the following sequence of statements?

Answer:

ExpressionCorrect Datatype
(a)print((10+3)*2)26
(b)print(11/2)5.5
(c)x = 25
print(x*2==50)
True
(d)print(x/2>=50)False
(e)a = 'Honda'
b = 'Audi'
print(a+b)
HondaAudi
(f)c = len(a) + len(b)
print(c)
9
(g)print(a)Honda
(h)print(a[1])o

3. What would be the output of following code?

a = 9000 # initializes and assigns value to variable
print("Now it's a", type(a))
a = float(a)
print("Now it's a", type(a))
a = str(a)
print("Now it's a", type(a))

Output:

Now it's a <class 'int'>
Now it's a <class 'float'>
Now it's a <class 'str'>

4. What would be the output of following code? Support your answer with reasons.

x = 4
y = 8
z = x/y*y
print(z)

Answer: For the given expression z = x / y * y, the operations involved are division and multiplication, which are of equal precedence. So, they are evaluated from left to right. As x/y = 0.5;
0.5*8 = 4.0.

Output: 4.o

5. Which of the following statements would yield 2? Assume that math module is imported. Recall that math.fabs() gives you absolute value of its argument.

  1. print(5/2)
  2. print(5.0//2)
  3. print((int) (5.0//2))
  4. print(math.fabs(-5.0/2))
  5. print(math.fabs(-5.0 // 2))
  6. print(math.fabs(-3//2))
  7. print(-5/2)
  8. print(-5/2)
  9. print (-5//2)
  10. print(-5.0//2)

Answer: The statements that yield 2 are (c) and (f).

6. Which of the following statements would yield 2.0? Assume that math module is imported. Recall that math.fabs() gives you absolute value of its argument.

  1. print(5/2)
  2. print(5.0//2)
  3. print((int) (5.0//2))
  4. print(math.fabs(-5.0/2))
  5. print(math.fabs(-5.0 // 2))
  6. print(math.fabs(-3//2))
  7. print(-5/2)
  8. print(-5/2)
  9. print (-5//2)
  10. print(-5.0//2)

Answer: The only statement that yields 2.0 is (b).

7. What would be the output produced by the print statements given in previous question?

Answer:

  1. (a) print(5/2)
    • Output: 2.5
  2. (b) print(5.0//2)
    • Output: 2.0
  3. (c) print((int)(5 // 2))
    • Output: 2
  4. (d) print(math.fabs(-5.0 / 2))
    • Output: 2.5
  5. (e) print(math.fabs(-5 // 2))
    • Output: 3
  6. (f) print(math.fabs(-3 // 2))
    • Output: 2
  7. (g) print(-5/2)
    • Output: -2.5
  8. (h) print(-5/2)
    • Output: -2.5
  9. (i) print(-5 // 2)
    • Output: -3
  10. (j) print(-5.0 // 2)
    • Output: -3.0

8. What would be the output of the following?

Answer: You can analyze the outputs for each statement or view detailed explanation:

StatementOutput
(a)print(type([1,2]))<class 'list'>
(b)print(type(1,2))TypeError: type() takes 1 or 3 arguments
(c)print(type((1,2)))<class 'tuple'>
(d)print(type(1/2))<class 'float'>
(e)print(type([1/2]))<class 'list'>
(f)print(type((1/2)))<class 'float'>
(g)print(type((1/2,)))<class 'tuple'>

9. What would be the output of the following?

x = True
y = False
z = False
print(x or y and z)

Answer: The expression is evaluated as y and z → False
x or False → True

So, the final output is True.

10. Given the following Boolean variables, what would be the output of the following statements?

x = True
y = False
z = False

Answer: View deep explanation for each statement or final answer below.

StatementOutput
(a)print(not x or y)False
(b)print(not x or not y and z)False
(c)print(not x or y or not y and x)True
(d)print('ant' < 'amoeba')False

11. Write a program to find the side of a square whose perimeter you read from user.

Answer: Here’s a simple Python program that reads the perimeter from the user and calculates the side length of the square. You can also view detailed explanation for this question.

# Program to find the side of a square given its perimeter

# Read the perimeter from the user
perimeter = float(input("Enter the perimeter of the square: "))

# Calculate the side length
side_length = perimeter / 4

# Display the result
print(f"The side length of the square is: {side_length}")

12. Write a program to calculate the distance of 1056 feet in terms of yards and miles. Given fixed values are 1 mile = 1760 yards and 1 yard = 3 feet.

Answer: View more information for the python program and output or get the code only below without any in-depth explanation:

# Fixed values
feet_to_yards = 1 / 3  # 1 yard = 3 feet
yards_to_miles = 1 / 1760  # 1 mile = 1760 yards

# Given distance in feet
distance_feet = 1056

# Calculate distance in yards
distance_yards = distance_feet * feet_to_yards

# Calculate distance in miles
distance_miles = distance_yards * yards_to_miles

# Display the results
print(f"Distance in yards: {distance_yards:.2f} yards")
print(f"Distance in miles: {distance_miles:.6f} miles")

13. An athlete is taking rounds of a triangular park with sides as 30 m, 25 m and 35 m. The athlete has run 560 m till now. Write a program to print how many rounds of park the athlete has completed.

This is a question requiring basic maths you practiced in earlier grades. Still, you can view more information on this question or get your code below.

# Side lengths of the triangular park
side1 = 30
side2 = 25
side3 = 35

# Distance run by the athlete
distance_run = 560

# Calculate the perimeter of the triangular park
perimeter = side1 + side2 + side3

# Calculate the number of rounds completed
rounds_completed = distance_run / perimeter

# Print the result
print(f"The athlete has completed {int(rounds_completed)} rounds of the park.")

14. Write a python program that calculates and prints on the screen the number of seconds in a year.

Answer: We can do this program directly in the print statement or by creating variables for storing constant time values, of which, the latter is demonstrated below.

Python
# Constants
seconds_per_minute = 60
minutes_per_hour = 60
hours_per_day = 24
days_per_year = 365

# Calculate the number of seconds in a year
seconds_in_a_year = seconds_per_minute * minutes_per_hour * hours_per_day * days_per_year

# Print the result
print(f"The number of seconds in a year is: {seconds_in_a_year}")

If sought, someone less familiar with Python can access a more detailed explanation of the program, explaining what and why things are going on like this.

"Spread the light within, illuminate the hearts around you. Sharing is not just an action, but a sacred journey of connecting souls."~ Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *