Python Project
Python Project
Example :
let a list contains = [-2, 1, -3, -15, 16, -17, 5, -3, -6]
after processing the list should look like this
[1, 16, 5, -6, -3, -17, -15, -3, -2]
for x in range(n):
if list1[x] > 0:
list2.append(list1[x])
print(list2)
Output
[1, 16, 5, -6, -3, -17, -15, -3, -2]
rem = a % b
While (rem! = 0):
a = b
b = rem
rem = a % b
print('Your GCD is :', b)
Output
5-Binary Search
x =[1,2,3,5,7,8,9,10,12,14,15,18,20,22]
data = 10
found =0
first=0
last =13
while (first<=last and found ==0):
mid = int((first+last)/2)
if(x[mid]==data):
found =1
if(x[mid]data):
last = mid-1
if(found ==0):
print("Data not found")
else:
print("Data found")
li = [1,2,3,4,5,6,7,8,9,10]
result = binarySearch(li,9,len(li),6)
if(result!=-1):
print('Data found at index :',result)
else:
print(' Data not found')
def sum_element(l):
if(len(l) == 1):
return l[0]
else:
value = l.pop()
return value+sum_element(l)
a = 0
b = 1
for i in range(1, 20):
if i == 1:
print(a, end=" ")
if i == 2:
print(b, end=" ")
if i > 2:
c = a+b
print(c, end=" ")
a = b
b = c
9-The Fibonacci number using recursion
if __name__ == "__main__":
for x in range(1, 21):
print(fab(x), end=" ")
10-Factorial of Number N using Loopin
def factorial(n):
if n==1:
return 1
else:
return n*(factorial(n-1))
#function call
n = int(input("Enter any number "))
result = factorial(n)
print("factorial of {} is {} ".format(n,result))
import MySQLdb
data = MySQLdb.connect("localhost", "root", "",
"binarynote")
cursor = data.cursor()
uname = input("Enter your user ID :")
upass = input("Enter your Password :")
Name = input(“Enter your username :”)
Password = input (“Enter your password :”)
Sql = “insert into user(uname, upass) value (‘”. Uname.
“’,’”. Upass .”’);”;
sql = "insert into user(uname,upass)
values('{}','{}');".format(uname, upass)
cursor.execute(sql)
data.close()
print("Record Added................")
16-Python Program to delete a record from MySQL Table
import MySQLdb
db = MySQLdb.connect(“localhost”,”root”,””,”cable”)
cursor = db.cursor()
name = input(“Enter any name : “)
sql =”delete from customer where name like ‘%” + name + “‘;”
cursor.execute(sql)
db.commit()
db.close()
print(“Row deleted successfully”)
import MySQLdb
db = MySQLdb.connect("localhost", "root", "", "binarynote")
cursor = db.cursor()
name = input("Enter current name : ")
new_name = input("Enter new name :")
sql = "update user set uname={} where name like
'%{}';".format(name, new_name)
cursor.execute(sql)
db.commit()
db.close()
print("Row deleted successfully")
Program to find out the total number of words in any given test file
file = open(r"abcd.txt")
words = lines = 0
for line in file.readlines():
lines += 1
words += len(line.split())
file.close()
print("Total Words in this File :", words)
print("Total Lines in this File :", lines)
22-Program to find out total number of lines that start with alphabet
‘A’
file = open(r"abcd.txt",'r')
count=0;
for line in file.readlines():
if line[0]=='A':
count+=1
file.close()
print("Total lines that start with alphabet 'A' :",count)
numbers = []
n = int(input('Enter value of n :'))
for i in range(n):
x = int(input('Enter any number :'))
numbers.append(x)
unique = []
frequency = []
for a in numbers:
if a not in unique:
unique.append(a)
count = numbers.count(a)
frequency.append(count)
x =[3,56,2,56,78,56,34,23,4,78,8,123,45]
for i in range(0,len(x)-1):
pos = i
low = x[pos]
for j in range(i+1,len(x)):
if(low>x[j]):
pos = j
low = x[j]
x[i],x[pos] = x[pos],x[i]
Or
word=input("Please enter a word")
revs=word[::-1]
if word == revs:
print("This word is a palindrome")
else:
print("This word is not a palindrome")
import random as r
counter = 0
while True:
letter = input('\nGuess your letter :')
if letter in word:
for i in range(0, len(word)):
if word[i] == letter:
lines[i] = letter
else: # letter is not in the word
counter += 1
# print the word with inserted letters
for i in lines:
print(i, end=" ")
cnt = "".join(lines).count('_')
if cnt == 0 or counter == 6:
break
# end of while loop
if counter >= 6:
print("\n\n\n You looser..............Think properly")
else:
print("\n\n\n Yes!!!!!!!!!!! You WON this match")