Std-10 AI Python Program
Std-10 AI Python Program
Write python codes for the following programs and show minimum two test cases along with
them.
1. Write a program to print out the name given by the user as input.
2. Check whether a number is even or odd.
Enter name:
2.
In |l:# Checking whether number entered is even or odd
3.
In []: # Python program to check if year is a leap year or not
year = int(input("Enter year:"))
# To get year (integer input) from the user
# year = int( input ("Enter a year: "))
if (year % 4) == 0:
if (yeår % 100) == 0:
if (year % 40e) == 0:
print(year, "is a leap year")
else:
print (year, "is not a leap year")
else:
print (year, "is a leap year")
else:
print (year, "is not a leap year")
Enter year:
Test Case 2
Test Case 1
Enter year:1997
Enter year:2004 1997 is not a leap year
2004 is a leap year
Test Case 2
Test Case 1
Enter 1st number: 22.4
Enter 1st number: 2.3 Enter 2nd number: 3.14
Enter 2nd number: 4.5 Enter 3rd number: 17
Enter 3rd number: 6.2 The largest number is 22.4
The largest number is 6.2
if x > 1:
# check for factors
for i in range(2, x) :
if (x % i) == 0:
print(x, "is not a prime number")
print(i, "times",x//i, "is",x)
break
else:
print (x, "is a prìme number'")
else:
print (x, "is not a prime number")
Enter Number:
if n < 0:
1 1
1
2
13
21
if n == sum:
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")
Enter a number:
n -= 1
print ("The sum is", sum)
Enter Number:
Test Case 1
Test Case 2
Enter Number:15
Enter Number:6
The sum is 120
The sum is 21
# Program to calculate the length of a string
11.
my string (input("Enter a string: "))
count
for s in mystring:
count count +1
print("Length of the input string is:", count)
Enter a string:
Test Case 2
Test Case 1
age:"))
13. In ]: age = int(input ("Enter
if age)=18:
vote")
print("You are eligible cast
else: cast vote")
print("You are ineligible to
Enter age:
Test Case 2
Test Case 1
Enter age:15
Enter age:24 You are ineligible to cast vote
vote
You are eligible cast
list
order of elements of a
14. In : #Reversing the
i=5
a=)
while is0:
print ("Enter number")
num input()
a.append(num)
i = i-1
print (a)
ements of a
# reverse el
a.reverse()
print (a)
Enter number
Test Case 1 Test Case 2
15.
In [): #Ask user to give integer inputs to make a list. store only even values given ond print the list.
i: 4
while is0:
print("Enter number")
num = int (input ())
if num.2 == 9:
à. append (num)
i = i-1
print (a)
Enter number
(2, 4, 6, 8]
3 WORKSHEET:5
Q1. In the following line of code, when input is entered, the computer system takes that input as what
data type?
In []: num = input ("Enter a number: ")
Enter a number:
Q2. How to ensure that the following program accepts only integer values as input?
Sol. Test Case 1: (When integer is provided as input)
In [1]: num = int(input("Enter a number: "))
Enter a number: 10
In [1]: i=6
a = )
while i>0:
print ("Enter number")
num = input()
à. append (num)
i = i-1
prìnt (a)
Enter number
2
Enter number
3
Enter number
Enter number
5
Enter number
6
Enter number
7
|'2', 3', '4', '5', '6', '7']
WORKSHEET: 7
Copy element 27 and 9 from the following tuple into a new tuple.
tuplel =(41, 2, 34, 27, 9, 6)
Expected Output:
tuple? =(27, 9)
Write a Python program to calculate the profit gained or loss incurred, taking the cost
price as the inputs from the user. price and selling
Sol.
In [1]: # Program to find profit or loss
cp =
sp =
float(input("Enter cost price (in Rs) : "))
float(input(" Enter selling price (in Rs): "))
if cp> sp:
print ("Loss incurred: cp-sP, "Rs")
elif cp == sp:
print("No profit, no loss")
else:
print ("Profit gained: ) Sp-cp, "Rs")
Test Case 1:
Enter cost price (in Rs): 200
Enter selling price (in Rs): 300
Profit gained: 100.0 Rs
Test Case 2:
Test Case 1:
7
8
Test Case 2:
Enter value where loop should break: 6
2
4
6