0% found this document useful (0 votes)
24 views4 pages

Python Output Exercises and Solutions

The document contains a series of Python code snippets, each followed by a question asking for the output of the code. It covers various operations including arithmetic, string concatenation, conditional statements, and variable assignments. The document is structured as a worksheet for practicing Python output predictions.

Uploaded by

v.mahato2411
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Python Output Exercises and Solutions

The document contains a series of Python code snippets, each followed by a question asking for the output of the code. It covers various operations including arithmetic, string concatenation, conditional statements, and variable assignments. The document is structured as a worksheet for practicing Python output predictions.

Uploaded by

v.mahato2411
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Output Worksheet

1. What is the output of the following code?

python
Copy code
print(10 + 5)

2. What is the output of the following code?

python
Copy code
print(2 * 3)

3. What is the output of the following code?

python
Copy code
print("Hello, " + "World!")

4. What is the output of the following code?

python
Copy code
print(5 / 2)

5. What is the output of the following code?

python
Copy code
print(5 // 2)

6. What is the output of the following code?

python
Copy code
a = 10
b = 20
print(a + b)

7. What is the output of the following code?

python
Copy code
x = 5
y = x + 2
print(y)

8. What is the output of the following code?

python
Copy code
if 5 > 2:
print("Five is greater than two!")
9. What is the output of the following code?

python
Copy code
if 3 < 1:
print("Three is less than one")
else:
print("Three is not less than one")

10. What is the output of the following code?

python
Copy code
if 10 == 10:
print("Ten is equal to ten")
else:
print("Ten is not equal to ten")

11. What is the output of the following code?

python
Copy code
if 7 != 7:
print("Seven is not equal to seven")
else:
print("Seven is equal to seven")

12. What is the output of the following code?

python
Copy code
a = 15
b = 20
if a < b:
print("a is less than b")
else:
print("a is not less than b")

13. What is the output of the following code?

python
Copy code
x = 5
if x > 0 and x < 10:
print("x is between 0 and 10")
else:
print("x is not between 0 and 10")

14. What is the output of the following code?

python
Copy code
a = True
b = False
print(a and b)

15. What is the output of the following code?


python
Copy code
a = True
b = False
print(a or b)

16. What is the output of the following code?

python
Copy code
x = 10
y = 5
print(not (x < y))

17. What is the output of the following code?

python
Copy code
a = 2
b = 3
c = 4
if a < b and b < c:
print("a < b < c")
else:
print("Condition not met")

18. What is the output of the following code?

python
Copy code
num = 15
if num % 2 == 0:
print("Even")
else:
print("Odd")

19. What is the output of the following code?

python
Copy code
a = 5
b = 10
if a + b > 10:
print("Sum is greater than 10")
else:
print("Sum is not greater than 10")

20. What is the output of the following code?

python
Copy code
score = 85
if score >= 90:
print("Grade A")
elif score >= 80:
print("Grade B")
elif score >= 70:
print("Grade C")
else:
print("Grade D")

21 What will be the output from the code given below?

python
Copy code
x, y, z = 5, 7, 9
y, x, z = y + 3, x + 3, z + 3
print(x, y, z)

22 What will be the output from the code given below?

python
Copy code
a, b, c = 12, 15, 18
c, a, b = c // 3, a // 3, b // 3
print(a, b, c)

23 What will be the output from the code given below?

python
Copy code
i, j, k = 6, 2, 4
k, i, j = k * 2, i * 2, j * 2
print(i, j, k)

24 What will be the output from the code given below?

python
Copy code
m, n, o = 14, 28, 42
n, m, o = n - 7, m - 7, o - 7
print(m, n, o)

25 What will be the output from the code given below?

python
Copy code
u, v, w = 3, 6, 9
v, u, w = v % 3, u % 3, w % 3
print(u, v, w)

Common questions

Powered by AI

In `u, v, w = 3, 6, 9; v, u, w = v % 3, u % 3, w % 3`, the first assignment sets u, v, w as 3, 6, 9. The second changes them to `v = 6 % 3 (0)`, `u = 3 % 3 (0)`, `w = 9 % 3 (0)`. As all reassignments use the modulo operator with multiples of 3, each evaluates to 0, demonstrating full divisibility and resetting values based on remainder logic .

Upon executing this code, the condition `score >= 90` evaluates to `False`, while `score >= 80` holds true, leading to the printing of `Grade B`. This logic demonstrates an ordered evaluation where the first true condition's block is executed, assessing student performance through condition chaining .

The output of `a = True; b = False; print(a and b)` is `False`. This is because the `and` operator in Python returns `True` only if both operands are `True`; here, since `b` is `False`, the expression evaluates to `False` demonstrating how logical conjunction requires both conditions to be satisfied .

The code `print(5 // 2)` outputs `2` because the `//` operator in Python is used for floor division, which divides the two numbers and rounds down to the nearest whole number. In contrast, `print(5 / 2)` outputs `2.5` as the `/` operator performs a standard division returning a float result .

The code uses the modulo operator `%` to determine the remainder when `num` is divided by 2. If `num % 2 == 0`, the number is even, prompting `print("Even")`. Otherwise, it's odd, executing `print("Odd")`. This demonstrates parity checking through remainder analysis, a fundamental method to categorize numbers as even or odd .

The code `x, y, z = 5, 7, 9; y, x, z = y + 3, x + 3, z + 3` first sets x, y, z to 5, 7, 9. It then reassigns y to `y + 3` (10), x to `x + 3` (8), and z to `z + 3` (12). The final print statement would output `8, 10, 12`, demonstrating how multiple variables can be efficiently reassigned with new calculated values using tuple unpacking .

This condition checks if `10 == 10`, which evaluates to `True`, so it prints `Ten is equal to ten`. The `else` block is skipped because the equality condition holds true. This demonstrates the use of equality (`==`) in conditionals to direct program flow based on whether the specified condition holds .

The code evaluates the condition `5 > 2`, which is `True`, leading to the statement `print("Five is greater than two!")`. This demonstrates the flow of execution when conditional evaluations involve direct numeric comparisons, reinforcing intuitive truths in computational logic .

The code uses the logical `and` operator to check two conditions: `x > 0` and `x < 10`. For the print statement to execute, both conditions must be true; thus, if `x` is between 0 and 10, inclusive of 0 but exclusive of 10, the output `x is between 0 and 10` is printed. If either condition fails, the else block will execute, and `x is not between 0 and 10` is printed .

This code initially assigns `a = 12`, `b = 15`, `c = 18`. After reassignment, the variables become `c = 18 // 3 (6)`, `a = 12 // 3 (4)`, `b = 15 // 3 (5)`. The final values are `a = 4`, `b = 5`, `c = 6`, demonstrating how reassignment with calculations and integer division can alter initial values .

You might also like