list-assignment-2
September 11, 2024
[1]: #1.WAP to print the sum of all the list items using a function.
def addition(list1):
sum=0
for i in list1:
sum+=i
return f"sum of list elements = {sum}"
list1=[12,1,23,34]
result=addition(list1)
print(result)
sum of list elements = 70
[2]: #2.WAP to print the sum of all the even items from the list using a function.
def even_sum(list1):
sum=0
for i in list1:
if i%2==0:
sum+=i
return f"sum of all even numbers = {sum}"
list1 = [12,1,23,34]
even_sum(list1)
[2]: 'sum of all even numbers = 46'
[3]: #3.WAP to print the sum of all the odd items from the list using a function.
def sum_odd(list1):
sum=0
for i in range(len(list1)):
if i%2!=0:
sum+=i
return f"sum of all odd items from the list = {sum}"
list1=[1,2,3,4,5,6]
sum_odd(list1)
[3]: 'sum of all odd items from the list = 9'
1
[4]: #4.WAP to print the sum of all the even position items from the list using a␣
↪function.
def even_position_sum(list1):
sum=0
for i in range(len(list1)):
if i%2==0:
sum+=list1[i]
return f"sum of elements at even index = {sum}"
list1=[1,2,3,4,5,6]
even_position_sum(list1)
[4]: 'sum of elements at even index = 9'
[5]: # 5.WAP to print the sum of all the odd position items from the list using a␣
↪function.
def odd_position_sum(list1):
sum=0
for i in range(len(list1)):
if i%2!=0:
sum+=list1[i]
return f"sum of elements at odd index = {sum}"
list1=[1,2,3,4,5,6]
odd_position_sum(list1)
[5]: 'sum of elements at odd index = 12'
[6]: #6.WAP to print the average of all the list items using a function.
def average(list1):
sum=0
for i in range(len(list1)):
sum=sum+list1[i]
average=sum/5
return f"average of all items in the list = {average}"
list1=[1,2,3,4,5]
average(list1)
[6]: 'average of all items in the list = 3.0'
[7]: #7.WAP to print the average of all the even items from the list using a␣
↪function.
def average_even_sum(list1):
sum=0
for i in list1:
if i%2==0:
sum=sum+i
average=sum/6
return f"average of all even items from the list = {average}"
2
list1=[1,2,3,4,5,6]
average_even_sum(list1)
[7]: 'average of all even items from the list = 2.0'
[8]: #8.WAP to print the average of all the odd items from the list using a function.
def average_odd_sum(list1):
sum=0
for i in list1:
if i%2!=0:
sum=sum+i
average=sum/6
return f"average of all odd items from the list = {average}"
list1=[1,2,3,4,5,6]
average_odd_sum(list1)
[8]: 'average of all odd items from the list = 1.5'
[9]: #9.WAP to print the count of all the even items from the list using a function.
def even_count(list1):
count=0
for i in list1:
if i%2==0:
count+=1
return f"count of all even items from the list = {count}"
list1=[1,2,3,4,5,6]
even_count(list1)
[9]: 'count of all even items from the list = 3'
[12]: #10. WAP to print the count all the items from the list
def count_all(list1):
count=0
for i in list1:
count+=1
return f"count of all items from the list = {count}"
list1=[1,2,3,4,5,6,7]
count_all(list1)
[12]: 'count of all items from the list = 7'
[10]: #11.WAP to print the count of all the odd items from the list using a function.
def odd_count(list1):
count=0
for i in list1:
if i%2!=0:
count+=1
3
return f"count of all odd items from the list = {count}"
list1=[1,2,3,4,5,6,7,8,9]
odd_count(list1)
[10]: 'count of all odd items from the list = 5'
[1]: #12 WAP print all the palindrome items from the list
def palindrome_list(lst):
for num in lst:
temp = num
rev = 0
while num > 0:
rem = num % 10
rev = rev * 10 + rem
num //= 10
if temp == rev:
print(temp, "is a palindrome")
numbers = [121, 123, 131, 456, 565]
palindrome_list(numbers)
121 is a palindrome
131 is a palindrome
565 is a palindrome
[2]: #13. WAP print all the prime items from the list
def print_prime_numbers(numbers):
for num in numbers:
if num < 2:
continue
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num)
numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
print_prime_numbers(numbers)
2
3
5
7
11
[3]: def armstrong_numbers(numbers):
for num in numbers:
4
temp = num
sum = 0
order = len(str(num))
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
if num == sum:
print(num)
numbers = [153, 370, 371, 407, 123, 234]
print("Armstrong numbers in the list:")
armstrong_numbers(numbers)
Armstrong numbers in the list:
153
370
371
407
[5]: #15. WAP print all the sum of prime items from the list
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
def sum_of_primes(numbers_list):
total_sum = 0
for num in numbers_list:
if is_prime(num):
total_sum += num
return total_sum
numbers_list = [2, 3, 4, 5, 6, 7, 8, 9, 11]
total_sum = sum_of_primes(numbers_list)
print(f"Sum of prime numbers in the list: {total_sum}")
Sum of prime numbers in the list: 28
[6]: #16. WAP print all the sum of palindrome items from the list
def is_palindrome(num):
num_str = str(num)
return num_str == num_str[::-1]
def sum_of_palindromes(numbers_list):
5
total_sum = 0
for num in numbers_list:
if is_palindrome(num):
total_sum += num
return total_sum
numbers_list = [121, 123, 484, 232, 345, 676]
total_sum = sum_of_palindromes(numbers_list)
print(f"Sum of palindrome numbers in the list: {total_sum}")
Sum of palindrome numbers in the list: 1513
[7]: #17. WAP print all the sum of Armstrong items from the list
def is_armstrong(num):
original_num = num
sum = 0
count = 0
while num > 0:
count += 1
num //= 10
num = original_num
while num > 0:
rem = num % 10
sum += rem ** count
num //= 10
return original_num == sum
def sum_of_armstrong(numbers_list):
total_sum = 0
for num in numbers_list:
if is_armstrong(num):
total_sum += num
return total_sum
numbers_list = [153, 370, 407, 9474, 123]
total_sum = sum_of_armstrong(numbers_list)
print(f"Sum of Armstrong numbers in the list: {total_sum}")
Sum of Armstrong numbers in the list: 10404
[8]: #18. WAP print the count of palindrom items from the list
def is_palindrome(num):
num_str = str(num)
6
return num_str == num_str[::-1]
def count_palindromes(numbers_list):
count = 0
for num in numbers_list:
if is_palindrome(num):
count += 1
return count
numbers_list = [121, 123, 484, 232, 345, 676, 10]
count = count_palindromes(numbers_list)
print(f"Count of palindrome numbers in the list: {count}")
Count of palindrome numbers in the list: 4
[14]: #19. WAP print the count of prime items from the list
def is_prime(num):
if num <= 1:
return False
if num == 2:
return True # 2 is the only even prime number
if num % 2 == 0:
return False # Other even numbers are not prime
for i in range(3, int(num**0.5) + 1, 2):
if num % i == 0:
return False
return True
def count_primes(numbers_list):
count = 1
for num in numbers_list:
if is_prime(num):
count += 1
return count
numbers_list = [2, 3, 4, 5, 6, 7, 8, 9, 11]
count = count_primes(numbers_list)
print(f"Count of prime numbers in the list: {count}")
Count of prime numbers in the list: 6
[15]: #20. WAP print he count of Armstrong items from the list
def is_armstrong(num):
original_num = num
sum = 0
count = 0
7
while num > 0:
count += 1
num //= 10
num = original_num
while num > 0:
rem = num % 10
sum += rem ** count
num //= 10
return original_num == sum
def count_armstrong(numbers_list):
count = 0
for num in numbers_list:
if is_armstrong(num):
count += 1
return count
numbers_list = [153, 370, 407, 9474, 123, 10, 9474]
count = count_armstrong(numbers_list)
print(f"Count of Armstrong numbers in the list: {count}")
Count of Armstrong numbers in the list: 5
[16]: #21. seprate the even and odd items from the list in different list
#list1=[10,10,20,30,5,3,9]
list1 = [10, 10, 20, 30, 5, 3, 9]
even_list = []
odd_list = []
for num in list1:
if num % 2 == 0:
even_list.append(num)
else:
odd_list.append(num)
print("Even numbers:", even_list)
print("Odd numbers:", odd_list)
Even numbers: [10, 10, 20, 30]
Odd numbers: [5, 3, 9]
8
[1]: # 22. all the even itmes shoud be in left side and odd items should be in right␣
↪side.
def even_odd_sort(lst):
even = []
odd = []
for num in lst:
if num % 2 == 0:
even.append(num)
else:
odd.append(num)
return even + odd
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
result = even_odd_sort(my_list)
print(result)
[2, 4, 6, 8, 1, 3, 5, 7, 9]
[3]: #23. all the zeros (0) should be in left side of the list and 1 should be in␣
↪roght side.
#list2=[1,0,1,0,0,1,1]
#output: [0,0,0,1,1,1,1]
def all_zeros(lst):
zero = []
one = []
for num in lst:
if num == 0:
zero.append(num)
else:
one.append(num)
return zero + one
my_list = [1,0,1,0,0,1,1]
result = all_zeros(my_list)
print(result)
[0, 0, 0, 1, 1, 1, 1]
[ ]: