1.
Write a program to swap two numbers using a third
variable.
a = int(input("Enter an Integer: "))
b = int(input("Enter an another Integer: "))
print("Numbers BEFORE swapping: ")
print("a = ",a,"b = ",b)
temp = a
a = b
b = temp
print("Numbers AFTER swapping: ")
print("a = ",a,"b = ",b)
OUTPUT:
Enter an Integer: 2
Enter an another Integer: 3
Numbers BEFORE swapping:
a= 2b= 3
Numbers AFTER swapping:
a= 3b= 2
2
2. Write a program to enter two integers and perform
all arithmetic operations on them.
a = int(input("Enter an Integer: "))
b = int(input("Enter an another Integer: "))
print("Given Numbers are: ")
print("a = ",a,", b = ",b)
print("Addition: ", a+b)
print("Subtraction: ", a-b)
print("Multiplication: ",a*b)
print("Division: ", a/b)
print("Exponent: ", a**b)
print("Floor division: ",a//b)
print("Modulas division: ", a%b)
Enter an Integer: 25
Enter an another Integer: 6
Given Numbers are:
a = 25 , b = 6
Addition: 31
Subtraction: 19
3
Multiplication: 150
Division: 4.166666666666667
3. Write a program that prints minimum and maximum
of five numbers entered by the user.
(Hint: Use nested If statements only)
# Input five numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
num4 = float(input("Enter the fourth number: "))
num5 = float(input("Enter the fifth number: "))
# Assume the first number is both minimum and maximum
initially
minimum = num1
maximum = num1
# Check and update for minimum and maximum using nested
if statements
if num2 < minimum:
minimum = num2
else:
if num2 > maximum:
maximum = num2
10
if num3 < minimum:
minimum = num3
else:
if num3 > maximum:
maximum = num3
if num4 < minimum:
minimum = num4
else:
if num4 > maximum:
maximum = num4
if num5 < minimum:
minimum = num5
else:
if num5 > maximum:
maximum = num5
# Display the minimum and maximum
print("Minimum number:", minimum)
print("Maximum number:", maximum)
OUTPUT:
Enter the first number: -1
Enter the second number: 5
Enter the third number: 3
11
Enter the fourth number: 7
4. Write a function to print the table of a given number.
The number has to be entered by the user.
(Hint: Use for loop only)
# User-defined function to generate multiplication table
def print_multiplication_table(number):
print("Multiplication Table for", number, ":")
for i in range(1, 11):
result = number * i
print(number, "x", i, "=", result)
# Input the number for which you want the table
number = int(input("Enter the number: "))
# Call the user-defined function
print_multiplication_table(number)
OUTPUT:
Enter the number: 5
Multiplication Table for 5 :
5x1=5
5 x 2 = 10
5 x 3 = 15 15
5 x 4 = 20
5. Write a program to find the sum of digits of an
integer number, input by the user.
(Hint: Use While loop only)
# Input an integer number from the user
number = int(input("Enter an integer number: "))
# Initialize sum and a temporary variable to store the
original number
sum_of_digits = 0
temp_number = abs(number) # Handle negative numbers by
taking the absolute value
# Calculate the sum of digits using a while loop
while temp_number > 0:
digit = temp_number % 10
sum_of_digits += digit
temp_number //= 10
# Display the sum of digits
print("Sum of digits in the entered number:",
sum_of_digits)
OUTPUT:
17
Enter an integer number: 345
Sum of digits in the entered number: 12
6. Write a program to check whether an input number is a
palindrome or not. (Hint: Can’t use slicing operation)
# Input a number from the user
number = int(input("Enter a number: "))
original_number = number
# Initialize variables for reversing the number
reversed_number = 0
# Reverse the number using a while loop
while number > 0:
digit = number % 10
reversed_number = (reversed_number * 10) + digit
number //= 10
# Check if the reversed number is equal to the original
if original_number == reversed_number:
print(original_number, "is a palindrome.")
else:
print(original_number, "is not a palindrome.")
OUTPUT:
18
Enter a number: 12321
12321 is a palindrome.
7. Write a program to print the following patterns:
12345
1234
123
12
(Hint: Use nested for loop statement only)
# Number of rows in the pattern
rows = 5
# Loop to print the pattern
for i in range(rows, 0, -1):
for j in range(1, i + 1):
print(j, end=" ")
print()
OUTPUT:
12345
1234
123
12 19
1
8. Write a program to input line(s) of text from the user until
enter is pressed. Count the total number of characters in
the text (including white spaces), total number of
alphabets, total number of digits, total number of special
symbols and total number of words in the given text.
(Assume that each word is separated by one space).
# Initialize counters
total_characters = 0
total_alphabets = 0
total_digits = 0
total_special_symbols = 0
total_words = 0
# Input lines of text from the user until Enter is
pressed
print("Enter text (Press Enter to finish):")
while True:
line = input()
if line == "":
break # Exit the loop when Enter is pressed
total_characters += len(line)
total_words += len(line.split()) # Count words based
on spaces
25
for char in line:
if char.isalpha():
total_alphabets += 1
elif char.isdigit():
total_digits += 1
else:
total_special_symbols += 1
# Display the results print("\
nResults:")
print("Total Characters:", total_characters)
print("Total Alphabets:", total_alphabets)
print("Total Digits:", total_digits)
print("Total Special Symbols:", total_special_symbols)
print("Total Words:", total_words)
OUTPUT:
Enter text (Press Enter to finish):
leArning python and, becoming a pythonista in 2024 is an amazing journey!
Results:
Total Characters: 73
Total Alphabets: 56
Total Digits: 4
Total Special Symbols: 13
26
Total Words: 12
9. Write a program to find the number of times an
element occurs in the list.
# Input a list from the user using eval()
user_list = eval(input("Enter a list of elements: "))
# Input the element to count occurrences
element_to_count = eval(input("Enter the element to count
occurrences: "))
# Count occurrences using the count method
occurrences_count = user_list.count(element_to_count)
# Display the result without using format
print("The element '", element_to_count, "' occurs",
occurrences_count, "times in the list.")
OUTPUT:
Enter a list of elements: [1,2,3,2,4,3]
Enter the element to count occurrences: 2
The element ' 2 ' occurs 2 times in the list.
Enter a list of elements: ['a','b','c','a','b']
Enter the element to count occurrences: 'a'
30 The element ' a ' occurs 2 times in the list.
10. Write a 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.
# Input a list from the user
user_list = eval(input("Enter a list of elements: "))
# Remove duplicates using a loop
unique_list = []
for element in user_list:
if element not in unique_list:
unique_list.append(element)
# Display the modified list without duplicates
print("Original List:", user_list)
print("List without Duplicates:", unique_list)
OUTPUT:
Enter a list of elements: [1,2,1,3,4,2,5]
Original List: [1, 2, 1, 3, 4, 2, 5]
List without Duplicates: [1, 2, 3, 4, 5]
33
11. Write a Python program to create a dictionary from a
string.
Note: Track the count of the letters from the string. Sample
string : ‘2nd pu course’
Expected output : {‘ ‘:2, ‘2’:1, ‘n’:1, ‘d’:1, ‘o’:1, ‘p’:1, ‘u’:2, ’c’:1,
‘s’:1, ‘r’:1, ‘e’:1}
# Accept any string as input
input_string = input("Enter a string: ")
# Initialize an empty dictionary to store character
counts
char_counts = {}
# Iterate through each character in the input string
for char in input_string:
# Update the count in the dictionary
char_counts[char] = char_counts.get(char, 0) + 1
# Display the resulting dictionary
print("Input String:", input_string)
print("Character Counts:", char_counts)
OUTPUT:
38
Enter a string: 2nd puc course
Input String: 2nd puc course
Character Counts: {'2': 1, 'n': 1, 'd': 1, ' ': 2, 'p': 1, 'u': 2, 'c': 2, 'o': 1, 'r': 1, 's': 1, 'e': 1}
12. Create a dictionary with the roll number, name and marks of n students
in a class and display the names of students who have marks above 75.
# Input the number of students
n = int(input("Enter the number of students: "))
# Initialize an empty dictionary to store student information
students_dict = {}
# Input student details (roll number, name, and marks) for i in range(n):
roll_number = input(f"Enter roll number for student
{i+1}: ")
name = input(f"Enter name for student {i+1}: ") marks = float(input(f"Enter marks for student {i+1}:
"))
# Store details in the dictionary students_dict[roll_number] = {'name': name, 'marks':
marks}
# Display the names of students with marks above 75 print("Names of students with marks above
75:")
for roll_number, details in students_dict.items(): if details['marks'] > 75:
print(details['name'])
OUTPUT:
Enter the number of students: 3 Enter roll number for student 1: 101 Enter name for student 1: aaa
Enter marks for student 1: 55
Enter roll number for student 2: 102 Enter name for student 2: bbb Enter marks for student 2: 78
Enter roll number for student 3: 103 Enter name for student 3: ccc
Enter marks for student 3: 90
Names of students with marks above 75: bbb
ccc