Sei sulla pagina 1di 37

# KENDRIYA VIDYALAYA

JANAKPURI
Session-2018-19

TOPIC:
COMPUTER SCIENCE INVESTIGATORY
PROJECT

Submitted By:
Sachin Kumar
1
XI(B) Roll no.31

CERTIFICATE

## This is to certify that Sachin Kumar Yadav of

class XI has successfully completed the
project work on computer science for class XI
practical examination of the Central Board of
Secondary Education in the year 2018-2019.
All the programs in this project were
performed by the student. It is further
certified that this project is the individual
work of the candidate.

Signature:

2
Date:

3
ACKNOWLEDGMENT

## I would like to express a deep sense of thanks

and gratitude to my computer science teacher
Mrs. Sonia Sharma for guiding me immensely
through the course of my project. Her
have been responsible for the successful
completion of my project.

## Secondly I would also like to thank my parents

and friends who helped me a lot in finishing
this project within the time.

4
INDEX
S.No. Title Page No.
1. Certificate 2
2. Acknowledgment 3
3. Introduction 7
4. Python Programs 8-24
-To print Hello World! 8
-To find the area of a triangle. 8
-To generate a random number. 9
-To convert Celsius into Fahrenheit. 9
-To find LCM. 10
-To find HCF and GCD. 11
-To check leap year. 12
-To find the factorial of a number. 12
-To display the multiplication table. 13
-To print the Fibonacci sequence. 14
-To check Armstrong number. 15
-To convert decimal into binary. 16
5
-To convert decimal into octal. 17
-To convert decimal into 17
-To find ASCII value of character. 18
-To make a simple calculator. 18
-To display calender. 20
-To sort words in alphabetic order. 21
-To illustrate different set operation 22
-To transpose a matrix. 23
5. SQL Queries 24
-Query to Retrieving Tables.
-Query for selecting columns from a
Table.
-Query for outputting data using a
Constraint.
-Query for outputting sorted data
using ‘Order By’.
-Query for outputting sorted data
using ‘Group By’.
-Query for data manipulation using
6
COUNT.
-Query for data manipulation using
SUM.
-Query for data manipulation using
AVG.
-Query for listing all views.
-Query for creating a view.
-Query to display user tables.
-Query to display primary key,unique
Key and foreign key.
-Query for swapping the values of
Two columns in a table.
-Query for making a Top 25 with the
SELECT TOP clause.

## -Query for finding the intersection of

6. Two tables.
References

7
8
INTRODUCTION
 Python is an interpreted, high-level, general-purpos programming
language. Created by Guido van Rossum and first released in 1991,
Python has a design philosophy that emphasizes code readability,
notably using significant whitespace. Van Rossum led the language
community until stepping down as leader in July 2018.
Python features a dynamic type system and automatic memory
management. It supports multiple programming paradigms,
including object-oriented, imperative, functional and procedural,
and has a large and comprehensive standard library.

##  SQL (Structured Query Language) is a domain-specific

language used in programming and designed for managing data
held in a relational database management system (RDBMS), or for
stream processing in a relational data stream management
system (RDSMS). It is particularly useful in handling structured
data where there are relations between different entities/variables
of the data. SQL offers two main advantages over older
read/write APIs like ISAM or VSAM: first, it introduced the concept
of accessing many records with one single command; and second, it
9
eliminates the need to specify how to reach a record, e.g. with or
without an index.

Python Programs

## # This program prints Hello, world!

print('Hello world!')

Output:
Hello world!

## 2.Python Program to find the area of a

triangle.
# Python Program to find the area of triangle

a=5
b=6
10
c=7

## # Un comment below to take inputs from the user

# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))

## # calculate the semi-perimeter

s = (a + b + c) / 2

## # calculate the area

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)

Output:
The area of the triangle is 14.70

## 3.Python Program to generate a random

random.
# Program to generate a random number between 0 and 999

## # import the random module

11
import random

print(random.randint(0,999))

Output:
785

## 4.Python Program to convert ccelsius into

faherenheit.
# Python Program to convert temperature in Celsius to Fahrenheit

## # change this value for a different result

celsius = 37.5

# calculate Fahrenheit
fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree
Fahrenheit' %(celsius,fahrenheit))

Output:
37.5 degree Celsius is equal to 99.5 degree Fahrenheit

12
5.Python Program to find LCM.
# Python Program to find the L.C.M. of two input number

# define a function
def lcm(x, y):
"""This function takes two
integers and returns the L.C.M."""

## # choose the greater number

if x > y:
greater = x
else:
greater = y

while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm

13
# change the values of num1 and num2 for a different result
num1 = 54
num2 = 24

## # un comment the following lines to take input from the user

#num1 = int(input("Enter first number: "))
#num2 = int(input("Enter second number: "))

## print("The L.C.M. of", num1,"and", num2,"is", lcm(num1, num2))

Output:
The L.C.M. of 54 and 24 is 216

## 6.Python Program to find HCF or GCD.

# Python program to find the H.C.F of two input number
# define a function
def computeHCF(x, y):

## # choose the smaller number

14
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf

num1 = 54
num2 = 24
# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))
print("The H.C.F. of", num1,"and", num2,"is", computeHCF(num1,
num2))

Output:
The H.C.F. of 54 and 24 is 6

## 7.Python Program to check leap year.

# Python program to check if the input year is a leap year or not
15
year = 2000

## # To get year (integer input) from the user

# year = int(input("Enter a year: "))

if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))

Output:
2000 is a leap year

## 8.Python Program to find the factorial of a

number.
16
# Python program to find the factorial of a number provided by the
user.

num = 7

## # uncomment to take input from the user

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

factorial = 1

## # check if the number is negative, positive or zero

if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

Output:
17
The factorial of 7 is 5040

## 9.Python Program to display multiplication

table.
''' Python program to find the
multiplication table (from 1 to 10)'''

num = 12

## # To take input from the user

# num = int(input("Display multiplication table of? "))
# use for loop to iterate 10 times
for i in range(1, 11):
print(num,'x',i,'=',num*i)

Output:
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
18
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120

## 10.Python Program to print the Fibonacci

sequence.
# Program to display the Fibonacci sequence up to n-th term where n
is provided by the user.

nterms = 10

## # uncomment to take input from the user

#nterms = int(input("How many terms? "))

## # first two terms

n1 = 0
n2 = 1
count = 0

19
# check if the number of terms is valid
if nterms <= 0:
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence upto",nterms,":")
while count < nterms:
print(n1,end=' , ')
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

Output:
Fibonacci sequence upto 10 :
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,

20
11.Python Program to check Armstrong
number.
# Python program to check if the number provided by the user is an
Armstrong number or not.

## # take input from the user

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

# initialize sum
sum = 0

## # find the sum of the cube of each digit

temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

## # display the result

if num == sum:
print(num,"is an Armstrong number")
21
else:
print(num,"is not an Armstrong number")

Output:
Enter a number: 663
663 is not an Armstrong number

## Enter a number: 407

407 is an Armstrong number

## 12.Python Program to convert decimal into

binary.
# Python program to convert decimal number into binary

dec = 344

## print("The decimal value of",dec,"is:")

print(bin(dec),"in binary.")

Output:
The decimal value of 344 is:

22
0b101011000 in binary.

## 13.Python Program to convert decimal into

octal.
# Python program to convert decimal number into octal

dec = 344

## print("The decimal value of",dec,"is:")

print(oct(dec),"in octal.")

Output:
The decimal value of 344 is:
0o530 in octal.

## 14.Python Program to convert decimal into

# Python program to convert decimal number into hexadecimal
number system

23
# Change this line for a different result
dec = 344

## print("The decimal value of",dec,"is:")

Output:
The decimal value of 344 is:

## 15.Python Program to find ASCII value of

character.
# Program to find the ASCII value of the given character

c = 'p'

## # Uncomment to take character from user

#c = input("Enter a character: ")

## print("The ASCII value of '" + c + "' is",ord(c))

24
Output:
The ASCII value of 'p' is 112

## 16.Python Program to make a simple

calculator.
# Program make a simple calculator that can add, subtract, multiply
and divide using functions

return x + y

## # This function subtracts two numbers

def subtract(x, y):
return x - y

## # This function multiplies two numbers

def multiply(x, y):
return x * y

## # This function divides two numbers

def divide(x, y):
25
return x / y

print("Select operation.")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

## # Take input from the user

choice = input("Enter choice(1/2/3/4):")

## num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

if choice == '1':

## elif choice == '2':

print(num1,"-",num2,"=", subtract(num1,num2))

## elif choice == '3':

print(num1,"*",num2,"=", multiply(num1,num2))
26
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")

Output:
Select operation.
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 3
Enter first number: 15
Enter second number: 14
15 * 14 = 210

## 17.Python Program to display calender.

# Python program to display calendar of given month of the year

# import module
import calendar
27
yy = 2019
mm = 2

## # To ask month and year from the user

# yy = int(input("Enter year: "))
# mm = int(input("Enter month: "))

## # display the calendar

print(calendar.month(yy, mm))

Output:

## 18.Python Program to sort words in alphabetic

order.
# Program to sort alphabetically the words form a string provided by
the user
28
# change this value for a different result
my_str = "Hello this Is an Example With cased letters"

## # uncomment to take input from the user

#my_str = input("Enter a string: ")

## # breakdown the string into a list of words

words = my_str.split()

words.sort()

## print("The sorted words are:")

for word in words:
print(word)

Output:
The sorted words are:
Example
29
Hello
Is
With
an
cased
letters
this

## 19.Python Program to illustrate different set

opertions.
# Program to perform different set operations like in mathematics

## # define three sets

E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};

# set union
print("Union of E and N is",E | N)

# set intersection
print("Intersection of E and N is",E & N)
30
# set difference
print("Difference of E and N is",E - N)

## # set symmetric difference

print("Symmetric difference of E and N is",E ^ N)

Output:
Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8}
Intersection of E and N is {2, 4}
Difference of E and N is {8, 0, 6}
Symmetric difference of E and N is {0, 1, 3, 5, 6, 8}

## 20.Python Program to transpose a matrix.

# Program to transpose a matrix using nested loop

X = [[12,7],
[4 ,5],
[3 ,8]]

result = [[0,0,0],
[0,0,0]]
31
# iterate through rows
for i in range(len(X)):
# iterate through columns
for j in range(len(X)):
result[j][i] = X[i][j]

for r in result:
print(r)

Output:
[12, 4, 3]
[7, 5, 8]

SQL Queries
1. Query for Retrieving Tables
This query can be run to retrieve the list of tables present in a database
where the database is “My_Schema”.
SELECT * FROM My_Schema.Tables;

32
2. Query for Selecting Columns from a Table
In the example below, we are extracting the “Student_ID” column or
attribute from the table “STUDENT”.
SELECT Student_ID FROM STUDENT;

## If we want to display all the attributes from a particular table, this is

the right query to use:
SELECT * FROM STUDENT;

## 3. Query for Outputting Data Using a Constraint

This query retrieves the specified attributes from the table on the
constraint Employee ID =0000
SELECT EMP_ID, NAME FROM EMPLOYEE_TBL WHERE EMP_ID =
'0000';

## 4. Query for Outputting Sorted Data Using ‘Order By’

This query orders the results with respect to the attribute which is
referenced to using “Order By” – so for example, if that attribute is an
integer data type, then the result would either be sorted in ascending
or descending order; likewise, if the data type is a String then the result
would be ordered in alphabetical order.
SELECT EMP_ID, LAST_NAME FROM EMPLOYEE
WHERE CITY = 'Seattle' ORDER BY EMP_ID;

33
The ordering of the result can also be set manually, using “asc ” for
ascending and “desc” for descending.
SELECT EMP_ID, LAST_NAME FROM EMPLOYEE_TBL
WHERE CITY = 'INDIANAPOLIS' ORDER BY EMP_ID asc;

## 5. Query for Outputting Sorted Data Using ‘Group By’

The ‘Group By’ property groups the resulting data according to the
specified attribute.
SELECT Name, Age FROM Patients WHERE Age > 40
GROUP BY Age ORDER BY Name;

## 6. Data Manipulation Using COUNT

This query displays the total number of customers by counting each
customer ID. In addition, it groups the results according to the country
of each customer.
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY
Country;

## 7. Data Manipulation Using SUM

SUM calculates the total of the attribute that is given to it as an
argument.
SELECT SUM(Salary)FROM Employee WHERE Emp_Age < 30;
34
8. Data Manipulation Using AVG
Simple – an average of a given attribute.
SELECT AVG(Price)FROM Products;

## 9. Query for Listing all Views

This query lists all the views available in the schema.
SELECT * FROM My_Schema.views;

## 10. Query for Creating a View

A view is a tailored table that is formed as a result of a query. It has
tables and rows just like any other table. It’s usually a good idea to run
queries as independent views because this allows them to be retrieved
later to view the query results, rather than computing the same
command every time for a particular set of results.
CREATE VIEW Failing_Students AS
SELECT S_NAME, Student_ID
FROM STUDENT
WHERE GPA > 40;

## 11. Query to Display User Tables

A user-defined table is a representation of defined information in a
table, and they can be used as arguments for procedures or
35
user-defined functions. Because they’re so useful, it’s useful to keep
track of them using the following query.
SELECT * FROM Sys.objects WHERE Type='u'

## 12. Query to Display Primary Keys,Foreign keys and Unique keys.

A primary key uniquely identifies all values within a table. The following
query lists all the fields in a table’s primary key.
SELECT * from Sys.Objects WHERE Type='PK'

A Unique Key allows a column to ensure that all of its values are
different.
SELECT * FROM Sys.Objects WHERE Type='uq'

Foreign keys link one table to another – they are attributes in one table
which refer to the primary key of another table.
SELECT * FROM Sys.Objects WHERE Type='f'

## 13. Swapping the Values of Two Columns in a table

With this in mind, we can easily imagine an Orders table which likewise
contains the indexed customer ID field, along with details of each order
placed by the customer. This table will include order Number, Quantity,
Date, Item, and Price. In our first SQL example, imagine a situation

36
where the zip and phone fields were transposed and all the phone
numbers were erroneously entered into the zip code field. We can
easily fix this problem with the following SQL statement:
UPDATE Customers SET Zip=Phone, Phone=Zip

## 14. Making a Top 25 with the SELECT TOP Clause.

In this example we will return the top 25 from our Customers table:
SELECT TOP 25 FROM Customers WHERE Customer_ID<>NULL;

## 15. Finding the Intersection of Two Tables.

The JOIN statement accomplishes this core objective of SQL and makes
the task easy. Here we are going to fetch a list of all records which have
matches in the Customers and Orders tables:
SELECT ID FROM Customers INNER
JOIN Orders ON Customers.ID = Orders.ID

References

37