Python and SQL Syntax
Python and SQL Syntax
Python Syntax
Chapter 5 | Python Introduction
print()
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” ……)
input()
variable = input (“prompt string”)
Comments in Python
# It is Single line Comment
Output: 55978395
min() Returns the minimum value in a min(list) L = [21, 98]
list. print(min(L))
Output: 21
max() Returns the maximum value in a max(list) L = [21, 98]
list. print(max(L))
Output: 98
sum() Returns the sum of values in a sum(list) L = [21, 98]
list. print(sum(L))
Output: 119
format() Returns a formatted string (e.g., format(value, print(format(14, 'b'))
binary, octal, fixed-point format_spec)
notation). Output: 1110
round() Returns the nearest integer to round(number print(round(17.9))
its input, or a number rounded [,ndigits])
to a specified number of digits. Output: 18
Mathematical Functions using math module
floor() Returns the largest integer less [Link](x) x=26.7
than or equal to x. print([Link](x))
Output: 26
ceil() Returns the smallest integer [Link](x) x=26.7
greater than or equal to x. print([Link](x))
Output: 27
sqrt() Returns the square root of x. [Link](x) b=49
(Note: x must be greater than 0). print([Link](b))
Output: 7.0
format( ) function
(“String to be displayed with {} and {}”.format(var1,var2))
Chapter 13 | CSV
Category Operation / Method Syntax
[Link]() [Link](fileobject,delimiter,fmtparams)
[Link]() [Link](fileobject,delimiter,fmtparams)
Reading CSV
next() method next(reader_object)
# to skip the first row
[Link]() [Link](fileobject,delimiter,fmtparams)
writerow() [Link](row)
Writing CSV
# writes single row of list
(Lists)
writerows() [Link](rows)
# writes multiple rows of list
[Link]() [Link](fileobject, fieldnames, delimiter,
Writing CSV fmtparams)
(Dicts) writeheader() [Link]()
(writes the fieldnames as the first row)
writerow() [Link](dictionary_row)
writerows() [Link](list_of_dictionaries)
Skip Initial Space csv.register_dialect('myDialect',
skipinitialspace=True)
Custom Delimiter csv.register_dialect('myDialect', delimiter = '|’)
Dialects &
Formatting Custom Quote csv.register_dialect('myDialect', quotechar = '"',
quoting = csv.QUOTE_ALL)
Line Terminator csv.register_dialect('myDialect', lineterminator =
'\n')
itemgetter() [Link](col_no)
Data
Operations sorted() list = sorted(list_to_be_sorted, key =
[Link](col_no), reverse=True/False)
Note: skipinitalspace, custom delimiter and custom quote for [Link](), [Link](),
and [Link]() [Link]() are common for using csv.register_dialect().
[Link]()
[Link] (‘g++ ’ + <variable_name1> +‘ -<mode> ’ + <variable_name2>)
[Link]()
<opts>,<args>=[Link](argv, options, [long_options])
Importing matplotlib
import [Link] as plt
To create plots
i) Line chart
[Link] (x_values, y_values)
[Link] (y_values)
# to create a line plot with one value (default taken as y axis)
SQL Syntax
Chapter 12
Creating table
CREATE TABLE <table-name>
(<column name><data type>[<size>]
(<column name><data type>[<size>]……
);
INSERT Command
INSERT INTO <table-name> [column-list] VALUES (values);
DELETE Command
DELETE FROM table-name WHERE condition;
# to delete the rows that satisfy the condition
UPDATE Command
UPDATE <table-name> SET column-name = value, column-name = value,… WHERE
condition;
ALTER Command
ALTER TABLE <table-name> ADD <column-name><data type><size>;
# to add a column
TRUNCATE Command
TRUNCATE TABLE table-name;
DROP Command
DROP TABLE table-name;
# to drop a table
SELECT Command :
SELECT * FROM<table-name>;
# to select all columns from the table
SELECT <column-list>FROM<table-name>;
# to select required columns from the table
ORDER BY Clause
SELECT <column-name>[,<column-name>,….] FROM <table-name>ORDER BY <column1>,
<column2>, …ASC| DESC ;
GROUP BY Clause
SELECT <column-names> FROM <table-name> GROUP BY <column-name> HAVING
condition];
SAVEPOINT Command
SAVEPOINT savepoint_name;
ROLLBACK Command
ROLL BACK TO save point name;
COMMIT Command
COMMIT;
Additional One Marks
(From Points to Remember)
Chapter 2
Chapter 3
● Abstract Data Type (ADT) is a type (or
class) for objects whose behavior is ● Scope refers to the visibility of variables,
parameters and functions in one part of a
program to another part of the same Chapter 4
program.
● The process of binding a variable name ● An algorithm is a finite set of
with an object is called mapping. instructions to accomplish a particular
● Namespaces are containers for mapping task.
names of variables to objects. ● Algorithm consists of step-by-step
● The LEGB rule is used to decide the order instructions that are required to
in which the scopes are to be searched for accomplish a task and helps the
scope resolution. programmer to develop the program.
● Local scope refers to variables defined in ● Program is an expression of algorithm in
the current function. a programming language.
● A variable which is declared outside of all ● Algorithm analysis deals with the
the functions in a program is known as execution or running time of various
global variable. operations involved.
● A function (method) within another ● Space complexity of an algorithm is the
function is called nested function. amount of memory required to run to its
● A variable declared inside a function completion.
containing another function definition ● Big Oh is often used to describe the
creates an enclosed scope, where the worst-case of an algorithm.
inner function can access the outer ● Big Omega is used to describe the lower
function's variable. bound which is the best way to solve the
● Built-in scope has all the names that are space complexity.
pre-loaded into program scope when we ● The Time complexity of an algorithm is
start the compiler or interpreter. given by the number of steps taken by the
● The process of subdividing a computer algorithm to complete the process.
program into separate sub-programs is ● The efficiency of an algorithm is defined
called modular programming. as the number of computational
● Access control is a security technique resources used by the algorithm.
that regulates who or what can view or use ● A way of designing algorithm is called
resources in a computing environment. algorithmic strategy.
● Public members are accessible from ● A space-time or time-memory tradeoff is
outside the class. a way of solving a problem in less time by
● Protected members of a class are using more storage space.
accessible from within the class and are ● Asymptotic Notations are languages that
also available to its sub-classes. use meaningful statements about time
● Private members of a class are denied and space complexity.
access from the outside the class and ● Bubble sort is a simple sorting algorithm
handled only from within. that compares each pair of adjacent
● Python emulates protected and private items and swaps them if they are in the
behavior by prefixing the name with a unsorted order.
single or double underscore. ● The selection sort improves on the bubble
● All members in a Python class are public sort by making only one exchange for
by default. every pass through the list.
● By default, in C++ and Java, all members ● Insertion sort is a simple sorting
are private. algorithm that builds the final sorted array
or list one item at a time and maintains a
sorted sublist.
● Dynamic programming is used when the ● The three types of flow of control are
solutions to a problem can be viewed as Sequencing, Iteration, and branching or
the result of a sequence of decisions. alternative.
● An algorithm that yields ● In Python, branching is done using various
expected output for a valid input forms of 'if' structures.
is called an algorithmic solution. ● Indentation plays a vital role in Python
● A way of designing algorithm programming; it is what groups
is called algorithmic strategy statements without the need to use {}.
● An estimation of the time and ● Python Interpreter will throw error for all
space complexities of an algorithm indentation errors.
for varying input sizes is called ● To accept input at runtime, latest versions
algorithm analysis of Python support input().
● print() supports the use of escape
sequence to format the output to the
user’s choice.
● range() is used to supply a range of values
Chapter 5
in for loop.
● break, continue, and pass act as jump
● Python is a general purpose
statements in Python.
programming language created by Guido
● pass statement is a null statement, it is
Van Rossum.
generally used as a place holder.
● Python shell can be used in two ways, viz.,
interactive mode and Script mode.
● Python uses whitespace (spaces and
tabs) to define program blocks.
● Whitespace separation is necessary
Chapter 7
between tokens, identifiers or keywords.
● Functions are named blocks of code that
● A Program needs to interact with end
are designed to do one specific job.
user to accomplish the desired task, this is
● Types of Functions are User defined,
done using Input-Output facility.
Built-in, lambda and recursion.
● Python breaks each logical line into a
● Function blocks begin with the keyword
sequence of elementary lexical
def followed by function name and
components known as tokens.
parenthesis ().
● Keywords are special words that are used
● A “return” with no arguments is the same
by Python interpreter to recognize the
as return None.
structure of program.
● In Python, statements in a block should
begin with indentation.
● A block within a block is called nested
block.
Chapter 6
● Arguments are used to call a function and
include types like Required, Keyword,
● Programs consist of statements which are
Default, and Variable-length.
executed in sequence, to alter the flow
● Required arguments are the arguments
we use control statements.
passed to a function in correct positional
● A program statement that causes a jump
order.
of control from one part of the program to
another is called control structure or
control statement.
● Keyword arguments will invoke the Chapter 9
function after the parameters are
recognized by their parameter names. ● A list is known as a sequence data type,
● A Python function allows to give default and each value within it is called an
values for parameters in the function element.
definition; we call it as default argument. ● The elements of list should be specified
● Variable-Length arguments are not within square brackets [].
specified in the function’s definition and ● Each element in a list has a unique value
an asterisk (*) is used to define such called index number which begins with
arguments. zero.
● Anonymous Function is a function that is ● Python allows positive and negative
defined without a name. values as index.
● Scope of variable refers to the part of the ● Loops are used to access all elements
program where a variable is accessible or from a list.
can be referred to. ● The for loop is a suitable loop to access
● The value returned by a function used as all the elements of a list one by one.
an argument for another function in a ● The append (), extend () and insert ()
nested manner is called composition. functions are used to include more
● A function which calls itself is known as elements in a List.
recursion. ● The del, remove () and pop () are used to
delete elements from a list.
● The range ( ) function is used to generate
a series of values.
Chapter 8 ● Tuples consist of a number of values
separated by comma and enclosed within
● Strings are immutable, that means once parentheses.
you define string, it cannot be changed ● Iterating tuples is faster than list.
during execution. ● The tuple ( ) function is also used to
● Defining strings within triple quotes also create Tuples from a list.
allows creation of multiline strings. ● Creating a Tuple with one element is
● In a String, Python allocates an index called a singleton tuple.
value for each character which is known as ● A set is a mutable and an unordered
a subscript. collection of elements without
● The subscript can be positive or negative duplicates.
integer numbers. ● A set is created by placing all the elements
● Slice is a substring of a main string. separated by comma within a pair of curly
● Stride is a third argument in a slicing brackets.
operation. ● A dictionary is a mixed collection of
● Escape sequences start with a backslash elements.
and can be interpreted differently.
● The format( ) function used with strings is
a very versatile and powerful function
used for formatting strings. Chapter 10
● The ‘in’ and ‘not in’ operators can be used
with strings to determine whether a string ● Classes and Objects are the key features
is present in another string. of Object Oriented Programming.
● In Python, a class is defined by using the ● Relational Model represents data as
keyword class. relations or tables.
● Variables defined inside a class are called ● Network model is similar to Hierarchical
Class Variables and functions are called model but it allows a record to have more
Methods. than one parent.
● The process of creating object is called as ● ER model consists of entities, attributes
class Instantiation. and relationships.
● Constructor is the special function that is ● Object model stores data as objects,
automatically executed when an object attributes, methods, classes and
of a class is created. inheritance.
● In Python, a special function called init is ● Normalization reduces data redundancy
used as Constructor. and improves data integrity.
● Destructor is a special method that gets ● Database Normalization was proposed by
execution automatically when an object Dr. Edgar F Codd.
exits from the scope. ● Relational Algebra is used for modeling
● In Python, the __del__() method is used as data stored in relational databases and
destructor. for defining queries on it.
● A variable prefixed with double
underscore becomes private in nature.
Chapter 12
“The beautiful thing about learning is that no one can take it away from you."
– B.B. King