DAV PUBLIC SCHOOL N.T.P.
C,
FARIDABAD
PRACTICAL FILE
NAME – JAY SHARMA
CLASS - Xth A1
ROLL NO. – 4
SUBMITTED TO - [Link]
SUBMITTED BY – JAY SHARMA
Write a program to create a simple calculator (add,
subtract, multiply, divide).
# Simple Calculator in Python
print("----- Simple Calculator -----")
print("Select operation:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
# take input from user
choice = input("Enter choice (1/2/3/4): ")
# taking numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# conditions
if choice == '1':
print("Result =", num1 + num2)
elif choice == '2':
print("Result =", num1 - num2)
elif choice == '3':
print("Result =", num1 * num2)
elif choice == '4':
if num2 != 0:
print("Result =", num1 / num2)
else:
print("Error! Division by zero.")
else:
print("Invalid Input")
Write a program to sum all elements of a list.
# Program to sum all elements of a list
# define a list
numbers = [10, 20, 30, 40, 50]
# method 1: using built-in sum() function
total = sum(numbers)
print("The list is:", numbers)
print("Sum of all elements =", total)
Write a program to reverse a string.
# Program to reverse a string
# input from user
text = input("Enter a string: ")
# method 1: using slicing
reversed_text = text[::-1]
print("Original String:", text)
print("Reversed String:", reversed_text)
Write a program to print multiplication table of a number.
# Program to print multiplication table of a number
# input from user
num = int(input("Enter a number: "))
print("Multiplication Table of", num)
for i in range(1, 11):
print(num, "x", i, "=", num * i)
Write a program to find the largest of three numbers.
# Program to find the largest of three numbers
# input from user
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
# checking conditions
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is:", largest)
Write a Python program to check whether a number is
even or odd.
# Program to check whether a number is even or odd
# input from user
num = int(input("Enter a number: "))
# check condition
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
Write a program to read marks of 5 students and print
their average
# Program to read marks of 5 students and print their average
# taking input
m1 = float(input("Enter marks of Student 1: "))
m2 = float(input("Enter marks of Student 2: "))
m3 = float(input("Enter marks of Student 3: "))
m4 = float(input("Enter marks of Student 4: "))
m5 = float(input("Enter marks of Student 5: "))
# calculate average
average = (m1 + m2 + m3 + m4 + m5) / 5
# display result
print("Average Marks =", average)
Write a program to check whether a number is
palindrome or not.
# Program to check whether a number is palindrome or not
# input from user
num = int(input("Enter a number: "))
# reverse the number
rev = 0
n = num
while n > 0:
digit = n % 10
rev = rev * 10 + digit
n = n // 10
# check palindrome
if num == rev:
print(num, "is a Palindrome number")
else:
print(num, "is NOT a Palindrome number")
Write a program to count the frequency of each
character in a string.
# Program to count frequency of each character in a string
# input from user
text = input("Enter a string: ")
# empty dictionary to store frequency
freq = {}
# loop through each character
for ch in text:
if ch in freq:
freq[ch] += 1
else:
freq[ch] = 1
# display result
print("Character Frequency:")
for key, value in [Link]():
print(key, ":", value)
Write a program to swap two numbers without using a
third variable.
# Program to swap two numbers without using a third variable
# input from user
a = int(input("Enter first number (a): "))
b = int(input("Enter second number (b): "))
print("Before Swapping: a =", a, ", b =", b)
# swapping using arithmetic
a=a+b
b=a-b
a=a-b
print("After Swapping: a =", a, ", b =", b)
Write a program to find the sum of digits of a number.
# Program to find the sum of digits of a number
# input from user
num = int(input("Enter a number: "))
total = 0
n = num
while n > 0:
digit = n % 10 # extract last digit
total += digit # add digit to sum
n = n // 10 # remove last digit
print("Sum of digits of", num, "=", total)
Write a program to check whether a number is
Armstrong or not.
# Program to check whether a number is Armstrong or not
# input from user
num = int(input("Enter a number: "))
# find number of digits
power = len(str(num))
# calculate sum of digits raised to power
total = 0
n = num
while n > 0:
digit = n % 10
total += digit ** power
n //= 10
# check Armstrong condition
if total == num:
print(num, "is an Armstrong number")
else:
print(num, "is NOT an Armstrong number")
Write a program to print all even numbers from 1 to 100.
# Program to print all even numbers from 1 to 100
print("Even numbers from 1 to 100:")
for num in range(1, 101):
if num % 2 == 0:
print(num, end=" ")
Write a program to check whether a year is leap year or
not.
# Program to check whether a year is a leap year or not
year = int(input("Enter a year: "))
# Leap year conditions:
# 1. Year is divisible by 4
# 2. If divisible by 100, it must also be divisible by 400
if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):
print(year, "is a Leap Year")
else:
print(year, "is NOT a Leap Year")
Write a program to find the smallest and largest number
in a list.
# Program to find smallest and largest number in a list
# define a list
numbers = [25, 67, 12, 89, 45, 3, 78]
print("The list is:", numbers)
# find smallest and largest
smallest = min(numbers)
largest = max(numbers)
print("Smallest number =", smallest)
print("Largest number =", largest)