0% found this document useful (0 votes)
3 views31 pages

Chandu Python Lab

The document outlines various Python programming exercises, including calculating compound interest, reading personal information, printing triangle patterns, identifying character types, finding prime numbers, and checking for duplicates in lists. Each exercise includes an aim, input/output format, and source code examples, with execution results confirming successful test cases. The exercises are designed for educational purposes at Kommuri Pratap Reddy Institute Of Technology.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views31 pages

Chandu Python Lab

The document outlines various Python programming exercises, including calculating compound interest, reading personal information, printing triangle patterns, identifying character types, finding prime numbers, and checking for duplicates in lists. Each exercise includes an aim, input/output format, and source code examples, with execution results confirming successful test cases. The exercises are designed for educational purposes at Kommuri Pratap Reddy Institute Of Technology.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Date: 2026-04-

Page No: 1
[Link]: 1 Exp. Name: Annual Interest Rate
15

Aim:

ID:
Write a Python program that prompts the user to enter the principal amount, the annual
interest rate, and the number of years to compute the compound interest.

Use the formula:


• Total Amount A = P × (1 +
r

100
)
t

Note: Assume that the interest is compounded once per year.

Input Format:
• The first line of input is a floating-point value representing the principal amount P .
• The second line of input is a floating-point value representing the annual interest
rate r.

2022-2050-Faculty
• The third line of input is an integer representing the number of years t.

Output Format:
• The output is a floating-point value representing the total amount after applying
compound interest, formatted to 2 decimal places.
Source Code:

Kommuri Pratap Reddy Institute Of Technology


compound_interest.py

p=float(input())
r=float(input())
t=int(input())
ci=p*(1+(r/100))**t
print(format(ci,'.2f'))

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1000.0
5.0
2
1102.50
3
4.0
1500.0

1687.30
User Output
Test Case - 2

Kommuri Pratap Reddy Institute Of Technology 2022-2050-Faculty ID: Page No: 2


Exp. Name: Personal Information Input Date: 2026-04-
[Link]: 2

Page No: 3
and Display Program 15

Aim:
Write a Python program that reads the name, address, email, and phone number of a

ID:
person from the keyboard and prints the details exactly as entered.

Input Format:
Four lines of input:
• A string representing the person's name.
• A string representing the person's address.
• A string representing the person's email.
• A string representing the person's phone number.

Output Format:
• Print each input on a new line in the same order it was received.

2022-2050-Faculty
Source Code:

[Link]

n=input()
a=input()
e=input()

Kommuri Pratap Reddy Institute Of Technology


p=int(input())
print(n)
print(a)
print(e)
print(p)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
john
123 Main Street, NY
john@[Link]
987654321
john
123 Main Street, NY
john@[Link]
987654321

Page No: 4
Test Case - 2

User Output

ID:
Alice
456 Elm Avenue, California
alice@[Link]
1234567890
Alice
456 Elm Avenue, California
alice@[Link]
1234567890

2022-2050-Faculty
Kommuri Pratap Reddy Institute Of Technology
Date: 2026-05-
[Link]: 3 Exp. Name: Triangle Pattern

Page No: 5
07

Aim:
Write a Python program to print a triangle pattern based on a user-input integer n.

ID:
The triangle starts with n in the first row and decreases by 1 in each subsequent row. The
number in each row is repeated as many times as the row number (1-based index).

Input Format:
• The program prompts "Enter a number: " followed an integer n, represents the
number of rows in the triangle.

Output Format:
• The triangle pattern, where each row contains the same number repeated row-
number times. Each number in the row should be separated by a space.

2022-2050-Faculty
Note: Refer to the visible test cases for better understanding.
Source Code:

Triangle_Pattern.py

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


for i in range(n,0,-1):

Kommuri Pratap Reddy Institute Of Technology


for j in range(n-i+1):
print(i,end=" ")
print()

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter a number:
5
5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
3
3

2 2
1 1 1
User Output
Enter a number:
Test Case - 2

Kommuri Pratap Reddy Institute Of Technology 2022-2050-Faculty ID: Page No: 6


Date: 2026-04-
[Link]: 4 Exp. Name: Character Type Identifier

Page No: 7
15

Aim:
Write a Python program to check whether a given single-character input is:

ID:
1. A digit (0-9)
2. A lowercase letter (a-z)
3. An uppercase letter (A-Z)
4. A special character (any character that is not a digit or letter).
Use an 'if-else-if' ladder to determine the type of character.

Input Format:
• The input line reads a single character.

Output Format:
The program outputs a string that classifies the input character into one of the following

2022-2050-Faculty
categories:
• "Digit" - If the character is a numeric digit (0-9).
• "Lowercase Letter" - If the character is a lowercase letter (a-z).
• "Uppercase Letter" - If the character is an uppercase letter (A-Z).
• "Special Character" - If the character does not belong to any of the above
categories.

Kommuri Pratap Reddy Institute Of Technology


Note: This question doesn't contain partial weightages, all test cases must be passed.
Source Code:

Character_Type_Identifier.py

n=input()
if [Link]():
print("Digit")
elif [Link]():
print("Lowercase Letter")
elif [Link]():
print("Uppercase Letter")
else:
print("Special Character")

Execution Results - All test cases have succeeded!


Test Case - 1
User Output

Page No: 8
5
Digit

Test Case - 2

ID:
User Output
@
Special Character

Test Case - 3

User Output
a

2022-2050-Faculty
Lowercase Letter

Test Case - 4

User Output
Z

Kommuri Pratap Reddy Institute Of Technology


Uppercase Letter
Exp. Name: Prime Numbers in an Date: 2026-05-
[Link]: 5

Page No: 9
Interval 11

Aim:
Write a Python program to print all prime numbers in a given interval. The program

ID:
should use the break statement to optimize the check for prime numbers.

Input Format:
• The first input line reads an integer representing the starting number of the
interval.
• The second input line reads an integer representing the ending number of the
interval.

Output Format:
• A list of all prime numbers within the given interval(both are inclusive), each
printed on a new line.

2022-2050-Faculty
Source Code:

Prime_Numbers_in_an_Interval.py

s = int(input())
e = int(input())
for num in range(s, e + 1):

Kommuri Pratap Reddy Institute Of Technology


if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
print(num)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1
10
2
3
5
7
7
5
3
2
2

13
11
15
User Output
Test Case - 2

Kommuri Pratap Reddy Institute Of Technology 2022-2050-Faculty ID: Page No: 10


Exp. Name: Convert List and Tuple to Date: 2026-05-
[Link]: 6

Page No: 11
Arrays 11

Aim:
Write a Python program to read elements for a list and a tuple from the user and convert

ID:
both into integer arrays.

Input Format:
• The first input line contains space-separated integers representing the elements of
the list.
• The second input line contains space-separated integers representing the
elements of the tuple.

Output Format:
• The first line prints the list of given elements.
• The second line prints the tuple of given elements.

2022-2050-Faculty
• The third line prints the array converted from the list.
• The fourth line prints the array converted from the tuple.

Note: Refer to visible test cases for better understanding of input and output formats.
Source Code:

[Link]

Kommuri Pratap Reddy Institute Of Technology


import array
a=map(int,input().split())
b=map(int,input().split())
a1=list(a)
b1=tuple(b)
print(a1)
print(b1)
a11=[Link]('i',a1)
b11=[Link]('i',b1)
print(a11)
print(b11)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
12345
7 8 9 10 6

Page No: 12
[1, 2, 3, 4, 5]
(7, 8, 9, 10, 6)
array('i', [1, 2, 3, 4, 5])
array('i', [7, 8, 9, 10, 6])

ID:
Test Case - 2

User Output
1 1 -1 -1
2 2 -2 -2
[1, 1, -1, -1]
(2, 2, -2, -2)
array('i', [1, 1, -1, -1])

2022-2050-Faculty
array('i', [2, 2, -2, -2])

Kommuri Pratap Reddy Institute Of Technology


Exp. Name: Common Values Between Date: 2026-05-
[Link]: 7

Page No: 13
Two Arrays 11

Aim:
Write a program in Python to find common values between two arrays. The program

ID:
should take two arrays as input and output the common elements.

Input Format:
• The first input line reads comma-separated integers representing the first array
elements.
• The second input line reads comma-separated integers representing the second
array elements.

Output Format:
• A list of integers representing the common values between the two arrays.

2022-2050-Faculty
Note:
• The common values are printed in the same order they first appear in the first
array, not in ascending or sorted order. Duplicate occurrences are ignored, and
each common value appears only once.
Source Code:

Common_values.py

Kommuri Pratap Reddy Institute Of Technology


array1 = input().split(",")
array2 = input().split(",")
array1 = [int([Link]()) for num in array1]
array2 = [int([Link]()) for num in array2]
common_elements = []
for i in array1:
if i in array2 and i not in common_elements:
common_elements.append(i)
print(common_elements)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
10, 20, 30
[10, 1]
[20, 30]

1,2,3,4,10
20, 30, 40

20,30,10,1
User Output
Test Case - 2

Kommuri Pratap Reddy Institute Of Technology 2022-2050-Faculty ID: Page No: 14


Date: 2026-04-
[Link]: 8 Exp. Name: Palindrome Checker

Page No: 15
15

Aim:
Write a Python function called palindrome() that takes a string argument and returns

ID:
"True" if the string is a palindrome and "False" otherwise.

Input Format:
• The input line reads a string.

Output Format:
• The output will be a string "True" or "False"

Note: This question does not have partial weightages for test cases. All the test cases
must be passed.

2022-2050-Faculty
Source Code:

Palindrome_Checker.py

# Write your code here for function palindrome


def palindrome(s):
a=s

Kommuri Pratap Reddy Institute Of Technology


b=a[::-1]
if a==b:
return True
else:
return False

s = input()
print(palindrome(s))

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
world
False
True
level
User Output
Test Case - 2

Kommuri Pratap Reddy Institute Of Technology 2022-2050-Faculty ID: Page No: 16


Date: 2026-05-
[Link]: 9 Exp. Name: Check if a List is Sorted

Page No: 17
07

Aim:
Write a Python function called is_sorted() that takes a list as a parameter and returns

ID:
"True" if the list is sorted in ascending order, and "False" otherwise.

Input Format:
• A single list of integers (comma-separated).

Output Format:
• If the list is sorted in ascending order, print "True".
• If the list is not in ascending order, print "False".

Note: This question does not have partial weightages for test cases. All the test cases
must be passed.

2022-2050-Faculty
Source Code:

Check_if_List_is_Sorted.py

def is_sorted(lst):
a=lst
b=sorted(a)

Kommuri Pratap Reddy Institute Of Technology


if a==b:
return True
else:
return False
lst = list(map(int, input().split(',')))
print(is_sorted(lst))

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
10, 20, 30
True

Test Case - 2
False
20, 50, 10
User Output

Kommuri Pratap Reddy Institute Of Technology 2022-2050-Faculty ID: Page No: 18


Date: 2026-05-
[Link]: 10 Exp. Name: Duplicates in a List

Page No: 19
11

Aim:
Write a Python function called has_duplicates() that takes a list as a parameter and

ID:
returns "True" if any element in the list appears more than once, and "False" otherwise.
The function should not modify the original list.

Input Format:
• A single list of integers (comma-separated).

Output Format:
• If the list contains duplicates, print "True".
• If the list does not contain duplicates, print "False".

Note: This question does not have partial weightages for test cases. All the test cases

2022-2050-Faculty
must be passed.
Source Code:

Duplicates_in_a_List.py

def has_duplicates(lst):
return len(lst) != len(set(lst))

Kommuri Pratap Reddy Institute Of Technology


lst = list(map(int, input().split(',')))
print(has_duplicates(lst))

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
10, 10, 10, 5
True

Test Case - 2

User Output
20, 30, 40, 50
False
Date: 2026-05-
[Link]: 11 Exp. Name: Remove Duplicates

Page No: 20
11

Aim:
Write a Python function called remove_duplicates() that takes a list as a parameter and

ID:
returns a new list containing only the unique elements from the original list. The order of
elements in the new list does not need to match the original order.

Input Format:
• A single list of integers (comma-separated).

Output Format:
• A list containing only the unique elements from the original list.
Source Code:

2022-2050-Faculty
Remove_Duplicates.py

def remove_duplicates(lst):
return list(set(lst))
lst = list(map(int, input().split(',')))
print(remove_duplicates(lst))

Kommuri Pratap Reddy Institute Of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
10, 20, 20, 10, 30
[10, 20, 30]

Test Case - 2

User Output
80, 20, 70, 70
[80, 20, 70]
Date: 2026-05-
[Link]: 12 Exp. Name: Update Word List in a File

Page No: 21
11

Aim:
Write a Python program that reads a list of words from a file, adds the words "I" and "a",

ID:
and writes the updated word list back to the file. The program should take the filename
as input and handle the case where the file does not exist.

Input Format:
• A single line containing the filename (string), e.g., "[Link]".

Output Format:
• The program should print all words from the updated file. Each word must appear
on a separate line.
• The words "I" and "a" must be included at the end of the list.
• If the file does not exist, print: "Error: File not found".

2022-2050-Faculty
Source Code:

[Link]

filename = input().strip()

try:

Kommuri Pratap Reddy Institute Of Technology


with open(filename, "r") as file:
words = [Link]().split()

[Link]("I")
[Link]("a")

with open(filename, "w") as file:


for word in words:
[Link](word + "\n")

for word in words:


print(word)

except FileNotFoundError:
print("Error: File not found")
[Link]

Page No: 22
banana
cherry
grape

ID:
[Link]

grape
apple
elephant

Execution Results - All test cases have succeeded!

2022-2050-Faculty
Test Case - 1

User Output
[Link]
banana
cherry

Kommuri Pratap Reddy Institute Of Technology


grape
I
a

Test Case - 2

User Output
[Link]
grape
apple
elephant
I
a

Test Case - 3

User Output
[Link]
Error: File not found

Kommuri Pratap Reddy Institute Of Technology 2022-2050-Faculty ID: Page No: 23


Date: 2026-05-
[Link]: 13 Exp. Name: Invert a Dictionary

Page No: 24
11

Aim:
Write a Python program to read a dictionary from the user and construct a function to

ID:
invert its content. The keys should become values, and the values should become keys.

Input Format:
• A single line containing multiple key-value pairs separated by commas.
• Each key and value is separated by a colon.
• Keys and values are strings without duplicate values.

Output Format:
• Print the inverted dictionary as value:key pairs separated by commas.
• Maintain the same order as in the input.

2022-2050-Faculty
Sample Input:
key1:value1, key2:value2
Sample Output:
value1:key1, value2:key2

Note:
• Refer to the visible test cases for better understanding of input and output format.

Kommuri Pratap Reddy Institute Of Technology


Source Code:

Invert-a_Dictionary.py

s = input().strip()
p = [Link](", ")
result = []
for pa in p:
key, value = [Link]().split(":")
[Link](value + ":" + key)

print(", ".join(result))

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
apple:red, banana:yellow,
green:kiwi

Page No: 25
red:apple, yellow:banana, kiwi:green

Test Case - 2

ID:
User Output
apple:fruit, dog:animal, sun:star,
red:color, circle:shape
fruit:apple, animal:dog, star:sun, color:red, shape:circle

2022-2050-Faculty
Kommuri Pratap Reddy Institute Of Technology
Exp. Name: Add Commas Between Date: 2026-05-
[Link]: 14

Page No: 26
Characters 11

Aim:
Write a Python program that takes a single string as input and inserts a comma between

ID:
every character of the string. For example, if the input string is "Apple", the program
should print the string "A,p,p,l,e".

Input Format:
• The input consists of a single string s, which include spaces. The string length will
be at least 1.

Output Format:
• The output should be a single string where each character of the input is separated
by a comma.

2022-2050-Faculty
Constraints:
• The input string will not be empty.
• No additional spaces or trailing commas should appear in the output.

Note:
• Spaces in the string are also treated as characters and will have commas inserted
around them.

Kommuri Pratap Reddy Institute Of Technology


Source Code:

adding_commas.py

a=input()
b=','.join(a)
print(b)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
apple
a,p,p,l,e

Test Case - 2
User Output

Page No: 27
Codetantra
C,o,d,e,t,a,n,t,r,a

Test Case - 3

ID:
User Output
pineapple banana
p,i,n,e,a,p,p,l,e, ,b,a,n,a,n,a

2022-2050-Faculty
Kommuri Pratap Reddy Institute Of Technology
Exp. Name: Remove a Word from a Date: 2026-06-
[Link]: 15

Page No: 28
String 04

Aim:
Write a Python program that removes all occurrences of a specified word from a given

ID:
string (sentence). The program should handle cases where the word occurs multiple times
or is surrounded by punctuation or spaces. The modified string should exclude all
instances of the given word.

Input Format:
• A single string s, which represents the sentence. The sentence may contain spaces,
letters, numbers, and punctuation.
• A single string word, representing the word to be removed from the sentence. The
word is case-sensitive.

Output Format:

2022-2050-Faculty
• Print the modified sentence after removing all exact occurrences of the specified
word.
• The spacing and punctuation of other parts of the sentence must be preserved.

Constraints:
• The input sentence will contain at least one word.
• The word to remove will be an exact substring of the sentence.

Kommuri Pratap Reddy Institute Of Technology


• Words are sequences of characters separated by spaces. Punctuation attached to
words is considered part of the word.
Source Code:

remove_word.py

s = input()
w = input()

result = " ".join(w1 for w1 in [Link]() if w1 != w)

print(result)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Python is fun
Python

Page No: 29
is fun

Test Case - 2

ID:
User Output
Good morning!
Good
morning!

Test Case - 3

User Output

2022-2050-Faculty
this is a test! test case
test
this is a test! case

Kommuri Pratap Reddy Institute Of Technology


Exp. Name: Word Capitalization Date: 2026-05-
[Link]: 16

Page No: 30
Without Built-in Methods 16

Aim:
Write a Python function called custom_title() that takes a sentence as an input

ID:
parameter and replaces the first letter of every word with the corresponding uppercase
letter. The rest of the letters in the word should be converted to lowercase. Do not use
built-in functions.

Input Format:
• A single line containing a sentence with words separated by spaces.
• Words may contain uppercase and lowercase letters, and numbers.

Output Format:
• A single line where each word starts with an uppercase letter, and the remaining
letters are in lowercase.

2022-2050-Faculty
Source Code:

[Link]

def custom_title(sentence):
words = [Link]()
result = []

Kommuri Pratap Reddy Institute Of Technology


for w in words:
[Link](w[0].upper() + w[1:].lower())
return " ".join(result)
sentence = input()

print(custom_title(sentence))

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
hEllo woRLd
Hello World

Test Case - 2
User Output
Python3 Is cOOl
Python3 Is Cool

Kommuri Pratap Reddy Institute Of Technology 2022-2050-Faculty ID: Page No: 31

You might also like