0% found this document useful (0 votes)
65 views4 pages

Xii CS Syntax and Examples

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)
65 views4 pages

Xii CS Syntax and Examples

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
You are on page 1/ 4

THE LEADERS MATRIC HR SEC SCHOOL – KARAIKUDI

PROGRAM SYNTAX & EXAMPLES


STD: XII COMPUTER SCIENCE
S TOPIC SYNTAX / EXAMPLES
CHAPTER - 1 FUNCTION
1. Function definition let rec fn a1 a2 ... an := k
2. Function types x → y ; x1 → x2 → y; x1 → ... → xn → y
CHAPTER - 2 DATA ABSTRACTION
1. Constructor Pseudo code Distance (city1, city2):
lt1, lg1 := getlat(city1), getlon(city1)
lt2, lg2 := getlat(city2), getlon(city2)
return ((lt1 - lt2)**2 + (lg1 - lg2)**2))1/2
2. Rational number Pseudo code x,y:=8,3 rational(n,d)
numer(x)/denom(y) - - output : 2.6666666666666665
3. Multi part object Pseudo code class Person:
creation( )
firstName := " "
lastName := " "
id := " "
email := " "
CHAPTER - 5 PYTHON -VARIABLES AND OPERATORS
The print() function print (“string to be displayed as output ” )
print (variable )
print (“String to be displayed as output ”, variable)
print (“String1 ”, variable, “String 2”, variable, “String 3” ……)
2. The input() function Variable = input (“prompt string”)
3. Conditional operator Variable Name = [on_true] if [Test expression] else [on_false]
CHAPTER - 6 CONTROL STRUCTURES
1. Simple if statement if <condition>:
statements-block1
2. if..else statement if <condition>:
statements-block 1
else:
statements-block 2
3. Nested if..elif...else if <condition-1>:
statements-block 1
elif <condition-2>:
statements-block 2
else:
statements-block n
4. while loop while <condition>:
statements block 1
[else:
statements block2]
5. for loop for counter_variable in sequence:
statements-block 1
[else: # optional block
statements-block 2]
6 break statement break
7. continue statement continue
8. pass statement pass

1
PREPARED BY B. MOHAMED YOUSUF M.C.A.., B.Ed., (PG ASST IN COMPUTER SCIENCE)
CHAPTER - 7 PYTHON FUNCTIONS
1. User defined function def <function_name ([parameter1, parameter2…] )> :
<Block of Statements>
return <expression / None>
2. Passing Parameters def function_name (parameter(s) separated by comma):
3. Variable-Length Arguments def function_name(*args):
function_body
return_statement
4. Anonymous Functions lambda [argument(s)] :expression
5. Return statement return [expression list ]
CHAPTER - 9 LIST,TUPLES, SETS AND DICTIONARY
1. Create a List Variable = [element-1, element-2, element-3 …… element-n]
2. Accessing List elements List_Variable = [E1, E2, E3 …… En]
print (List_Variable[index of a element])
3. Accessing elements using for loop for index_var in list:
print (index_var)
4. Changing list elements List_Variable [index of an element] = Value to be changed
List_Variable [index from : index to] = Values to changed
5. Adding more elements in a list List.append (element to be added)
List.extend ( [elements to be added])
6. Inserting elements in a list List.insert (position index, element)
7. Deleting elements from a list del List [index of an element] # to delete a particular element
del List [index from : index to] # to delete multiple elements
del List # to delete entire list
8. Remove , Pop , Clear () List.remove(element) # to delete a particular element
List.pop(index of an element)
List.clear( )
9. Range ( ) function range (start value, end value, step value)
10 Creating a list (Rang () ) List_Varibale = list ( range ( ) )
11 List comprehensions List = [ expression for variable in range ]
12 Creating Tuples Tuple_Name = ( ) # Empty tuple
Tuple_Name = (E1, E2, E2 ……. En) # Tuple with n number elements
Tuple_Name = E1, E2, E3 ….. En # Elements of a tuple without parenthesis
13 Creating tuples using tuple( ) function Tuple_Name = tuple( [list elements] )
14 Delete Tuple del tuple_name
15 Creating a Set Set_Variable = {E1, E2, E3 …….. En}
16 defining a dictionary: Dictionary_Name = { Key_1: Value_1,
Key_2:Value_2, …….
Key_n:Value_n }
17 Creating a Dictionary # Empty dictionary Dict1 = { }
# Dictionary with Key Dict_Stud = { 'RollNo': '1234',
'Name':'Murali', 'Class':'XII', 'Marks':'451'}
18 Deleting a Dictionary # To delete a particular element. del dictionary_name[key]
# To delete all the elements dictionary_name.clear( )
# To delete an entire dictionary del dictionary_name
CHAPTER - 10 PYTHON CLASSES AND OBJECTS
1 Defining classes class class_name:
statement_1
statement_2
…………..
…………..
statement_n

2
PREPARED BY B. MOHAMED YOUSUF M.C.A.., B.Ed., (PG ASST IN COMPUTER SCIENCE)
2. Creating objects Object_name = class_name( )
3. Accessing Class Members Object_name . class_member
4. Constructor general format def __init__(self, [args ……..]):
__init__ method <statements>
CHAPTER - 12 STRUCTURED QUERY LANGUAGE (SQL)
1. Creating data base 1.To create a database, type the following command in the prompt:
CREATE DATABASE database_name;
For example to create a database to store the tables:
CREATE DATABASE stud;
2. To work with the database, type the following command.
USE DATABASE;
For example to use the stud database created, give the command
USE stud;
2. CREATE TABLE Command CREATE TABLE <table-name>
(<column name><data type>[<size>]
(<column name><data type>[<size>]…… );
3. SQL command CREATE TABLE Student
(Admno integer,
Name char(20),
Gender char(1),
Age integer,
Place char(10), );
4. A table created with constraint CREATE TABLE <table-name>
(<column name><data type>[<size>]<column constraint>,
(<column name><data type>[<size>]<column constraint>……
<table constraint>(<column name>,[<column name>….])…..
);
5. INSERT command INSERT INTO <table-name> [column-list] VALUES (values);
6. DELETE COMMAND DELETE FROM table-name WHERE condition;
7. UPDATE COMMAND UPDATE <table-name> SET column-name = value, column-name =
value,… WHERE condition;
8. ALTER COMMAND ALTER TABLE <table-name> ADD <column-name><data type><size>;
ALTER <table-name> MODIFY<column-name><data type><size>;
ALTER <table-name> RENAME old-column-name TO new-column-name;
ALTER <table-name> DROP COLUMN <column-name>;
9. TRUNCATE command TRUNCATE TABLE table-name;
10 DROP TABLE command DROP TABLE table-name;
11 SELECT command SELECT <column-list>FROM<table-name>;
12 DISTINCT Keyword SELECT DISTINCT Place FROM Student;
13 ALL Keyword SELECT ALL Place FROM Student;
14 SELECT command with SELECT <column-name>[,<column-name>,….] FROM <table-
WHERE Clause name>WHERE condition>;
15 BETWEEN and NOT SELECT Admno, Name, Age, Gender FROM Student WHERE Age
BETWEEN Keywords BETWEEN 18 AND 19;
16 IN Keyword SELECT Admno, Name, Place FROM Student WHERE Place IN
(“Chennai”, “Delhi”);
17 ORDER BY clause SELECT <column-name>[,<column-name>,….] FROM <table-
name>ORDER BY <column1>,<column2>,…ASC| DESC ;
18 WHERE clause SELECT * FROM Student WHERE Age>=18 ORDER BY Name;
19 GROUP BY clause SELECT <column-names> FROM <table-name> GROUP BY
<column-name>HAVING condition];
20 HAVING clause SELECT Gender , count(*) FROM Student GROUP BY Gender
HAVING Place = ‘Chennai’;

3
PREPARED BY B. MOHAMED YOUSUF M.C.A.., B.Ed., (PG ASST IN COMPUTER SCIENCE)
21 COMMIT command COMMIT;
22 ROLLBACK command ROLL BACK TO save point name;
23 SAVEPOINT command SAVEPOINT savepoint_name;
CHAPTER - 13 PYTHON AND CSV FILES
1 csv.reader() csv.reader(fileobject,delimiter,fmtparams)
2. Read A CSV File And Store It list = [] # Start as the empty list
In A List list.append(element) # Use append() to add elements
3. csv.writer() csv.writer(fileobject,delimiter,fmtparams)
CHAPTER -14 IMPORTING C++ PROGRAMS IN PYTHON
1. Execute the Python program Python <filename.py> -i <C++ filename without cpp extension>
2. accessing the functions from <module name> . <function name>
the module
3. Python's OS Module os.system (‘g++ ’ + <variable_name1> ‘ -<mode> ’ +
<variable_name2>
4. getopt.getopt method <opts>,<args>=getopt.getopt(argv, options, [long_options])
5. command for wrapping C++ if __name__=='__main__':
code main(sys.argv[1:])
CHAPTER - 15 DATA MANIPULATION THROUGH SQL
1. Creating a Database using # importing module import sqlite3
SQLit # connecting to the database connection = sqlite3.connect ("Academy.db")
# cursor cursor = connection.cursor()
2. Creating a Table CREATE TABLE Student ( RollnoINTEGER, SnameVARCHAR(20),
GradeCHAR(1), gender CHAR(1), Average float(5.2), birth_date
DATE, PRIMARY KEY (Rollno) );
CHAPTER - 16 DATA VISUALIZATION USING PYPLOT: (LINE,PIE, BAR CHART)
1. Matplotlib using the command import matplotlib.pyplot as plt

4
PREPARED BY B. MOHAMED YOUSUF M.C.A.., B.Ed., (PG ASST IN COMPUTER SCIENCE)

You might also like