0% found this document useful (0 votes)
99 views55 pages

Execution Results: Eldestage - Py

Uploaded by

theinhumanus0
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)
99 views55 pages

Execution Results: Eldestage - Py

Uploaded by

theinhumanus0
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
You are on page 1/ 55

S.No: 1 Exp.

Name: Eldest of three Date: 2024-10-15

Aim:
In a classroom, there are three students of varying ages. The teacher, who is responsible for the class, has a

Page No: 1
specific task at hand to determine the oldest student among the three. To achieve this, the teacher plans to
develop a Python program that takes the ages of the students as input and identifies the eldest among them.
Could you help the teacher in crafting this Python program?

ID: 249X5A04M0
Input format:
The first three lines are the integers that represent the ages of the three students.

Output format:
The output is the integer that represents the age of the elder student.
Source Code:

eldestAge.py

a=int(input())
b=int(input())
c=int(input())
if a>b:

2023-2027-ECE-B
if a>c:
print(a)
else:
if b>c: print(b)
else:print(c)

G Pulla Reddy Engineering College (Autonomous)


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

User Output
12
14
13
14

Test Case - 2

User Output
11
43
23
43
Exp. Name: Prime numbers from 2 to the given
S.No: 2 Date: 2024-10-15
positive number

Aim:

Page No: 2
Implement a program that takes an integer n input from the user and prints all the prime numbers from 2 to n.

Note: A prime number is divisible only by 1 and itself.

Input format:

ID: 249X5A04M0
• The input contains a positive integer n.
Output Format:
• Contains the list of prime numbers up to n

Example:
Input:
10
Output:
[2, 3, 5, 7]

Constraints:
• The input n should be a positive integer greater than or equal to 2.

2023-2027-ECE-B
• The algorithm should be efficient, aiming for a time complexity of approximately O(n ∗ sqrt(n)) where n
is the input number.
• The algorithm should handle large inputs reasonably well, without consuming excessive memory or taking
an unreasonable amount of time to execute.
Source Code:

CTP32252.py

G Pulla Reddy Engineering College (Autonomous)


n=int(input())
l=[]
for i in range (2,n+1):
for j in range (2,i):
if i%j==0:
break
else:
l.append(i)
print(l)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
10
[2, 3, 5, 7]

Test Case - 2

User Output
20
[2, 3, 5, 7, 11, 13, 17, 19]

Test Case - 3

User Output

Page No: 3
30
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

ID: 249X5A04M0
2023-2027-ECE-B
G Pulla Reddy Engineering College (Autonomous)
S.No: 3 Exp. Name: Swap two numbers Date: 2024-10-15

Aim:
Write a Python program that reads two numbers from the user and swaps their values using only two variables.

Page No: 4
Source Code:

swapping.py

a=float(input("Enter the first number: "))

ID: 249X5A04M0
b=float(input("Enter the second number: "))
print("Original numbers:")
print("First number:",a)
print("Second number:",b)
print("Swapped numbers:")
print("First number:",b)
print("Second number:",a)

Execution Results - All test cases have succeeded!

2023-2027-ECE-B
Test Case - 1

User Output
Enter the first number:
2
Enter the second number:

G Pulla Reddy Engineering College (Autonomous)


3
Original numbers:
First number: 2.0
Second number: 3.0
Swapped numbers:
First number: 3.0
Second number: 2.0

Test Case - 2

User Output
Enter the first number:
-3.5
Enter the second number:
2.5
Original numbers:
First number: -3.5
Second number: 2.5
Swapped numbers:
First number: 2.5
Second number: -3.5
Exp. Name: Arithmetic Operations (Addition,
S.No: 4 Date: 2024-10-15
Subtraction, Multiplication, Division)

Aim:

Page No: 5
Write a program to read two integer inputs from the user and perform the following arithmetic operations
addition, subtraction, multiplication, and division, and print the result.

Input layout:
The first two lines represent the input of 2 integers num1 and num2.

ID: 249X5A04M0
Output layout:
First line should print the addition of provided inputs.
Second line should print the subtraction of provided inputs.
Third line should print the multiplication of provided inputs.
Fourth line should print the division of provided inputs.

Example1:
Input:
num1: 40
num2: 20

2023-2027-ECE-B
Output:
Addition of 40 and 20 = 60
Subtraction of 40 and 20 = 20
Multiplication of 40 and 20 = 800
Division of 40 and 20 = 2.0
Source Code:

G Pulla Reddy Engineering College (Autonomous)


Arithexample2.py

a=int(input("num1: "))
b=int(input("num2: "))
c=a+b
d=a-b
e=a*b
f=a/b
print("Addition of",a,"and",b,"=",c)
print("Subtraction of",a,"and",b,"=",d)
print("Multiplication of",a,"and",b,"=",e)
print("Division of",a,"and",b,"=",f)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
num1:
40
num2:
20
Addition of 40 and 20 = 60
Subtraction of 40 and 20 = 20
Multiplication of 40 and 20 = 800
Division of 40 and 20 = 2.0

Test Case - 2

Page No: 6
User Output
num1:
4

ID: 249X5A04M0
num2:
6
Addition of 4 and 6 = 10
Subtraction of 4 and 6 = -2
Multiplication of 4 and 6 = 24
Division of 4 and 6 = 0.6666666666666666

2023-2027-ECE-B
G Pulla Reddy Engineering College (Autonomous)
S.No: 5 Exp. Name: logical operator Date: 2024-10-15

Aim:
Write a Python program that demonstrates the usage of logical operators. Your program should read two integer

Page No: 7
values from the user and perform logical operations (and, or, not). Display the corresponding output for each
operation.

Input Format:
• The program should read two integer values from the user, each on a new line.

ID: 249X5A04M0
Output Format:
• Print the result of num1 and num2 in the format "{num1} and {num2}: result"
• Print the result of num1 or num2 in the format "{num1} or {num2}: result"
• Print the result of not num1 in the format "not {num1}: result"
• Print the result of not num2 in the format "not {num2}: result"
Source Code:

logicoperator.py

a=int(input())
b=int(input())

2023-2027-ECE-B
print(a," and ",b,": ",a and b, sep='')
print(a," or ",b,": ",a or b, sep='')
print("not ",a,": ",not a,sep='')
print("not ",b,": ",not b,sep='')

G Pulla Reddy Engineering College (Autonomous)


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

User Output
3
4
3 and 4: 4
3 or 4: 3
not 3: False
not 4: False

Test Case - 2

User Output
0
1
0 and 1: 0
0 or 1: 1
not 0: True
not 1: False

Test Case - 3
0
0

0 or 0: 0
0 and 0: 0

not 0: True
not 0: True
User Output

G Pulla Reddy Engineering College (Autonomous) 2023-2027-ECE-B ID: 249X5A04M0 Page No: 8
S.No: 6 Exp. Name: Comparison Operators Date: 2024-10-16

Aim:
Write a python program for demonstrating the usage of comparison operators.The program should read two

Page No: 9
integers from the input. Then, perform each of the six comparison operations and print the result. Each line of
output should contain the comparison operation followed by a boolean value (True or False), indicating whether
the comparison holds true or not.

Input format:

ID: 249X5A04M0
The first 2 lines reads integers.

Output format:
The output line contains boolean values which represent the results of various comparisons operations ==, !=, <,
<=, >, >= and the format should be like below:
{num1} (boolean condition) {num2} : boolean value respectively
Source Code:

operators.py
num1=int(input( ))
num2=int(input( ))

2023-2027-ECE-B
print(f"{num1} == {num2} : {num1==num2}")
print(f"{num1} != {num2} : {num1!=num2}")
print(f"{num1} < {num2} : {num1<num2}")
print(f"{num1} <= {num2} : {num1<=num2}")
print(f"{num1} > {num2} : {num1>num2}")
print(f"{num1} >= {num2} : {num1>=num2}")

G Pulla Reddy Engineering College (Autonomous)


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

User Output
4
4
4 == 4 : True
4 != 4 : False
4 < 4 : False
4 <= 4 : True
4 > 4 : False
4 >= 4 : True

Test Case - 2

User Output
3
2
3 == 2 : False
3 != 2 : True
3 < 2 : False
3 <= 2 : False
3 > 2 : True
3 >= 2 : True

Test Case - 3

Page No: 10
User Output
5
5

ID: 249X5A04M0
5 == 5 : True
5 != 5 : False
5 < 5 : False
5 <= 5 : True
5 > 5 : False
5 >= 5 : True

2023-2027-ECE-B
G Pulla Reddy Engineering College (Autonomous)
Exp. Name: Writing an example using identity
S.No: 7 Date: 2024-10-15
operator "is not"

Aim:

Page No: 11
Take two integers x and y as input from the console using input() function. Using the identity operator
is not check if x and y are same objects or not, print to the console, the result of the two input integers as
shown in the example.

ID: 249X5A04M0
Sample Input and Output 1:

x: -8
y: -8
-8 is not -8 True

Sample Input and Output 2:

x: 256
y: 256
256 is not 256 False

2023-2027-ECE-B
Source Code:

IdenopExample3.py

x=int(input("x: "))
y=int(input("y: "))
print(x, 'is not', y,x is not y)

G Pulla Reddy Engineering College (Autonomous)


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

User Output
x:
-8
y:
-8
-8 is not -8 True

Test Case - 2

User Output
x:
256
y:
256
256 is not 256 False
S.No: 8 Exp. Name: Bitwise Operator Date: 2024-10-16

Aim:
Take two integers x and y as inputs from the console using input() function. For each bitwise operator ( >> , <<,

Page No: 12
&, |, ~, and ^ ), print to the console, the result of applying these operators on the two input integers as shown in
the example.

Sample Input and Output:


x: 52

ID: 249X5A04M0
y: 20
52 >> 20 is 0
52 << 20 is 54525952
52 & 20 is 20
52 | 20 is 52
~ 52 is -53
52 ^ 20 is 32
Source Code:

Bitopexample1.py

x=int(input('x: '))
y=int(input('y: '))

2023-2027-ECE-B
print(x,'>>',y,'is',x>>y)
print(x,'<<',y,'is',x<<y)
print(x,'&',y,'is',x&y)
print(x,'|',y,'is',x|y)
print('~',x,'is',~x)
print(x,'^',y,'is',x^y)

G Pulla Reddy Engineering College (Autonomous)


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

User Output
x:
30
y:
2
30 >> 2 is 7
30 << 2 is 120
30 & 2 is 2
30 | 2 is 30
~ 30 is -31
30 ^ 2 is 28

Test Case - 2

User Output
x:
10
y:
20

~ 10 is -11
10 & 20 is 0

10 ^ 20 is 30
10 | 20 is 30
10 >> 20 is 0
10 << 20 is 10485760

G Pulla Reddy Engineering College (Autonomous) 2023-2027-ECE-B ID: 249X5A04M0 Page No: 13
Exp. Name: Write a program on assignment
S.No: 9 Date: 2024-10-16
operators =,+=,-= and *=

Aim:

Page No: 14
Take two integers x and y as inputs from the console using input() function. For each assignment operator (
= , += , -= , and *= ), print to the console, the result of applying these operators on the two input integers as
shown in the example.

ID: 249X5A04M0
Assumption:
• y is a non-zero integer
Sample Input and Output:

x: 20
y: 30
x = y: 30
x += y: 50
x -= y: -10
x *= y: 600

Hint: Before every assignment operation, reset the values of x and y to the initial values.

2023-2027-ECE-B
Source Code:

Assignexample2.py

x=int(input('x: '))
y=int(input('y: '))
print('x = y:',y)
print('x += y:',x+y)

G Pulla Reddy Engineering College (Autonomous)


print('x -= y:',x-y)
print('x *= y:',x*y)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
x:
20
y:
30
x = y: 30
x += y: 50
x -= y: -10
x *= y: 600

Test Case - 2

User Output
x:
6
3
y:

x = y: 3

x -= y: 3
x += y: 9

x *= y: 18

G Pulla Reddy Engineering College (Autonomous) 2023-2027-ECE-B ID: 249X5A04M0 Page No: 15
S.No: 10 Exp. Name: Ternary Operator Date: 2024-10-15

Aim:
Write a python program to find the maximum value of given two numbers using ternary operator.

Page No: 16
Input Format:
• First line should prompt the user to enter first integer
• Second line should prompt the user to enter second integer

ID: 249X5A04M0
Output Format:
• Output should print the maximum value using ternary operator
Source Code:

ternary.py
a=int(input())
b=int(input())
max_num=a if a>b else b
print(max_num)

2023-2027-ECE-B
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
91

G Pulla Reddy Engineering College (Autonomous)


123
123

Test Case - 2

User Output
15
-23
15
S.No: 11 Exp. Name: Membership Operator Date: 2024-10-15

Aim:
Write a python program to demonstrate membership operator.

Page No: 17
Predefined list of integers is already provided to you.

Input:
• The program should prompt the user to enter an integer number.

ID: 249X5A04M0
Output:
• Use "in" operator to check whether the given integer is present in the list or not and print the result.
• Use "not in" operator to check whether the given integer is not present in the list and print the result.
Source Code:

membership.py

num_list = [1, 2, 3, 4, 5, 15, 25, 89, 54, 123, 489]


a=int(input(''))
print(a in num_list)
print(a not in num_list)

2023-2027-ECE-B
Execution Results - All test cases have succeeded!
Test Case - 1

G Pulla Reddy Engineering College (Autonomous)


User Output
21
False
True

Test Case - 2

User Output
-3
False
True

Test Case - 3

User Output
5
True
False

Test Case - 4

User Output
True
False

G Pulla Reddy Engineering College (Autonomous) 2023-2027-ECE-B ID: 249X5A04M0 Page No: 18
Exp. Name: Write a program to addition,
S.No: 12 subtraction and multiplication and division of two Date: 2024-10-16
complex numbers.

Page No: 19
Aim:
Take two complex numbers as input from the console. For each arithmetic operator( + , - , * , and / ),
perform these operations on the two complex numbers and print the result as shown in the example.

ID: 249X5A04M0
Sample Input and Output:

c1: 10+3j
c2: 14+3j
a1 + b2 = (24+6j)
a1 - b2 = (-4+0j)
a1 * b2 = (131+72j)
a1 / b2 = (0.7268292682926829+0.05853658536585366j)

Source Code:

ComplexNums.py

2023-2027-ECE-B
c1=complex(input('c1: '))
c2=complex(input('c2: '))
print("a1 + b2 =",c1+c2)
print("a1 - b2 =",c1-c2)
print("a1 * b2 =",c1*c2)
print("a1 / b2 =",c1/c2)

G Pulla Reddy Engineering College (Autonomous)


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

User Output
c1:
10+3j
c2:
41+3j
a1 + b2 = (51+6j)
a1 - b2 = (-31+0j)
a1 * b2 = (401+153j)
a1 / b2 = (0.24792899408284022+0.055029585798816574j)

Test Case - 2

User Output
c1:
5+10j
c2:
25+15j
a1 - b2 = (-20-5j)
a1 * b2 = (-25+325j)
a1 / b2 = (0.3235294117647059+0.20588235294117646j)

Page No: 20
ID: 249X5A04M0
2023-2027-ECE-B
G Pulla Reddy Engineering College (Autonomous)
S.No: 13 Exp. Name: Print Multiplication Table Date: 2024-10-16

Aim:
Write a python program to print multiplication table of given number in given format.

Page No: 21
Input:
• Prompt the user to enter a number (n) for which they want to generate the multiplication table.

Output Format:

ID: 249X5A04M0
• Print the multiplication table of the entered number in the following format for numbers from 1 to 10:
• Each line should display the multiplication operation in the format {n}*{i}={result}, where {n} is the entered
number, {i} ranges from 1 to 10, and {result} is the product of {n} and {i}.
Source Code:

multiplication_table.py
a=int(input("Enter a number : "))
for i in range (1,11):
print(a,'*',i,'=',a*i,sep='')

2023-2027-ECE-B
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter a number :

G Pulla Reddy Engineering College (Autonomous)


2
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20

Test Case - 2

User Output
Enter a number :
13
13*1=13
13*2=26
13*3=39
13*4=52
13*5=65
13*6=78
13*9=117
13*8=104

13*10=130

G Pulla Reddy Engineering College (Autonomous) 2023-2027-ECE-B ID: 249X5A04M0 Page No: 22
S.No: 14 Exp. Name: Series Calculation 1 Date: 2024-10-16

Aim:
Your friend is a mathematician, and he needs your help in verifying a mathematical series. You have three inputs a

Page No: 23
, b, and N . Your task is to print the output with the series that results from the following equation
1 2 N
a + b , a + b , .., a + b

Input Format:
• The first line of the input consists of an integer, a.

ID: 249X5A04M0
• The second line of the input consists of an integer b representing the base for the series.
• The third line of the input consists an integer, N representing the number of terms in the series.

Output Format:
• The output is a single line containing the space-separated terms of the given series.
Source Code:

series.py

a=int(input('a: '))
b=int(input('b: '))
N=int(input('N: '))

2023-2027-ECE-B
print('result: ',end='')
for i in range (1,N+1):
print(a+pow(b,i),end=' ')

Execution Results - All test cases have succeeded!

G Pulla Reddy Engineering College (Autonomous)


Test Case - 1

User Output
a:
3
b:
2
N:
3
result: 5 7 11

Test Case - 2

User Output
a:
4
b:
3
N:
5
result: 7 13 31 85 247
S.No: 15 Exp. Name: Area of the Pyramid Date: 2024-10-16

Aim:
Humpty and Dumpty both went to Egypt to explore the Pyramids. On the site of the pyramids, Humpty saw a

Page No: 24
unique pyramid and asked Dumpty what's the area of this pyramid. Dumpty has no idea about it. Can you help
Dumpty with this?

Functional Description:
Area = (height * base) / 2

ID: 249X5A04M0
Constraints:
• height and base must be >= 1
• Display the area computed upto 3 decimal places (Precision)

Sample Test case :


b: 31.5
h: 43.7
Area : 688.275
Source Code:

areaofpyramid.py

2023-2027-ECE-B
a=float(input("b: "))
b=float(input("h: "))
A=(a*b/2)
print("Area: ",end="")
print(f"{A:.3f}")

G Pulla Reddy Engineering College (Autonomous)


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

User Output
b:
31.5
h:
43.7
Area: 688.275

Test Case - 2

User Output
b:
8.2
h:
10.63
Area: 43.583
S.No: 16 Exp. Name: Applications of Loops Date: 2024-10-16

Aim:
Write a Python program that prints the right-angled triangle pattern as shown in the example. This program

Page No: 25
prompts the user to input an integer, which in turn determines the number of rows in the pattern

Example:
If the given integer is 5, then the pattern would be
*

ID: 249X5A04M0
* *
* *
* *
* * * * *

Input format:
• The input is the integer that represents the number of rows in the pattern.

Output format:
• The output is the pattern that resembles the pattern as shown above.
Source Code:

2023-2027-ECE-B
print_pattern.py

rows = int(input())
for i in range(1, rows):
for j in range(0,i):
if j==0 or j== i-1:
print("*",end =" ")
else:

G Pulla Reddy Engineering College (Autonomous)


print(" ",end =" ")
print( )
for i in range(1, rows+1):
print("*", end=" ")
print( )

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
5
*
* *
* *
* *
* * * * *

Test Case - 2

User Output
8
*
*
*
*
*
* *
*
*
*
*
*
* * * * * * * *

G Pulla Reddy Engineering College (Autonomous) 2023-2027-ECE-B ID: 249X5A04M0 Page No: 26
S.No: 17 Exp. Name: Perfect Roll number Date: 2024-10-16

Aim:
In a group of students, everyone has a unique roll number. One day they are willing to find the students who

Page No: 27
have the perfect roll numbers.

They need your assistance to determine whether the given roll number is the perfect number or not. Can you
assist them by writing a program in Python?

ID: 249X5A04M0
Note: A perfect Number is a number for which the sum of its positive divisors (excluding itself) is equal to the
number itself.

Constraints:
Roll Number must be a positive integer.

Input Format:
The input line reads an integer representing a roll number.

Output Format:
The output line prints whether the given roll number is a perfect number or not.
Source Code:

2023-2027-ECE-B
PerfectRollNumber.py

def is_perfect_number(number):
c=0
for i in range (1,number):
if number %i==0:

G Pulla Reddy Engineering College (Autonomous)


c=c+i
if c==number:
return c
roll_number = int(input())

if is_perfect_number(roll_number):
print("Perfect number")
else:
print("Not a perfect number")

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
6
Perfect number

Test Case - 2

User Output
25
Not a perfect number
S.No: 18 Exp. Name: Function with multiple return values Date: 2024-10-16

Aim:
Write a Python program that defines a function with multiple return values to calculate sum, mean and maximum

Page No: 28
of the given list of numbers.

Input Format:
• A list of space-separated integers.

ID: 249X5A04M0
Output Format:
• The total sum of the integers.
• The mean (average) of the integers, rounded to two decimal places.
• The maximum value among the integers

Constraints:
• 0 < length of the list <= 100
Source Code:

multiplefun.py

def calculate_stats(n):

2023-2027-ECE-B
t_s=sum(n)
m=round(t_s/len(n),2)
m_v=max(n)
print(t_s)
print(m)
print(m_v)
n=list(map(int, input().split()))

G Pulla Reddy Engineering College (Autonomous)


calculate_stats(n)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
12 23 34 45 56 67 78 89
404
50.5
89

Test Case - 2

User Output
-23 -45 -56 -90 -12 -87
-313
-52.17
-12
S.No: 19 Exp. Name: Default Arguments Function Date: 2024-10-16

Aim:
Write a program to define a function using default arguments.

Page No: 29
Input Format:
• First line of Input should prompt the user to enter the name by displaying "-1 for default: ", If -1 is entered,
the default value "Dude" is used.
• Second line of input should prompt the user to enter the age by displaying: "-1 for default: ", If -1 is

ID: 249X5A04M0
entered, the default value "0" is used.
• Third line of input should prompt the user to enter the country by displaying: "-1 for default: ", If -1 is
entered, the default value "Space" is used.

Output Format: The output should print the following line with provided inputs.
• Hello, {name}! You are {age} years old and you are from {country}.
Source Code:

default.py
def greet( n,a,c):
print(f'Hello, {n}! You are {a} years old and you are from {c}')

2023-2027-ECE-B
def get_user_input( ):
a=input('-1 for default: ')
b=input('-1 for default: ')
c=input('-1 for default:')
x=a if a!='-1' else 'Dude'
y=b if b!='-1' else '0'
z=c if c!='-1' else 'Space'

G Pulla Reddy Engineering College (Autonomous)


return x,y,z
n,a,c=get_user_input( )
greet(n,a,c)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
-1 for default:
-1
-1 for default:
-1
-1 for default:
-1
Hello, Dude! You are 0 years old and you are from Space

Test Case - 2

User Output
-1 for default:
Ram
-1
-1 for default:
-1
Hello, Ram! You are 0 years old and you are from Space

Page No: 30
Test Case - 3

User Output
-1 for default:

ID: 249X5A04M0
Vikas
-1 for default:
26
-1 for default:
America
Hello, Vikas! You are 26 years old and you are from America

2023-2027-ECE-B
G Pulla Reddy Engineering College (Autonomous)
S.No: 20 Exp. Name: Length of the String Date: 2024-10-17

Aim:
Write a program to find the length of the string without using any libraryfunctions.

Page No: 31
Input Format:
A single line containing a string s.

Output Format:

ID: 249X5A04M0
A single integer representing the length of the input string.
Source Code:

length.py

def string_length(s):
c=0
for i in s:
c+=1
return c
s=str(input())
print(string_length(s))

2023-2027-ECE-B
Execution Results - All test cases have succeeded!
Test Case - 1

G Pulla Reddy Engineering College (Autonomous)


User Output
Code Tantra
11

Test Case - 2

User Output
Hello@World!!
13

Test Case - 3

User Output
12345678
8
S.No: 21 Exp. Name: Count of Substring in given string Date: 2024-10-16

Aim:
The user enters a string and a substring. You have to print the number of times that the substring occurs in the

Page No: 32
given string. String traversal will take place from left to right, not from right to left.

Constraints:
• 1 <= len(string) <=200.
• Each character in the string is an ASCII character.

ID: 249X5A04M0
Input Format:
• The first line of input contains the main string.
• The second line contains the substring.

Output Format:
• Output is an integer representing the total number of occurrences of the substring in the main string.

Source Code:

SubstringCount.py

2023-2027-ECE-B
def count_substring_occrence(main_string,sub_string):
count = 0
sub_len = len(sub_string)
for i in range(len(main_string) - sub_len + 1):
if main_string[i:i+sub_len]== sub_string:
count +=1
return count

G Pulla Reddy Engineering College (Autonomous)


main_string = input( )
sub_string = input( )
print(count_substring_occrence(main_string,sub_string))

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
ABCDCDC
CDC
2

Test Case - 2

User Output
CodeTantra
co
0
S.No: 22 Exp. Name: List operations Date: 2024-10-17

Aim:
Write a program to perform the following operations on a list.

Page No: 33
i. Addition
ii. Insertion
iii. Slicing

Input Format:

ID: 249X5A04M0
• The first line should take space-separated integers to form the list.
• The second line of input should prompt the user to enter two integers separated by a space representing
the key and value for the insertion.
• The third line should take space-separated start index and end index for slicing.

Output Format:
• First line should display the initial list.
• Second line should display the list after insertion.
• Third line should display the list after slicing.

Example 1:
Input:

2023-2027-ECE-B
12345
05
03

Output
[1, 2, 3, 4, 5]
[5, 1, 2, 3, 4, 5]

G Pulla Reddy Engineering College (Autonomous)


[5, 1, 2]

Note:
• In Python, adding an element with append adds it to the end of a list. To insert an element at a specific
index, insert operation need to be used.
Source Code:

listOp.py
a=list(map(int, input().split()))
b=list(map(int, input().split()))
c=list(map(int, input().split()))
print(a)
a.insert(b[0],b[1])
print(a)
a=a[c[0]:c[1]]
print(a)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
12345
03
[1, 2, 3, 4, 5]
[5, 1, 2, 3, 4, 5]
[5, 1, 2]

Page No: 34
Test Case - 2

User Output
12 52 65 41 23 89 654

ID: 249X5A04M0
7 485
08
[12, 52, 65, 41, 23, 89, 654]
[12, 52, 65, 41, 23, 89, 654, 485]
[12, 52, 65, 41, 23, 89, 654, 485]

2023-2027-ECE-B
G Pulla Reddy Engineering College (Autonomous)
S.No: 23 Exp. Name: Demonstration of Working with Lists Date: 2024-10-17

Aim:
Write a program to demonstrate working with lists in python.

Page No: 35
Sample Test Case:

Input:
Enter a list of elements (separated by commas): 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

ID: 249X5A04M0
Output:
List of elements: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Length of list: 10
Minimum value: 1
Maximum value: 10
Sum of values: 55
Sorted list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Source Code:

list.py

2023-2027-ECE-B
N=list(map(int,input('Enter a list of elements (separated by commas): ').split(',')))
print('List of elements:',N)
print('Length of list:',len(N))
print('Minimum value:',min(N))
print('Maximum value:',max(N))
print('Sum of values:',sum(N))
print('Sorted list:',sorted(N))

G Pulla Reddy Engineering College (Autonomous)


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

User Output
Enter a list of elements (separated by commas):
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
List of elements: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Length of list: 10
Minimum value: 1
Maximum value: 10
Sum of values: 55
Sorted list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Test Case - 2

User Output
Enter a list of elements (separated by commas):
3, 8, 2, 9, 5
List of elements: [3, 8, 2, 9, 5]
Length of list: 5
Maximum value: 9
Sum of values: 27
Sorted list: [2, 3, 5, 8, 9]

G Pulla Reddy Engineering College (Autonomous) 2023-2027-ECE-B ID: 249X5A04M0 Page No: 36
S.No: 24 Exp. Name: List of Odd squares Date: 2024-10-17

Aim:
The given Python program defines two functions odd_checker and square_list, both functions take the list as the

Page No: 37
argument

square_list: This function squares each element of the list that has passed and returns the list of squares.
odd_checker: This function filters out the odd numbers from the list that has passed and returns a list of odd
numbers.

ID: 249X5A04M0
Your task is to write the required logic in the function's body in accordance with the functionalities given.

Input format:
• The input is the list of integers separated by comma(,)

Output format:
• The output is the list of integers that represents a list of squared odd numbers from the input list.

Note: For simplicity, the code for reading input & printing output has already been given. You just need to fill the
code in the body of the functions given.
Source Code:

2023-2027-ECE-B
func_comp.py
def odd_checker(lst):
b=[]
for i in range (0,len(lst)):
if lst[i]%2==1:

G Pulla Reddy Engineering College (Autonomous)


c=lst[i]
b.append(c)
return b
def square_list(lst):
a=[]
for i in range (0,len(lst)):
k=lst[i]*lst[i]
a.append(k)
return a
input_lst = list(map(int,input().split(",")))
print(odd_checker(square_list(input_lst)))

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1,2,3,4,5,6
[1, 9, 25]

Test Case - 2

User Output
[81, 121]

G Pulla Reddy Engineering College (Autonomous) 2023-2027-ECE-B ID: 249X5A04M0 Page No: 38
S.No: 25 Exp. Name: Chocolate Distribution Date: 2024-10-17

Aim:
Complete the given Python function order_chocolate that takes two integer arguments, n (Number of

Page No: 39
chocolates) and m (Number of students).

The function is designed to distribute chocolates among students. It checks whether it's possible to evenly
distribute n chocolates among m students. If it's possible, the function returns 0. If it's not possible to evenly
distribute the chocolates, the function calculates the minimum number of extra chocolates needed to make the

ID: 249X5A04M0
distribution even and returns that number.

Hint:
• When you divide n by m if you have 0 remainder, then it is possible to distribute evenly.
• Otherwise, you can take away the remainder from the number of students.

Input format:
• The first line of the input is an integer that represents the number of chocolates.
• The second line of the input is an integer that represents the number of students.

Output format:
• If the number of students entered is a positive integer greater than zero, then output the minimum integer

2023-2027-ECE-B
representing the number of chocolates needed to evenly distribute among them otherwise print "Invalid".

Note: For simplicity, the code for reading input & printing output has already been given. You just need to fill the
code in the function body given.
Source Code:

order_chocolate.py

G Pulla Reddy Engineering College (Autonomous)


def order_chocolate(n,m):
if m==0:
return 'Invalid'
else:
a=n%m
if a==0:
return 0
else:
return m-a
chclts = int(input())
stdnts = int(input())
print(order_chocolate(chclts,stdnts))

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
78
8
2
Test Case - 2

User Output
85
6

Page No: 40
5

Test Case - 3

ID: 249X5A04M0
User Output
9
3
0

Test Case - 4

User Output
25

2023-2027-ECE-B
0
Invalid

G Pulla Reddy Engineering College (Autonomous)


S.No: 26 Exp. Name: Maximum product among all the pairs. Date: 2024-10-17

Aim:
In the ancient city of Arithmetica, a unique method of encoding messages has been discovered, involving a

Page No: 41
special mathematical technique. The encoded messages require finding the product between pairs of numbers in
a given list and then determining the maximum product achievable. Your task is to create a Python program to
decipher these messages by finding and displaying the largest product that can be obtained by multiplying any
two numbers from the provided list.

ID: 249X5A04M0
Input Format:
The first line contains space-separated positive integers representing the elements of the list.

Output Format:
The output is the integer that represents the maximum product achievable by multiplying any two numbers from
the given list.

Constraints:
The elements of the list must be positive.

Example:
Input: 18 5 12 8 3

2023-2027-ECE-B
Output: 216

Explanation:
In the list [18, 5, 12, 8, 3], the maximum product is obtained by multiplying 18 and 12, resulting in 216.

Note:
For simplicity, the code for taking input and printing output is provided to you in the editor. You just need to fill

G Pulla Reddy Engineering College (Autonomous)


in the required function body to find the maximum product.
Source Code:

maxproduct.py

def find_max_product(nums):
a=nums
a.sort()
b=a[-1]*a[-2]
return b
input_numbers = input()
numbers_list = list(map(int, input_numbers.split()))
result = find_max_product(numbers_list)
print(result)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
12345
20
216
18 5 12 8 3
User Output
Test Case - 2

G Pulla Reddy Engineering College (Autonomous) 2023-2027-ECE-B ID: 249X5A04M0 Page No: 42
Exp. Name: Product of maximum and minimum
S.No: 27 Date: 2024-10-17
palindrome numbers in given list

Aim:

Page No: 43
In a small town named Palindromia, there's a coding event going on. The challenge in the event is to find and
showcase the product of the maximum and minimum palindrome numbers from a list of numbers.

Can you showcase your talent by cracking the challenge?

ID: 249X5A04M0
Input Format:
• The input line reads space-separated integers.

Output Format:
• The output is an integer representing the product of maximum and minimum palindrome numbers. If
there are no palindrome numbers in the given list, print "0" (zero).

Note: If there is only one palindromic number in the list, it will be considered both the maximum and minimum
palindromic number. In such cases, the product of the maximum and minimum palindromic numbers will be the
square of that single palindromic number.
Source Code:

2023-2027-ECE-B
Min_max_palindrome_product.py

def is_palindrome(num):
return str(num)==str(num)[::-1]
def find_palindrome_product(numbers):
palindromes=[num for num in numbers if is_palindrome(num)]
if not palindromes:

G Pulla Reddy Engineering College (Autonomous)


return 0
max_palindrome = max(palindromes)
min_palindrome = min(palindromes)
return max_palindrome*min_palindrome
input_numbers=list(map(int,input().split()))
result=find_palindrome_product(input_numbers)
print(result)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
121 15 78 11
1331

Test Case - 2

User Output
95 48 74 621 35
0
S.No: 28 Exp. Name: Tuple Creation Date: 2024-10-17

Aim:
Write a Python program that prompts the user to enter details for two members and creates tuples for each

Page No: 44
member consisting of (name(string), age(int), address(string), college(string)). Concatenate these tuples into a
single tuple and print the concatenated tuple.

Input Format:
• The first line should take user input with Name, Age, Address, College as space-separated values for

ID: 249X5A04M0
Member 1.
• The second line should take user input with Name, Age, Address, College as space-separated values for
Member 2.

Output Format:
• In the first line print the tuple for Member 1.
• Print the tuple for Member 2 in the next line.
• Finally, print the concatenated tuple of both members.

Example1:

Input:

2023-2027-ECE-B
John 12 Tampa St.Joseph
Jane 15 Florida St.Mary

Output:
('John',·'12',·'Tampa',·'St.Joseph') // Member 1
('Jane',·'15',·'Florida',·'St.Mary') // Member 2
('John',·'12',·'Tampa',·'St.Joseph',·'Jane',·'15',·'Florida',·'St.Mary') // Concatenated Tuple

G Pulla Reddy Engineering College (Autonomous)


Source Code:

stdTup.py

a=input()
b=input()
n1,a1,ad1,c1=a.split()
t1=(n1,int(a1),ad1,c1)
n2,a2,ad2,c2=b.split()
t2=(n2,int(a2),ad2,c2)
print(t1)
print(t2)
print(t1+t2)

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
John 12 Tampa St.Joseph
Jane 15 Florida St.Mary
('John', 12, 'Tampa', 'St.Joseph')
('Jane', 15, 'Florida', 'St.Mary')
('John', 12, 'Tampa', 'St.Joseph', 'Jane', 15, 'Florida', 'St.Mary')
Test Case - 2

User Output
Ram 22 Delhi IITDelhi
Varun 24 Mumbai NITWarangal

Page No: 45
('Ram', 22, 'Delhi', 'IITDelhi')
('Varun', 24, 'Mumbai', 'NITWarangal')
('Ram', 22, 'Delhi', 'IITDelhi', 'Varun', 24, 'Mumbai', 'NITWarangal')

ID: 249X5A04M0
2023-2027-ECE-B
G Pulla Reddy Engineering College (Autonomous)
S.No: 30 Exp. Name: Vowel Counter Date: 2024-10-17

Aim:
Write a Python program to count the number of vowels in a given string without using control flow statements.

Page No: 46
Input Format:
The program should prompt the user to enter a string.

Output Format:

ID: 249X5A04M0
Display the number of vowels present in the entered string.
Source Code:

vowel.py
def count_vowels(s):
v='aeiouAEIOU'
return sum(map(s.count,v))
a=input()
print(count_vowels(a))

2023-2027-ECE-B
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
BCDFGHJKL

G Pulla Reddy Engineering College (Autonomous)


0

Test Case - 2

User Output
CodeTantra
4

Test Case - 3

User Output
Python Programming
4
Exp. Name: Program to check if Key exists in
S.No: 31 Date: 2024-10-19
Dictionary

Aim:

Page No: 47
Write a Python program that checks if a given key exists in a dictionary.

Input Format:
• First line of input contains space separated strings that represents keys of a dictionary
• Second line contains space separated strings that represents values of a dictionary

ID: 249X5A04M0
• Third line contains the key (as a string) to check whether it is present in the dictionary or not.

Output Format:
• The first line of the output should print the dictionary.
• Next it should print "Exist" if key is present in the dictionary or print "Does not Exist" if key is not present in
the dictionary and if the length of keys does not match with length of values then print "Invalid" .

Refer to shown test cases to match with input and output format.
Source Code:

keyExist.py

2023-2027-ECE-B
k=input().split()
v=input().split()
if len(k)!=len(v):
print('Invalid')
else:
k_c=input()
d=dict(zip(k,v))

G Pulla Reddy Engineering College (Autonomous)


print(d)
if k_c in d:
print('Exist')
else:
print('Does not Exist')

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
12345
ABCDE
5
{'1': 'A', '2': 'B', '3': 'C', '4': 'D', '5': 'E'}
Exist

Test Case - 2

User Output
10 20 30 40 50 60 70 80
Ten Twenty Thirty Forty Fifty Sixty
Test Case - 3

User Output
John Reena Sara David
24 32 18 41

Page No: 48
Jane
{'John': '24', 'Reena': '32', 'Sara': '18', 'David': '41'}
Does not Exist

ID: 249X5A04M0
2023-2027-ECE-B
G Pulla Reddy Engineering College (Autonomous)
S.No: 32 Exp. Name: Add Key Value Pair to Dictionary Date: 2024-10-19

Aim:
Write a Python program that adds a new key-value pair to an existing dictionary.

Page No: 49
Input Format:
• The first line contains space-separated strings that represent the keys of a dictionary.
• The second line contains space-separated strings that represent the values of the dictionary.
• The third line contains two space-separated strings that represent the new key and value to be added to

ID: 249X5A04M0
the existing dictionary.

Output Format:
• First line should display the original dictionary before adding the new key-value pair.
• Second line should display the updated dictionary after adding the new key-value pair.

Note: If the number of keys and values does not match, display "Invalid".
Source Code:

keyValue.py

k=input().split()

2023-2027-ECE-B
v=input().split()
if len(k)==len(v):
nk,nv=input().split()
d=dict(zip(k,v))
print(d)
d[nk]=nv
print(d)

G Pulla Reddy Engineering College (Autonomous)


else:
print('Invalid')

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Mobile Laptop Adopter Headset
10000 50000 2000
Invalid

Test Case - 2

User Output
thirty fifty seventy ninety forty eighty
30 50 70 90 40 80
four 4
{'thirty': '30', 'fifty': '50', 'seventy': '70', 'ninety': '90', 'forty': '40', 'eighty':
'80'}
{'thirty': '30', 'fifty': '50', 'seventy': '70', 'ninety': '90', 'forty': '40', 'eighty':
'80', 'four': '4'}
S.No: 33 Exp. Name: Sum of all items in the dictionary Date: 2024-10-19

Aim:
Write a program that allows the user to create a dictionary by entering key-value pairs. Calculate and print the

Page No: 50
sum of all values in the dictionary.

Input Format:
To populate this dictionary, prompt the user to enter an integer indicating the size of the dictionary.
For each key-value pair:

ID: 249X5A04M0
• Prompt the user to enter a key (string).
• Prompt the user to enter a value (integer).

Output Format:
• The output should display the sum of the values in the dictionary formatted as follows: "Sum: <the value
after adding the values>".

Note: Values in key-value pairs must be integers.


Source Code:

sumOfValues.py

2023-2027-ECE-B
a=int(input('size:'))
d={}
for i in range(a):
k=input('key:')
v=int(input('value:'))
d[k]=v
s=sum(d.values())

G Pulla Reddy Engineering College (Autonomous)


print('Sum:',s,sep='')

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
size:
1
key:
ninety
value:
90
Sum:90

Test Case - 2

User Output
size:
4
key:
five
6
5

60
12
six

key:
key:
key:

sixty
twelve

Sum:83
value:
value:
value:
value:

G Pulla Reddy Engineering College (Autonomous) 2023-2027-ECE-B ID: 249X5A04M0 Page No: 51
S.No: 34 Exp. Name: Sorting the key-value pairs Date: 2024-10-19

Aim:
Hitler's junior brother Pitler is a Grammer nazi and he wants everything in order. One day he ordered you to

Page No: 52
develop a program that accepts the serial number and name of the soldier as dictionary key-value pairs and to
print the sorted details depending on the serial number.

Complete the task and impress Pitler to get rewarded.

ID: 249X5A04M0
Input Format:
The first line reads an integer representing the number of soldiers.
The next lines read an integer and a string representing the serial number and name of the soldier.

Output Format:
The output is a sorted dictionary.
Source Code:

SortedDictbyKeys.py

n = int(input())
my_dict = {}

2023-2027-ECE-B
for i in range(n):
key=int(input())
value=input()
my_dict[key]=value
a=dict(sorted(my_dict.items()))
print(a)

G Pulla Reddy Engineering College (Autonomous)


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

User Output
3
512
Rakesh
325
Arjun
408
Jay
{325: 'Arjun', 408: 'Jay', 512: 'Rakesh'}

Test Case - 2

User Output
0
{}
S.No: 35 Exp. Name: Manage birthdays Date: 2024-10-19

Aim:
Write a Python program that manages your friends' birthdays using a dictionary:

Page No: 53
1. Initialize a dictionary where the keys are your friends' names and the values are their birthdays.
2. Print the items in the dictionary in the sorted order of the names.
3. Prompt the user to enter a name and check if it is present in the dictionary. If the name is not found,
prompt the user to enter the birthday and add it to the dictionary.

ID: 249X5A04M0
Note: Refer to the displayed test cases for better understanding.
Source Code:

birthdaydict.py

# Input names and birthdays


names = input("Names: ").split(' ')
birthdays = input("Birthdays: ").split(' ')

# Create and sort the dictionary


bd_dict = dict(zip(names, birthdays))

2023-2027-ECE-B
sorted_dict = dict(sorted(bd_dict.items()))
# Print sorted dictionary
for name, bday in sorted_dict.items():
print(f"{name}: {bday}")
while True:
n_t_c=input("Name to check (or 'quit' to exit): ")
if n_t_c=='quit':

G Pulla Reddy Engineering College (Autonomous)


break
if n_t_c in bd_dict:
print(f"Birthday: {bd_dict[n_t_c]}")
else:
print("Name not found")
b_t_add=input("Birthday to add: ")
bd_dict[n_t_c]=b_t_add
print("Added to dictionary")
sorted_dict=dict(sorted(bd_dict.items()))
print("Final dictionary:")
for name,bday in sorted_dict.items():
print(f"{name}: {bday}")

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Names:
Swathi Sangeetha Samatha
Birthdays:
20/09/2001 07/03/2005 11/04/2007
Samatha: 11/04/2007
Sangeetha: 07/03/2005
Swathi: 20/09/2001
Name to check (or 'quit' to exit):
Aanya
Name not found

Page No: 54
Birthday to add:
27/10/2022
Added to dictionary
Name to check (or 'quit' to exit):

ID: 249X5A04M0
Samatha
Birthday: 11/04/2007
Name to check (or 'quit' to exit):
quit
Final dictionary:
Aanya: 27/10/2022
Samatha: 11/04/2007
Sangeetha: 07/03/2005
Swathi: 20/09/2001

2023-2027-ECE-B
Test Case - 2

User Output
Names:
John Doe Tina Mary
Birthdays:
01/01/2000 15/02/1998 29/08/1992

G Pulla Reddy Engineering College (Autonomous)


30/06/1999
Doe: 15/02/1998
John: 01/01/2000
Mary: 30/06/1999
Tina: 29/08/1992
Name to check (or 'quit' to exit):
Tina
Birthday: 29/08/1992
Name to check (or 'quit' to exit):
quit
Final dictionary:
Doe: 15/02/1998
John: 01/01/2000
Mary: 30/06/1999
Tina: 29/08/1992

You might also like