0% found this document useful (0 votes)
12 views9 pages

AI Practical File

The document contains various Python code snippets for performing different tasks such as checking if a number is positive, negative, or zero; finding the cube root; calculating the area of a circle; determining leap years; summing odd and even numbers; generating multiplication tables; checking for prime numbers; and manipulating lists. It also includes data science operations like reading and exploring a CSV file, sorting data, and creating visualizations using matplotlib. Additionally, it covers basic computer vision tasks like loading, displaying, cropping, and resizing images using OpenCV.

Uploaded by

nehaahuja.m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

AI Practical File

The document contains various Python code snippets for performing different tasks such as checking if a number is positive, negative, or zero; finding the cube root; calculating the area of a circle; determining leap years; summing odd and even numbers; generating multiplication tables; checking for prime numbers; and manipulating lists. It also includes data science operations like reading and exploring a CSV file, sorting data, and creating visualizations using matplotlib. Additionally, it covers basic computer vision tasks like loading, displaying, cropping, and resizing images using OpenCV.

Uploaded by

nehaahuja.m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

AI

#To check if the number is positive , negative or zero

N = int(input("Enter a number - "))

if N > 0:

print("The number is positive")

elif N < 0:

print("The number is negative")

else:

print("The number is 0")

#To fine the cube root

import numpy as np

num = int(input("enter a number : "))

print("The cube root of",num,"is ",np.cbrt(num))

#To find the area of circle when radius is given by user

import math

r = int(input("Enter the radius : "))

area = math.pi * pow(r,2)

print(area)

# To check if it’s a leap year or no

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

if (year%4==0 and year%100!=0 or year%400==0):

print("This is a leap year.")

else:

print("This is not a leap year.")

#Sum of odd and even number between 1 to 15

sum_odd = 0

sum_even = 0

for i in range(1,15):
if i%2!=0 :

sum_odd += i

else:

sum_even += i

print("Sum of odd numbers :",sum_odd)

print("Sum of even numbers :",sum_even)

# Multiplication table

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

for i in range(1,11):

print(num,"*",i,"=",num*i)

# To find out if the number is prime or not

num = int(input("Enter the number : "))

if num > 1:

for i in range (2, int(num/2) + 1):

if (num % i ):

print(num , "is not a prime number")

break

else:

print(num, "is a prime number")

else:

print(num, "is not a prime number")

# To add the elements in the list and product

myList = []

for i in range(5):

e = int(input("enter the value : "))

myList.append(e)

result = 1

for i in range(0 , len(myList)):

result = result * myList[i]

print(result)
# avg marks of student in 5 subs

l = []

for i in range(5):

m = float(input("Enter the marks : "))

l.append(m)

sum1 = 0

for i in l:

sum1 += i

avg = sum1/5

print("The average marks are : " ,avg)

# swap 2 elements in a list

list = []

for i in range(5):

e = int(input("Enter the value : "))

list.append(e)

first_pos = int(input("Enter the first position : "))

second_pos = int(input("Enter the second position : "))

first_ele = list.pop(first_pos)

second_ele = list.pop(second_pos)

list.insert(first_pos , second_ele)

list.insert(second_pos , first_ele)

print(list)
# 1 – add and element , 2 – delete a element, 3 – display the minimun no in the list , 4 – list in decending
order, 5- maximun no in list

n = int(input("Enter the total numbers in the list : "))

l = []

print("Enter the number : ")

for i in range(n):

m = int(input())

l.append(m)

while True:

print("1.Add a new element. \n2.Delete the element. \n3.Display the list.\n4.Display the list in the
descending order.\n5.Display the list in ascending order.")

choice = int(input("Enter the new choice : "))

if choice == 1:

new = int(input("Enter the new element : "))

l.append(new)

print("The updated list is : ", l)

break

elif choice == 2:

e = int(input("Enter the element to be deleted : "))

if e not in l:

print("The element is not present in the list : ")

else:

l.remove(e)

print("The updated list : ", l)

break

elif choice == 3:

print(l)

break

elif choice == 4:

l.sort(reverse = True)

print("Sorted list in descending order" , l)


break

elif choice == 5:

l.sort(reverse = False)

print("Sorted list in ascending order" , l)

break

else:

print("Wrong Choice. Kindly enter the correct option")

# write the code to print a triangle

rows = 5

space = rows - 1

for j in range(1 , rows + 1):

num = j

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

print(" ", end = " ")

space = space - 1

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

print(num , end = " ")

num = num + 1

num = num - 2

for i in range(1 , j):

print(num , end = " ")

num = num - 1

print("\n")

DATA SCIENCE
#importing

import pandas as pd

import matplotlib.pyplot as plt

import numpy as np

df = pd.read_csv("JaipurFinalCleanData.csv")

type(df)

# Reading the csv file

df = pd.read_csv("JaipurFinalCleanData.csv")

type(df)

# Exploring the data

df

print(df.head())

df.tail()

df.dtypes

#To drop a columb

df = df.drop(["max_dew_pt_2"])

print(df)

# To drop a row at index 1

df = df.drop(1,axis = 0)

print(df)

# To sort the values in descending order order of date and print first 5 rows

jaipur_weather = df.sort_values(by=’date’ , ascending = False)

print(jaipur_weather.head())

# To sort the values in ascending order order of mean temperature and print first 5 rows

jaipur_weather = df.sort_values(by=’mean_temperature’ , ascending =True)

print(jaipur_weather.head())

#Scatter plot

x = df.date

y = df.mean_temperature

plt.scatter(x,y)

plt.xticks(np.arange(0,676,60))

plt.xticks(rotation = 30)

plt.xlabel(“Date” , fontsize = 14)


plt.ylabel(“Mean Temperature” , fontsize = 14)

plt.title(‘Mean temperature at Jaipur’ , fontsize = 20)

plt.show()

#Line plot

plt.figure(figsize = (20,10))

x = df.date

y_1 = df.max_temperature

y_2 = df.min_temperature

y_3 = df.mean_temperature

z = y_1-y_2

plt.plot(x,y_1 , label = “Max temp”)

plt.plot(x,y_2 , label = “Min temp”)

plt.plot(x,y_3 , label = “Mean temp”)

plt.plot(x,z, label = “range”)

plt.xticks(np.arange(0,676,60))

plt.xticks(rotation=30)

plt.legend()

plt.show()

#Bar chart

x = df.date

y = df.max_temperature

plt.bar(x,y)

plt.xticks(np.arange(0,676,60))

plt.xticks(rotation = 30)

plt.show()

#Histograph

y = df.mean_temperature

plt.hist(y,bins = 10)

plt.ylabel(“No.of days”)

plt.xlabel(“Temperature”)

plt.title(‘Probability distribution of temperature over 2 years ( 2016 – 2018) in Jaipur’)

plt.show()

COMPUTER VISION
#importing

import cv2

import matplotlib.pyplot as plt

import numpy as np

# Load the image file into memory

img = cv2.imread('flower.jpg')

plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))

plt.title('flower image')

plt.axis('on')

plt.show()

print(img.shape)

#Display the image as a gray scale

img = cv2.imread('flower.jpg' , 0)

plt.imshow(img, cmap = 'gray')

plt.title('folwer image')

plt.axis('on')

plt.show()

print(img.shape)

# Cropping image

img = cv2.imread('flower.jpg')

plt.imshow(cv2.cvtColor(img , cv2.COLOR_BGR2RGB))

roi = img[2000:3100 , 1400:2650]

plt.imshow(cv2.cvtColor(roi, cv2.COLOR_BGR2RGB))

plt.title('flower')

plt.axis('off')

plt.show()

#copy flower in multiple places

img = cv2.imread('flower.jpg')

flower = img[2000:3100,1400:2650]

img[0:1100,0:1250]=img[0:1100,2500:3750]=img[4555:5800,0:1250]=img[4555:5800,2500:3750]=flower

plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

plt.title('more flower')
plt.axis('on')

plt.show()

# Saving our flower

cv2.imwrite('more_flower.jpg',img)

#Resizing image , maintain aspect , ratio

img = cv2.imread('flower.jpg')

print(img.shape)

resized = cv2.resize(img, (int(img.shape[1]/4) , int(img.shape[0]/4)))

plt.imshow(cv2.cvtColor(resized, cv2.COLOR_BGR2RGB))

plt.title('Flower')

plt.axis('off')

plt.show()

print(resized.shape)

You might also like