Sunteți pe pagina 1din 25

SARASWATI

VIDYA
MANDIR COMPUTER
SCIENCE(083)(PYTHON)

RAMBAGH,BA
STI
PREFACE

 ..
ACKNOWLEDGEMENT

 ..
PYTHON PROGRAM
1: TO SHIFT
NUMBERS
# Right Rotating a list to n
positions
n=3

list_1 = [1, 2, 3, 4, 5, 6]
list_1 = (list_1[-n:] + list_1[:-n])

print(list_1)

OUTPUT
:
[4,5,6,1,2,3]
PYTHON PROGRAM 2;
EMI CALCULATOR
def emi_calculator(p, r, t):

r = r / (12 * 100) # one month interest


t = t * 12 # one month period
emi = (p * r * pow(1 + r, t)) / (pow(1 + r, t)
- 1)
return emi

principal= int(input(“Enter principal: ”));


rate = int(input(“Enter rate: ”));
time = int(input(“Enter time(in months): ”));
emi = emi_calculator(principal, rate, time);

print("Monthly EMI is= ", emi)


PYTHON PROGRAM 3 :
GCD USING LOOPING METHOD
# Euclid's algorithm for computing the gcd

a = input('Please input the first integer:’)

b = input('Please input the second integer:’)

while b != 0:
gcd = b
b=a%b
a = gcd print(gcd )
PYTHON PROGRAM 4:
GCD USING RECURSION
# method to compute gcd
( recursion ) Output:
def hcfnaive(a,b): The gcd of 60 and 48 is :12
if(b==0):
return a
else:
return hcfnaive(b,a%b)
a = 60
b= 48

# prints 12
print ("The gcd of 60 and 48 is :
",end="")
print (hcfnaive(60,48))
PYTHON PROGRAM 5:
BINARY SEARCH
def binarySearch (arr, l, r, x):
# Check base case
if r >= l:
mid = l + (r - l)/2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binarySearch(arr, l, mid-1, x)
else:
return binarySearch(arr, mid+1, r, x)
else:
return -1
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print "Element is present at index %d" % result
else:
print "Element is not present in array"
PYTHON PROGRAM 6:
BINARY SEARCH(USING LOOP)
def binarySearch(arr, l, r, x):
while l <= r:
mid = l + (r - l)/2;
if arr[mid] == x:
return mid
elif arr[mid] < x:
l = mid + 1
else:
r = mid - 1

return -1

result = binarySearch(arr, 0, len(arr)-1, x)


if result != -1:
print "Element is present at index %d" %
result
else:
print "Element is not present in array"
PYTHON PROGRAM 7: SUM
OF LIST
ELEMENTS(RECURSION)
# Python program to find sum of elements in list
OUTPUT:
total = 0
Sum of all
elements in the
# creating a list
list is : 74
list1 = [11, 5, 17, 18, 23]

# Iterate each element in list


# and add them in variable total
for ele in range(0, len(list1)):
total = total + list1[ele]

# printing total value


print("Sum of all elements in given list: ", total)
PYTHON PROGRAM 8:
FIBONACCI SERIES

# Function for nth Fibonacci number


OUTPUT:
def Fibonacci(n):
if n<0: 21
print("Incorrect input")
# First Fibonacci number is 0
elif n==1:
return 0
# Second Fibonacci number is 1
elif n==2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
print(Fibonacci(9))
PYTHON PROGRAM 9 :
FIBONACCI NUMBERS(LOOP)
def fibonacci(n):
a=0b=1 OUTPUT:
if n < 0:
print("Incorrect input") 21
elif n == 0:
return a
elif n == 1:
return b
else:
for i in range(2,n):
c=a+b
a=b
b=c
return b
print(fibonacci(9))
PYTHON PROGRAM 10:
FACTORIAL CALC (LOOP)
OUTPUT:
# Python code to demonstrate loop method The factorial of
# to compute factorial 23 is:
n = 23 2585201673
fact = 1 8884976640
000
for i in range(1,n+1):
fact = fact * i

print ("The factorial of 23 is : ",end=“ ")


print (fact)
PYTHON PROGRAM 11:
FACTORIAL
CALC(RECURSION) OUTPUT:
120
# Python 3 program to find
# factorial of given number
def factorial(n):

# single line to find factorial


if (n==1 or n==0):
return 1
else:
print(n * factorial(n - 1));

# Driver Code
num = 5;
print("Factorial of",num,"is”)
factorial(num))
PYTHON PROGRAM 12:
OUTPUT:
CHARACTER RHOMBUS
nm=input("Your String is here ...")
x=len(nm)

for i in range(0,len(nm)):
for j in range(len(nm)-i,0,-1):
print(" ",end=" ")
for k in range(0,i+1):
print(nm[k]," ",end=" ")
print( ) f
or i in range(0,len(nm)):
for j in range(-2,i):
print(" ",end=" ")
for k in range(0,len(nm)-i-1): CHARACTER
print(nm[k]," ",end=" ")
print() RHOMBUS
PYTHON PROGRAM 13:
SELECTION SORT
OUTPUT:
# Python program for implementation of Selection SORTED ARRAY
# Sort
import sys [11,12,22,25,6
A = [64, 25, 12, 22, 11] 4]
# Traverse through all array elements
for i in range(len(A)):
min_idx = i
for j in range(i+1, len(A)):
if A[min_idx] > A[ j]:
min_idx = j
A[i], A[min_idx] = A[min_idx], A[i]
print ("Sorted array")
for i in range(len(A)):
print("%d" %A[i]),
PYTHON PROGRAM 14:
PALINDROME
# function which return reverse of a string OUTPUT:
def reverse(s): Yes
return s[::-1]
def isPalindrome(s):
# Calling reverse function
rev = reverse(s)
# Checking if both string are equal or not
if (s == rev):
return True
return False
s = "malayalam"
ans = isPalindrome(s)
if ans == 1:
print("Yes")
else:
print("No")
PYTHON PROGRAM 15:
CREATE A TEXT FILE

file= open(“thisf.txt” “w+”)

file.write(“This is Saumya and this is my


first Python File handling program”)
file.write(“This is the second line”)
file.write(“Third line of the same
program”)
#always close the file
file.close
Print(“File generated please check”)
PYTHON PROGRAM 16:
FIND A WORD

Word=input(“Enter the word:”)

F= open(“myfile.txt”, “r”)
#apply condition

if word in f.read( ).split( ):


print(“Word is present in the text file”)
else:
print(“Word isn’t found”)
PYTHON PROGRAM 17:
SELECTION SORT INPUT:
text = open("sample.txt", "r") Mango banana apple pear
d = dict() Banana grapes
for line in text: strawberry
line = line.strip() Apple pear mango banana
# lowercase to avoid case mismatch Kiwi apple mango
line = line.lower() strawberry
words = line.split(" ")
for word in words:
# Check if the word is already in dictionary OUTPUT:
if word in d:
# Increment count of word by 1 mango : 3
d[word] = d[word] + 1
banana : 3
apple : 3
else: pear : 2
d[word] = 1 grapes : 1
for key in list(d.keys()): strawberry : 2
print(key, ":", d[key])
kiwi : 1
PYTHON PROGRAM 18:
CONNECT PYTHON WITH
MYSQL SERVER

importMySql.db
db= MySql.db.connect(“localhost”, “root”,
“ ”,“cable”)
#prepare a cursor
cursor=cursor.db( )

cursor.execute(“SELECT VERSION( )”)


data=cursor.fetchone( )
Print(“Database version is: %s” %data)
#disconnect
db.close
PYTHON PROGRAM 19:
DISPLAY RECORD FROM
MYSQL TABLE
PYTHON PROGRAM 20:
CREATE A BAR CHART

import matplotlib.pyplot as plt; plt.rcdefaults()


OUTPUT:
import numpy as np
import matplotlib.pyplot as plt

objects = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp')


y_pos = np.arange(len(objects))
performance = [10,8,6,4,2,1]

plt.bar(y_pos, performance, align='center', alpha=0.5)


plt.xticks(y_pos, objects)
plt.ylabel('Usage')
plt.title('Programming language usage')

plt.show( )
PYTHON PROGRAM 21:
CREATE A PIE CHART OUTPUT:

import matplotlib.pyplot as plt

labels = ['Cookies', 'Jellybean', 'Milkshake',


'Cheesecake']
sizes = [38.4, 40.6, 20.7, 10.3]
colors = ['yellowgreen', 'gold', 'lightskyblue',
'lightcoral']
patches, texts = plt.pie(sizes, colors=colors,
shadow=True, startangle=90)
plt.legend(patches, labels, loc="best")
plt.axis('equal')
plt.tight_layout()
plt.show()

S-ar putea să vă placă și