[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:
Expression | Correct Datatype | |
---|---|---|
(a) | s = ___ + 17 | int |
(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:
Expression | Correct Datatype | |
---|---|---|
(a) | print((10+3)*2) | 26 |
(b) | print(11/2) | 5.5 |
(c) | x = 25 | True |
(d) | print(x/2>=50) | False |
(e) | a = 'Honda' | HondaAudi |
(f) | c = len(a) + len(b) | 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.
print(5/2)
print(5.0//2)
print((int) (5.0//2))
print(math.fabs(-5.0/2))
print(math.fabs(-5.0 // 2))
print(math.fabs(-3//2))
print(-5/2)
print(-5/2)
print (-5//2)
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.
print(5/2)
print(5.0//2)
print((int) (5.0//2))
print(math.fabs(-5.0/2))
print(math.fabs(-5.0 // 2))
print(math.fabs(-3//2))
print(-5/2)
print(-5/2)
print (-5//2)
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:
- (a)
print(5/2)
- Output:
2.5
- Output:
- (b)
print(5.0//2)
- Output:
2.0
- Output:
- (c)
print((int)(5 // 2))
- Output:
2
- Output:
- (d)
print(math.fabs(-5.0 / 2))
- Output:
2.5
- Output:
- (e)
print(math.fabs(-5 // 2))
- Output:
3
- Output:
- (f)
print(math.fabs(-3 // 2))
- Output:
2
- Output:
- (g)
print(-5/2)
- Output:
-2.5
- Output:
- (h)
print(-5/2)
- Output:
-2.5
- Output:
- (i)
print(-5 // 2)
- Output:
-3
- Output:
- (j)
print(-5.0 // 2)
- Output:
-3.0
- Output:
8. What would be the output of the following?
Answer: You can analyze the outputs for each statement or view detailed explanation:
Statement | Output | |
---|---|---|
(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.
Statement | Output | |
---|---|---|
(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.
# 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.