Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Subodh Public School, Rambagh

(A Senior Secondary Co-Educational CBSE Affiliated School)


Session: 2023-24
Since 1985 Department of Computer
L-4 Introduction to Python
Subject : Artificial Intelligence (417) Class : Flyers 1 (IX)

Python programs

1. Python program to add two numbers


# Python program to add two numbers
num1 = 15
num2 = 12

# Adding two nos


sum = num1 + num2

# printing values
print("Sum of", num1, "and", num2 , "is", sum)
2. Maximum of two numbers in Python
# Python program to return maximum of two numbers

# Getting input from user


num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
# printing the maximum value
if(num1 > num2):
print(num1, "is greater")
elif(num1 < num2):
print(num2, "is greater")
else:
print("Both are equal")
3.Program to find area and perimeter of a circle
# Python program to find the
# area and perimeter of circle in python

# Initialising the value of PI


PI = 3.14
# Getting input from user
R = float(input("Enter radius of the circle: "))

# Finding the area and perimeter of the circle


area = (PI*R*R)
perimeter = (2*PI*R)

# Printing the area and perimeter of the circle


print("The area of circle is", area)
print("The perimeter of circle is", perimeter)
4. Simple Interest Calculation
# Python program to find simple interest

p = float(input("Enter the principle amount : "))


r = float(input("Enter the rate of interest : "))
t = float(input("Enter the time in the years: "))

# calculating simple interest


si = (p*r*t)/100

# printing the values


print("Principle amount: ", p)
print("Interest rate : ", r)
print("Time in years : ", t)
print("Simple Interest : ", si)
5. Python program to check the given year is a leap year or not
# input the year
y=int(input('Enter the value of year: '))

# To check for non century year


if y%400==0 or y%4==0 and y%100!=0:
print('The given year is a leap year.')
else:
print('The given year is a non-leap year.')

6. Find largest of three number using nested if else


# input three integer numbers

a=int(input("Enter A: "))

b=int(input("Enter B: "))
c=int(input("Enter C: "))

# conditions to find largest


if a>b:
if a>c:
g=a
else:
g=c
else:
if b>c:
g=b
else:
g=c

# print the largest number


print("Greater = ",g)
7.Given two numbers and we have to design a calculator type
application that will perform add, subtract, multiply and divide
operations using Python.

# menus
print("Calculator")
print("1.Add")
print("2.Substract")
print("3.Multiply")
print("4.Divide")

# input choice
ch=int(input("Enter Choice(1-4): "))

if ch==1:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a+b
print("Sum = ",c)
elif ch==2:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a-b
print("Difference = ",c)
elif ch==3:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a*b
print("Product = ",c)
elif ch==4:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a/b
print("Quotient = ",c)
else:
print("Invalid Choice")
8. Print all the numbers between 1 to N.
n=int(input("Enter N: "))

for i in range(1,n+1):

print(i)

9. Print table of a number

n=int(input("Enter N: "))
for i in range(1,11):
print(n,"x",i,"=",i*n)

10. Find the sum of N number


n=int(input("Enter N: "))
s=0
for i in range(1,n+1):
s=s+i
print("Sum = ",s)

11. Check prime number


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

c=0

for i in range(1,n+1):
if n%i==0:

c=c+1
if c==2:

print(n,"is Prime")

else:

print(n,"is Not Prime")


12. Python while Loop
# program to display numbers from 1 to 5

# initialize the variable

i=1
n=5

# while loop from i = 1 to 5

while i <= n:
print(i)

i=i+1

13. Python break Statement with while Loop


# program to find first 5 multiples of 6

i=1

while i <= 10:


print('6 * ',(i), '=',6 * i)

if i >= 5:
break

i=i+1
14. Python continue Statement with while Loop
# program to print odd numbers from 1 to 10

num = 0

while num < 10:


num += 1

if (num % 2) == 0:
continue

print(num)
15. To create a list in Python
#empty list
empty_list = []
#list of integers
age = [15,12,18]
#list with mixed data types
student_height_weight = ["Ansh", 5.7, 60]

16. To Access List Elements


languages = ["Python", "Swift", "C++"]

# access item at index 0


print(languages[0]) # Python

# access item at index 2


print(languages[2]) # C++

17. Negative Indexing in Python


languages = ["Python", "Swift", "C++"]

# access item at index 0


print(languages[-1]) # C++

# access item at index 2


print(languages[-3]) # Python

18. Add Elements to a List


numbers = [21, 34, 54, 12]

print("Before Append:", numbers)

# using append method


numbers.append(32)

print("After Append:", numbers)


19. Change List Items
languages = ['Python', 'Swift', 'C++']

# changing the third item to 'C'


languages[2] = 'C'

print(languages) # ['Python', 'Swift', 'C']

20. Remove an Item From a List


languages = ['Python', 'Swift', 'C++', 'C', 'Java', 'Rust', 'R']

# deleting the second item


del languages[1]
print(languages) # ['Python', 'C++', 'C', 'Java', 'Rust', 'R']

# deleting the last item


del languages[-1]
print(languages) # ['Python', 'C++', 'C', 'Java', 'Rust']

# delete the first two items


del languages[0 : 2] # ['C', 'Java', 'Rust']
print(languages)

You might also like