python practical file
python practical file
SESSION: 2024-25
Basics of Python
Programming
(BCAP 211)
II Year,3Sem
SOURCE CODE:
OUTPUT:
EXPERIMENT 2
AIM-: Write a program to check whether a number is an Armstrong number or not.
SOURCE CODE-:
def is_armstrong(n):
num_str = str(n)
num_digits = len(num_str)
sum_cubes = sum(int(digit) ** num_digits for digit in num_str)
return sum_cubes == n
if is_armstrong(num):
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
OUTPUT-:
EXPERIMENT 3
AIM-:Write a program to print the sum of all the primes between two ranges.
SOURCE CODE-:
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
OUTPUT-:
EXPERIMENT 4
AIM-:Write a program to swap two strings.
SOURCE CODE-:
OUTPUT-:
EXPERIMENT 5
AIM-:Write a menu driven program to accept two strings from the user and perform the
various functions using user defined functions.
SOURCE CODE-:
def find_length(str1):
return len(str1)
def main():
print("Menu Driven String Program")
print("1. Swap Strings")
print("2. Concatenate Strings")
print("3. Compare Strings")
print("4. Find Length of String")
print("5. Exit")
while True:
choice = int(input("Enter your choice: "))
if choice == 1:
str1, str2 = swap_strings(str1, str2)
print(f"The swapped strings are: {str1} and {str2}")
elif choice == 2:
print(f"The concatenated string is: {concatenate_strings(str1, str2)}")
elif choice == 3:
print(compare_strings(str1, str2))
elif choice == 4:
print(f"The length of the first string is: {find_length(str1)}")
print(f"The length of the second string is: {find_length(str2)}")
elif choice == 5:
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
OUTPUT-:
EXPERIMENT 6
AIM-:Write a program to find smallest and largest number in a list.
SOURCE CODE-:
def find_smallest_largest(numbers):
smallest = largest = numbers[0]
for num in numbers:
if num < smallest:
smallest = num
elif num > largest:
largest = num
return smallest, largest
OUTPUT-:
EXPERIMENT 7
AIM-:Create a dictionary whose keys are month names and whose values are the number
of days in the corresponding months.
• Ask the user to enter a month name and use the dictionary to tell them how many days
are in the month.
• Print out all keys in the alphabetically order
• Print out all the months with 31 days
• Print out the key value pairs sorted by number of days in each month
SOURCE CODE-:
month_days = {
"January": 31,
"February": 28,
"March": 31,
"April": 30,
"May": 31,
"June": 30,
"July": 31,
"August": 31,
"September": 30,
"October": 31,
"November": 30,
"December": 31
}
if month_name in month_days:
print(f"{month_name} has {month_days[month_name]} days.")
else:
print("Invalid month name.")
OUTPUTS-:
EXPERIMENT 8
AIM-:Make a list of first 10 letters of the alphabet, then use the slicing to do the
following operations:
• Print the first 3 letters of the list
• Print any 3 letters from the middle Print the letter from any particular index to the end
of the list
SOURCE CODE-:
# Print the letters from any particular index to the end of the list
print("\nLetters from index 5 to the end:")
print(alphabet[5:])
OUTPUT-:
EXPERIMENT 9
AIM-:Write a program that scans an email address and forms a tuple of user name and
domain.
SOURCE CODE-:
def parse_email(email):
username = email.split('@')[0]
domain = email.split('@')[1]
return (username, domain)
try:
username, domain = parse_email(email)
print(f"Username: {username}")
print(f"Domain: {domain}")
except IndexError:
print("Invalid email address. Please enter a valid email address.")
OUTPUT-:
EXPERIMENT 10
AIM-:Write a program that scans an email address and forms a tuple of user name and
domain.
SOURCE CODE-:
print(prefix_name(name, gender))
OUTPUT-: