0% found this document useful (0 votes)
8 views

python practical file

All the information related to python

Uploaded by

mb469676
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

python practical file

All the information related to python

Uploaded by

mb469676
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PRACTICAL FILE

SESSION: 2024-25

Basics of Python
Programming
(BCAP 211)
II Year,3Sem

Submitted to: Submitted by:


Name: Mr. Chitrangad Singh Tomar Name: HARSH KUMAR RAI
Designation:Assistant Professor Enrollment No-:01018002023

Department Of Computer Application


Delhi Technical Campus,Greater Noida
EXPERIMENT 1
AIM-: Write a program to enter two integers, two floating numbers and then perform all
arithmetic operations on them.

SOURCE CODE:

first_integer = int(input("Enter the first whole number: "))


second_integer = int(input("Enter the second whole number: "))
first_decimal_number = float(input("Enter the first decimal number: "))
second_decimal_number = float(input("Enter the second decimal number: "))
print("Results of operations on whole numbers:")
print("Addition: ", first_integer + second_integer)
print("Subtraction: ", first_integer - second_integer)
print("Multiplication: ", first_integer * second_integer)
print("Division: ",
first_integer / second_integer if second_integer != 0
else "Cannot divide by zero, please try again!")
print("\nResults of operations on decimal numbers:")
print("Addition: ", first_decimal_number + second_decimal_number)
print("Subtraction: ", first_decimal_number - second_decimal_number)
print("Multiplication: ", first_decimal_number * second_decimal_number)
print("Division: ",
first_decimal_number / second_decimal_number
if second_decimal_number != 0
else "Cannot divide by zero, please try again!")

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

num = int(input("Enter a number: "))

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

def sum_of_primes(start, end):


prime_sum = 0
for num in range(start, end + 1):
if is_prime(num):
prime_sum += num
return prime_sum

start_range = int(input("Enter the start of the range: "))


end_range = int(input("Enter the end of the range: "))

result = sum_of_primes(start_range, end_range)


print(f"The sum of primes between {start_range} and {end_range} is {result}.")

OUTPUT-:
EXPERIMENT 4
AIM-:Write a program to swap two strings.
SOURCE CODE-:

def swap_strings(str1, str2):


str1, str2 = str2, str1
return str1, str2

str1 = input("Enter the first string: ")


str2 = input("Enter the second string: ")

str1, str2 = swap_strings(str1, str2)

print(f"The swapped strings are: {str1} and {str2}")

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 swap_strings(str1, str2):


str1, str2 = str2, str1
return str1, str2

def concatenate_strings(str1, str2):


return str1 + str2

def compare_strings(str1, str2):


if str1 == str2:
return "Strings are equal"
else:
return "Strings are not equal"

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")

str1 = input("Enter the first string: ")


str2 = input("Enter the second string: ")

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

numbers = input("Enter a list of numbers (separated by spaces): ")


numbers = [int(num) for num in numbers.split()]

smallest, largest = find_smallest_largest(numbers)

print(f"The smallest number is: {smallest}")


print(f"The largest number is: {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
}

month_name = input("Enter a month name: ").strip().title()

if month_name in month_days:
print(f"{month_name} has {month_days[month_name]} days.")
else:
print("Invalid month name.")

print("\nAll month names in alphabetical order:")


for month in sorted(month_days.keys()):
print(month)

print("\nMonths with 31 days:")


for month, days in month_days.items():
if days == 31:
print(month)

print("\nMonth names and days in sorted order:")


for month, days in sorted(month_days.items(), key=lambda x: x[1]):
print(f"{month}: {days}")

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-:

# Create a list of the first 10 letters of the alphabet


alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']

# Print the first 3 letters of the list


print("First 3 letters:")
print(alphabet[:3])

# Print any 3 letters from the middle


print("\nMiddle 3 letters:")
print(alphabet[3:6])

# 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)

email = input("Enter an email address: ")

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-:

def prefix_name(name, gender):


if gender.upper() == 'M':
return 'Mr. ' + name
elif gender.upper() == 'F':
return 'Ms. ' + name
else:
return 'Invalid gender. Please enter M for Male or F for Female.'

name = input("Enter your name: ")


gender = input("Enter your gender (M for Male, F for Female): ")

print(prefix_name(name, gender))
OUTPUT-:

You might also like