Python Basics: Data Types & Programs
Python Basics: Data Types & Programs
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 1: Write a python program to demonstrate basic data types of python
and the concept of object identity.
OBJECTIVE: To understand the implementation of various data type in python.
DESCRIPTION: A data type in programming refers to the kind of data that a variable can hold.
SOURCE CODE:
a=10
x="tiya"
y=2.4
print("value of variable y=",y)
z=3+2j
print("value of variable z=",z)
print("class type of variable z=",type(z))
l1=["nikk","sonam","binni"]
print("value of l1=",l1)
print("ID of l1=",id(l1))
t1=("ginni","rashi")
print("value of t1=",t1)
r1=(1,2,3,4)
print("value of r1=",r1)
print("ID of r1=",id(r1))
m1={"name":"John","age":23}
print("value of m1=",m1)
print("ID of m1=",id(m1))
b1=True
print("value of b1=",b1)
print("calss type of b1=",type(b1))
print("ID of b1=",id(b1))
OUT PUT
value of variable a= 10
ID of variable a= 140703357629640
ID of variable a= 2269711611344
value of variable y= 2.4
ID of variable y= 2269708792208
ID of variable z= 2269711554736
ID of l1= 2269709705856
ID of m1= 2269711690240
value of b1= True
ID of b1= 140703356744112
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 2 Write a python program to swap to two variable using arithmetic
operator only.
OBJECTIVE-To write a Python program that swaps the values of two variables without using a
third variable or built-in swap methods, but instead using arithmetic operators.
DESCRIPTION-This program demonstrates how to interchange (swap) the values of two
variables using addition and subtraction (or alternatively, multiplication and division).
SOURCE CODE
a = int(input("Enter first number: "))
print("Before swapping:")
print("a =", a)
print(“b=”,b)
a=a+b
b=a-b
a=a-b
print("After swapping:")
print("a =", a)
print("b =", b)
OUTPUT
a=5,b=1
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 3 Write a Python program to find the greatest of three numbers and
arrange the numbers in ascending order.
OBJECTIVE-To find the greatest of three numbers and display the numbers in ascending order.
DESCRIPTION-This program takes three numbers as input, compares them to determine the
greatest number, and then arranges all three numbers in ascending order using simple
[Link] or sorting
SOURCE CODE-
a = float(input("Enter first number: "))
greatest = a
else:
greatest = c
numbers = [a, b, c]
[Link]()
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 4 Write a python program to print whether a given year is leap year
or not.
OBJECTIVE-To check whether the entered year is a leap year or not
DESCRIPTION-This program takes a year as input and determines if it’s a leap year.
A year is a leap year if:
It is divisible by 4,
SOURCE CODE:
year=int(input("Enter a year:"))
if(year%4==0 and year%100!=0) or(year%400==0):
print(f"{year}is a leap year.")
else:
print(f"{year}is not a leap year")
OUTPUT:
Enter a year:2024
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 5: Write a program to check whether the number is even or odd.
EXPLAINATION: This program takes an integer an integer as input and check if it is divisible
by 2.
-If the remainder is 0, the number is even.
-Otherwise, it is odd.
SOURCE CODE:
num=int(input("Enter a number:"))
if num%2==0:
print("The number is Even.")
else:
print("The number is Odd.")
OUTPUT:
Enter a number:5
ROLL NO: 80
SECTION: B2
EXPLAINATION: This program takes an integer as input and finds its factorial.
The factorial of a number n (written as n!) is the product of all positive integer from 1 to n.
SOURCE CODE:
prod = 1
for i in range(1, n + 1, 1):
prod = prod * i
print("factorial of the number:", prod)
OUTPUT:
ROLL NO: 80
SECTION: B2
OBJECTIVE: To print the Fibonacci series up to a given number ‘n’ using Python.
EXPLAINATION: The Fibonacci series is a sequence where each number is the sum of the two
preceding ones, starting from 0 and 1. This program takes an integer input ‘n’ and prints all
Fibonacci numbers less than or equal to ‘n’.
SOURCE CODE:
OUTPUT:
0 1 1 2 3 5 8 13
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
EXPLAINATION: A prime number is a number greater than 1 that has no divisors other than 1
and itself.
SOURCE CODE:
num=int(input("Enter a number:"))
if num>1:
for i in range(2,int(num**0.5)+1):
if num%i==0:
print(num,"is not prime")
else:
print(num,"is prime")
OUTPUT:
Enter a number:7
7 is prime
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
EXPLAINATION: This program takes an integer as input and reverse its digits by repeatedly
extracting the last digit using the modulus operator (%) and building the reverse number step by
step using integer division (//).
SOURCE CODE:
num=int(input("Enter a number:"))
rev=0
while num>0:
rev=(rev*10)+(num%10)
num=num//10
print("Reversed number is:",rev)
OUTPUT:
Enter a number:1234
ROLL NO: 80
SECTION: B2
EXPLAINATION: A palindrome number is one of that remains the same when its digits are
reversed. This program reversed a given number using a while loop and compares it with the
original number.
SOURCE CODE:
num=int(input("Enter a number:"))
original=num
reverse=0
while num>0:
digit=num%10
reverse=reverse*10+digit
num=num//10
if original==reverse:
print("Palindrome number.")
else:
print("Not a palindrome.")
OUTPUT:
Enter a number:141
Palindrome number.
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 11: Write a program to check whether a number is Armstrong or not.
EXPLAINATION: An Armstrong number is a number that is equal to the sum of the cubes of its
digits (for 3-digit numbers).
SOURCE CODE:
num=int(input("Enter a number:"))
temp=num
sum=0
while num>0:
digit=num%10
sum+=digit**3
num//=10
if temp==sum:
print(temp,"is an Armstrong number.")
else:
print(temp,"is not an Armstrong number")
OUTPUT:
Enter a number:143
143 is not an Armstrong number
.
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
PROBLEM STATEMENT 12: Write a Python program to print the digit sum of a number.
EXPLAINATION: This program takes an integer as input and repeatedly extends each digit
using the modulus (%) operators, adds them together, and removes the last digit using integer
division (//).
SOURCE CODE:
num=int(input("Enter a number:"))
original_num=num
sum_digit=0
while num > 0:
digit = num % 10
sum_digit += digit
num = num // 10
print(f"Sum of digits of {original_num}is: {sum_digit}")
OUTPUT:
Enter a number:12345
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 13: Write a function to find whether a given number is prime or not.
OBJECTIVE: To define a function that checks whether a given number is prime or not.
EXPLAINATION: A prime number is a number greater than 1 that has no divisors other than 1
and itself. This program defines a function is prime(num) that returns whether a number is prime
or not using a simple loop.
SOURCE CODE:
num=int(input("Enter a number:"))
if num>1:
for i in range(2,int(num**0.5)+1):
if num%i==0:
print(num,"is not prime")
else:
print(num,"is prime")
OUTPUT:
Enter a number:8
8 is not prime
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
PROBLEM STATEMENT 14: Write a recursive function to find the factorial of a number.
EXPLAINATION: A factorial of a number n (written as n!) is the product of all positive integer
up to n. In recursion, a function calls itself until a base condition is met -here, when n becomes 1.
SOURCE CODE:
def fact(n):
if n == 0:
return 1
else:
return n * fact(n - 1)
num=int(input("Enter a number:"))
print("The factorial of", num, "is:", fact(num))
OUTPUT:
Enter a number:5
ROLL NO: 80
PROBLEM STATEMENT 15: Write a function that returns the sum of digits of a number using
recursion.
def sum_digits(n):
if n==0:
return 0
return n % 10 + sum_digits(n // 10)
num=int(input("Enter a number:"))
print("Sum of digits:", sum_digits(num))
OUTPUT:
Enter a number:1234
Sum of digits: 10
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 16- Write a Python program to find all prime numbers in a given
range using a function.
OBJECTIVE-To find and display all prime numbers within a given range using a function
DESCRIPTION- This program defines a function find_primes(start, end) that checks each
number in the specified range to determine whether it is prime. A prime number is a number
greater than 1 that has no divisors other than 1 and itself. The function returns a list of all such
prime numbers.
SOURCE CODE-
if num > 1:
if num % i == 0:
break
else:
[Link](num)
return primes
OUTPUT
Enter the start of the range: 10
Enter the end of the range: 50
Prime numbers between 10 and 50 are: [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 17: Write a function to find the LCM and GCD of two numbers
without using built-in functions. Having short objective and description.
OBJECTIVE: To find the LCM (Least Common Multiple) and GCD (Greatest Common Divisor)
Of two numbers without using built-in functions.
- find_gcd (a ,b) finds the greatest number that divides both a and b.
- find_lcm (a, b) uses the relation LCM*GCD=a*b to compute the least common multiple.
SOURCE CODE:
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT18: Write a program using a lambda function to calculate the area of a
circle having short objective and description.
OBJECTIVE: To calculate the area of a circle using a lambda function.
EXPLAINATION: This program uses a lambda function to define a one-line formula for finding
the area of a circle.
SOURCE CODE:
area=lambda r:3.14159*r*r
radius = float(input("Enter the radius of the circle:"))
print("The area of the circle is:",area(radius))
OUTPUT:
ROLL NO: 80
PROBLEM STATEMENT 19: Write a python program to count vowels and consonants in a
given string.
OUTPUT:
Number of vowels: 3
Number of consonants: 7
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 20: Write a program to check whether a string is palindrome or not.
EXPLAINATION: A palindrome string is one that reads the same backward and forward. This
program compares the original string with its reverse to determine if it’s a palindrome.
SOURCE CODE:
OUTPUT:
Enter a string:hello
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 21: Write a python function to count the frequency of each character
in a string.
OBJECTIVE: To count the frequency of each character in a given string using a function.
EXPLAINATION: This program defines a function that takes a string as input and uses a loop to
count how many times each character appears. The results are stored in a dictionary, where each
key is a character and its value is the count.
SOURCE CODE:
def char_frequency(text):
freq = {}
for ch in text:
if ch in freq:
freq[ch]+=1
else:
freq[ch]=1
return freq
string =input("Enter a string:")
print("Character frequencies:")
for key, value in char_frequency(string).items():
print(key,":",value)
OUTPUT:
Enter a string:hello
Character frequencies:
h:1
e:1
l:2
o:1
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 22: Write a program to remove all punctuation marks and spaces from
a string and check if it is palindrome.
OBJECTIVE: To remove punctuation marks and spaces from a string and check whether it is a
palindrome.
EXPLIANATION: This program removes all punctuation and spaces using a loop and string
filtering.
SOURCE CODE:
import string
text = input("Enter a string:")
cleaned =""
for ch in [Link]():
if ch not in [Link] and ch!="":
cleaned+=ch
if cleaned == cleaned[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
OUTPUT:
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 23: Write a program to find longest and shortest word in a sentence.
EXPLAINATION: This program takes a sentence as input, splits it into individual words, and
then uses the min () and max () function to find the words with the smallest and largest lengths.
SOURCE CODE:
OUTPUT:
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 24: Write a program to count the frequency of words in a sentence.
The output should be displayed in a dictionary format where each unique word will be the key
and its frequency will be the value of the dictionary.
OBJECTIVE: To count the frequency of each word in a sentence and display the result in a
dictionary format.
EXPLAINATION: This program takes a sentence as input, splits it into words, and counts how
many times each word appears.
Each unique word is used as a key, and its frequency is stored as the value in a dictionary.
SOURCE CODE:
words = [Link]().split()
freq = {}
if word in freq:
freq[word] += 1
else:
freq[word] = 1
OUTPUT:
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 25: Write a Python program to create a list of numbers and print their
sum and average.
OBJECTIVE: To create a list of numbers and calculate their sum and average.
EXPLAINATION: This program takes multiple numbers as input from the user, stores them in a
list, and then uses the built-in sum() and len() functions to compute the total sum and average of
the numbers.
SOURCE CODE:
OUTPUT:
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 26: Write a program to find the largest and the smallest element in a
list.
OBJECTIVE: To find the largest and smallest elements in a list.
EXPLAINATION: This program goes through each element in the list using a loop and
compares values to find the maximum and minimum manually
SOURCE CODE:
largest = numbers[0]
smallest = numbers[0]
smallest = num
print("List:", numbers)
print("Largest element:", largest)
OUTPUT:
List: [12, 45, 7, 23, 56, 2, 99]
Largest element: 99
Smallest element: 2
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 27: Write a Python function to remove duplicates from a list without
using set().
OBJECTIVE: Remove duplicate elements from a list while preserving order.
EXPLAINATION: This function iterates through the input list, adding each element to a new list
only if it hasn't been added before.
SOURCE CODE:
def remove_duplicates(lst):
result = []
[Link](item)
return result
numbers = [1, 2, 2, 3, 4, 4, 5]
print(remove_duplicates(numbers))
OUTPUT:
[1, 2, 3, 4, 5]
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 28: Write a program to sort a list of tuples based on the second
element of each tuple.
OBJECTIVE: Sort a list of tuples based on the second element of each tuple.
EXPLAINATION: The program uses the 'sorted' function with a lambda key to sort tuples by
their second value.
SOURCE CODE:
def sort_by_second_element(tuple_list):
sorted_data = sort_by_second_element(data)
print(sorted_data)
OUTPUT:
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 29: Write a function that accepts a list of integers and returns a new
list with only prime numbers.
OBJECTIVE: Extract prime numbers from a list of integers.
EXPLAINATION: The function checks each number in the input list and returns a new list
containing only prime numbers.
SOURCE CODE:
def is_prime(n):
if n < 2:
return False
if n % i == 0:
return False
return True
def filter_primes(lst):
return [num for num in lst if is_prime(num)]
prime_numbers = filter_primes(numbers)
print(prime_numbers)
OUTPUT:
[2, 3, 5, 7]
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 30: Write a program to create and display a tuple containing numbers
and string.
OBJECTIVE: Create a tuple containing numbers and strings, and display it.
EXPLAINATION: The program demonstrates how to define a tuple with mixed data types and
print its contents.
SOURCE CODE:
OUTPUT:
The tuple is: (1, 2, 3, 'apple', 'banana', 'cherry')
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 31: Write a Python program to convert a list of tuples into
dictionary.
OBJECTIVE: Convert a list of tuples into a dictionary.
EXPLAINATION: Each tuple in the list contains a key-value pair, which is added to the
dictionary.
SOURCE CODE:
dict_result = {}
dict_result[key] = value
OUTPUT:
The dictionary is: {'a': 1, 'b': 2, 'c': 3}
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 32: Write a program to count the frequency of each character in a
string using a dictionary.
OBJECTIVE: Count the frequency of each character in a string.
EXPLAINATION: The program iterates through the string and updates a dictionary with the
number of occurrences of each character.
SOURCE CODE:
char_frequency = {}
if char in char_frequency:
char_frequency[char] += 1
else:
char_frequency[char] = 1
OUTPUT:
Character frequency: {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 33: Write a Python program to find the most frequent element in a
tuple.
OBJECTIVE: Find the element that appears most frequently in a tuple.
EXPLAINATION: The program iterates through the tuple and uses a dictionary to count the
occurrences of each element. It then identifies the element with the highest frequency and
displays it.
SOURCE CODE:
my_tuple = (1, 2, 3, 2, 4, 2, 5, 3, 1)
freq_dict = {}
if item in freq_dict:
freq_dict[item] += 1
else:
freq_dict[item] = 1
OUTPUT:
The most frequent element is: 2
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 34: write a program to merge two dictionaries and sum values of
common keys.
OBJECTIVE: Merge two dictionaries, summing the values of keys that appear in both.
EXPLAINATION: The program iterates through both dictionaries. If a key exists in both, it adds
their values; otherwise, it keeps the unique key-value pairs.
SOURCE CODE:
merged_dict = [Link]()
merged_dict[key] += value
else:
merged_dict[key] += value
OUTPUT:
Merged dictionary: {'a': 100, 'b': 350, 'c': 400, 'd': 250}
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 35: Write a function to invert a dictionary, swap keys and values.
OBJECTIVE: Invert a dictionary so that its keys become values and its values become keys.
EXPLAINATION: The function iterates through the original dictionary and creates a new
dictionary with keys and values swapped. Note: If the original dictionary has duplicate values,
later keys will overwrite earlier ones.
SOURCE CODE:
def invert_dictionary(d):
inverted = {}
inverted[value] = key
return inverted
inverted_dict = invert_dictionary(original_dict)
OUTPUT:
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 36: Write a Python program to demonstrate set creation and basic set
operation union-intersection difference.
OBJECTIVE: Demonstrate how to create sets and perform basic set operations: union,
intersection, and difference.
EXPLAINATION: The program creates two sets and shows how to combine them (union), find
common elements (intersection), and find elements present in one set but not the other
(difference).
SOURCE CODE:
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
union_set = set1 | set2
print("Union:", union_set)
print("Intersection:", intersection_set)
print("Difference (set1 - set2):", difference_set)
OUTPUT:
Set 1: {1, 2, 3, 4, 5}
Set 2: {4, 5, 6, 7, 8}
Union: {1, 2, 3, 4, 5, 6, 7, 8}
Intersection: {4, 5}
Difference (set1 - set2): {1, 2, 3}
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 37: Write a Python program to read the content of a text file and
display it on the screen.
OBJECTIVE: Read the contents of a text file and display them on the screen.
EXPLAINATION: The program opens a text file in read mode, reads its content, and prints it. It
automatically closes the file using a with statement.
SOURCE CODE:
file_path = '[Link]'
try:
content = [Link]()
print("File content:\n")
print(content)
except FileNotFoundError:
OUTPUT:
File content:
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 38: Write a Python program to count the total number of lines in a
file.
OBJECTIVE: Count the total number of lines present in a text file.
EXPLAINATION: The program opens a file in read mode and iterates through each line,
incrementing a counter to calculate the total number of lines.
SOURCE CODE:
file_path = '[Link]'
try:
line_count = 0
for line in file:
line_count += 1
except FileNotFoundError:
print(f"Error: The file '{file_path}' does not exist.")
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 39: Write a Python program to copy the content of one file to another
file.
OBJECTIVE: Copy the content of a source file into a destination file.
DESCRIPTION: The program opens the source file in read mode and the destination file in write
mode. It reads the content from the source and writes it to the destination file.
SOURCE CODE:
source_file = '[Link]'
destination_file = '[Link]'
try:
content = [Link]()
with open(destination_file, 'w') as dest:
[Link](content)
except FileNotFoundError:
print(f"Error: The file '{source_file}' does not exist.")
ROLL NO: 80
SECTION: B2
SOURCE CODE:
class BankAccount:
def __init__(self, owner, balance=0):
[Link] = owner
[Link] = balance
if amount > 0:
[Link] += amount
print(f"{amount} deposited. New balance: {[Link]}")
else:
if amount > 0:
else:
print("Insufficient balance.")
else:
print("Withdrawal amount must be positive.")
def display_balance(self):
account.display_balance()
[Link](500)
[Link](200)
account.display_balance()
Output:
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 41-Write a class student that stores name, roll number, and marks of
three subjects and computes total and grade.
OBJECTIVE: Create a class to store student details, compute total marks, and determine the
grade based on marks.
DESCRIPTION: The Student class stores a student’s name, roll number, and marks of three
subjects. It has methods to calculate total marks and assign a grade based on the total percentage.
SOURCE CODE:
class Student:
[Link] = name
self.roll_no = roll_no
[Link] = marks
def total_marks(self):
return sum([Link])
def grade(self):
total = self.total_marks()
percentage = total / 3
return 'A'
elif percentage >= 75:
return 'B'
elif percentage >= 60:
return 'C'
return 'D'
else:
return 'F'
def display(self):
print(f"Name: {[Link]}")
print(f"Marks: {[Link]}")
print(f"Total: {self.total_marks()}")
print(f"Grade: {[Link]()}")
[Link]()
OUTPUT:
Total: 255
Grade: B
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 42-Write a class Rectangle that computes Area and Perimeter.
OBJECTIVE: Create a class to represent a rectangle and calculate its area and perimeter.
DESCRIPTION: The Rectangle class stores the length and width of a rectangle. It provides
methods to compute the area and perimeter.
SOURCE CODE:
class Rectangle:
[Link] = length
[Link] = width
def area(self):
return [Link] * [Link]
def perimeter(self):
def display(self):
print(f"Area: {[Link]()}")
print(f"Perimeter: {[Link]()}")
rect = Rectangle(5, 3)
[Link]()
OUTPUT:
Length: 5, Width: 3
Area: 15
Perimeter: 16
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 43-Write a simple tkinter program to create a window with the label
Welcome to GUI Programming.
OBJECTIVE: Create a basic GUI window in Python using Tkinter with a welcoming label.
DESCRIPTION: The program uses the Tkinter library to create a window, adds a Label widget
with a message, and runs the main event loop to display the window.
SOURCE CODE:
window = Tk()
[Link]("Simple GUI")
[Link]("400x200")
[Link](pady=50)
[Link]()
OUTPUT-
A GUI window appears with the title “Simple GUI” and a centered label displaying:
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 44-Write a GUI-based program to accept two numbers and display
their sum.
OBJECTIVE: Create a GUI application to input two numbers and display their sum.
DESCRIPTION: The program uses Tkinter to create input fields for two numbers, a button to
calculate the sum, and a label to display the result.
SOURCE CODE:
def calculate_sum():
try:
num1 = float([Link]())
num2 = float([Link]())
except ValueError:
window = Tk()
[Link]("Sum Calculator")
[Link]("300x200")
entry1 = Entry(window)
[Link](pady=5)
label_result.pack(pady=5)
[Link]()
OUTPUT:
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 45-Create a GUI application to change the background color of the
window.
OBJECTIVE: Create a GUI application that allows the user to change the window’s background
color.
DESCRIPTION: The program uses Tkinter to create buttons that, when clicked, change the
background color of the main window.
SOURCE CODE:
from tkinter import *
def change_bg(color):
[Link](bg=color)
window = Tk()
[Link]("400x200")
Label(window, text="Click a button to change background color:", font=("Arial",
12)).pack(pady=10)
Button(window, text="Red", width=10, command=lambda: change_bg("red")).pack(pady=5)
change_bg("yellow")).pack(pady=5)
[Link]()
OUTPUT:
• A GUI window appears with an instruction label.
• Clicking a button changes the window’s background color to the selected color.
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 46-Write a tkinter GUI application to take a name, input, and display
a personalized greeting message.
OBJECTIVE: Create a GUI application that accepts a user’s name and displays a personalized
greeting message.
DESCRIPTION: The program uses Tkinter to create an input field for the name, a button to
submit, and a label to display the greeting message.
SOURCE CODE:
def greet():
name = entry_name.get()
if [Link]():
else:
window = Tk()
[Link]("Greeting App")
[Link]("400x200")
[Link]()
OUTPUT:
• A GUI window appears with a label “Enter your name:”, an entry box, and a “Greet Me”
button.
• When the user enters their name (e.g., Alice) and clicks the button, the label updates:
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 47-Create a GUI-based student marks calculator with input fields for
three subjects and a button to calculate total and grade.
OBJECTIVE: Create a GUI application to input marks for three subjects and display the total
marks and grade.
DESCRIPTION: The program uses Tkinter to create input fields for three subjects, a button to
calculate the total and grade, and labels to display the results. The grade is assigned based on
percentage:
• ≥ 90: A
• ≥ 75: B
• ≥ 60: C
• ≥ 50: D
• < 50: F
SOURCE CODE:
def calculate():
try:
marks1 = float([Link]())
marks2 = float([Link]())
marks3 = float([Link]())
percentage = total / 3
grade = 'B'
grade = 'D'
else:
grade = 'F'
except ValueError:
label_result.config(text="Please enter valid numeric marks.")
window = Tk()
[Link]("400x350")
[Link](pady=5)
[Link](pady=5)
[Link](pady=5)
label_result.pack(pady=10)
[Link]()
OUTPUT
• Total: 255
• Grade: B
NAME: GOPAL RANA DATE:17/11/2025
ROLL NO: 80
SECTION: B2
PROBLEM STATEMENT 48-Write a Tkinter GUI application to display an image and provide
buttons to zoom in and out.
OBJECTIVE: Create a GUI application that displays an image and allows the user to zoom in
and out using buttons.
DESCRIPTION: The program uses Tkinter along with PIL (Python Imaging Library) to load and
display an image. Buttons are provided to increase or decrease the size of the image dynamically
SOURCE CODE:
def zoom_in():
zoom *= 1.2
tk_img = [Link](new_img)
[Link](image=tk_img)
def zoom_out():
zoom /= 1.2
tk_img = [Link](new_img)
[Link](image=tk_img)
root = Tk()
tk_img = [Link](img)
[Link]()
[Link]()
OUTPUT-
+---------------------------------------+
| |
+---------------------------------------+
| [ Zoom In ] [ Zoom Out ] |
+---------------------------------------+