Python PDF
Python PDF
Python PDF
Python Overview
Python is a general-purpose, interpreted, high-level programming language which is widely
used nowadays .It is an open source language which was developed by Guido Van Rossum in
the late 1980s .Python Software Foundation(PSF), a non-profit organization, holds the
intellectual property rights of Python.Python was released in 1991 at Centrum Wiskunde &
Informatica (CWI) in the Netherlands as a successor to the ABC language. Rossum named this
language after a popular comedy show called 'Monty Python's Flying Circus' (and not after
Python-the snake).
In the last few years, the popularity of python has increased immensely due to its wide
apllications. According to most of the tech surveys, Python is in the top ten Most Popular
Technologies in 2019.
Currently, PSF supports two versions, Python 2.x & Python 3.x. Python 2.0 was released in
October 2000 and includes a large number of features. PSF continues to support version
Python 2 because a large body of existing code could not be forward ported to Python 3. So,
they will support Python 2 until 2020.
Python 3.0 was released on December 3rd, 2008. It was designed to rectify certain flaws in
earlier version. This version is not completely backward-compatible with previous versions.
However, many of its major features have since been back-ported to the Python 2.6.x and 2.7.x
version series. Releases of Python 3 include 2 to 3 utilities to facilitate the automation of
translation of Python 2 code to Python 3.
● Python provides enhanced readability. For that purpose, uniform indents are used to
delimit blocks of statements instead of curly brackets, like in many languages such as C,
C++ and Java.
● Python is free and distributed as open-source software. A large programming community
is actively involved in the development and support of Python libraries for various
applications such as web frameworks, mathematical computing and data science.
● Python is a cross-platform language. It works equally on different OS platforms like
Windows, Linux, Mac OSX etc. Hence Python applications can be easily ported across
OS platforms.
● Python supports multiple programming paradigms including imperative, procedural,
object-oriented and functional programming styles.
● Python is an extensible language. Additional functionality (other than what is provided in
the core language) can be made available through modules and packages written in
other languages (C, C++, Java etc)
● A standard DB-API for database connectivity has been defined in Python. It can be
enabled using any data source (Oracle, MySQL, SQLite etc.) as a backend to the Python
program for storage, retrieval and processing of data.
● Standard distribution of Python contains the Tkinter GUI toolkit, which is the
implementation of popular GUI library called Tcl/Tk. An attractive GUI can be
constructed using Tkinter. Many other GUI libraries like Qt, GTK, WxWidgets etc. are
also ported to Python.
● Python can be integrated with other popular programming technologies like C, C++,
Java, ActiveX and CORBA.
Python gained a lot of popularity recently because of its Data science libraries. Huge amount of
data is being generated today by web applications, mobile applications and other devices.
Companies need business insights from this data. To analayze this data, the libraries of Python
comes quite handy.
Today Python has become the language of choice for data scientists. Python libraries like
NumPy, Pandas and Matplotlib are extensively used in the process of data analysis, including
the collection, processing and cleansing of data sets, applying mathematical algorithms and
generating visualizations for the benefit of users. Commercial and community Python
distributions by third-parties such as Anaconda and ActiveState provide all the essential libraries
required for data science.
This is another key application area of Python. Python libraries such as Scikit-learn, Tensorflow
and NLTK are widely used for the prediction of trends like customer satisfaction, projected
values of stocks etc. Some of the real-world applications of machine learning include medical
diagnosis,sales prediction, feedback analysis etc.
This is another application area in which Python is becoming very popular. Web application
framework libraries like django, Pyramid, Flask etc. make it very easy to develop and deploy
simple as well as complex web applications. These frameworks are used extensively by various
IT companies. Dropbox for example uses django as a backend to store and, synchronize local
folders.
Most of the web servers today are compatible with WSGI (Web Server Gateway Interface) - a
specification for the universal interface between Python web frameworks and web servers. All
leading web servers such as Apache, IIS, Nginxetc. can now host Python web applications.
Google's App Engine hosts web applications built with almost all Python web frameworks.
The OpenCV library is commonly used for face detection and gesture recognition. OpenCV is a
C++ library, but has been ported to Python. Because of the rapid development of this feature,
Python is a very popular choice from image processing.
Python is a popular choice for game developers. The PyGame library is extensively used for
building games for desktop as well as for mobile platforms. It includes a huge set of computer
graphics and sound libraries. PyGame is a Python wrapper for the SDL library. SDL is a
cross-platform library for accessing computer multimedia hardware components (sound, video,
input, etc.). SDL is an extremely powerful tool for building all kinds of things, but it’s written in C,
and hence in Python, pygame is used.
Python can also be used to develop Android apps. Python's Kivy library has all the
functionalities required for a mobile application.Kivy is an open-source Python library for
developing cross-platform GUI applications. It allows you to write pure-Python graphical
applications that run on the main desktop platforms (Windows, Linux, and macOS) and on iOS
& Android.
Python is extremely useful and widely used for automating CRON (Command Run ON) jobs.
Certain tasks like backups, defined in Python scripts can be scheduled to be invoked
automatically by the operating system scheduler to be executed at predefined times.
1. http://tdc-www.harvard.edu/Python.pdf
2.http://www.seas.upenn.edu/~cis391/Lectures/python-tutorial.pdf
1. Starting a line with a # : If a #is used in the beginning of a line, Python will
consider the rest of the line as a comment.
Example :
Example:
1.3 Identifiers in Python
● Identifier is a name used to reserved memory locations to store values of a
variable, function, class, module or other object.
● An identifier starts with a letter A to Z or a to z or an underscore (_) followed by
zero or more letters, underscores and digits (0 to 9).
● Python does not allow punctuation characters such as @, $, and % within
identifiers.
● Python is case sensitive; hence, Sum and sum are two different identifiers.
● In a class, an identifier with a single leading underscore indicates that the
identifier is private and an identifier with two leading underscores indicates a
strongly private identifier.
● Numbers
● String
● List
● Tuple
● Dictionary
1.5.1 Numbers
● This data type stores numeric values.
● We can create a Number objects by assigning a value to a variable.
Syntax:
Var1 = 10
Count = 0
● The reference to a number object can be deleted by using the del statement.
Syntax:
del Var1,Count
int : It is a whole number, positive or negative, without decimals, of unlimited length.
Example :
X = 20
Y = 12344556677
Z = -201
Example :
X = 20.567
Y = - 12. 5
Z = -12.5e100
Example :
X = 20.567j
Note : String, List, Tuples and Dictionaries are discussed in the later section.
● Arithmetic Operators
● Comparison or Relational Operators
● Assignment Operators
● Logical Operators
● Bitwise Operators
● Membership Operators
● Identity Operators
Arithmetic Operators are used with numeric values to perform common mathematical
operations.
Operators are :
+ Addition
- Subtraction
/ Division
* Multiplication
% Modulus
** Exponentiation
// Floor Division
1.5.1.2 Comparison or Relational Operators
Operators are:
== Equal
!= Not Equal
!= Not Equal
Operators are:
+= adds right operand to the left operand and assign the result to left operand
-= subtracts right operand from the left operand and assign the result to left operand
*= multiply right operand from the left operand and assign the result to left operand
/= divides left operand with the right operand and assign the result to left operand
%= returns the remainder when left operand is divided by the right operand and assigns
the result to the left operand.
//= divides left operand with the right operand and assign the floor value result to left
operand.
**= calculate exponent value using operands and assign the result to the left operand.
>>= performs bitwise right shift on operands and store values on left operand
<<= performs bitwise left shift on operands and store values on left operand
1.5.1.4 Logical Operators
Operators are:
or - returns true if either of the statement is true not reverses the result
Example:
.1.5.1.5 Bitwise Operators
Operators are:
^ bitwise XOR, sets each bit to 1 if only one of the two bits is 1.
Operators are:
not in returns True if it doesn't find a value in a specified sequence & False
otherwise.
1.5.1.7 Identity Operator
Operators are:
is returns true if both the operands are identical , that is, two values are located in the
same part of memory location is not returns true if operands are not identical.
For the detailed table stating the precedence of operators, use the following link.
https://www.programiz.com/python-programming/precedence-associativity
In case of some operators, the precedence is same. When we use two operators with
the same precedence in an expression, associativity helps in determining the order of
their execution. Almost all the operators have left-to-right associativity.
1. Conditional Constructs
Conditional constructs are used to perform different computations or actions depending
on whether the condition evaluates to true or false. The conditions usually uses
comparisons and arithmetic expressions with variables. These expressions are
evaluated to the Boolean values True or False. The statements for the decision taking
are called conditional statements, alternatively known as conditional expressions or
constructs.
● If Statement
● If .. Else Statement
● If .. Elseif .. else statement
● Nested if statement
● Switch .. case statement
1.1.1 If statement
The if statements in Python. It is made up of three main components:
Syntax
if expression:
expr_true_suite/statements
The suite of if clause, (expr_true_suite/statements) will be executed only if the above
conditional expression results in a boolean TRUE value. If the conditional expression
results in a boolean FALSE value no output is obtained or the execution resumes at the
next statement following the suite.
● The colon (:) is significant. It separates the header of the compound statement
from the body.
● The line after the colon must be indented. It is standard in Python to use four
spaces for indenting.
● All lines indented the same amount after the colon will be executed whenever the
BOOLEAN_EXPRESSION is true.
Syntax :
if expression:
statement(s) //if condition evaluates to be true
else:
statement(s) //if condition evaluates to be false
Syntax :
if expression1:
statement(s) //if expression1 evaluates to true
elif expression2:
statement(s) //if expression2 evaluates to true
elif expression3:
statement(s) //if expression3 evaluates to true
else:
statement(s) //if none of the above conditions are true
1.1.4 Nested If statements
In Python one if condition can also be nested within another if condition. Indentation is
the way to figure out the level of nesting.
Syntax :
if condition:
statements
else:
if condition:
statements
else:
statements
This is a Function to convert number into string. Switcher is dictionary data type here
get() method of dictionary data type returns value of passed argument if it is present in
dictionary otherwise second argument will be assigned as default value of passed
argument.
2. Iterative Constructs
A iterative construct is a structure which allows a block of statements to be executed
repeatedly.
The while statement in Python is a conditional looping statement. It is so called the entry
controlled looping. The suite in a while clause will be executed continuously in a loop
until the given condition is no longer satisfied.
Since the test for exit of the loop is performed before the first instruction in the loop,
based on the condition it may not execute for a single time. The variables appearing in
the logical expression are initialized prior to the beginning of the while loop. This is
mandatory as the logical expression in the while loop is tested before the loop is
executed.
Syntax
while expression :
suite_to_repeat
The suite_to_repeat clause of the while loop will be executed continuously in a loop until
expression evaluates to Boolean False.
Output:
Output:
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
Syntax:
suite_to_repeat
With each loop, the iter_var iteration variable is set to the current element of the iterable
(sequence, iterator, or object that supports iteration), presumably for use in
suite_to_repeat.
Output:
Output:
Current Letter: P
Current Letter: y
Current Letter: t
Current Letter: h
Current Letter: o
Current Letter: n
When iterating over a string, the iteration variable will always consists of only single
characters(strings of length 1).
Output:
David Victor
Paulo Victor
Wesley Victor
In the above example, a list is iterated over, and for each iteration, the
eachNamevariablecontains the list that we are on for that partricular iteration of the
loop.
The built in function range() can turn the foreach-like for loop to the traditional type of for
loop.
Syntax:
range() will return a list where for any k, start_value <= k < end_value and k iterates
from start_value to end value in increment of increment_value. Increment value cannot
be 0, or an error will occur.
Output:
Value is: 2
Value is: 5
Value is: 8
Value is: 11
Value is: 14
Value is: 17
If the incremental_value is ommited and only two arguments are given, increment_value
takes a default value of 1.
Output:
Value is: 2
Value is: 3
Value is: 4
Value is: 5
Value is: 6
Output:
Variable value : 7
Variable value : 6
Loop terminated
terminate normally.
Output:
One python statement not found in C is the pass statement. The pass statement does
absolutely nothing. Its a true NOP, i.e No Operation assembly.It is useful in places where the
code will eventually go, but has not been written yet.
This is more helpful during the development or debugging stages because youwant the
structure to be there while the code is being created, but you do not want it to interface with
other parts of the code. In places where you want nothing to be executed,
1. Functions
Functions are construct to structure programs and are useful to utilize code in more
than section in a program. It increases reusability of code and removes redundancy.
Syntax:
def function_name(parameters):
The function body consist of indented statements. To end the function body, the indent
is to be ended. Every time, a function is called the function body is executed. The
parameters in the function definition are optional.
A function may have a return statement which returns a result. Once the return
statement is executed in the function body the function is ended.
Example:
...
Syntax:
>>> function_name(value)
If we want to find the square of 9, then we can call the function like:
>>> square(9)
81
Examples:
1.2.2 Passing Parameters in a function by reference
Here, a reference of actual parameter is passed to the function.
The advantage of it is that whenever the parameter value is modified the change
reflects back in the function call.
Example:
Note: In Python, scalar values are passed by-value. Lists and other objects are passed
by reference.
1.3 Arguments
The values passed in the parameters of a function are called arguments. The
arguments provided in the function definition should match while calling the function. Or
else, it will result in error.
Example:
Default Arguments:
Example:
Here name does not have a default value. So it is going to take the value that is
provided during the function call. But greeting has a default value and hence not needed
to be provided with any value while calling the function.
Note: If a function has default arguments, all the arguments to its right are to provided
as default arguments i.e. no non-default arguments can be present once a default
argument is provided.
Python Keyword Arguments:
When a function is called using some values, the values are assigned to the arguments
according to their position.
Here , in the above function, 'Hi' is assigned to greeting and 'Mark' is assigned to name
when the function greetings is called.
Python allows functions to be called using keyword arguments. When we call functions
using keyword arguments, the order of the parameters can be changed.
It is also possible to have positional arguments with keyword arguments while calling
the function.
However, the positional arguments cannot be placed after the keyword argument.
Variable length Arguments:
Example:
2. Scripts
A script is a simple program, stored in a plain text file.
Syntax:
python
Example:
python testCode.py
Syntax:
3. Modules in Python
A module is a set of code written in a file having extension .py. Module contains function
definitions and other statements. Module allows organize python code logically. We can make a
group of related codes by creating a module. This makes it easier to understand the code and
maintain the code. A module can define variables, functions and classes and runnable code.
Example:
Syntax:
import
Example:
After importing the module, using the module name, we can access the functions or variables
defined in that module.
Example:
Here, the function welcomeUser() defined in the module myModule is called passing “Progoti”
as the argument to print the welcome message
We can use any Python source file as a module by executing an import statement in another
Python source file. When the interpreter gets an import statement, it imports the module if it is
present in the search path. A module is loaded only once regardless of number of times it is
imported.
Syntax :
Example:
To import the function calculateAvg(scores) from the module myModule to find the average of
the scores stored in the list scores,
Note: When importing an attribute of a module using the from keyword, do not use the module
name when referring to elements in the module. For Example , in the above code , we have
used calculateAvg(scores), not mymodule.calculateAvg(scores)
Built in function dir() lists the names of the attributes defined in a module.
Example:
Example:
Each built-in module contains resources for certain system-specific functionalities such as OS
management, disk IO, etc. The standard library also contains many Python scripts (with the .py
extension) containing useful utilities.
4. Packages
● A package is a bundle of Python modules.
● A package is a directory with Python files.
● A Python package can have sub-packages and modules.
● A package must have a file with the name __init__.py.
● It is possible to put several modules into a Package.
● Packages help in structuring files in a hierarchical structure that defines a single
Python application environment
1. Create a directory. Name of the directory will be the name of the package.
2. Create a file named __init__.py inside the directory. Presence of this file in the
directory let Python know that the directory is a package.
3. Create all Python files in this directory, which will be modules or sub modules of
the package. This file is invoked when the package or a module in the package is
imported. This can be used for execution of package initialization code, such as
initialization of package-level data.
4.2 Accessing the Packages:
To access modules or functions from a package we have to use import statement.
Syntax:
Example:
Let us create a package named Books and build three modules in it namely, Novel,
Horror and General_Knowledge.
2. Now create the modules inside this directory by creating three files novel.py,
horror.py and GK.py and define code in these files.
3. Finally, we create the file __init__.py inside Book directory . To make all of your
functions available when you will imported Book, you need to put explicit import
statements in __init__.py as follows :
To use the package Book created, create a file demoPackage.py in the directory where
the directory Book is present.
When we execute this file, the functions defined in the files included in the package are
executed.
In case, the import statements are not included in the __init__.py file , we have to add
them before we invoke the objects defined in the files included in the package.
For example, if we keep the __init__.py file empty for the Book package created and
have to access the printNovels() function from the Book package , we have to define the
script as below:
Note: In the above example, we have taken example of a single function in each file, but
we can have multiple functions in each files, also define different Python classes in
those files and then can create packages of those classes.
1. Sequences
Sequences are the container data-types in Python which stores objects in a specific
order. The objects can be accessed using their position or index. Indexes are numbered
from 0 in Python.
There are six sequence types in Python and we will discuss them one by one below.
1.1.Strings
● A contiguous set of characters represented in the quotation marks is a string.
● We can use either single quotation or double quotation marks.
● Strings in Python are arrays of bytes representing unicode characters.
● Subsets of strings can be taken using the slice operator ([ ] and [:] )
● + is the string concatenation operator.
● * is the repetition operator.
● len() method returns the length of string
● upper() method converts the string to upper case
● lower() method converts the string to lower case
Example:
1.2. Lists
List is a compound data type.
● It contains items separated by commas and enclosed within square brackets ([]).
● Items belonging to a list can be of different data type.
● List is ordered and changeable.
● List allow duplicate data.
● List can be created by using the list constructor list().
● append() object method is used to add an item to the list.
● remove() object method is used to remove a specific item from the list.
● len() method is used to get a count of elements in the list.
Example:
1.3. Tuples
● A tuple is an ordered and unchangeable list or collection.
● In Python tuples are written with round brackets.
● Items in the tuple are separated by commans.
● Can use tuple() constructor to make a tuple.
● len() method returns the number of items in a tuple.
Example:
1.4. Bytes
The bytes object is a sequence of small integers. The elements of a byte object are in
the range 0 through 255, corresponding to ASCII characters. Like tuples, bytes is an
immutable sequence. Bytes can be directly stored in the memory without encoding
which is not in the case of strings. In strings, first we have to encode the data and then
store it. A byte object can be created using the byte() function or using the prefix b.
Example :
1.6 Range
The range() type is an immutable sequence of numbers in Python within a given range.
a. start : the starting integer of the sequence.Thsi si optional. if no start is given, Python
considers the start as 0.
b. stop : the integer before which the sequence of integers are to be considered.
c. step : step is optional parameter. It determines the value by which each integer in the
sequence is to be inserted.
Example :
1. Collections
Python offers two default collections and five high performing collections from
the collection module. We are going to discuss them one by one.
1.1 Dictionaries
Example:
1.2. Sets
● A set is an unordered collection
● It is iterable, mutable and has no duplicate elements.
● Sets are enclosed by curly braces ({ }).
● A set can be created using the set constructor.
● Elements can be added to a set using the add() method.
● A frozen set is an immutable object which can be created using the
frozenset constructor.
Example :
1.3 High Performance collection datatypes
We have already discussed how we can use Python built in collections,
dictionaries and sets to group multiple values together in a collection. Here, we
are going to discuss about the collections module.
To use the classes available in the collections module, we have to import the
module as below.
>>>import collections
● Counter
● Deque
● Defaultdict
● Namedtuple
● Ordereddict
1.3.1 Counter
● Counter is a container included in the collections module of Python.
● It is a subclass of dictionary and is an unordered collection where elements
and their respective count are stored as dictionary.
● It has a method called elements() that returns an iterator over elements
repeating each as many times as its count.
● It has a method called most_common() that returns most common
elements and their counts from the most common to the least.
Counter Initialization
The constructor of counter can be called in any one of the following ways :
Example:
Example:
Example:
Example:
most_common() method return a list of the n most common elements and their
counts from the most common to the least.
If n is omitted , or nor none , it returns all elements in the counter. Elements with
equal counts are ordered arbitrarily.
Example:
In the above code snippet, created a counter object c and used the
most-common() method to print the elements with their counts.
In the above code snippet, printed the first 2 most common elements.
Counter c is:
Counter b is:
1.3.2 Deque
Double Ended Queue or deque is used as the data structure stack. In situations
where there is a requirement of structuring the data in a way that facilitates
quickly appending to either end or retrieving from either end then a deque can be
used.
● append() - used to insert the value in its argument to the right end of
deque.
● appendleft() - used to insert the value in its argument to the left end of
deque.
● clear()- used to remove all elements from the deque leaving it with length 0.
Example:
1.3.3 Defaultdict
defaultdict is a container in the collections class of Python . This container is
similar to the usual dictionary container having only one difference. Unlike
dictionary, defaultdict allows to specify the default type of the value.
Next , in a for loop , using the list.append() operation , attached the value to the
new list. When keys are encountered again, the look-up proceeds normally
(returning the list for that key) and the list.append() operation adds another value
to the list.
1.3.4 Namedtuple
It is a Factory Function for Tuples with Named Fields. Named tuples help to have
meaning of each position in a tuple. Using it, we can increase the readability of
the code as the code becomes self-documenting.
To instantiate, we pass in the name of the type we want to create. Then we pass
in a list of field names.
Example:
Using namedtuple, we can grab data from a csv .
In the below example , we have a .csv file “employee.csv” having values for
employee name and salary.
1.3.5 Ordereddict
An OrderedDict is a dictionary that remembers the order of the keys that were
inserted first. If a new entry overwrites an existing entry, the original insertion
position is left unchanged. When we are iterating over the OrderedDict , the order
will reflect the order in which the keys were added.
As it is a sub class of Dictionary, all the methods of dictionary are applicable for
it.
1. Regular Expressions
Regular expressions are used to check the existence of a pattern in a given
sequence of characters. The pattern to be checked is the regular expression.
Regular expressions are widely used in many applications to validate the user
inputs like email-ids, passwords etc.
Lets discuss some of them which will be very useful while using regular
expressions.
The match function checks the text(sequence) with the pattern and returns a
matched object if they matches and a none when they do not match.
Basic Syntax:
re.match(pattern,sequence,flags=0)
Example:
1.1.2 Search Function
The search function scans for the pattern in the entire sequence and returns a
matched object if the pattern in found in any position in the sequence. If the
pattern is found nowhere in the sequence, then it returns None.
Basic Syntax:
re.search(pattern,sequence,flags=0)
Example:
Note: The difference between search and match is that match matches the entire
pattern with the sequence but search matches the pattern if it is anywhere
present in the sequence
This function substitutes the found pattern with the given pattern and replaces it
with the given new pattern and returns the new sequence. If the given pattern is
not found then it is not replaced and returned as it is.
Basic Syntax:
Example:
1.2 Basic Patterns
Basic Patterns are the easiest to use in Python. Here ordinary characters are
used as regular expressions. The ordinary characters matches themselves and
no other meaning is interpreted. They do not have any special meaning in their
regular expression syntax.
Let us try to use regular expressions to match two patterns and see the output
Special characters are those characters which do not match themselves but have
some other meaning while matched with a sequence.
Below a list of most widely special characters are provided with some examples.
1.3.2. Repetitions
Characte Use
r
+ Checks for the presence of one or more characters as the same
character as its left
{x,y} Repeats at least x number of times but not more than y number of
times.
Let us take an example how to use the last three characters to handle repetition.
Suppose we want to validate the contact number provided. A contact number has
to be of 10 digits so we can validate it as following.
https://docs.python.org/3/howto/regex.html
https://www.tutorialspoint.com/python/python_reg_expressions.htm
https://www.regular-expressions.info/python.html
https://regexone.com/references/python
1. Comprehensions
Comprehensions are constructs that allow to build sequences from other
sequences.
● List Comprehensions
● Dictionary Comprehensions
● Set Comprehensions
1.1.List Comprehensions
Syntax:
Here,
outpt_expr -> Expression producing elements of the output list from members of
the Input Sequence
Predicate expression -> a condition to be satisfied for the output element created.
This is optional.
Example:
We want to create an output list, which contains squares of all the numbers of an
input list.
Our input list may have some non-numeric values also. In that case the
expression used will not work. Hence we can put a condition in the
comprehension to check whether the element in the list is a number or not and if
so the expression will be executed and element for the output element will be
generated.
This time we have used a predicate to check whether the element of the input list
is an integer or not.
Syntax:
Example:
Suppose we have to create a list by taking elements from a list of lists.
Example 1:
Here, names in the input list ,name_length is the dictionary created using
comprehension.
Example 2:
Given two lists containing the names of students and their corresponding scores,
construct a dictionary which maps the student names with their respective
scores.
Here, studentNames list has list of names of students and studentScores list has
scores of the students. Output dictionary ,name_scores is created from
studentNames and studentsScores lists using comprehension .
Example:
○ Alternative to for loop -> Nested for loops makes code confusing and
complex. Use of dictionary comprehensions are better in such
situations and can simplify the readability and your understanding of
the code.
○ Alternative to lamda functions -> Complex ,multiline lamda functions
can be replaced with a single line of code using dictionary
comprehensions.
Syntax:
Where,
Except the variable and input_set, rest of the components are optional.
Example:
In the above example, a new set of names created from the existing set of names
excluding the name Shruti.
1. File Handling
File handling in python plays an essential role to work with files either to write to
a file or to read data from it. With file handling, operating on the data present in
the file becomes very easy.
Syntax:
bufsize: If the value is set to zero (0), no buffering will occur while
A file object allows us to use, access and manipulate all the user accessible files.
Example:
Files can also be created by calling the file object constructor , which is identical
to open().
r – Read mode which is used when the file is only being read.
w – Write mode which is used to edit and write new information to the file.
a – Appending mode, which is used to add new data to the end of the file; that is
new information is automatically amended to the end.
The mode character can be followed by b for binary data such as 'rb' or 'wb'.
A file can be opened for updates by supplying a plus(+) such as 'r+' or 'w+'.
Output:
Opening mode : wb
fileObject.read()
read() - This method returns the entire file as string unless an optional length
parameter is given specifying the maximum number of bytes.
readline() - This method returns the next line of input, including the terminating
newline. This methid optionally accepts a maximum line length,n. If a line length
longer than n is read, the first n bytes are returned. The remaining line data is not
discarded and will be returned on subsequent read operations.
readlines() - This method returns all the input lines as a list of strings. This
method accepts a size parameter that specifies the approximate number of bytes
to read before stopping. The actual number of bytes read may be larger than this
depending on how much data has been buffered.
xreadlines() - This method returns an iterator for reading the file line by line. This
method is only provided for backward compatibility because files can already be
used as iterators.
Syntax
fileObject.write()
writelines() method writes a list of strings to th file. This method does not add
newline characters to the output, so the supplied list of output stringsshould
already be formatted as necessary.
In both write() and writelines() method, the string can contain binary data,
including embedded NULL characters.
Syntax:
list_name.append(item)
The append() method takes a single item and adds it to the end of the list.
The item can be numbers, strings, another list, dictionary etc.
Output:
Syntax:
fileObject.close()
Syntax:
os.rename(current_file_name, new_file_name)
You can use the remove() method to delete files by providing the name of the file
Syntax:
os.remove(file_name)
Syntax:
This compares the files file1 and file2 and returns True if they’re equal, False if
not. By default, files that have identical attributes as returned by os.stat() are
considered to be equal. If the shallow parameter is specified and is False, the
contents of the two files are compared to determine equality.
This compares the contents of the files contained in the list common in the two
directories dir1 and dir2. This inturn returns a tuple containing three lists of
filenames (match, mismatch, errors). match lists the files that are the same in
both directories, mismatch lists the files that don’t match, and errors lists the
files that could not be compared for some reason.The shallow parameter has the
same meaning as for cmp().
This creates a directory comparison object that can be used to perform various
comparison operations on the directories dir1 and dir2. ignore is a list of
filenames to ignore and hide is a list of filenames to hide.
1.8. Sample demo case study
Create and open a text file named Pyth.txt and insert any two lines into it. Make
sure the file is opened in the right mode.Then display the first 10 characters of
the file Pyth.txt. Finally append another line to the same file and display the entire
contents of the file Pyth.txt.(Sample snippet is shown below)
1. Anonymous Functions
1.1 Lambda
Lambda operators or Lambda functions are used to create small functions in
Python. This functions are anonymous and hence, only of one-time use.
A lambda function can have many arguments but only one expression. It cannot
have a return statement. It returns a function object which can be assigned to a
variable.
Basic syntax:
Small one liner python functions can be converted into lambda functions. Let us
take an example to understand it better.
Example:
Now let us see how to convert this function into lambda and use it.
1.2. Map
Maps are also anonymous functions which can be used to perform an operation
on all the input items. The arguments to be provided are a function object and
any number of iterables like list, dictionary etc. The function is then applicable for
each item of the iterable.
Basic Syntax:
Examples:
Let us take a function to find the product of of the elements in a list with itself.
Let us define a normal function first for the requirement.
Neither we can access the elements of the map object with index nor we can use
len() t o find the length of the map object
We can force convert the map output i.e. the map object to list as shown below:
1.3. Filter
Like Map, Filter also takes a function object and a iterable. But here, Filter can
take only one iterable as input not many which is not the case with Map.
The function object returns a boolean value. This object is called for each
element of the iterable and filter return only those elements for which the value of
the object is true.
Basic Syntax:
filter(function object,iterable)
Example:
Here filter returns only those values from list for which the lambda function
returns true.
Here all the records which has a score greater than or equal to 80 are returned by
the filter.
1.4. Reduce
Reduce also takes a function object and an iterable as input. It is useful when we
want to perform s
reduce(function object,iterable)
Example:
If we use reduce to fulfill this requirement it can be done in a very simple way.
1. Exception Handling
Exception handling is used to handle unexpected errors in the programs. Even if
the statement in your code is synatctically correct, it might throw an error at the
time of execution. Hence, in that case, your program will not behave as expected.
These unexpected errors which occur during runtime are called exceptions.
Examples:
4. End user may not understand the problem. There is no clarity for the end user
on the errors, like whether the exception is due to user mistake or application
behaviour.As we have seen, the impact is more on an application when an
exception occurs.
Handling all the run time errors by conveying the user with a proper message and
bringing the application back to its previous state , is called exception handling.
During the Application development, the programmer should know about the
-How to make suspicious code block to communicate with the actions block upon
exception.
To implement Exception handling , we will use try and except blocks in Python:
Let us see some scenarios, which will cause an exception to occur and as
programmers,we would need to take care of necessary measures for handling the
exceptions effectively.
● By validating the input data before performing operations like Date, String,
Numeric validations.
● By validating the data before performing Arithmetic operations
For Ex: Denominator validation
After identification of such suspicious codes, we need to keep the code in try
block so that whenever an exception is raised due to the code in the try block
control moves on to the exception handling block, i.e. except block.
● All resources being used by the application like connection with third party
components like printer, scanner and libraries before application gets
terminated.
● We will print custom message swhich describes the error to the
user.
1.3.1. Syntax
Here is simple syntax of try....except...else block:
#!/usr/bin/python
try:
......................
except Exception1:
except Exception2:
else:
1.3.2 Examples
Example1:
The output of the above code is shown below
In this program, we loop until the user enters an integer that has a valid
reciprocal.
The code that can cause exception is placed inside try block.
Here, we print the name of the exception using ex_info() function inside sys
module and ask the
user to try again. We can see that the values 'a' and '1.3' causes ValueError and '0'
causes ZeroDivisionerror1.
Example2:
The below program describes to read the records from the ProductDetails.txt file,
Which contain
After reading each product it will verify that, the record is if belongs to
Soap/Shampoo/Brush type,
It will Display the record for the searcher else it wont display.
This leads to runtime error, So By handling this error, we will address to the user
saying that
Then user will be alerted with the mistake and check for the file or change the
permission
So in the next run, The user would see the smooth run, without any runtime error.
1.4. Raising Exception
The raise statement is used to explicitly force an expection to occur in the code
for a particular condition. It allows you to throw an expection any time in the
code.Let us take an example to understand it better.Suppose ,if we want to check
if a user input is within a range( e.g. Between 1 and 10). If it is not within the
range, we will raise a value error saying that input is not within range.
https://www.learnpython.org/
Till now, we have designed our programs using functions and loops. This is called procedural way
of programming. There is another way of organizing our code into which is to combine data and
functionalities together and wrap it inside something that is called an object. This is called object
oriented approach of programming. It provides a modular structure of programming as compared
to procedural way of programming. This is very helpful while developing large programs.
Classes and Objects are the main aspects of OOP. Lets discuss them one by one.
1.1. Object
An object is an entity (tangible or intangible) that has well defined structure and behavior. In OOP,
a program is seen as comprising of a collection of objects, that act on each other. Each object has
a distinct role or responsibility.
1.2. Class
A class is a blueprint of an object. This defines a set of attributes that characterizes any object
that is instantiated from this class. An object is a realized version of a class. It can also be defined
as an abstract datatype.
Classes Vs Objects
Class Object
Class is a type/template for similar objects Object is an instance of the class, with each
instance behaving identically
There are many features of OOP. Of that , we consider four of them to be the pillars of Object
Oriented Approach of Programming. They are :
a. Abstraction
b.Encapsulation
c. Inheritance
d.Polymorphism
Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of
an object from the user. Consider your mobile phone, you just need to know what buttons are to
be pressed to send a message or make a call, What happens when you press a button, how your
messages are sent, how your calls are connected is all abstracted away from the user.
Encapsulation is the process of combining data and functions into a single unit called class. In
Encapsulation, the data is not accessed directly; it is accessed through the functions present
inside the class. In simpler words, attributes of the class are kept private and public getter and
setter methods are provided to manipulate these attributes. Thus, encapsulation makes the
concept of data hiding possible.
Abstraction and Encapsulation can be achieved using access specifiers or including files as
modules. These will be discussed elaborately in the property section in Python later.
We define classes using the class keyword, similar to how we define functions using the def
keyword.
class Classname:
statement 1
…................
statement N
Example
Objects can store data using ordinary variables that belong to the object. Variables that belong to
an object or class are referred to as fields. Objects can also have functionality by using functions
that belong t o a class. Such functions are called methods of the class. This terminology is
important because it helps us to differentiate between functions and variables which are
independent and those which belong to a class or object. Collectively, the fields and methods can
be referred to as the attributes of that class.
Fields are of two types - they can belong to each instance/object of the class or
they can belong to the class itself. They are called instance variables and class
variables respectively.
When we define a class method, a variable is needed to be provided at the beginning of the
parameter list. But whenever this method is called, no value is needed to be provided for this
parameter. Python provides it by default. This variable is nothing but the object itself which is
referred using the self keyword in the method.
3. Init Method
There are various methods which have special significance in Python classes. The init method is
one such method. It is used to initialize the object of a class. As soon as an object of a is created
or instantiated, the init method is called automatically. We need not explicitly call the method.
Filename: person.py
Output:
4. Class Variables and Object Variables
The variables (or data fields) of a class or an object are nothing but ordinary variables that are
bound to the name spaces of the classes and objects. This means that these names are valid
within the context of these classes and objects only.
There are two types of data fields- class variables and object variables which are classified
depending on whether the class or the object owns t he variables respectively.
Class variables are shared - they can be accessed by all instances of that class.
There is only one copy of the class variable and when any one object makes a
change to a class variable, that change will be seen by all the other instances.
Object variables are owned by each individual object/instance of the class. In this
case, each object has its own copy of the field i.e. they are not shared and are not
related in any way to the field by the same name in a different instance.
In this example, a person class is declared which has a class variable as count and a class
method is declared to get the count of employees.
There are two other functionalities, one to remove and employee and another to receive a greeting
from any employee. Both these methods are object variables i.e. they will be called by an instance
of that class. Let us look at the implementation below.
Output:
As mentioned above, we can see that the methods to greet and to remove employee are being
called by the objects employee1, employee2 and employee3. So the values returned in the output
are different for every object. But the method get_count() is called bt=y the class and hence
returns the value with respect to the class.
https://pdfs.semanticscholar.org/presentation/6ee1/add06c8938ed02a1b4a5d01515c234051677.pdf
https://www.udemy.com/python-oops-beginners/
https://www.johnny-lin.com/pyintro/ed01/free_pdfs/ch07.pdf
http://pdf.th7.cn/down/files/1411/Mastering%20Object-oriented%20Python.pdf
http://abi.inf.uni-tuebingen.de/Teaching/Old/SS11/BILW/handouts-1/Object%20Oriented%20progra
mming%20in%20Python.pdf
In Python, a database driver called "MySQL Connector/Python" is used to access the MySQL
database server .
We do not need to install any MySQL client library or any Python modules except the standard
library for this driver.
Before using , you need to verify whether the connector has been installed in your system or
not.
Following are the steps you need to perform to verify the installation:
>>> mysql.connector.connect(host='localhost',database='mysql',user='root',password='your
pass')
If the connector is installed in the system, you will get the following output after execution of the
above commands.
1. First, import the mysql.connector and Error objects from the MySQL Connector/Python
package.
a connection object. The connect() function accepts four parameters: host, database, user
and password as shown in the below
code snippet.
If all the details are correctly provided, it returns a connection object. In the above example it is
cnx.
The MySQL Connection constructor initializes the attributes and tries to connect to the server. If
the connection is successfully established, a connection object is returned as shown in the
below code snippet.
4. The fourth and the final step is to close the database connection using the close() method of
the MySQLConnection object.
The following code snippet shows how to handle the connection errors and finally close the
connection.
Creating tables in the MySQL database using the connector:
To create tables in the database, we need to use a handle structure called cursor, which is
available in the connection class.
To create a new table in a MySQL database , we have to follow the below steps:
● First connect to the MySQL database server by creating a new MySQLConnection
object.
● Next initiate a MySQLCursor object from the MySQLConnectionobject.
● Then execute the CREATE statement to create the table.
● Finally, Close the database connection.
cursor = db.cursor()
Make sure you define the name of the database when you create the connection. In the above
example, ‘test’ is the name of the database.
To insert new rows into a MySQL table, we have to follow the below steps:
● First connect to the MySQL database server by creating a new MySQLConnection
object.
● Next initiate a MySQLCursor object from the MySQLConnectionobject.
● Then execute the INSERT statement to insert data into the table.
● Finally close the database connection.
MySQL Connector/Python provides API that allows insertion of one or multiple rows into a table
at a time.
The below code is an example of how to insert data in a table called PERSONS.
#database connection
cursor = connection.cursor()
cursor.execute(insert1)
cursor.execute(insert2)
connection.close()
Insert Data into a Table Using Python variables in a MySQL INSERT query :
Sometimes we need to insert a value stored in python variable into a table’s column.
● We can insert Python variable into the table using the prepared statement and
parameterized query.
● Using a parameterized query we can pass python variables as a query parameter in which
placeholders (%s) is used for parameters.
cursor = db.cursor()
cursor.execute(query, values)
db.commit()
To insert multiple rows into the table, we have to use the executemany() method.
This method takes a query as the first argument and a list of tuples containing the data
as a second argument.
cursor = db.cursor()
cursor.executemany(query, values)
db.commit()
To retrieve data in a MySQL database from Python, we need to follow the below steps:
The following code demonstrates how data can be fetched from database using cursor.
The following code demonstrates how data can be fetched from database using cursor, and
how the exceptions are to be taken care of.
In the code below, the "table does not exist error" is being taken care of.
If there are no errors, the data that being fetched is to be displayed one by one from the cursor.
import mysql.connector
try:
connection =
mysql.connector.connect(host='localhost',database='test',user='root',password='dbms')
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
except Error as e:
if (connection.is_connected()):
connection.close()
cursor.close()
To delete data from a table from a Python program, we need to follow the below steps:
The following code gives a demo on how to remove unwanted data from my database by using
the unquoted %s markers and data in tuples.
Import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
,
passwd="passwd1"
database="test"
mycursor =mydb.cursor()
sql ="DELETE FROM personss WHERE address = 'Mountain 21'"
mycursor.execute(sql)
mydb.commit()