Python and MySQL for Class 12 Computer Science
Python and MySQL for Class 12 Computer Science
Submitted by: Your Name
Class: 12th
School: Your School Name
Date of Submission:
Table of Contents
1. Python Basics
2. MySQL Commands
3. Python-MySQL Connectivity
4. Student Database Example
5. Outputs and Explanation
6. Conclusion
Python and MySQL for Class 12 Computer Science
Chapter 1: Python Basics
Python is a versatile and powerful programming language.
Here are more examples of Python code to demonstrate its features.
1. Factorial Calculation using Recursion:
Code:
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print("Factorial of 5:", factorial(5))
Output:
Factorial of 5: 120
2. Prime Number Check:
Code:
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
Python and MySQL for Class 12 Computer Science
return False
return True
print("7 is prime:", is_prime(7))
Output:
7 is prime: True
3. Sum of Numbers in a List:
Code:
numbers = [10, 20, 30, 40]
total = sum(numbers)
print("Sum of the list:", total)
Output:
Sum of the list: 100
4. Fibonacci Series:
Code:
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
fibonacci(10)
Python and MySQL for Class 12 Computer Science
Output:
0 1 1 2 3 5 8 13 21 34
5. Reverse a String:
Code:
def reverse_string(s):
return s[::-1]
print(reverse_string("hello"))
Output:
olleh
6. Palindrome Checker:
Code:
def is_palindrome(s):
return s == s[::-1]
print("madam is palindrome:", is_palindrome("madam"))
Output:
madam is palindrome: True
Python and MySQL for Class 12 Computer Science
Chapter 2: MySQL Commands
In addition to basic SQL commands, here are more advanced examples.
1. Join Tables:
SELECT students.name, grades.subject, grades.score
FROM students
JOIN grades ON students.id = grades.student_id;
2. Group By:
SELECT grade, COUNT(*)
FROM students
GROUP BY grade;
3. Order By:
SELECT * FROM students
ORDER BY name ASC;
Python and MySQL for Class 12 Computer Science
Chapter 4: Student Database Example
Expanded database management example demonstrating CRUD operations.
Code:
1. Create Database:
CREATE DATABASE school;
2. Insert Data:
INSERT INTO students (name, grade) VALUES ('Alex', 'B');
3. Retrieve and Filter Data:
SELECT * FROM students WHERE grade = 'A';
4. Update Data:
UPDATE students SET grade = 'A' WHERE name = 'Alex';
Python and MySQL for Class 12 Computer Science
Conclusion
This enhanced assignment provided a comprehensive overview of Python and MySQL.
It covered programming basics, database commands, and integration examples.