Python and SQL Report
1. Python Programs
Hello World:
print("Hello, World!")
Explanation: This is a simple program that outputs 'Hello, World!' to the console.
Calculate the Sum of Two Numbers:
a = 10
b = 20
print("Sum:", a + b)
Explanation: This program calculates and displays the sum of two numbers.
Factorial of a Number:
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
print("Factorial:", factorial(5))
Explanation: This recursive function calculates the factorial of a given number.
Check if a Number is Prime:
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
Python and SQL Report
print("Is Prime:", is_prime(7))
Explanation: This function checks whether a number is a prime number.
Fibonacci Sequence:
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
fibonacci(10)
Explanation: This program generates the first 'n' numbers in the Fibonacci sequence.
Reverse a String:
string = "Python"
print("Reversed:", string[::-1])
Explanation: This program reverses a given string using slicing.
Palindrome Checker:
def is_palindrome(s):
return s == s[::-1]
print("Is Palindrome:", is_palindrome("radar"))
Explanation: This function checks if a given string is a palindrome.
Find Largest Number in a List:
numbers = [3, 1, 4, 1, 5, 9]
print("Largest:", max(numbers))
Python and SQL Report
Explanation: This program finds and displays the largest number in a list.
Sort a List:
numbers = [3, 1, 4, 1, 5, 9]
print("Sorted:", sorted(numbers))
Explanation: This program sorts a list of numbers in ascending order.
Simple Calculator:
def calculator(a, b, operation):
if operation == "add":
return a + b
elif operation == "subtract":
return a - b
elif operation == "multiply":
return a * b
elif operation == "divide":
return a / b
else:
return "Invalid Operation"
print("Result:", calculator(10, 5, "add"))
Explanation: This is a simple calculator that performs basic arithmetic operations.
Count Vowels in a String:
def count_vowels(s):
return sum(1 for char in [Link]() if char in "aeiou")
print("Vowel Count:", count_vowels("Hello World"))
Explanation: This program counts the number of vowels in a given string.
Python and SQL Report
Generate Random Numbers:
import random
for _ in range(5):
print([Link](1, 100))
Explanation: This program generates and displays five random numbers between 1 and 100.
Find the GCD of Two Numbers:
import math
print("GCD:", [Link](48, 18))
Explanation: This program calculates the greatest common divisor (GCD) of two numbers.
Convert Celsius to Fahrenheit:
celsius = 25
fahrenheit = (celsius * 9/5) + 32
print("Fahrenheit:", fahrenheit)
Explanation: This program converts a temperature from Celsius to Fahrenheit.
Check Armstrong Number:
def is_armstrong(n):
digits = [int(d) for d in str(n)]
return sum(d ** len(digits) for d in digits) == n
print("Is Armstrong:", is_armstrong(153))
Explanation: This program checks if a number is an Armstrong number.
2. SQL Queries
Table: Employees
Python and SQL Report
EmpID Name Department Salary
1 Alice HR 50000
2 Bob IT 60000
3 Charlie IT 55000
4 Diana HR 52000
5 Edward Sales 45000
Table: Departments
DeptID DeptName
1 HR
2 IT
3 Sales
Select All Records:
SELECT * FROM Employees;
Explanation: This query selects and displays all the records from the Employees table.
Filter by Department:
SELECT * FROM Employees WHERE Department = 'IT';
Explanation: This query filters and displays employees who work in the IT department.
Find Maximum Salary:
SELECT MAX(Salary) AS MaxSalary FROM Employees;
Explanation: This query finds the maximum salary among all employees.
Python and SQL Report
Group by Department:
SELECT Department, AVG(Salary) AS AvgSalary FROM Employees GROUP BY Department;
Explanation: This query calculates the average salary of employees grouped by their department.
Join with Another Table:
SELECT [Link], [Link], [Link]
FROM Employees e
JOIN Departments d
ON [Link] = [Link];
Explanation: This query joins the Employees table with the Departments table based on the department name.
3. Python-SQL Connectivity
Connect to SQLite and Create a Table:
import sqlite3
conn = [Link]('[Link]')
cursor = [Link]()
[Link]('''CREATE TABLE IF NOT EXISTS Employees (
EmpID INTEGER PRIMARY KEY,
Name TEXT,
Department TEXT,
Salary REAL
)''')
[Link]()
[Link]()
Explanation: This program connects to an SQLite database, creates a table named Employees if it does not already
exist, and then closes the connection.
Python and SQL Report
Insert Records:
conn = [Link]('[Link]')
cursor = [Link]()
[Link]("INSERT INTO Employees (Name, Department, Salary) VALUES (?, ?, ?)",
("Alice", "HR", 50000))
[Link]()
[Link]()
Explanation: This program inserts a new record into the Employees table.
Query Records:
conn = [Link]('[Link]')
cursor = [Link]()
[Link]("SELECT * FROM Employees")
rows = [Link]()
for row in rows:
print(row)
[Link]()
Explanation: This program retrieves all records from the Employees table and displays them.
Update a Record:
conn = [Link]('[Link]')
cursor = [Link]()
[Link]("UPDATE Employees SET Salary = ? WHERE Name = ?", (55000, "Alice"))
[Link]()
[Link]()
Explanation: This program updates the salary of an employee named Alice in the Employees table.