0% found this document useful (0 votes)
8 views

Std-10 AI Python Program

Uploaded by

singhganesha2008
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Std-10 AI Python Program

Uploaded by

singhganesha2008
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

D.

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.

3. Check if a certain year isa leap year or not.


numbers.
4. Write a program to find the largest number among the three input

5. Check whether the number entered is a prime number.

6. Find the factorial of a number provided by


the user.

number entered by the user.


7. Display the multiplication table of a
n.
term, where the user provides the value of
8. Display the Fibonacci sequence up to the n"

whether the number entered is an Armstrong number.


9. Check

ARTIFICIAL INTELLIGENCE | Kips 209


user enters n.
10. of the natural numbers up to n. where the
e tne sumn
the user.
11. program to find the length of astring entered by
nte d
digits ofa number.
12. Write aprogram to find the sum of
age: 18years).
person is eligible to cast avote (Minimumvoting
13. Check whether a
each element. Print the i
elements. where the User enters the value of
14. CTeate a list of five
created and also the reverse order of that list.
print
inputs to make a list. Store only theeven values given and
15. ASk the user to provide the integer
the list.
n Write Python codes for the following programs and show minimum two test cases along with
them.

In [*): name = input( Enter name:")


print (name

Enter name:

Test Case 1 Test Case 2


Enter key to check:A
Key is present and value of the key is: Enter key to check:D
1 Key isn't present!

2.
In |l:# Checking whether number entered is even or odd

num = float (input ("Enter a number: "))


if num >= 0:
if num == :
print ("Zero")
else:
print ("Positive number")
else:
print("Negative number")
Enter a number:

Test Case 1 Test Case 2


Enter name :Rahul Enter name: Daniel
Rahul Daniel

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

three input numbers


to find the Largest number among the
4. * ! : # Python program
number:))
numi = float (input("Enter 1st number: "))
num2 = float(input ("Enter 2nd
number: ))
num3 = float(input ("Enter 3rd
if (num1 >= num2 ) and (num1 >= num3) :
largest = num1
elif (num2 >= numi) and (num2 >= num3) :
largest = num2
else:
largest = num3

print ("The largest number is", largest)


Enter 1st number:

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

5. In ]: # Program to check if a number is prime or not


X = int(input("Enter Number:"))

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:

Test Case 1 Test Case 2


Enter Number:27 Enter Number:1223
27 is not a prime number 1223 is a prime number
3 times 9 is 27
6 In [): # Python program to find the factor ial of anumber provided by the
user.
n = int (input ("Enter a number: "))
fact = 1

if n < 0:

print("sorry, factorial does not exist


elif n =: 0: for negative numbers")
print ("The factor ial of o is 1")
else:
for i in range(1,n + 1):
fact = fact*i
print("The factorial of",n, "is",fact)
Enter a number:

Test Case 1 Test Case 2


Enter a number: 5
Enter a number: 12
The factorial of 5 is 120
The factorial of 12 is 479001600

7. In ]: # Multiplication table (from 1 to 10) in Python


number = int(input ("Display multiplication table of? "))
for i in range(1, 11):
print (number, 'x, i, '=, number*i)
Display multiplicat ion table of?

Test Case 1 Test Case 2


Display multiplication table of ? 12 Display multiplication table of? 5
12 x 1 = 12
5 X 1 = 5
12 x 2 = 24 5 X 2 = 10
12 x 3 = 36
5 X 3 = 15
12 x 4 = 48
5 X 4 = 20
12 x 5 = 60
5 x5 = 25
12 x 6 = 72 5 X 6 = 30
12 x 7 = 84 5 x7 = 35
12 × 8 = 96 5 X 8 = 40
12 × 9 = 108 5 X 9 = 45
12 x 10 = 120
5 X 10 = 50

8. In [']: Programn to display the Fibonacci seguence up to n-th tera


nterms = int(input("How nany terms ?

first two terms


na, nb =0, 1
count =0

# check if the number of terms is valid


if nterms (= 0:
print("Please enter a posítive integer")
elif nterms == 1:
prìnt("Fibonacci sequence upto",nterms,:)
print(na)
else:
print ("Fibonacci sequence:")
hile count < nterms:
print(na)
nc = na + nb
update values
na = nb
nb = nc
Count += 1

How rany terms?


Test Case 2
Test Case 1

How many terms? 5 How many terms? 9


Fibonacci sequence: Fibonacci sequence:

1 1
1
2

13
21

9. In [*]: # Python program to check if the number is an Armstrong number or not


n = int(input ("Enter a number: "))
Sum = e
t =n
while t > e:
digit = t % 10
Sum += digit ** 3
t //= 10

if n == sum:
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")
Enter a number:

Test Case 1 Test Case 2

Enter a number: 153 Enter a number: 277


153 is an Armstrong number 277 is not an Armstrong number

10. In [*]: # Sum of natural numbers up to n

n= int (input ("Enter Number:"))


if n < :
print("Enter a positive number")
else:
Sum 0

while(n > 0):


Sum += n

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

Enter a string: Vernie Enter a string: abcdefghijklmnopq


Length of the input string is: 6 Length of the input string is: 17

x-int (input(Enter a number:"))


12. count-0
while(x>0):
digit=x$10
count-count+digit
X=X//10

print ("The total sum of digits is:",count)


Enter à number:
Test Case 2
Test Case 1
Enter a number:345
Enter a number:12321 12
is: 9 The total sum of digits is:
The total sum of digits

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

Enter number Enter number


1 11
Enter number Enter number
12
Enter number Enter number
5 13
Enter number Enter number
2 14
Enter number Enter number
7 15
['1', '3', '5', '2, '7'] ['11', '12' '13, '14', '15']
['7', 2> 5> '3', '1'] ['15', '14', '13', '12', '11']

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

Test Case 1 Test Case 2

Enter number Enter number


12 1
Enter number Enter number
14 2
Enter number Enter number
15 3
Enter number Enter number
17 4
Enter number Enter number
2
Enter number Enter number
6
(12, 14, 2, 4] Enter number
7
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:

Sol. The number entered is treated as a string.

In [1]: num = input("Enter a number: ")


Enter a number: 10

In (2]: print(type (num) )


<class 'str'>

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 [2]: print(type (num))


<class 'int'>

Test Case 2: (When string is provided as input)

In (1]: num = int (input ("Enter a number: "))


Enter a number: Hello

ValueError Traceback (most recent call last)


<ipython-input-1-8d33bdf77337> in <modules
-- - ) 1 num
int(input("Enter a number: "))

ValueError: invalid literal for int() with base 10: 'Hello'


WORKSHEET:6
screen.
integer inputs from the user. store them in a list, and print them on the
lake six
Sol.

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)

Sol. In (1]: tuple1 =(11, 22, 33, 44, 55, 66)


tuple2 = tuplel[3: -1]
print (tuple2 =" tuple2)

tuple2 = (44, 55)


BWORKSHEET: 8

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:

Enter cost price (in Rs): 250


Enter selling price (in Rs) : 250
No profit, no loss
Test Case 3:
Enter cost price (in Rs): 450
Enter selling price (in Rs): 300
Loss incurred: 150.0 Rs
WORKSHEET: 9
15] till an arbitrs
Vwrite a Python program to display all the elements of the list (2, 4, 6, 7, 8, 9, 11, 12,
element x, which is taken as an input from the user.
Sol.

1n TJ: # program to display aLL the elements before number arbritary x


X= int (input("Enter value where loop should break: "))
for num in [2, 4, 6, 7, 8, 9, 11, 12, 15J:
print(num)
if (num==x):
break

Enter value where loop should break:

Test Case 1:

Enter value where loop should break: 8


2
4

7
8

Test Case 2:
Enter value where loop should break: 6
2
4
6

You might also like