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

Python Programming Basics and MySQL

The document outlines a series of programming experiments in Python, covering topics such as recursion, prime number checks, Fibonacci series, character frequency, sorting algorithms, file handling, and MySQL database operations. Each experiment includes an aim, logic, and sample code to demonstrate the concept. Additionally, it provides SQL commands for creating tables and querying data from a database.

Uploaded by

elatedelgamal4
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 views8 pages

Python Programming Basics and MySQL

The document outlines a series of programming experiments in Python, covering topics such as recursion, prime number checks, Fibonacci series, character frequency, sorting algorithms, file handling, and MySQL database operations. Each experiment includes an aim, logic, and sample code to demonstrate the concept. Additionally, it provides SQL commands for creating tables and querying data from a database.

Uploaded by

elatedelgamal4
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

✨ INDEX

1. Factorial using Recursion


2. Prime Number Check
3. Fibonacci Series
4. Character Frequency in String
5. Odd & Even Numbers from List
6. Multiplication Table
7. Largest of Three Numbers
8. Word Count in a Text File
9. Copy Contents from One File to Another
10. Student Details – File Handling
11. Palindrome Check using Function
12. Character Frequency (Alternative Method)
13. Bubble Sort
14. Linear Search
15. Insertion Sort
16. Search Record in CSV File
17. Python–MySQL Connection
18. Insert Records into MySQL
19. Display MySQL Records
20. Update MySQL Records
21. SQL Commands

EXPERIMENT – 1

Program to Find Factorial of a Number Using Recursion

🎯 Aim

To calculate the factorial of a given number using recursion in Python.

🧠 Logic

Factorial of a number n is calculated as:


n! = n × (n-1)!

💻 Program

def factorial(n):
if n == 0 or n == 1:
return 1
else:

1
return n * factorial(n-1)

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


print("Factorial =", factorial(num))

📌 Output

Factorial of the entered number is displayed on the screen.

EXPERIMENT – 2

Program to Check Whether a Number Is Prime

🎯 Aim

To check whether the given number is prime or not.

💻 Program

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

if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not a Prime Number")
break
else:
print("Prime Number")
else:
print("Not a Prime Number")

EXPERIMENT – 3

Program to Print Fibonacci Series

🎯 Aim

To generate Fibonacci series up to n terms.

2
💻 Program

n = int(input("Enter number of terms: "))


a, b = 0, 1

for i in range(n):
print(a, end=" ")
a, b = b, a + b

EXPERIMENT – 4

Program to Count Frequency of Each Character in a String

string = input("Enter a string: ")


frequency = {}

for char in string:


frequency[char] = [Link](char, 0) + 1

print(frequency)

EXPERIMENT – 5

Program to Display Odd and Even Numbers from a List

lst = [1, 2, 3, 4, 5, 6, 7, 8]
print("Even Numbers:", [x for x in lst if x % 2 == 0])
print("Odd Numbers:", [x for x in lst if x % 2 != 0])

3
EXPERIMENT – 6

Multiplication Table

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


for i in range(1, 11):
print(num, "x", i, "=", num*i)

EXPERIMENT – 7

Largest of Three Numbers

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


b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Largest number is:", max(a, b, c))

EXPERIMENT – 8

Count Number of Words in a Text File

file = open("[Link]", "r")


words = [Link]().split()
print("Number of words:", len(words))
[Link]()

EXPERIMENT – 9

Copy Contents from One File to Another

with open("[Link]", "r") as s, open("[Link]", "w") as d:


[Link]([Link]())

4
EXPERIMENT – 10

Write and Read Student Details in a File

file = open("[Link]", "w")


[Link]("Name: Rahul
Class: 12
Roll: 5")
[Link]()

file = open("[Link]", "r")


print([Link]())
[Link]()

EXPERIMENT – 11

Palindrome Check Using Function

def is_palindrome(s):
return s == s[::-1]

value = input("Enter value: ")


print("Palindrome" if is_palindrome(value) else "Not Palindrome")

EXPERIMENT – 12

Bubble Sort

lst = [64, 34, 25, 12, 22, 11, 90]


for i in range(len(lst)):
for j in range(len(lst)-i-1):
if lst[j] > lst[j+1]:
lst[j], lst[j+1] = lst[j+1], lst[j]
print("Sorted List:", lst)

5
EXPERIMENT – 13

Linear Search

lst = [10, 20, 30, 40, 50]


key = int(input("Enter element to search: "))

if key in lst:
print("Element Found")
else:
print("Element Not Found")

EXPERIMENT – 14

Insertion Sort

lst = [12, 11, 13, 5, 6]


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("Sorted List:", lst)

SECTION B – PYTHON & MySQL PROGRAMS

EXPERIMENT – 15

Connect Python with MySQL

import [Link]
conn = [Link](
host="localhost",
user="root",
password="password",
database="school"

6
)
print("Connected Successfully")

EXPERIMENT – 16

Insert Records into MySQL

cursor = [Link]()
[Link]("INSERT INTO student VALUES (1,'Amit')")
[Link]()

EXPERIMENT – 17

Display Records from MySQL

[Link]("SELECT * FROM student")


for row in [Link]():
print(row)

EXPERIMENT – 18

Update Record in MySQL

[Link]("UPDATE student SET name='Rahul' WHERE id=1")


[Link]()

SECTION C – SQL COMMANDS

EXPERIMENT – 19

To Create a New Table

CREATE TABLE employees (


emp_id INT PRIMARY KEY,

7
name VARCHAR(50),
salary INT,
department VARCHAR(30)
);

EXPERIMENT – 20

Display Names of Employees Whose Name Ends with 'n'

SELECT name FROM employees


WHERE name LIKE '%n';

3️⃣ List All Customers Whose Name Contains 'sh'

SELECT * FROM customers


WHERE name LIKE '%sh%';

4️⃣ List Products Sorted by Category (A–Z) and Price (High to Low)

SELECT * FROM products


ORDER BY category ASC, price DESC;

5️⃣ Display Employees Whose Salary Is Between 30000 and 50000

SELECT * FROM employees


WHERE salary BETWEEN 30000 AND 50000;

You might also like