Introduction to Python
Algorithm
An algorithm is a logical step-by-step method or a procedure to solve the identified problem. An
algorithm includes calculations, reasoning and data processing. Algorithms can be presented by
natural languages, pseudocode and flowcharts, etc.
Example: The basic steps to make instant noodles are:
Flowchart
A flowchart is the graphical or pictorial representation of an algorithm with the help of different
symbols, shapes and arrows in order to demonstrate a process or a program.
Example: A flowchart to find the largest of 2 numbers.
Program
A computer program is a collection of instructions that perform a specific task when executed by a
computer. It is usually written by a computer program in a programming language like BASIC, C++,
Java, Python, etc.
Why Python for AI?
• Python has a simple structure and syntax. It is easy to experiment and modify.
• Python has a large number of libraries with tools for data analysis and visualization.
• Python is compatible on major operating systems like Windows, macOS and Linux.
• Python is an interpreted programming language. That means it executes code line by line,
without the need for compilation.
Applications of Python
• Software development
• Desktop applications
• Business applications
• Web and Internet development
• Game and 3D graphics
Program 1: Write a python program to calculate the sum of any two numbers.
num1 = input(“Enter the first number: ”)
num1=int(num1)
num2=input("Enter the second number: ")
num2=int(num2)
sum = num1 + num2
print (“The sum is:”, sum)
Program 2: Program to calculate Area and Perimeter of a rectangle
l = input(“Enter the length: ”)
l=int(l)
b=input("Enter the breadth: ")
b=int(b)
perimeter = 2*(l+b)
print('Perimeter of the rectangle:', perimeter)
area = (l*b)
print('Area of the rectangle:', area)
Program 3: Program to calculate Simple Interest
p = int(input(" Enter Principle Amount : "))
t = float(input(" Enter Time in Years : "))
r = float(input(" Enter Rate of Interest in Percentage : "))
si = (p*t*r) / 100
print(" Simple Interest : ", si)
Program 4: Program to check whether the number is even or odd
n = int(input(" Enter a Number : "))
if n%2 == 0:
print(n, " is an EVEN Number ")
else:
print(n, " is an ODD Number ")
Program 5: Program to check whether a person is Eligible for Voting or not
age = int(input(" Enter the Age of a person : "))
if age >= 18:
print(" You are Eligible for Voting ")
else:
print(" You are NOT Eligible for Voting ")
Program 6: Program to design a Simple Calculator
a = int(input(" Enter the First Number : "))
b = int(input(" Enter the Second Number : "))
add = a+b
sub = a-b
mul = a*b
div = a/b
rem = a%b
print(" Addition of ", a, "and " , b, " is : ", add)
print(" Difference of ", a, "and " , b, " is : ", sub)
print(" Product of ", a, "and " , b, " is : ", mul)
print(" Quotient of ", a, "and " , b, " is : ", div)
print(" Remainder of ", a, "and " , b, " is : ", rem)
Program 7: Write a Python Program to create a LIST of fruits and perform the following actions -
Add a new element ‘kiwi’ at the end of the List, Add a new element input by the user at a position
specified by the user, Delete an existing element ‘apple’ from the List, Reverse the List, Length of
the List, Clear the entire List.
fruits=["apple","orange","banana"]
[Link]('kiwi')
position = int(input(" Enter the Position where you want to enter a new Element : "))
element = input(" Enter the Element you want to enter : ")
[Link](position, element)
print(fruits)
[Link]('apple')
print(fruits)
[Link]()
print(fruits)
print(len(fruits))
[Link]()
print(fruits)
Program 8: Program to add the elements in a list
list1=[5,10,15,20,25,30]
sum=0
for x in list1:
sum = sum + x
print(“The sum of all elements is:”,sum)
Program 9: Program to add the elements in 2 lists
list1=[1,2,3,4,5]
list2=[10,20,30,40,50]
sum=[]
#add elements in list1 and list2
for i in range(len(list1)):
[Link](list1[i] + list2[i])
print(“The new list is:”,sum)
Program 10: Program to calculate mean and median using NumPy
import numpy as np
data = [10, 20, 20, 30, 40, 50, 50, 60]
x = [Link](data)
y = [Link](data)
print("Mean =", x)
print("Median =", y)