List Practice Question ( DS150423 ) - 08-05-2023

Write a python program to create list which have numbers from 1 - 20 except multiple of 2 and 3.

1 Like

l = [i for i in range(1,21) if i%2!=0 and i%3!=0]

1 Like
lst = list(range(1, 21))
for i in lst:
    if i%2 == 0 or i%3 == 0:
        lst.remove(i)

print(lst)



# 2nd Way

lst = []
for i in range(1, 21):
    if i%2 == 0 or i%3 == 0:
        continue
    else: 
        lst.append(i) 

print(lst)

2 Likes
limit=20
i=0
lst = []
while i<=limit:
    if i%6!=0:
        lst.append(i)
    i+=1
print(lst)

Screenshot (349)

2 Likes
size=int(input("Enter a size of list:"))
lst=[]
for x in range(1,size+1):
    if x%2!=0 or x%3!=0:
        lst.append(x)
print("List:",lst)        
        
        
    
1 Like
size = int(input("enter the total no.of elements"))
lst = []
for i in range (size):
    element = int(input(("please enter the element")))
    lst.append(element)
lst2=[]
for x in lst:
    if x>1 and x<20 and x%2 !=0 and x%3 !=0:
      lst2.append(x)

print(lst2)
1 Like

result = [num for num in range(1, 21) if num % 2 != 0 and num % 3 != 0]
print(result)

numbers = list(range(1, 21)) # create a list of numbers from 1 to 20
exclude = # create a list to store the multiples of 2 and 3
for num in numbers:
if num % 2 == 0 or num % 3 == 0:
exclude.append(num)
result = [num for num in numbers if num not in exclude] # create the result list
print(result) # print the result list

result = []
for num in range(1, 21):
    if num % 2 != 0 and num % 3 != 0:
        result.append(num)

print(result)