0% found this document useful (0 votes)
1 views9 pages

Class12 CS PracticalFile Python SQL

The document is a practical file for Class 12 Computer Science containing various Python programs and SQL connectivity examples. It includes code snippets for calculating factorials, checking prime numbers, summing digits, reversing numbers, generating Fibonacci series, and basic database operations like creating, updating, and deleting records in a student table. Each program is accompanied by sample inputs and outputs to demonstrate functionality.

Uploaded by

Prabhjot Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views9 pages

Class12 CS PracticalFile Python SQL

The document is a practical file for Class 12 Computer Science containing various Python programs and SQL connectivity examples. It includes code snippets for calculating factorials, checking prime numbers, summing digits, reversing numbers, generating Fibonacci series, and basic database operations like creating, updating, and deleting records in a student table. Each program is accompanied by sample inputs and outputs to demonstrate functionality.

Uploaded by

Prabhjot Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Class 12 Computer Science Practical File

Name: [Your Name]

School: [Your School Name]

Session: 2024–25

1. Find Factorial Using Recursion


Code:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
num = int(input("Enter number: "))
print("Factorial is:", factorial(num))

Output:
Input: 5
Output: 120

2. Check Prime Number


Code:
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")

Output:
Input: 7
Output: Prime
3. Sum of Digits
Code:
n = int(input("Enter a number: "))
sum = 0
while n != 0:
sum += n % 10
n //= 10
print("Sum of digits:", sum)

Output:
Input: 123
Output: 6

4. Reverse a Number
Code:
n = int(input("Enter number: "))
rev = 0
while n > 0:
rev = rev * 10 + n % 10
n = n // 10
print("Reversed number:", rev)

Output:
Input: 456
Output: 654

5. Fibonacci Series
Code:
n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b

Output:
Input: 6
Output: 0 1 1 2 3 5
6. Palindrome Check (String)
Code:
s = input("Enter a string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

Output:
Input: madam
Output: Palindrome

7. Count Vowels in String


Code:
s = input("Enter string: ")
vowels = "aeiouAEIOU"
count = 0
for ch in s:
if ch in vowels:
count += 1
print("Vowel Count:", count)

Output:
Input: Hello
Output: 2

8. Check Prime Number Using Function

def is_prime(n):

if n <= 1:

return False

for i in range(2, n):

if n % i == 0:

return False
return True

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

if is_prime(num):

print(num, "is a Prime Number")

else:

print(num, "is not a Prime Number")

Sample Output:

Enter a number: 7

7 is a Prime Number

9. Calculate Factorial Using Function

def factorial(n):

fact = 1

for i in range(1, n+1):

fact *= i

return fact

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

result = factorial(num)
print("Factorial of", num, "is", result)

Sample Output:

Enter a number: 5

Factorial of 5 is 120.

10. Count Frequency of Element


Code:
lst = [1, 2, 2, 3, 1, 4]
element = int(input("Enter element: "))
print("Frequency:", [Link](element))

Output:
Input: 1
Output: 2

11. Factorial Using Loop


Code:
n = int(input("Enter number: "))
f=1
for i in range(1, n+1):
f *= i
print("Factorial:", f)

Output:
Input: 4
Output: 24

12. Merge Two Lists


Code:
a = [1, 3, 5]
b = [2, 4, 6]
c = sorted(a + b)
print("Merged List:", c)

Output:
Output: [1, 2, 3, 4, 5, 6]

13. Print Multiplication Table


Code:
n = int(input("Enter number: "))
for i in range(1, 11):
print(n, "x", i, "=", n*i)

Output:
Input: 5
Output: 5 x 1 = 5 ... 5 x 10 = 50

14. Find Largest Number


Code:
a, b, c = map(int, input("Enter 3 numbers: ").split())
print("Largest:", max(a, b, c))

Output:
Input: 3 7 5
Output: 7

15. Count Even and Odd Numbers


Code:
lst = [1, 2, 3, 4, 5]
even = odd = 0
for i in lst:
if i % 2 == 0:
even += 1
else:
odd += 1
print("Even:", even, "Odd:", odd)
Output:
Output: Even: 2 Odd: 3
Python Programs with SQL Connectivity
1. Create and Insert into Student Table
Code:
import sqlite3
con = [Link]("[Link]")
cur = [Link]()
[Link]("CREATE TABLE IF NOT EXISTS student(roll INTEGER, name TEXT)")
[Link]("INSERT INTO student VALUES(1, 'John')")
[Link]()
[Link]()
print("Record inserted")

Output:
Output: Record inserted

2. Display All Records from Student Table


Code:
import sqlite3
con = [Link]("[Link]")
cur = [Link]()
[Link]("SELECT * FROM student")
rows = [Link]()
for row in rows:
print(row)
[Link]()

Output:
Output: (1, 'John')

3. Update Student Record


Code:
import sqlite3
con = [Link]("[Link]")
cur = [Link]()
[Link]("UPDATE student SET name='Jane' WHERE roll=1")
[Link]()
[Link]()
print("Record updated")

Output:
Output: Record updated

4. Delete Student Record


Code:
import sqlite3
con = [Link]("[Link]")
cur = [Link]()
[Link]("DELETE FROM student WHERE roll=1")
[Link]()
[Link]()
print("Record deleted")

Output:
Output: Record deleted

You might also like