0% found this document useful (0 votes)
2 views16 pages

Python Practical File Same Page Output

The document contains various Python programs demonstrating different functionalities such as checking leap years, voting eligibility, palindrome checking, string and list traversal, list item increment, matrix addition, binary and linear search, sorting algorithms (selection, bubble, insertion), file handling with pickle, and MySQL operations (INSERT and UPDATE queries). Each program includes code snippets and example outputs. The document serves as a reference for basic programming concepts and database interactions in Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Python Practical File Same Page Output

The document contains various Python programs demonstrating different functionalities such as checking leap years, voting eligibility, palindrome checking, string and list traversal, list item increment, matrix addition, binary and linear search, sorting algorithms (selection, bubble, insertion), file handling with pickle, and MySQL operations (INSERT and UPDATE queries). Each program includes code snippets and example outputs. The document serves as a reference for basic programming concepts and database interactions in Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Leap Year Program


Program Code:
year = int(input('Enter year: '))
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
print('Leap Year')
else:
print('Not a Leap Year')

Output:
Enter year: 2024
Leap Year
2. Eligible for Vote
Program Code:
age = int(input('Enter age: '))
if age >= 18:
print('Eligible for Vote')
else:
print('Not Eligible')

Output:
Enter age: 20
Eligible for Vote
3. Palindrome Program
Program Code:
s = input('Enter string: ')
if s == s[::-1]:
print('Palindrome')
else:
print('Not Palindrome')

Output:
Enter string: madam
Palindrome
4. Traversal of String
Program Code:
s = input('Enter string: ')
for ch in s:
print(ch)

Output:
Enter string: hi
h
i
5. Traversal of List
Program Code:
lst = [1, 2, 3, 4]
for i in lst:
print(i)

Output:
1
2
3
4
6. Increment of List Items by 5
Program Code:
lst = [1, 2, 3]
for i in range(len(lst)):
lst[i] = lst[i] + 5
print(lst)

Output:
[6, 7, 8]
7. Matrix Addition
Program Code:
A = [[1,2],[3,4]]
B = [[5,6],[7,8]]
C = [[A[i][j] + B[i][j] for j in range(2)] for i in range(2)]
print(C)

Output:
[[6, 8], [10, 12]]
8. Binary Search Program
Program Code:
lst = [1,2,3,4,5]
key = 4
low = 0
high = len(lst) - 1
while low <= high:
mid = (low + high) // 2
if lst[mid] == key:
print('Found')
break
elif lst[mid] < key:
low = mid + 1
else:
high = mid - 1

Output:
Found
9. Linear Search Program
Program Code:
lst = [10, 20, 30]
key = 20
for i in lst:
if i == key:
print('Found')

Output:
Found
10. Selection Sort Program
Program Code:
lst = [64, 25, 12]
for i in range(len(lst)):
min_index = i
for j in range(i + 1, len(lst)):
if lst[j] < lst[min_index]:
min_index = j
lst[i], lst[min_index] = lst[min_index], lst[i]
print(lst)

Output:
[12, 25, 64]
11. Bubble Sort Program
Program Code:
lst = [5, 1, 4]
for i in range(len(lst)):
for j in range(0, len(lst) - i - 1):
if lst[j] > lst[j + 1]:
lst[j], lst[j + 1] = lst[j + 1], lst[j]
print(lst)

Output:
[1, 4, 5]
12. Insertion Sort Program
Program Code:
lst = [12, 11, 13]
for i in range(1, len(lst)):
key = lst[i]
j = i - 1
while j >= 0 and key < lst[j]:
lst[j + 1] = lst[j]
j -= 1
lst[j + 1] = key
print(lst)

Output:
[11, 12, 13]
13. File Handling using Pickle
Program Code:
import pickle
f = open('[Link]', 'wb')
[Link]([1, 2, 3], f)
[Link]()

Output:
Data stored successfully
14. Python MySQL INSERT Query
Program Code:
import [Link]
con = [Link](host='localhost', user='root', password='root',
database='school')
cur = [Link]()
[Link]("INSERT INTO student VALUES (1, 'Amit')")
[Link]()

Output:
Record inserted successfully
15. Python MySQL UPDATE Query
Program Code:
[Link]("UPDATE student SET name='Rahul' WHERE id=1")
[Link]()

Output:
Record updated successfully
16. SQL Queries
1. SELECT * FROM student;
2. SELECT name FROM student WHERE id = 1;
3. UPDATE student SET name = 'Ravi' WHERE id = 2;
4. DELETE FROM student WHERE id = 3;
5. SELECT [Link], [Link] FROM student JOIN marks ON [Link] = [Link];

You might also like