Saturday 25 November 2023

a = set() for n in range(21, 30): if n % 2 == 0: a.add(n) print(a)

 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.


In one line : a = {n for n in range(21, 30) if n % 2 == 0}






0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (117) C (77) C# (12) C++ (82) Course (62) Coursera (179) coursewra (1) Cybersecurity (22) data management (11) Data Science (95) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (748) Python Coding Challenge (221) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses