Code :
a = set()
for n in range(21, 30):
if n % 2 == 0:
a.add(n)
print(a)
Solution and Explanation:
Let's break down the code step by step:
Initialize an empty set:
a = set()
A new empty set named a is created.
Loop through a range of numbers (21 to 29):
for n in range(21, 30):
The for loop iterates over the numbers from 21 to 29 (inclusive).
Check if the number is even:
if n % 2 == 0:
The if statement checks if the current number (n) is even by using the modulo operator (%) to check if it's divisible by 2.
Add even numbers to the set:
a.add(n)
If the condition in the if statement is true (meaning n is even), the current even number is added to the set a using the add method.
Print the set after the loop:
print(a)
The print(a) statement is outside the for loop, so it will be executed after the loop has finished. It prints the final contents of the set a.
In summary, when you run this code, it will output the set of even numbers between 21 and 29. In this specific case, the set a will contain the even numbers 22, 24, 26, and 28.
0 Comments:
Post a Comment