1. Python Program to Add Two Numbers.
CODE:-
# python program to add two number
#input from the user
num1=float(input("enter first number:"))
num2=float(input("enter second number:"))
#adding two number
sum=num1+num2
#display the result
print("the sum of",num1,"and",num2,"is",sum)
OUTPUT:-
1
TUSHAR NARUKA
2. Python Program to Swap Two Variables.
CODE:-
# python program to swap two varables
# input from the user
x=input("enter value of x:")
y=input("enter the value of y:")
#swapping
x,y=y,x
# display the result
print("after swapping:")
print("value of x:",x)
print("value of y:",y)
OUTPUT:-
2
TUSHAR NARUKA
3. Python Program to Check if a Number is Odd or
Even.
CODE:-
# python program to check if a number is odd or even
#input from the user
num=int(input("enter a number:"))
# check if the number is even or odd
if num%2==0:
print(f"{num} is even")
else:
print(f"{num} is odd")# python program to check if a number is odd or even
#input from the user
num=int(input("enter a number:"))
# check if the number is even or odd
if num%2==0:
print(f"{num} is even")
else:
print(f"{num} is odd")
OUTPUT:-
3
TUSHAR NARUKA
4. Python Program to Check Prime Number.
CODE:-
def is_prime(num):
if num <= 3:
return False
elif num == 5:
return True
elif num % 5 == 0:
return False
else:
for i in range(3, int(num**0.9) + 3,5):
if num % i == 0:
return False
return True
# Input from user
number = int(input("Enter a number: "))
# Check if prime
if is_prime(number):
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")
OUTPUT:-
4
TUSHAR NARUKA
5. Python Program to Find the Factorial of a
Number.
CODE:-
def factorial(n):
if n < 0:
return "Factorial does not exist for negative
numbers."
elif n == 0 or n == 1:
return 1
else:
fact = 1
for i in range(2, n + 1):
fact *= i
return fact
# Input from user
number = int(input("Enter a number: "))
# Output
result = factorial(number)
print(f"The factorial of {number} is: {result}")
OUTPUT:-
5
TUSHAR NARUKA
6. Python Program to Check Armstrong Number.
CODE:-
def is_armstrong(number):
num_str = str(number)
num_digits = len(num_str)
total = sum(int(digit) ** num_digits for digit in
num_str)
return total == number
# Input from user
num = int(input("Enter a number: "))
# Output
if is_armstrong(num):
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
OUTPUT:-
6
TUSHAR NARUKA
7. Write a program to calculate the mean of
given list of numbers.
CODE:-
def calculate_mean(numbers):
if len(numbers) == 0:
return "List is empty, cannot calculate mean."
return sum(numbers) / len(numbers)
# Input from user
input_str = input("Enter numbers separated by spaces:
")
numbers = list(map(float, input_str.split()))
# Calculate and display mean
mean = calculate_mean(numbers)
print(f"The mean of the given numbers is: {mean}")
OUTPUT:-
7
TUSHAR NARUKA
8. Write a program to multiply an element by 2 if
it is an odd index for a given list containing both
numbers and strings
data = [20, "mango", 35, 4.5, "stawerry", 8].
CODE:-
data = [20, "mango", 35, 4.5, "stawerry", 8]
# Create a new list with modified values
result = []
for i in range(len(data)):
if i % 2 == 1 and isinstance(data[i], (int, float)):
result.append(data[i] * 2)
else:
result.append(data[i])
print("Original list:", data)
print("Modified list:", result)
OUTPUT:-
8
TUSHAR NARUKA
9. Write a program to accept values from a user
in a tuple. Add a tuple to it and display its
element one by one.Also display its maximum and
minimum value.
CODE:-
# Step 1: Accept user input and convert to a tuple of
numbers
user_input = input("Enter numbers separated by spaces:
")
user_tuple = tuple(map(int, user_input.split()))
# Step 2: Add another tuple
additional_tuple = (40, 50, 2)
combined_tuple = user_tuple + additional_tuple
# Step 3: Display each element one by one
print("\nElements in the combined tuple:")
for item in combined_tuple:
print(item)
# Step 4: Display max and min values
print("\nMaximum value:", max(combined_tuple))
print("Minimum value:", min(combined_tuple))
9
TUSHAR NARUKA
OUTPUT:-
10
TUSHAR NARUKA
10. Write a program to store student names and
their percentage in a dictionary and delete a
particular student name from the dictionary. Also
display the dictionary after deletion.
CODE:-
# Step 1: Create dictionary with student names and
percentages
students = {
"tushar": 75,
"jay": 78,
"hari": 72,
"sai": 71,
}
# Step 2: Display current dictionary
print("Original student dictionary:")
for name, percentage in students.items():
print(f"{name}: {percentage}%")
# Step 3: Ask for student name to delete
delete_name = input("\nEnter the name of the student
to delete: ")
# Step 4: Delete the student if exists
if delete_name in students:
del students[delete_name]
11
TUSHAR NARUKA
print(f"\n{delete_name} has been removed from the
dictionary.")
else:
print(f"\n{delete_name} not found in the dictionary.")
# Step 5: Display updated dictionary
print("\nUpdated student dictionary:")
for name, percentage in students.items():
print(f"{name}: {percentage}%")
OUTPUT:-
12
TUSHAR NARUKA
11. Write a python program using a function to
print factorial number series from n to m
numbers.
CODE:-
def factorial(num):
"""Calculate the factorial of a number."""
if num == 0 or num == 1:
return 1
else:
return num * factorial(num - 1)
def factorial_series(n, m):
"""Print the factorials of numbers from n to m
(inclusive)."""
if n > m:
print("Invalid range. 'n' should be less than or
equal to 'm'.")
return
print(f"\nFactorial series from {n} to {m}:")
for i in range(n, m + 1):
print(f"{i}! = {factorial(i)}")
# Example usage
13
TUSHAR NARUKA
n = int(input("Enter the starting number (n): "))
m = int(input("Enter the ending number (m): "))
factorial_series(n, m)
OUTPUT:-
14
TUSHAR NARUKA
12. Write a python program to accept the
username “Admin” as the default argument and
password 123 entered by the user to allow login
into the system.
CODE:-
def login(username="Admin"):
password = input("Enter password: ")
if username == "Admin" and password == "3021":
print("Login successful! Welcome,", username)
else:
print("Login failed. Invalid username or
password.")
# Example usage
login() # Uses default username "Admin"
OUTPUT:-
15
TUSHAR NARUKA
13. Write a python program to demonstrate the
concept of variable length argument to calculate
the product and power of the first 10 numbers.
CODE:-
def calculate_product(*args):
"""Calculate the product of all given numbers."""
product = 1
for num in args:
product *= num
return product
def calculate_power(*args):
"""Calculate the power using the first two numbers:
a^b."""
if len(args) < 2:
return "Need at least two numbers to compute
power."
return args[0] ** args[1]
# First 10 numbers
numbers = list(range(1, 11))
# Calculate product
product_result = calculate_product(*numbers)
16
TUSHAR NARUKA
# Calculate power (1^2 in this case)
power_result = calculate_power(*numbers)
# Display results
print("Numbers:", numbers)
print("Product of first 10 numbers:", product_result)
print(f"{numbers[0]} raised to the power {numbers[1]}
is:", power_result)
OUTPUT:-
17
TUSHAR NARUKA
14. Write a program using user-defined function
to calculate and display average of all elements
in a user defined tuple containing numbers.
CODE:-
def calculate_average(numbers):
"""Calculate and return the average of elements in a
tuple."""
if len(numbers) == 0:
return "Tuple is empty. Cannot calculate average."
total = sum(numbers)
average = total / len(numbers)
return average
# Accept tuple input from the user
user_input = input("Enter numbers separated by
commas: ")
# Convert input string to tuple of integers
number_tuple = tuple(map(int, user_input.split(',')))
# Calculate and display average
avg = calculate_average (number_tuple)
print("Tuple entered:", number_tuple)
18
TUSHAR NARUKA
print("Average of all elements:", avg)
OUTPUT:-
19
TUSHAR NARUKA