AI Class 10 Python Codes
AI Class 10 Python Codes
# Calculate simple i n t e r e s t
s i m p l e i n t e r e s t = principal amount ∗ i n t e r e s t r a t e ∗ time period
# C a l c u l a t e t o t a l amount
total amount = principal amount + s i m p l e i n t e r e s t
# Print the r e s u l t s
p r i n t ( ” P r i n c i p a l Amount : $ ” , p r i n c i p a l a m o u n t )
p r i n t ( ” I n t e r e s t Rate : ” , i n t e r e s t r a t e ∗ 1 0 0 , ”%”)
p r i n t ( ” Time P e r i o d : ” , t i m e p e r i o d , ” y e a r s ” )
p r i n t ( ” Simple I n t e r e s t : $ ” , s i m p l e i n t e r e s t )
p r i n t ( ” T o t a l Amount a f t e r ” , t i m e p e r i o d , ” y e a r s : $ ” , t o t a l a m o u n t )
Output :
P r i n c i p a l Amount : $ 1000
I n t e r e s t Rate : 5 . 0 %
Time P e r i o d : 3 y e a r s
Simple I n t e r e s t : $ 1 5 0 . 0
T o t a l Amount a f t e r 3 y e a r s : $ 1 1 5 0 . 0
1
2. Area of the Circle with Pi as constant
# Example v a l u e s
radius = 5
# Print the r e s u l t s
p r i n t ( ” Radius : ” , r a d i u s )
p r i n t ( ” Area o f t h e C i r c l e : ” , a r e a )
Output :
Radius : 5
Area o f t h e C i r c l e : 78.53975
2
3. Swap using a temporary variable
# Example v a l u e s
a = 5
b = 10
# P r i n t v a l u e s b e f o r e swapping
p r i n t ( ” B e f o r e swapping : ” )
p r i n t ( ” a =”, a )
p r i n t ( ” b =”, b )
# Swap u s i n g a temporary v a r i a b l e
temp = a
a = b
b = temp
# P r i n t v a l u e s a f t e r swapping
p r i n t ( ” \ n A f t e r swapping : ” )
p r i n t ( ” a =”, a )
p r i n t ( ” b =”, b )
Output :
B e f o r e swapping :
a = 5
b = 10
A f t e r swapping :
a = 10
b = 5
3
4. Factorial Calculation for loop
# Example v a l u e
number = 5
# I n i t i a l i z e the r e s u l t v a r i a b l e
result = 1
# Calculate f a c t o r i a l
f o r i i n r a n g e ( 1 , number + 1 ) :
result = result ∗ i
# Print the r e s u l t
p r i n t ( f ”The f a c t o r i a l o f {number} i s : { r e s u l t } ” )
Output :
The f a c t o r i a l o f 5 i s : 120
4
5. Fibonacci Series
# I n i t i a l i z e t h e f i r s t two terms
a = 0
b = 1
# D i s p l a y t h e f i r s t few terms o f t h e F i b o n a c c i s e r i e s
f o r i in range ( 1 0 ) :
p r i n t ( a , end =”, ” )
a = b
b = a + b
Output :
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,
5
6. Greatest Among 3 numbers
# Input t h r e e numbers
num1 = i n t ( i n p u t ( ” Enter t h e f i r s t number : ” ) )
num2 = i n t ( i n p u t ( ” Enter t h e s e c o n d number : ” ) )
num3 = i n t ( i n p u t ( ” Enter t h e t h i r d number : ” ) )
# Display the r e s u l t
p r i n t ( ” The g r e a t e s t number among ” , num1 , ” , ” , num2 , ” , and ” , num3 , ” i s : ” , g r e a t e s t )
Output :
Enter t h e f i r s t number : 25
Enter t h e s e c o n d number : 12
Enter t h e t h i r d number : 30
The g r e a t e s t number among 25 , 12 , and 30 i s : 30
6
7. Shopping with 10 percentage Discount
# Prices of 4 items
item1 price = 20.0
item2 price = 15.0
item3 price = 30.0
item4 price = 25.0
# C a l c u l a t e d i s c o u n t (10%)
d i s c o u n t p e r c e n t a g e = 10
discount amount = ( d i s c o u n t p e r c e n t a g e / 100) ∗ t o t a l c o s t b e f o r e d i s c o u n t
# Print the r e s u l t s
p r i n t ( ” P r i c e s o f Items : ” )
p r i n t ( ” Item 1 : $ ” , i t e m 1 p r i c e )
p r i n t ( ” Item 2 : $ ” , i t e m 2 p r i c e )
p r i n t ( ” Item 3 : $ ” , i t e m 3 p r i c e )
p r i n t ( ” Item 4 : $ ” , i t e m 4 p r i c e )
p r i n t ( ” \ nTotal Cost b e f o r e D i s c o u n t : $ ” , t o t a l c o s t b e f o r e d i s c o u n t )
p r i n t ( ” D i s c o u n t ( ” , d i s c o u n t p e r c e n t a g e , ”%): $ ” , d i s c o u n t a m o u n t )
p r i n t ( ” T o t a l Cost a f t e r D i s c o u n t : $ ” , t o t a l c o s t a f t e r d i s c o u n t )
Output :
Prices of Items :
Item 1 : $ 20.0
Item 2 : $ 15.0
Item 3 : $ 30.0
Item 4 : $ 25.0
T o t a l Cost b e f o r e D i s c o u n t : $ 9 0 . 0
D i s c o u n t ( 10 %): $ 9 . 0
T o t a l Cost a f t e r D i s c o u n t : $ 8 1 . 0
7
8. Shopping with Conditional Discount
# Prices of 4 items
item1 price = 20.0
item2 price = 15.0
item3 price = 30.0
item4 price = 25.0
# C a l c u l a t e d i s c o u n t amount
discount amount = ( d i s c o u n t p e r c e n t a g e / 100) ∗ t o t a l c o s t b e f o r e d i s c o u n t
# Print the r e s u l t s
p r i n t ( ” P r i c e s o f Items : ” )
p r i n t ( ” Item 1 : $ ” , i t e m 1 p r i c e )
p r i n t ( ” Item 2 : $ ” , i t e m 2 p r i c e )
p r i n t ( ” Item 3 : $ ” , i t e m 3 p r i c e )
p r i n t ( ” Item 4 : $ ” , i t e m 4 p r i c e )
p r i n t ( ” \ nTotal Cost b e f o r e D i s c o u n t : $ ” , t o t a l c o s t b e f o r e d i s c o u n t )
p r i n t ( ” D i s c o u n t Amount : $ ” , d i s c o u n t a m o u n t )
p r i n t ( ” T o t a l Cost a f t e r D i s c o u n t : $ ” , t o t a l c o s t a f t e r d i s c o u n t )
Output :
Prices of Items :
Item 1 : $ 20.0
Item 2 : $ 15.0
Item 3 : $ 30.0
Item 4 : $ 25.0
T o t a l Cost b e f o r e D i s c o u n t : $ 9 0 . 0
Applied D i s c o u n t ( 5 %)
D i s c o u n t Amount : $ 4 . 5
T o t a l Cost a f t e r D i s c o u n t : $ 8 5 . 5
8
9. While Loop to Print Numbers 1 to 10
count = 1
w h i l e count <= 1 0 :
p r i n t ( count )
count = count + 1
Output
1
2
3
4
5
6
7
8
9
10
9
10. Multiplication Table of 5 using While Loop
num = 5
count = 1
w h i l e count <= 1 0 :
r e s u l t = num ∗ count
print ( result )
count = count + 1
Output :
5
10
15
20
25
30
35
40
45
50
10
These are extra programs for practice purposes only.
# I n i t i a l i z i n g a v a r i a b l e t o s t o r e t h e sum
sum of numbers = 0
# Adding numbers u s i n g a f o r l o o p
f o r num i n numbers :
sum of numbers = sum of numbers +num
# P r i n t t h e l i s t and t h e sum
p r i n t ( ” L i s t o f Numbers : ” , numbers )
p r i n t ( ”Sum o f Numbers : ” , sum of numbers )
Output :
L i s t o f Numbers : [ 5 , 1 0 , 1 5 , 2 0 , 2 5 ]
Sum o f Numbers : 75
Python code :
import c s v
# I n i t i a l i z e empty l i s t s t o s t o r e data
names = [ ]
ages = [ ]
cities = []
# Skip t h e h e a d e r i f i t e x i s t s
next ( c s v r e a d e r , None )
11
# P r i n t t h e imported data
p r i n t ( ” Names : ” , names )
p r i n t ( ” Ages : ” , a g e s )
p r i n t (” C i t i e s : ” , c i t i e s )
Output :
import numpy a s np
from s c i p y import s t a t s
# Example u s a g e :
data = np . a r r a y ( [ 1 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 5 ] )
# C a l c u l a t e mean u s i n g NumPy
mean = np . mean ( data )
# C a l c u l a t e median u s i n g NumPy
median = np . median ( data )
# Print the r e s u l t s
p r i n t ( ” Mean : ” , mean )
p r i n t ( ” Median : ” , median )
p r i n t ( ” Mode : ” , mode , ” ( count : ” , mode count , ” ) ” )
Output :
Mean : 3 . 0
Median : 3 . 0
Mode : 3 ( count : 3 )
12