0% found this document useful (0 votes)
75 views12 pages

AI Class 10 Python Codes

The document contains 10 Python code examples for practicing basic AI concepts: 1. Calculate simple interest on a principal amount. 2. Calculate the area of a circle using a predefined pi value. 3. Swap the values of two variables using a temporary variable. 4. Calculate the factorial of a number using a for loop. 5. Print the first 10 terms of the Fibonacci series. 6. Find the greatest among three user-input numbers. 7. Calculate shopping costs with a 10% discount applied. 8. Calculate conditional discounts based on a threshold total cost. 9. Print numbers from 1 to 10 using a while loop. 10. Print the multiplication table

Uploaded by

Sneha Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
75 views12 pages

AI Class 10 Python Codes

The document contains 10 Python code examples for practicing basic AI concepts: 1. Calculate simple interest on a principal amount. 2. Calculate the area of a circle using a predefined pi value. 3. Swap the values of two variables using a temporary variable. 4. Calculate the factorial of a number using a for loop. 5. Print the first 10 terms of the Fibonacci series. 6. Find the greatest among three user-input numbers. 7. Calculate shopping costs with a 10% discount applied. 8. Calculate conditional discounts based on a threshold total cost. 9. Print numbers from 1 to 10 using a while loop. 10. Print the multiplication table

Uploaded by

Sneha Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

Python code for AI Practical Practice

1. Calculate simple interest


# Example v a l u e s
p r i n c i p a l a m o u n t = 1000
i n t e r e s t r a t e = 0 . 0 5 # 5%
time period = 3

# 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

# Calculate area of the c i r c l e


const pi = 3.14159
area = pi ∗ radius ∗ radius

# 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 : ” ) )

# Compare and f i n d t h e g r e a t e s t number


i f num1 >= num2 and num1 >= num3 :
g r e a t e s t = num1
e l i f num2 >= num1 and num2 >= num3 :
g r e a t e s t = num2
else :
g r e a t e s t = num3

# 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

# Calculate total cost before discount


total cost before discount = item1 price + item2 price + item3 price + item4 price

# 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

# Calculate total cost after discount


t o t a l c o s t a f t e r 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 − discount amount

# 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

# Calculate total cost before discount


total cost before discount = item1 price + item2 price + item3 price + item4 price

# Set the t hr e sh o ld f o r a higher discount


discount threshold = 100.0

# Determine t h e d i s c o u n t p e r c e n t a g e based on t h e t o t a l amount


i f t o t a l c o s t b e f o r e d i s c o u n t >= d i s c o u n t t h r e s h o l d :
d i s c o u n t p e r c e n t a g e = 20
else :
discount percentage = 5

# 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

# Calculate total cost after discount


t o t a l c o s t a f t e r 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 − discount amount

# 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 )

# Print the applied discount percentage


p r i n t ( ” Applied 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 , ”%)”)

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.

1. Creating a list of integers and find total


# Creating a l i s t of i n t e g e r s
numbers = [ 5 , 1 0 , 1 5 , 2 0 , 2 5 ]

# 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

2. Import CSV file in python

Suppose you have a CSV f i l e named ” example . c s v ” with t h e


following content :

Name , Age , City


John , 2 5 , New York
Jane , 3 0 , Los A n g e l e s
Bob , 2 2 , Chicago

Python code :

import c s v

# S p e c i f y t h e path t o your CSV f i l e


c s v f i l e p a t h = ’ example . csv ’

# 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 = []

# Read CSV f i l e and p o p u l a t e l i s t s


with open ( c s v f i l e p a t h , ’ r ’ ) a s c s v f i l e :
c sv re a de r = csv . reader ( c s v f i l e )

# 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 )

# I t e r a t e through rows and p o p u l a t e l i s t s


f o r row i n c s v r e a d e r :
names . append ( row [ 0 ] )
a g e s . append ( i n t ( row [ 1 ] ) ) # Convert age t o i n t e g e r
c i t i e s . append ( row [ 2 ] )

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 :

Names : [ ’ John ’ , ’ Jane ’ , ’ Bob ’ ]


Ages : [ 2 5 , 3 0 , 2 2 ]
C i t i e s : [ ’ New York ’ , ’ Los Angeles ’ , ’ Chicago ’ ]

3. Use of array in Python with NumPy

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 )

# C a l c u l a t e mode u s i n g SciPy ’ s s t a t s . mode


m o d e r e s u l t = s t a t s . mode ( data )
mode = m o d e r e s u l t . mode [ 0 ]
mode count = m o d e r e s u l t . count [ 0 ]

# 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

You might also like