Professional Documents
Culture Documents
Sample 2023-24
Sample 2023-24
About Python
Python is a General Purpose object-oriented programming language, which means that it
can model real-world entities. It is also dynamically-typed because it carries out type-checking at
runtime.
It does so to make sure that the type of construct matches what we expect it to be.
The distinctive feature of Python is that it is an interpreted language.
The Python IDLE (Integrated Development Environment) executes instructions one line
at a time. This also lets us use it as a calculator.
Features
a. Easy- Python is very easy to learn and understand; any beginner can learn Python
easily. When writing code in Python, you need fewer lines of code compared to languages like
Java.
b. Interpreted - It is interpreted(executed) line by line. This makes it easy to test and
debug.
c. Object-Oriented -The Python programming language supports classes and objects and
hence it is object-oriented.
d. Free and Open Source - The language and its source code are available to the public
for free; there is no need to buy a costly license.
e. Portable - Since Python is open-source, you can run it on Windows, Mac, Linux or
any other platform. Your programs will work without any need to change it for every machine.
f. GUI Programming - You can use it to develop a GUI (Graphical User Interface). One
way to do this is through Tkinter.
g. Large Python Library Python provides you with a large standard library.
Applications of Python
Build a website using Python
Develop a game in Python
Perform Computer Vision (Facilities like face-detection and color-detection)
Implement Machine Learning (Give a computer the ability to learn)
Enable Robotics with Python
Perform Web Scraping (Harvest data from websites)
Perform Data Analysis using Python
Automate a web browser
Perform Scripting in Python
Perform Scientific Computing using Python
Build Artificial Intelligence
Tokens
In a passage of text, individual words and punctuation marks are called tokens or lexical
units or lexical elements. The smallest individual unit in a program is known as Tokens. Python
has following tokens:
Keywords
Keywords are the reserved words and have special meaning for python interpreter. Every
keyword is assigned specific work and it can be used only for that purpose.
Identifiers
Are the names given to different parts of program like variables, objects, classes, functions
etc. Identifier forming rules of Python are :
1
SNAKE GAME 2023-24
Is an arbitrarily long sequence of letters and digits
The first character must be letter or underscore
Upper and lower case are different
The digits 0-9 are allowed except for first character
It must not be a keyword
No special characters are allowed other than underscore is allowed.
Space not allowed
Literal
Literals are data items that have a fixed value. Python supports several kinds of literals:
o String Literal
o Numeric Literals
o Boolean Literals
o Special Literals – None
String Literal is a collection of character(s) enclosed in a double or single quotes. It can be
either Single line strings or Multiline Strings
Numeric literals in Python can belong to any of the following numerical types:
Integer Literals: it contain at least one digit and must not contain decimal point. It may
contain (+) or (-) sign.
Floating point Literals: Also known as real literals. Real literals are numbers having
fractional parts.
Complex number Literals: Complex number in python is made up of two floating
Boolean literals in Python is used to represent one of the two Boolean values i.e. True or False
Python has one Special literal, which is None. It indicates absence of value. In other languages
it is knows as NULL.
Operators
Type ofOperators Symbols
Arithmetic +, -, *, /, %, **,//
Logical and,or
Assignment =
2
SNAKE GAME 2023-24
Punctuators are symbols that are used in programming languages to organize sentence
structure, and indicate the rhythm and emphasis of expressions, statements, and program
structure. Common punctuators are: „ “ # $ @ []{}=:;(),.
Data Types
Data Type specifies which type of value a variable can store. type() function is used to
determine a variable's type in Python.
Data Types in Python - Number String Boolean List Tuple Set Dictionary
Number In Python:
It is used to store numeric values
Python has three numeric types:
Integers
Floating point numbers
Complex numbers
(i)Integers
Integers or int are positive or negative numbers with no decimal point. Integers in Python
3 are of unlimited size.
(ii)Floating point numbers
It is a positive or negative real numbers with a decimal point.e.g.
(iii)Complex numbers
Complex numbers are combination of a real and imaginary part.Complex numbers are in
the form ofX+Yj, where X is a real part and Y is imaginary part.
String In Python
A string is a sequence of characters. In python we can create string using single (' ') or
double quotes (" ").Both are same in python.
Boolean In Python
It is used to store two possible values either true or false
List In Python
List are collections of items and each item has its own index value.
Tuple In Python
List and tuple, both are same except,a list is mutable python objects and tuple is
immutable Python objects. Immutable Python objects mean you cannot modify the contents of a
tuple once it is assigned.
Set In Python
It is an unordered collection of unique and immutable (which cannot be modified)items.
Dictionary In Python
It is an unordered collection of items and each item consist of a key and a value.
Expression:
It is a valid combination of operators,literals and variable.
Logical expression :- a or b
3
SNAKE GAME 2023-24
String expression :- c=“comp”+”sc”
Type Conversion:
The process of converting the value of one data type (integer, string, float, etc.) to
anotherdata type is called type conversion.Python has two types of type conversion.
Jump statements are used to transfer the program's control from one location to another.
Means these are used to alter the flow of a loop like - to skip a part of a loop or terminate a loop
1. break
2. continue
3. pass
4
SNAKE GAME 2023-24
About Database
Data:
In simple words data can be facts related to any object in consideration. For example your
name, age, height, weight, etc are some data related to you. A picture,image , file , pdfetc can
also be considered data.
What is a Database?
Database is a systematic collection of data. Databases support storage and manipulation
of data. Databases make data management easy. Let's discuss few examples.
An online telephone directory would definitely use database to store data pertaining to
people, phone numbers, other contact details, etc.
Why do we need database
To manage large chunks of data: if size of data increases into thousands of records, it will
simply create a problem to manage.Database can manage large amount of data.
Ease of updating data: With the database, we can flexibly update the data according to
our convenience. Moreover, multiple people can also edit data at same time.
Security of data: With databases we have security groups and privileges to restrict access.
Data integrity: In databases, we can be assured of accuracy and consistency of data due to
the built in integrity checks and access controls.
● Databases reduce Redundancy: It removes duplication of data because data are kept at
one place and all the application refers to the centrally maintained database.
● Database controls Inconsistency: When two copies of the same data do not agree to each
other, then it is called Inconsistency. By controlling redundancy, the inconsistency is also
controlled.
● Database facilitates Sharing of Data; Data stored in the database can be shared among
several users.
● Database ensures Security: Data are protected against accidental or intentional disclosure
to unauthorized person or unauthorized modification.
● Database maintains Integrity: It enforces certain integrity rules to insure the validity or
correctness of data. For ex. A date can’t be like 31/31/2000.
5
SNAKE GAME 2023-24
What is a Database Management System (DBMS)?
Database Management System (DBMS) is a collection of programs which enables its
users to access database, manipulate data, reporting / representation of data.
It also helps to control access to the database.
Types of DBMS
Let's see how the DBMS family got evolved with the time. Following diagram shows the
evolution of DBMS categories.
There are 4 major types of DBMS.
Hierarchical DBMS
In a Hierarchical database, model data is organized in a tree-like structure. Data is Stored
Hierarchically (top down or bottom up) format. Data is represented using a parent-child
relationship. In Hierarchical DBMS parent may have many children, but children have only one
parent.
Network Model
The network database model allows each child to have multiple parents. It helps you to
address the need to model more complex relationships like as the orders/parts many-to-many
relationship. In this model, entities are organized in a graph which can be accessed through
several paths.
Relational model
Relational DBMS is the most widely used DBMS model because it is one of the easiest.
This model is based on normalizing data in the rows and columns of the tables. Relational model
stored in fixed structures and manipulated using SQL.
Object-Oriented Model
In Object-oriented Model data stored in the form of objects. The structure which is called
classes which display data within it. It defines a database as a collection of objects which stores
both data members values and operations.
What is Relational Model
The relational model represents the database as a collection of relations. A relation is
nothing but a table of values. Every row in the table represents a collection of related data values.
These rows in the table denote a real-world entity or relationship.
The table name and column names are helpful to interpret the meaning of values in each
row. The data are represented as a set of relations. In the relational model, data are stored as
tables. However, the physical storage of the data is independent of the way the data are logically
organized.
DDL commands are used for creating databases and tables. It contains necessary
statements for creating, manipulating, altering and deleting tables.
1. CREATE (create database and table)
2. ALTER (alter table)
3. DROP (delete table)
8
SNAKE GAME 2023-24
4. DELETE (delete data from table)
SQL commands:
Getting listings of databases;
mysql> SHOW DATABASES;
Creating a database-
mysql> CREATE database <databasename>;
mysql> CREATE database myschool;
Deleting a database
mysql> DROP database <databasename>;
mysql> DROP database myschool;
After we have created the database we use the USE statement to change the current
mysql> USE <database name>;
mysql> USE myschool;
Getting listings of tables in database(myschool)
mysql> SHOW TABLES;
INSERT command can also be used to take or derive values from one table and place
them in another by using it with a query. To do this, simply replace the VALUES clause with an
appropriate query as follows:
9
SNAKE GAME 2023-24
Conditions can be set with help of following operators:
Comparison operators are: < ;<= ; = ; != or <> ; >= ; >
Logical operators are:AND ; OR ; NOT
Comparison operator for special value NULL: IS
Selecting rows by using the WHERE clause in the SELECT command
mysql> SELECT * FROM student WHERE class=“4";
Selecting specific columns(Projection) by listing their names
mysql> SELECT first_name, class FROM student;
Update data in Table (UPDATE)
To modify or update entries in the table use the UPDATE command
mysql> UPDATE student SET class=“V" WHERE firstname=“freya";
All columns will be updated with same value
mysql> UPDATE student SET class=“V";
Delete data from Table
Deleting selected rows from a table using the DELETE command
mysql> DELETE FROM student WHERE firstname=“amar";
Eliminating Redundant Data: (with Keyword DISTINCT)
DISTINCT keyword eliminates duplicate rows from the result of a SELECT statement.
mysql> SELECT DISTINCT city FROM Student
mysql> SELECT DISTINCT city FROM Student WHERE class=4
BETWEEN - to access data in specified range
mysql> SELECT * FROM Student WHERE class between 4 and 6;
IN- operator allows us to easily test if the expression is in the list of values.
mysql> SELECT * FROM Student WHERE class in (4,5,6);
Pattern Matching – LIKE Operator
A string pattern can be used in SQL using the following wild card
%Represents a substring in any length
_Represents a single character
A pattern is case sensitive and can be used with LIKE operator.
mysql> SELECT Name, City FROM Student WHERE Class>=8 AND Name LIKE
‘%Kumar%’ ;
Altering Table
The SQL ALTER TABLE command is used to add, delete or modify columns in an
existing table ALTER TABLE command is also used to add and drop various constraints on an
existing table. Syntax
10
SNAKE GAME 2023-24
ALTER TABLE employee ADD (tel_number integer);
ALTER TABLE command to change the DATA TYPE of a column in a table is as follows.
A query result can be orders in ascending (A-Z) or descending (Z-A) order as per any
column. Default is Ascending order.
The HAVING clause is used to restrict the results returned by the GROUP BY clause.
11
SNAKE GAME 2023-24
Aggregate Functions
Name Purpose
SUM() Returns the sum of the given column.
MIN() Returns the minimum value in the given column.
MAX() Returns the maximum value in the given column.
AVG() Returns the Average value of the given column.
COUNT() Returns the total number of values/ records as per given column.
You may use any condition on group, if required. HAVING <condition> clause is used to
apply a condition on a group.
mysql> Select Job, Sum(Pay) from EMP Group By Job HAVING Avg(Pay)>=7000;
mysql> Select Job, Sum(Pay) from EMP Group By Job HAVING Count(*)>=5;
mysql> Select Job, Min(Pay),Max(Pay), Avg(Pay) from EMP Group By Job HAVING
Sum(Pay)>=8000;
It return all possible concatenation of all rows from both table i.e. one row of First table
is joined with all the rows of second table.
Equi-join
The join, in which columns are compared for equality is called Equi-Join. A non-equijoin
specifies condition with non-equality operator. In equi-join we put(*) in the select list therefore
the common column will appear twice in the output.
Natural Join
The JOIN in which only one of the identical columns exists is called Natural Join. It is
similar to Equi-join except that duplicate columns are eliminated in Natural join that would
otherwise appear in Equi-Join.
12