python programming 2024-25 and 2022-23
python programming 2024-25 and 2022-23
Welcome Message
python
Copy code
message = input("Enter a welcome message: ")
print("Welcome message:", message)
Input: Enter a welcome message: Hello, World!
Output: Welcome message: Hello, World!
mathematica
Copy code
Larger number: 20
Smaller number: 10
3. Largest/Smallest of Three Numbers
python
Copy code
a, b, c = map(int, input("Enter three numbers: ").split())
print("Largest number:", max(a, b, c))
print("Smallest number:", min(a, b, c))
Input:
mathematica
Copy code
Enter three numbers: 5 10 3
Output:
yaml
Copy code
Largest number: 10
Smallest number: 3
4. Patterns Using Nested Loops
Pattern-1
python
Copy code
for i in range(1, 6):
print('*' * i)
Output:
markdown
Copy code
*
**
***
****
*****
Pattern-2
python
Copy code
for i in range(5, 0, -1):
print(''.join(str(x) for x in range(1, i + 1)))
Output:
yaml
Copy code
12345
1234
123
12
1
Pattern-3
python
Copy code
for i in range(1, 6):
print(''.join(chr(65 + x) for x in range(i)))
Output:
css
Copy code
A
AB
ABC
ABCD
ABCDE
5. Sum of Series
Series 1
python
Copy code
x, n = map(int, input("Enter x and n: ").split())
s1 = sum(x**i for i in range(n + 1))
print("Sum of series 1:", s1)
Input:
mathematica
Copy code
Enter x and n: 2 3
Output:
mathematica
Copy code
Sum of series 1: 15
Series 2
python
Copy code
s2 = sum((-1)**i * x**i for i in range(n + 1))
print("Sum of series 2:", s2)
Input:
mathematica
Copy code
Enter x and n: 2 3
Output:
mathematica
Copy code
Sum of series 2: 1
Series 3
python
Copy code
s3 = sum(x**i / (i + 1) for i in range(n + 1))
print("Sum of series 3:", s3)
Input:
mathematica
Copy code
Enter x and n: 2 3
Output:
mathematica
Copy code
Sum of series 3: 3.0
Series 4
python
Copy code
s4 = sum(x**i / (i + 1)! for i in range(n + 1))
from math import factorial as f
s4 = sum(x**i / f(i) for i in range(n + 1))
print("Sum of series 4:", s4)
Input:
mathematica
Copy code
Enter x and n: 2 3
Output:
mathematica
Copy code
Sum of series 4: 3.0
6. Number Checks: Perfect, Armstrong, Palindrome
python
Copy code
def is_perfect(n):
return sum(i for i in range(1, n) if n % i == 0) == n
def is_armstrong(n):
return sum(int(d)**len(str(n)) for d in str(n)) == n
def is_palindrome(n):
return str(n) == str(n)[::-1]
css
Copy code
Enter a number: 28
Output:
csharp
Copy code
28 is perfect, not Armstrong, not palindrome.
7. Prime or Composite
python
Copy code
num = int(input("Enter a number: "))
is_prime = all(num % i != 0 for i in range(2, int(num**0.5) + 1))
print(f"{num} is {'prime' if is_prime and num > 1 else 'composite'}.")
Input:
css
Copy code
Enter a number: 7
Output:
csharp
Copy code
7 is prime.
8. Fibonacci Series
python
Copy code
n = int(input("Enter number of terms: "))
fib = [0, 1]
for i in range(2, n):
fib.append(fib[i-1] + fib[i-2])
print("Fibonacci series:", fib[:n])
Input:
mathematica
Copy code
Enter number of terms: 5
Output:
less
Copy code
Fibonacci series: [0, 1, 1, 2, 3]
9. GCD and LCM
python
Copy code
from math import gcd
a, b = map(int, input("Enter two integers: ").split())
print("GCD:", gcd(a, b))
print("LCM:", a * b // gcd(a, b))
Input:
mathematica
Copy code
Enter two integers: 12 15
Output:
makefile
Copy code
GCD: 3
LCM: 60
10. Count Vowels, Consonants, Uppercase, Lowercase
python
Copy code
s = input("Enter a string: ")
vowels = sum(1 for char in s if char.lower() in 'aeiou')
consonants = sum(1 for char in s if char.isalpha() and char.lower() not in 'aeiou')
uppercase = sum(1 for char in s if char.isupper())
lowercase = sum(1 for char in s if char.islower())
c
Copy code
Enter a string: Hello World!
Output:
yaml
Copy code
Vowels: 3, Consonants: 7, Uppercase: 2, Lowercase: 8
11. Palindrome Check and Case Conversion
python
Copy code
s = input("Enter a string: ")
print(f"Is palindrome: {s == s[::-1]}")
print("Converted case:", s.swapcase())
Input:
c
Copy code
Enter a string: Hello
Output:
vbnet
Copy code
Is palindrome: False
Converted case: hELLO
12. Largest/Smallest in a List/Tuple
python
Copy code
numbers = list(map(int, input("Enter numbers separated by space: ").split()))
print("Largest:", max(numbers))
print("Smallest:", min(numbers))
Input:
csharp
Copy code
Enter numbers separated by space: 10 20 5 15
Output:
makefile
Copy code
Largest: 20
Smallest: 5
13. Swap Elements at Even and Odd Indices
python
Copy code
lst = list(map(int, input("Enter numbers separated by space: ").split()))
for i in range(1, len(lst), 2):
lst[i], lst[i-1] = lst[i-1], lst[i]
print("Swapped list:", lst)
Input:
csharp
Copy code
Enter numbers separated by space: 1 2 3 4 5
Output:
less
Copy code
Swapped list: [2, 1, 4, 3, 5]
14. Search for an Element in a List/Tuple
python
Copy code
elements = input("Enter elements separated by space: ").split()
target = input("Enter element to search: ")
print(f"{target} {'found' if target in elements else 'not found'}.")
Input:
mathematica
Copy code
Enter elements separated by space: apple banana cherry
Enter element to search: banana
Output:
Copy code
banana found.
15. Dictionary for Student Marks
python
Copy code
n = int(input("Enter number of students: "))
students = {}
for _ in range(n):
roll_no = input("Enter roll number: ")
name = input("Enter name: ")
marks = int(input("Enter marks: "))
students[roll_no] = {'name': name, 'marks': marks}
mathematica
Copy code
Enter number of students: 3
Enter roll number: 1
Enter name: Alice
Enter marks: 80
Enter roll number: 2
Enter name: Bob
Enter marks: 70
Enter roll number: 3
Enter name: Charlie
Enter marks: 90
Output:
csharp
Copy code
Students with marks above 75:
Alice
Charlie
These programs cover all your requirements and are designed to be simple and clear!
Feel free to run them in your Python environment.
You said:
1. Input your name and welcome message and display it appropriately to wish you.
2. Input two numbers and display the larger & smaller number.
3. Input three numbers and display the largest & smallest number.
4. Generate the following patterns using nested loop.
Pattern-1 Pattern-2 Pattern-3
* 1 2 3 4 5 A
** 1 2 3 4 AB
*** 1 2 3 ABC
**** 1 2 ABCD
***** 1 ABCDE
5. Write a program to input the value of x and n and print the sum of the following
series:
o 1+x+x2
+x3
+x4
+. ..........xn
o 1-x+x2
-x
3
+x4
.................................xn
o x - x
2
+ x3
- x
4
+ ............xn
2 3 4 n
o x + x2
- x
3 + x
4
...................................xn
2! 3! 4! n!
6. Determine whether a number is a perfect number, an Armstrong number or a
palindrome.
7. Input a number and check if the number is a prime or composite number.
8. Display the N terms of a Fibonacci series.
9. Compute the greatest common divisor and least common multiple of two integers.
10. Count and display the number of vowels, consonants, uppercase, lowercase
characters in string.
11. Input a string and determine whether it is a palindrome or not; convert the
case of characters in a string (
Toggle Case ).
12. Accept a Number and Reverse it. Also check whether a given number is a
Palindrome or not
13. Accept a Number and Display the Multiplication Table of a Number
14. To print the words starting with a particular alphabet in a user entered
string.
15. To print the number of occurrences of a given alphabet in a given string.
16. Accept a String and count the alphabets and numbers in it separately.
17. Accept a Number and Display the Factors of it.
18. To find the factorial of an accepted Number
32. Write the Python Program to accept a List of elements and Multiply the Odd
elements by 7 and divide the
even Elements by 7and take the quotient.
33. Write the Python Program to accept the List of elements and insert a number
into the List and arrange the
list elements in sorted order.
34. Write the Python Program to accept the List / Tuple elements and find the
second largest element in the
List.
35. Write the Python Program to accept the List of elements and delete all the
occurrences of an element
from the List.
36. Write the Python Program to accept the List / Tuple of elements and display all
Prime numbers in the List.
37. Write the Python Program to accept the List / Tuple of elements and display all
Armstrong Numbers in the
List. ( Armstrong Numbers are those numbers whose sum of cubes of its digits is the
number itself )
38. Write the Python Program to accept two Lists of elements and
Merge/concatenate/join them.
39. Write the Python Program to calculate the mean of a given List / Tuple of
numbers/elements.
40. Write the Python Program to find the frequencies of all elements of a List /
Tuple.
41. Write the Python Program to input two lists and display the maximum element
from the elements of both
the list combined, along with its index in its list.
42. Write the Python Program to find the sum of all elements in a List / Tuple.
43. Write the Python Program to accept a List of elements and create two new lists,
one having all positive
numbers and the other having all negative numbers from the given list. Print all
three lists.
44. Write a Python Program to read a List / Tuple of integers and find the median.
(The Median Value of a list of values is the middle one when they are arranged in
order. If there are two
middle values then take the average.)
45. Write the Python Program to read a list of elements. Modify this list so that
it does not contain any
duplicate elements. i.e all elements occurring multiple times in the list should
appear only once.
46. Write the Python Program to reverse a list of integers.
write input and output for each program. each program should be in simplified way
ChatGPT said:
ChatGPT
Here's a collection of simplified Python programs for each of the tasks you've
listed, complete with sample inputs and outputs.
less
Copy code
Enter your name: Alice
Enter a welcome message: Have a great day!
Output:
css
Copy code
Welcome Alice! Have a great day!
2. Larger & Smaller of Two Numbers
python
Copy code
a, b = map(int, input("Enter two numbers: ").split())
print("Larger number:", max(a, b))
print("Smaller number:", min(a, b))
Input:
mathematica
Copy code
Enter two numbers: 10 20
Output:
mathematica
Copy code
Larger number: 20
Smaller number: 10
3. Largest & Smallest of Three Numbers
python
Copy code
a, b, c = map(int, input("Enter three numbers: ").split())
print("Largest number:", max(a, b, c))
print("Smallest number:", min(a, b, c))
Input:
mathematica
Copy code
Enter three numbers: 5 10 3
Output:
yaml
Copy code
Largest number: 10
Smallest number: 3
4. Generate Patterns
python
Copy code
# Pattern 1
for i in range(1, 6):
print('*' * i)
# Pattern 2
for i in range(1, 6):
print(' '.join(str(x) for x in range(1, 6 - i + 1)))
# Pattern 3
for i in range(1, 6):
print(' '.join(chr(64 + x) for x in range(1, i + 1)))
Output:
markdown
Copy code
*
**
***
****
*****
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
A
AB
ABC
ABCD
ABCDE
5. Sum of Series
python
Copy code
x, n = map(int, input("Enter x and n: ").split())
s1 = sum(x**i for i in range(n + 1))
s2 = sum((-1)**i * x**i for i in range(n + 1))
s3 = sum(x**i / (i + 1) for i in range(n + 1))
from math import factorial as f
s4 = sum(x**i / f(i) for i in range(n + 1))
mathematica
Copy code
Enter x and n: 2 3
Output:
mathematica
Copy code
Sum of series 1: 15
Sum of series 2: 1
Sum of series 3: 3.0
Sum of series 4: 3.0
6. Perfect Number, Armstrong Number, Palindrome
python
Copy code
def is_perfect(n):
return sum(i for i in range(1, n) if n % i == 0) == n
def is_armstrong(n):
return sum(int(d)**3 for d in str(n)) == n
def is_palindrome(n):
return str(n) == str(n)[::-1]
css
Copy code
Enter a number: 28
Output:
csharp
Copy code
28 is perfect, not Armstrong, not palindrome.
7. Prime or Composite
python
Copy code
num = int(input("Enter a number: "))
is_prime = all(num % i != 0 for i in range(2, int(num**0.5) + 1)) and num > 1
print(f"{num} is {'prime' if is_prime else 'composite'}.")
Input:
css
Copy code
Enter a number: 7
Output:
csharp
Copy code
7 is prime.
8. Fibonacci Series
python
Copy code
n = int(input("Enter number of terms: "))
fib = [0, 1]
for i in range(2, n):
fib.append(fib[i-1] + fib[i-2])
print("Fibonacci series:", fib[:n])
Input:
mathematica
Copy code
Enter number of terms: 5
Output:
less
Copy code
Fibonacci series: [0, 1, 1, 2, 3]
9. GCD and LCM
python
Copy code
from math import gcd
a, b = map(int, input("Enter two integers: ").split())
print("GCD:", gcd(a, b))
print("LCM:", a * b // gcd(a, b))
Input:
mathematica
Copy code
Enter two integers: 12 15
Output:
makefile
Copy code
GCD: 3
LCM: 60
10. Count Vowels, Consonants, Uppercase, Lowercase
python
Copy code
s = input("Enter a string: ")
vowels = sum(1 for char in s if char.lower() in 'aeiou')
consonants = sum(1 for char in s if char.isalpha() and char.lower() not in 'aeiou')
uppercase = sum(1 for char in s if char.isupper())
lowercase = sum(1 for char in s if char.islower())
c
Copy code
Enter a string: Hello World!
Output:
yaml
Copy code
Vowels: 3, Consonants: 7, Uppercase: 2, Lowercase: 8
11. Palindrome Check and Case Conversion
python
Copy code
s = input("Enter a string: ")
print(f"Is palindrome: {s == s[::-1]}")
print("Converted case:", s.swapcase())
Input:
c
Copy code
Enter a string: Hello
Output:
vbnet
Copy code
Is palindrome: False
Converted case: hELLO
12. Reverse a Number and Check Palindrome
python
Copy code
num = int(input("Enter a number: "))
reverse_num = int(str(num)[::-1])
is_palindrome = num == reverse_num
print("Reversed number:", reverse_num)
print(f"{num} is {'a palindrome' if is_palindrome else 'not a palindrome'}.")
Input:
css
Copy code
Enter a number: 121
Output:
csharp
Copy code
Reversed number: 121
121 is a palindrome.
13. Multiplication Table
python
Copy code
num = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
Input:
css
Copy code
Enter a number: 5
Output:
python
Copy code
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
14. Words Starting with a Particular Alphabet
python
Copy code
string = input("Enter a string: ")
char = input("Enter a character: ")
words = [word for word in string.split() if word.startswith(char)]
print("Words starting with", char + ":", words)
Input:
less
Copy code
Enter a string: Apple banana apricot orange
Enter a character: a
Output:
less
Copy code
Words starting with a: ['apple', 'apricot']
15. Occurrences of a Given Alphabet
python
Copy code
string = input("Enter a string: ")
char = input("Enter a character to count: ")
count = string.count(char)
print(f"The character '{char}' occurs {count} times.")
Input:
less
Copy code
Enter a string: hello world
Enter a character to count: l
Output:
bash
Copy code
The character 'l' occurs 3 times.
16. Count Alphabets and Numbers
python
Copy code
s = input("Enter a string: ")
alphabets = sum(c.isalpha() for c in s)
numbers = sum(c.isdigit() for c in s)
c
Copy code
Enter a string: Hello123
Output:
yaml
Copy code
Alphabets: 5, Numbers: 3
17. Display Factors of a Number
python
Copy code
num = int(input("Enter a number: "))
factors = [i for i in range(1, num + 1) if num % i == 0]
print("Factors:", factors)
Input:
css
Copy code
Enter a number: 12
Output:
makefile
Copy code
Factors: [1, 2, 3, 4, 6, 12]
18. Factorial of a Number
python
Copy code
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("Factorial:", factorial)
Input:
css
Copy code
Enter a number: 5
Output:
makefile
Copy code
Factorial: 120
19. Largest/Smallest in a List
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
print("Largest number:", max(numbers))
print("Smallest number:", min(numbers))
Input:
csharp
Copy code
Enter numbers separated by spaces: 3 1 4 1 5
Output:
yaml
Copy code
Largest number: 5
Smallest number: 1
20. Largest/Smallest in a Tuple
python
Copy code
numbers = tuple(map(int, input("Enter numbers separated by spaces: ").split()))
print("Largest number:", max(numbers))
print("Smallest number:", min(numbers))
Input:
csharp
Copy code
Enter numbers separated by spaces: 3 1 4 1 5
Output:
yaml
Copy code
Largest number: 5
Smallest number: 1
21. Swap Elements in a List
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
for i in range(0, len(numbers) - 1, 2):
numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i]
print("Swapped list:", numbers)
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 2 3 4 5 6
Output:
less
Copy code
Swapped list: [2, 1, 4, 3, 6, 5]
22. Search Element in a List
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
element = int(input("Enter element to search: "))
found = element in numbers
print(f"{element} is {'found' if found else 'not found'} in the list.")
Input:
mathematica
Copy code
Enter numbers separated by spaces: 1 2 3 4 5
Enter element to search: 3
Output:
csharp
Copy code
3 is found in the list.
23. Search Element in a Tuple
python
Copy code
elements = tuple(input("Enter elements separated by spaces: ").split())
element = input("Enter element to search: ")
found = element in elements
print(f"{element} is {'found' if found else 'not found'} in the tuple.")
Input:
less
Copy code
Enter elements separated by spaces: a b c d
Enter element to search: b
Output:
python
Copy code
b is found in the tuple.
24. Dictionary of Students
python
Copy code
n = int(input("Enter number of students: "))
students = {}
for _ in range(n):
roll = input("Enter roll number: ")
name = input("Enter name: ")
marks = int(input("Enter marks: "))
students[roll] = (name, marks)
mathematica
Copy code
Enter number of students: 2
Enter roll number: 1
Enter name: Alice
Enter marks: 80
Enter roll number: 2
Enter name: Bob
Enter marks: 70
Output:
csharp
Copy code
Students with marks above 75:
Alice
25. Count Odd and Even Numbers
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
odd_count = sum(1 for n in numbers if n % 2 != 0)
even_count = sum(1 for n in numbers if n % 2 == 0)
print(f"Odd numbers: {odd_count}, Even numbers: {even_count}")
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 2 3 4 5
Output:
yaml
Copy code
Odd numbers: 3, Even numbers: 2
26. Count Frequency of Elements
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
frequency = {n: numbers.count(n) for n in set(numbers)}
print("Frequency of each number:", frequency)
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 2 2 3 3 3
Output:
yaml
Copy code
Frequency of each number: {1: 1, 2: 2, 3: 3}
27. Sum of Elements in a Tuple
python
Copy code
numbers = tuple(map(int, input("Enter numbers separated by spaces: ").split()))
print("Sum of elements:", sum(numbers))
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 2 3 4
Output:
mathematica
Copy code
Sum of elements: 10
28. Delete an Element from a Tuple
python
Copy code
elements = tuple(input("Enter elements separated by spaces: ").split())
element_to_delete = input("Enter element to delete: ")
new_tuple = tuple(e for e in elements if e != element_to_delete)
print("New tuple:", new_tuple)
Input:
less
Copy code
Enter elements separated by spaces: a b c d
Enter element to delete: b
Output:
arduino
Copy code
New tuple: ('a', 'c', 'd')
29. Count Occurrences of a Number in a Tuple
python
Copy code
elements = tuple(map(int, input("Enter numbers separated by spaces: ").split()))
number = int(input("Enter number to count: "))
count = elements.count(number)
print(f"{number} occurs {count} times.")
Input:
mathematica
Copy code
Enter numbers separated by spaces: 1 2 3 1 2 1
Enter number to count: 1
Output:
bash
Copy code
1 occurs 3 times.
30. Record of a Student
python
Copy code
roll = input("Enter roll number: ")
name = input("Enter name: ")
marks = input("Enter marks: ")
print(f"Record of Student: Roll: {roll}, Name: {name}, Marks: {marks}")
Input:
mathematica
Copy code
Enter roll number: 1
Enter name: Alice
Enter marks: 90
Output:
yaml
Copy code
Record of Student: Roll: 1, Name: Alice, Marks: 90
31. Dictionary of States and Capitals
python
Copy code
states = {}
while True:
print("1. Accept a Dictionary")
print("2. Append a State and Capital")
print("3. Display the Dictionary")
print("4. Modify a Capital")
print("5. Search for a State")
print("6. Delete a State")
print("7. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
n = int(input("Enter number of states: "))
for _ in range(n):
state = input("Enter state: ")
capital = input("Enter capital: ")
states[state] = capital
elif choice == 2:
state = input("Enter state: ")
capital = input("Enter capital: ")
states[state] = capital
elif choice == 3:
print("Dictionary:", states)
elif choice == 4:
state = input("Enter state to modify: ")
if state in states:
new_capital = input("Enter new capital: ")
states[state] = new_capital
elif choice == 5:
state = input("Enter state to search: ")
print(f"Capital of {state}:", states.get(state, "Not found"))
elif choice == 6:
state = input("Enter state to delete: ")
states.pop(state, None)
elif choice == 7:
break
32. Modify List Based on Odd/Even
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
for i in range(len(numbers)):
if numbers[i] % 2 == 0:
numbers[i] //= 7
else:
numbers[i] *= 7
print("Modified list:", numbers)
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 2 3 4
Output:
less
Copy code
Modified list: [7, 0, 21, 0]
33. Insert and Sort a List
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
num_to_insert = int(input("Enter number to insert: "))
numbers.append(num_to_insert)
numbers.sort()
print("Sorted list:", numbers)
Input:
mathematica
Copy code
Enter numbers separated by spaces: 3 1 4
Enter number to insert: 2
Output:
less
Copy code
Sorted list: [1, 2, 3, 4]
34. Find Second Largest Element
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
unique_numbers = list(set(numbers))
unique_numbers.sort()
second_largest = unique_numbers[-2] if len(unique_numbers) >= 2 else None
print("Second largest number:", second_largest)
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 3 2 4 5
Output:
sql
Copy code
Second largest number: 4
35. Delete All Occurrences of an Element
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
element_to_remove = int(input("Enter element to remove: "))
numbers = [num for num in numbers if num != element_to_remove]
print("List after removal:", numbers)
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 2 2 3 4 2
Enter element to remove: 2
Output:
less
Copy code
List after removal: [1, 3, 4]
36. Display Prime Numbers in a List
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
primes = [n for n in numbers if all(n % i != 0 for i in range(2, int(n**0.5) + 1))
and n > 1]
print("Prime numbers in the list:", primes)
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 2 3 4 5 6
Output:
less
Copy code
Prime numbers in the list: [2, 3, 5]
37. Display Armstrong Numbers in a List
python
Copy code
def is_armstrong(num):
return num == sum(int(digit)**len(str(num)) for digit in str(num))
csharp
Copy code
Enter numbers separated by spaces: 153 94 370 947
Output:
less
Copy code
Armstrong numbers in the list: [153, 370]
38. Merge Two Lists
python
Copy code
list1 = list(map(int, input("Enter first list elements: ").split()))
list2 = list(map(int, input("Enter second list elements: ").split()))
merged_list = list1 + list2
print("Merged list:", merged_list)
Input:
mathematica
Copy code
Enter first list elements: 1 2 3
Enter second list elements: 4 5 6
Output:
less
Copy code
Merged list: [1, 2, 3, 4, 5, 6]
39. Calculate Mean of a List
python
Copy code
numbers = list(map(float, input("Enter numbers separated by spaces: ").split()))
mean = sum(numbers) / len(numbers) if numbers else 0
print("Mean of the list:", mean)
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 2 3 4
Output:
mathematica
Copy code
Mean of the list: 2.5
40. Frequency of Elements in a List
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
frequency = {n: numbers.count(n) for n in set(numbers)}
print("Frequency of elements:", frequency)
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 2 2 3 3 3
Output:
yaml
Copy code
Frequency of elements: {1: 1, 2: 2, 3: 3}
41. Maximum Element from Two Lists
python
Copy code
list1 = list(map(int, input("Enter first list elements: ").split()))
list2 = list(map(int, input("Enter second list elements: ").split()))
combined = list1 + list2
max_elem = max(combined)
index = combined.index(max_elem)
print(f"Maximum element: {max_elem}, Index in combined list: {index}")
Input:
mathematica
Copy code
Enter first list elements: 1 2 3
Enter second list elements: 4 5 6
Output:
yaml
Copy code
Maximum element: 6, Index in combined list: 5
42. Sum of Elements in a List
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
print("Sum of elements:", sum(numbers))
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 2 3 4
Output:
mathematica
Copy code
Sum of elements: 10
43. Separate Positive and Negative Numbers
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
positive = [n for n in numbers if n > 0]
negative = [n for n in numbers if n < 0]
print("Positive numbers:", positive)
print("Negative numbers:", negative)
Input:
csharp
Copy code
Enter numbers separated by spaces: -1 2 -3 4 5
Output:
less
Copy code
Positive numbers: [2, 4, 5]
Negative numbers: [-1, -3]
44. Find Median of a List
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
numbers.sort()
n = len(numbers)
median = (numbers[n // 2] if n % 2 != 0 else (numbers[n // 2 - 1] + numbers[n //
2]) / 2)
print("Median:", median)
Input:
csharp
Copy code
Enter numbers separated by spaces: 3 1 4 2
Output:
makefile
Copy code
Median: 2.5
45. Remove Duplicates from List
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
unique_numbers = list(set(numbers))
print("List without duplicates:", unique_numbers)
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 2 2 3 3 3
Output:
less
Copy code
List without duplicates: [1, 2, 3]
46. Reverse a List of Integers
python
Copy code
numbers = list(map(int, input("Enter numbers separated by spaces: ").split()))
numbers.reverse()
print("Reversed list:", numbers)
Input:
csharp
Copy code
Enter numbers separated by spaces: 1 2 3 4
Output:
less
Copy code
Reversed list: [4, 3, 2, 1]