Python Material
Python Material
Easy to use: The code is compiled into byte code and then executed in a virtual machine.
This means that precompiled code is portable between platforms. It supports automatic
garbage collection.
Open Source: There is no need to pay for Python software. Python can be freely downloaded
from www.python.org website. Its source code can be read, modified and can be used in
programs as desired by the programmers.
Platform Independent: Python’s byte code represents a fixed set of instructions that run on
all operating systems and hardware. Using a Python Virtual Machine (PVM) anybody can run
these byte code instructions on any computer system. Hence, python programs are not
dependent on any specific operating system. We can use python on almost all operating
systems like UNIX, Linux, Windows, Macintosh etc. This makes Python an ideal programming
language for any network or internet.
Python is Portable: When a program yields the same result on any computer in the world
then it is called a portable program. Python programs will give the same result since they are
platform independent. Once a python Program is written, it can run on any computer system
using PVM.
Embeddable: We can insert Python programs into a C or C++ or Java Program. Several
applications are already developed in Python which can be integrated into other
programming languages like C, C++, PHP, Java and .NET. It means programmers can use
these applications for their advantage in various software projects.
Support libraries: It provides a vast range of libraries for the various fields such as machine
learning, web developer, and also for the scripting. There are various machine learning
libraries, such as Tensor flow, Pandas, Numpy, Keras, and Pytorch, etc. Django, flask,
pyramids are the popular framework for Python web development.
PYTHON APPLICATIONS
Python is known for its general-purpose nature that makes it applicable in almost every
domain of software development. Python makes its presence in every emerging field. It is the
fastest-growing programming language and can develop any application.
Here, we are specifying application areas where Python can be applied.
Some of the applications are
Web Applications
We can use Python to develop web applications. It provides libraries to handle
internet protocols such as HTML and XML, Email processing, request, One of Python web-
framework named Django is used on Instagram.
Console-based Application
Console-based applications run from the command-line or shell. These applications
are computer program which are used commands to execute. This kind of application
was more popular in the old generation of computers.
Python can develop this kind of application very effectively. It is famous for having REPL,
which means the Read-Eval-Print Loop that makes it the most suitable language for the
command-line applications.
Software Development
Python is useful for the software development process. It works as a support language
and can be used to build control and management, testing, etc.
o SCons is used to build control.
o Buildbot and Apache Gumps are used for automated continuous compilation and
testing.
o Round or Trac for bug tracking and project management. Scientific and Numeric
This is the era of Artificial intelligence where the machine can perform the task the same
as the human. Python language is the most suitable language for Artificial intelligence or
machine learning. It consists of many scientific and mathematical libraries, which makes
easy to solve complex calculations.
Implementing machine learning algorithms require complex mathematical calculation.
Python has many libraries for scientific and numeric such as Numpy, Pandas, Scipy, Scikit-
learn, etc. If you have some basic knowledge of Python, you need to import libraries on the
top of the code. Few popular frameworks of machine libraries are given below.
o SciPy
o Scikit-learn
o NumPy
o Pandas
o Matplotlib
PYTHON IDLE
IDLE (Integrated Development and Learning Environment) is an integrated
development environment (IDE) for Python. The Python installer for Windows contains the
IDLE module by default.
IDLE can be used to execute a single statement just like Python Shell and also to create,
modify, and execute Python scripts.
To start an IDLE interactive shell, search for the IDLE icon in the start menu and double click
on it.
This will open IDLE, where you can write and execute the Python scripts, as shown below.
Steps to Execute
To execute a Python script, create a new file by selecting File -> New File from the
menu.
Enter multiple statements and save the file with extension .py using File -> Save.
Now, press F5 to run the script in the editor window. The IDLE shell will show the
output.
Thus, it is easy to write, test and run Python scripts in IDLE.
IDLE Startup Details
Most readers should be able to use IDLE immediately, as it is a standard
component on Mac OS X and most Linux installations today, and is installed automatically
with standard Python on Windows.
Technically, IDLE is a Python program that uses the standard library‘s tkinter GUI toolkit
(named Tkinter in Python 2.X) to build its windows. This makes IDLE portable —it works the
same on all major desktop platforms—but it also means that we‘ll need to have tkinter
support in our Python to use IDLE.
If you want to retrieve all keywords list in interpreter mode, the command used is
>>> help(‘keywords’)
Python Variables
Variable is a name that is used to refer to memory location. Python variable is
also known as an identifier and used to hold value.
In Python, we don't need to specify the type of variable because Python is a
infer language and smart enough to get variable type.
Variable names can be a group of both the letters and digits, but they have to
begin with a letter or an underscore.
It is recommended to use lowercase letters for the variable name.
Identifier Naming
Variables are the example of identifiers. An Identifier is used to identify the literals used
in the program. The rules to name an identifier are given below.
>>> a="python"
>>> a
'python'
>>> type(a)
<class 'str'>
>>>
>>> a=10
>>> type(a)
<class 'int'>
>>>
Object Identity
In Python, every created object identifies uniquely in Python. Python provides the guaranteed
that no two objects will have the same identifier.
The built-in id() function, is used to identify the object identifier. Consider the following
example.
>>> a=50
>>> b=50
>>> id(a)
140736484377184
>>> id(b)
140736484377184
>>> a=100
>>> id(a)
140736484378784
>>>
We assigned the b and a values are same, a and b both point to the same object. When
we checked by the id() function it returned the same number. We reassign a to 500; then
it referred to the new object identifier.
Variable Names
We have already discussed how to declare the valid variable. Variable names can be
any length can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and
underscore character(_).
Consider the following example of valid variables names.
marks = 80.50
print(name)
print(age)
print(marks)
Multiple Assignment
Python allows us to assign a value to multiple variables in a single statement, which is
also known as multiple assignments.
We can apply multiple assignments in two ways, either by assigning a single value to
multiple variables or assigning multiple values to multiple variables.
Consider the following example.
Assigning single value to multiple variables
Eg:
x=y=z=50
print(x)
print(y)
print(z)
Assigning multiple values to multiple variables:
Eg:
a,b,c=5,10,15
print a
print b
print c
The values will be assigned in the order in which variables appear.
Delete a variable
We can delete the variable using the del keyword. The syntax is given below.
Syntax –
del <variable_name>
In the following example, we create a variable x and assign value to it. We deleted variable x,
and print it, we get the error "variable x is not defined".
The variable x will no longer use in future.
Example -
# Assigning a value to x x =
6
print(x)
# deleting a variable.
del x
print(x)
objects - object to the printed. * indicates that there may be more than one object
file - must be an object with write(string) method. If omitted it, sys.stdout will be
used which prints objects on the screen.
flush - A Boolean, specifying if the output is flushed (True) or buffered (False). Default:
False
Example 1:
print('This sentence is output to the screen')
Output:
KCPG College
Master of Computer Applications
Case 2: print function with string operations (with arguments)
1.Print(‘helloworld’)
Output: helloworld
2.print(‘hello\nworld’)
Output: hello world
3.print(‘hello\tworld’)
Output: hello world
(b) a,b,c=10,20,30
print(‘values are:’,a,b,c)
Output: values are : 10 20 30
>>>print('values are:',10,20,30,sep="--")
Output: values are:--10--20—30
Case 4: print statement with end attribute
The end key of print function will set the string that needs to be appended when
printing is done. By default, the end key is set by newline character (By default, attribute end
=’\n’ in print function). So after finishing printing all the variables, a newline character is
appended.
Hence, we get the output of each print statement in different line. But we will now
overwrite the newline character by any character at the end of the print statement.
Example-1:
print('HAI') # in this statement by default end =’\n’ so it takes new line
print('SRIRAM’) # in this statement by default end =’\n’ so it takes new line
print('KRISHNA') # in this statement by default end =’\n’ so it takes new line
Output:
HAI SRIRAM KRISHNA
Note: when you observe output 1st print statement prints output HAI and immediately takes
new line character and execute 2nd print statement and followed.
Output :
MCA: 1, Portal: 5.33
Total students: 240, Boys: 120
031
3.561E+02
Example 2
(a) a=6
print(‘a value is =%i’ %a)
Output: a value is =6
(b) a=6;b=7;c=8
print('a value is =%i and b=%f and c=%i' %(a,b,c))
Output: a value is =6 and b=7.000000 and c=8
Example:2
name='Joh
n'
salary=100
0
print('hello my name is "{}" and my salary is "{}"'.format(name, salary))
Output: : hello my name is "John" and my salary is "1000"
*********we can also use index values and print the output*********
Example: 3
name='Joh
n'
salary=10
00
print('hello my name is {0} and my salary is {1} ‘.format(name, salary))
Output: hello my name is John and my salary is 1000
OUTPUT:
Enter Student Name RAMA KRISHNA
Branch MCA
Enter College name KCPG
Student Details
Student name is RAMA KRISHNA- MCA, College Name is KCPG
Accept an numeric input from User:
To accept an integer value from a user in Python. We need to convert an input string value into
an integer using a int() function.
Example:
first_number = int(input("Enter first number "))
We need to convert an input string value into an integer using a float() function.
Example:
first_number = float(input("Enter first number "))
Integers, floating point numbers and complex numbers fall under Python numbers
category. They are defined as int, and classes in Python. We can use the function to know
which class a variable or a value belongs to. Similarly, the function is used to check if an
object belongs to a Particular class.
A floating-point number is accurate up to 15 decimal places. Integer and floating points are
separated by decimal points. 1 is an integer, 1.0 is a floating-point number.
Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary
part. Here are some examples.
List
Python Lists are similar to arrays in C.
However, the list can contain data of different types. The items stored in
the list are separated with a comma (,) and enclosed within square
brackets [].
We can use slice [:] operators to access the data of the list.
The concatenation operator (+) and repetition operator (*) works with
the list in the same way as they were working with the strings.
Consider the following example.
Tuple
A tuple is similar to the list in many ways. Like lists, tuples also contain the collection of
the items of different data types.
The items of the tuple are separated with a comma (,) and enclosed in parentheses ().
A tuple is a read-only data structure as we can't modify the size and value of the items
of a tuple.
Dictionary
Dictionary is an unordered set of a key-value pair of items.
It is like an associative array or a hash table where each key stores a specific value.
Key can hold any primitive data type, whereas value is an arbitrary Python object.
The items in the dictionary are separated with the comma (,) and enclosed in the curly
braces {}.
print (d.keys())
print (d.values())
Boolean
Boolean type provides two built-in values, True and False.
These values are used to determine the given statement true or false. It denotes by the
class bool.
True can be represented by any non-zero value or 'T' whereas false can be represented
by the 0 or 'F'. Consider the following example.
Sets
A Python set is the collection of the unordered items. Each element in the set must be
unique, immutable, and the sets remove the duplicate elements.
Sets are mutable which means we can modify it after its creation.
Unlike other collections in Python, there is no index attached to the elements of the set,
i.e., we cannot directly access any element of the set by the index. However, we can
print them all together, or we can get the list of elements by looping through the set
The set is created by using a built-in function set(), or a sequence of elements is
passed in the curly braces and separated by the comma. It can contain various types of
values. Consider the following example.
OUTPUT:
Python Strings
String is sequene of Unicode characters. We can use single quotes or double quotes to
represent strings. Multi line can be denoted using triple quotes “ “ “.
Eg:
s = "This is a string"
print(s)
s = '''A multiline string'''
print(s)
Output:
This is a string
A multiline string
Just like a list and tuple, the slicing operator [ ] can be used with strings. Strings, however, are
immutable.
Type Conversion
The process of converting the value of one data type (integer, string, float, etc.) to
another data type is called type conversion. Python has two types of type conversion.
1. Implicit Type Conversion
2. Explicit Type Conversion
Output:
datatype of num_int: <class 'int'>
datatype of num_flo: <class 'float'>
Value of num_new: 124.23
We add two variables num_int and num_flo, storing the value in num_new.
We will look at the data type of all three objects respectively.
In the output, we can see the data type of num_int is integer whilt the data type of num_flo is
a float
Syntax :
<required_datatype>(expression)
Eg:
num_int = 123
num_str = "456"
num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))
num_sum = num_int + num_str
print("Sum of num_int and num_str:",num_sum)
Output:
Data type of num_int: <class 'int'>
Data type of num_str before Type Casting: <class 'str'>
Data type of num_str after Type Casting: <class 'int'>
Sum of num_int and num_str: 579
Data type of the sum: <class 'int'>
PYTHON OPERATORS
The operator can be defined as a symbol which is responsible for a particular operation
between two operands.
For example, 5 + 6 is an expression where + is an operator that performs arithmetic add
operation on numeric left operand 5 and the right side operand 6 and returns a sum of
two operands as a result.
Python includes the operator module that includes underlying methods for each
operator. For example, the + operator calls the operator.add(a,b) method.
Python includes the following categories of operators:
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Identity Operators
Membership Test Operators
Bitwise Operators
Arithmetic Operators
Arithmetic operators perform the common mathematical operation on the numeric
operands. The arithmetic operators return the type of result depends on the type of
operands, as below.
1. If either operand is a complex number, the result is converted to complex;
2. If either operand is a floating point number, the result is converted to
floating point;
3. If both operands are integers, then the result is an integer and no
conversion is needed.
Example in Python
Operation Operator Function Shell
Addition: Sum of two + operator.add(a,b) >>> x = 5; y = 6
operands >>> x + y
11
>>> import operator
>>> operator.add(5,6) 11
Assignment Operators
The assignment operators are used to assign values to variables. The
following table lists all the Assignment operators in Python:
= >>> x = 5;
>>> x
5
+= operator.iadd(a,b) >>> x = 5
>>> x += 5
10
>>> import operator
>>> x = operator.iadd(5, 5) 10
-= operator.isub(a,b) >>> x = 5
>>> x -= 2 3
>>> import operator
>>> x = operator.isub(5,2)
*= operator.imul(a,b) >>> x = 2
>>> x *= 3
6
>>> import operator
>>> x = operator.imul(2, 3)
/= operator.itruediv(a,b) >>> x = 6
>>> x /= 3 2
>>> import operator
>>> x = operator.itruediv(6, 3)
Comparison Operators
The comparison operators compare two operands and return a boolean
either True or False. The following table lists comparison operators in Python.
Logical Operators
The logical operators are used to combine two boolean expressions. The
logical operations are generally applicable to all objects, and support truth tests,
identity tests, and boolean operations.
It compares the memory location of two objects .Memory location can easily be
traced by using id( ) function.
Bitwise Operators
Bitwise operators perform operations on binary operands. These
operators ae used to perform bit level operations.
For example:
>>> 5 - 7
-2
Operator Description
** The exponent operator is given priority over all the others used in the
expression.
~+- The negation, unary plus, and minus.
* / % // The multiplication, divide, modules, reminder, and floor division.
+- Binary plus, and minus
>> << Left shift. and right shift
& Binary and.
^| Binary xor, and or
<= < > >= Comparison operators (less than, less than equal to, greater than, greater then
equal to).
<> == != Equality operators.
= %= /= //= - Assignment operators
=
+=
*= **=
is is not Identity operators
in not in Membership operators
not or and Logical operators
Example:
When you execute the above program, it produces the following result –
a = 20
b = 10
c = 15
d=5
e=0
e = (a + b) * c / d #( 30 * 15 ) / 5
print ("Value of (a + b) * c / d is ", e)
e = ((a + b) * c) / d # (30 * 15 ) / 5
print ("Value of ((a + b) * c) / d is ", e)
e = a + (b * c) / d; # 20 + (150/5)
print ("Value of a + (b * c) / d is ", e)
Value of (a + b) * c / d is 90
Value of ((a + b) * c) / d is 90
Value of (a + b) * (c / d) is 90
Value of a + (b * c) / d is 50
Associativity of Python Operators
When two operators have the same precedence, associativity helps to determine the
order of operations. Associativity is the order in which an expression is evaluated that has
multiple operators of the same precedence. Almost all the operators have left-to-right
associativity.
For example, multiplication and floor division have the same precedence. Hence, if both of
them are present in an expression, the left one is evaluated first.
# Left-right associtivity # Output: 3
Print(5 * 2 //3)
# shows left-right associativity # Output: 0
Print( 5 * (2//3))
CONTROL STATEMENTS
1, Decision Making Statements
Decision making statements in programming languages decides the direction of flow of
program execution. Decision making statements available in python are:
if statement
if..else statements
nested if statements
if-elif ladder
Short Hand if statement
Short Hand if-else statement
if statement
`if statement is the most simple decision making statement. It is used to decide whether a
certain statement or block of statements will be executed or not i.e if a certain condition is
true then a block of statement is executed otherwise not.
Syntax:
if condition:
# Statements to execute if
# condition is true
Here, condition after evaluation will be either true or false. if statement accepts boolean
values – if the value is true then it will execute the block of statements below it otherwise
not. We can use condition with bracket ‘(‘ ‘)’ also.
As we know, python uses indentation to identify a block. So the block under an if statement
will be identified as shown in the below example:
if condition:
statement1
statement2
Flowchart:-
Output:
I am Not in if
As the condition present in the if statement is false. So, the block below the if statement is
not executed.
if- else
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t. But what if we want to do something else if
the condition is false. Here comes the else statement. We can use the else statement
with if statement to execute a block of code when the condition is false.
Syntax:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Flow Chart:-
Output:
i is greater than 15
i'm in else Block
i'm not in if and not in else Block
The block of code following the else statement is executed as the condition present in the if
statement is false after call the statement which is not in block(without spaces).
nested-if
A nested if is an if statement that is the target of another if statement. Nested if statements
mean an if statement inside another if statement. Yes, Python allows us to nest if
statements within if statements. i.e, we can place an if statement inside another if
statement.
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
Flow chart:-
i = 10
if (i == 10):
# First if statement
if (i < 15):
print ("i is smaller than 15")
# Nested - if statement
# Will only be executed if statement above
# it is true
if (i < 12):
print ("i is smaller than 12 too")
else:
print ("i is greater than 15")
Output:
i is smaller than 15
i is smaller than 12 too
if-elif-else ladder
Here, a user can decide among multiple options. The if statements are executed from
the top down. As soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the ladder is bypassed. If none of the
conditions is true, then the final else statement will be executed.
Syntax:-
if (condition):
statement
elif (condition):
statement
.
.
else:
statement
Flow Chart:-
Example:-
# Python program to illustrate if-elif-else ladder
i = 20
if (i == 10):
print ("i is 10")
elif (i == 15):
print ("i is 15")
elif (i == 20):
print ("i is 20")
else:
print ("i is not present")
Output:
i is 20
Looping Statements
– for
– while
<statement-1>
<statement-n>
• The first line of code in a loop is sometimes called the loop header,which denotes the
number of iterations that the loop performs.
– The colon (:) ends the loop header.
• The loop body comprises the statements in the remaining lines of code, below
theheader.These statements are executed in sequence on each pass through the
loop.
– The statements in the loop body must be indented and aligned in the
samecolumn.
>>> for i in range(4):
print(i)
0
Count-Controlled Loops:
• Loops that count through a range of numbers are also called count-controlled loops.
• To count from an explicit lower bound, the programmer can supply a second integer
expression in the loop header. When two arguments are supplied to range, the count
rangesfrom the first argument to the second argument minus 1
• The only thing in this version to be careful about is the second argument of range, which
should specify an integer greater by 1 than the desired upper bound of the count.
<loop body>
>>>for i in range(5,10):
print(i)
5
6
7
8
9
The values contained in any sequence can be visited by running a for loop , as
follows:
– On each pass through the loop, the variable is bound to or assigned the
next value in the sequence, starting with the first one and ending with the
last one.
>>> name="Surya Lakshmi"
>>> nl=[45,36,644]
>>> nt=(4,22,6,1)
• A variant of Python’s range function expects a third argument that allows you to nicely
skip some numbers.
• The third argument specifies a step value, or the interval between the numbers used in
therange, as shown in the examples that follow:
>>> list(range(1, 6, 1)) # Same as using two
arguments[1, 2, 3, 4, 5]
>>> list(range(1, 6, 2)) # Use every other number
[1, 3, 5]
• Once in a while, a problem calls for counting in the opposite direction, from the upper
bound down to the lower bound.
• a loop displays the count from 10 down to 1 to show how this would be done:
• When the step argument is a negative number, the range function generates a
sequenceof numbers from the first argument down to the second argument plus 1.
• Conditional iteration requires that a condition be tested within the loop to determine
whether the loop should continue. Such a condition is called the loop’s continuation
condition.
– If the continuation condition is false, the loop ends.
– If the continuation condition is true, the statements within the loop are
executedagain.
• Syntax:
while <condition>:
<sequence of statements>
• The while loop is also called an entry-control loop, because its condition is tested at
the top of the loop.
• This implies that the statements within the loop can execute zero or more times.
– This implies that the statements within the loop can execute zero or more times.
• If the loop must run at least once, use a while True loop and delay the examination
ofthe termination condition until it becomes available in the body of the loop.
• Ensure that something occurs in the loop to allow the condition to be checked and
abreak statement to be reached eventually.
•
while True:
else:
print("Error: grade must be between 100 and 0")
OUTPUT
A trial run with just this segment shows the following interaction:
UNIT-II
Lists in Python
Python offers a range of compound data types often referred to as sequences. List is
one of the most frequently used and very versatile data types used in Python.
In Python programming, a list is created by placing all the items (elements) inside square
brackets [], separated by commas. It can have any number of items and they may be of
different types (integer, float, string etc.).
Eg:
# empty list
my_list = []
# list of integers
my_list = [1, 2, 3]
A list can also have another list as an item. This is called a nested list.
# nested list
my_list = ["mouse", [8, 4, 6], ['a']]
Access List Elements
There are various ways in which we can access the elements of a list.
List Index
We can use the index operator [] to access an item in a list. In Python, indices start at 0. So, a
list having 5 elements will have an index from 0 to 4.
Trying to access indexes other than these will raise an Index Error. he index must be an
integer. We can't use float or other types, this will result in Type Error.
Eg:
my_list = ['p', 'r', 'o', 'b', 'e']
print(my_list[0])
# Output: p
print(my_list[2])
# Output: o
print(my_list[4])
# Output: e
# Nested List
n_list = ["Happy", [2, 0, 1, 5]]
# Nested indexing
print(n_list[0][1])
print(n_list[1][3])
my_list = ['p','r','o','g','r','a','m','i','z']
Output
[1, 4, 6, 8]
[1, 3, 5, 7
We can add one item to a list using the append() method or add several items
using extend() method.
# Appending and Extending lists in Python
odd = [1, 3, 5]
odd.append(7)
print(odd)
odd.extend([9, 11, 13])
print(odd)
Output
[1, 3, 5, 7]
[1, 3, 5, 7, 9, 11, 13]
We can also use + operator to combine two lists. This is also called concatenation.
The * operator repeats a list for the given number of times.
# Concatenating and repeating lists
odd = [1, 3, 5]
print(odd + [9, 7, 5])
print(["re"] * 3)
Output
[1, 3, 5, 9, 7, 5]
['re', 're', 're']
Furthermore, we can insert one item at a desired location by using the method insert() or
insert multiple items by squeezing it into an empty slice of a list.
# Demonstration of list insert() method
odd = [1, 9]
odd.insert(1,3)
print(odd)
odd[2:2] = [5, 7]
print(odd)
Output
[1, 3, 9]
[1, 3, 5, 7, 9]
We can delete one or more items from a list using the keyword del. It can even delete the list
entirely.
# Deleting list items
my_list = ['p', 'r', 'o', 'b', 'l', 'e', 'm']
# delete one item
del my_list[2]
print(my_list)
# delete multiple items
del my_list[1:5]
print(my_list)
# delete entire list
del my_list
# Error: List not defined
print(my_list)
Output
['p', 'r', 'b', 'l', 'e', 'm']
['p', 'm']
We can use remove() method to remove the given item or pop() method to remove an item at
the given index.
The pop() method removes and returns the last item if the index is not provided. This helps us
implement lists as stacks (first in, last out data structure).
We can also use the clear() method to empty a list.
my_list = ['p','r','o','b','l','e','m']
my_list.remove('p')
print(my_list)
print(my_list.pop(1))
# Output: 'o'
print(my_list)
# Output: ['r', 'b', 'l', 'e', 'm']
print(my_list.pop())
# Output: 'm'
print(my_list)
# Output: ['r', 'b', 'l', 'e']
my_list.clear()
# Output: []
print(my_list)
Output
['r', 'o', 'b', 'l', 'e', 'm']
o
['r', 'b', 'l', 'e', 'm']
m
['r', 'b', 'l', 'e']
[]
Finally, we can also delete items in a list by assigning an empty list to a slice of elements.
print(my_list.index(8))
print(my_list.count(8))
my_list.sort()
# Output: [0, 1, 3, 4, 6, 8, 8]
print(my_list)
my_list.reverse()
# Output: [8, 8, 6, 4, 3, 1, 0]
print(my_list)
Output
1
2
[0, 1, 3, 4, 6, 8, 8]
[8, 8, 6, 4, 3, 1, 0]
We can test if an item exists in a list or not, using the keyword in.
my_list = ['p', 'r', 'o', 'b', 'l', 'e', 'm']
# Output: True
print('p' in my_list)
# Output: False
print('a' in my_list)
# Output: True
print('c' not in my_list)
Output
True
False
True
Iterating Through a List
Tuples in Python
A tuple in Python is similar to a list. The difference between the two is that we cannot
change the elements of a tuple once it is assigned whereas we can change the elements of a
list.
Creating a Tuple
A tuple is created by placing all the items (elements) inside parentheses (), separated
by commas. The parentheses are optional, however, it is a good practice to use them.
A tuple can have any number of items and they may be of different types (integer, float,
list, string, etc.).
# Empty tuple
my_tuple = ()
print(my_tuple)
# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
Output
()
(1,2,3)
(1, ‘Hello’,3.4)
(‘mouse’,[8,4,6],(1,2,3))
A tuple can also be created without using parentheses. This is known as tuple packing.
my_tuple = 3, 4.6, "dog"
print(my_tuple)
print(a) #3
print(b) # 4.6
print(c) # dog
Output:
(3,4.6,’dog’)
3
4.6
Dog
Creating a tuple with one element is a bit tricky.
Having one element within parentheses is not enough. We will need a trailing comma to
indicate that it is, in fact, a tuple.
my_tuple = ("hello")
print(type(my_tuple)) # <class 'str'>
# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple)) # <class 'tuple'>
Output:
<class ,’str’>
<class ‘tuple’>
<class ‘tuple’>
print(my_tuple[0]) # 'p'
print(my_tuple[5]) # 't'
# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
# nested index
print(n_tuple[0][3]) # 's'
print(n_tuple[1][1]) #4
Output:
P
T
S
4
2. Negative Indexing
Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last item and so on.
# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')
# Output: 't'
print(my_tuple[-1])
# Output:
'p'
print(my_tuple[-6])
Output:
T
P
3. Slicing
We can access a range of items in a tuple by using the slicing operator colon :.
# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')
Output:
(‘r’, ‘o’, ‘g’)
(‘p’, ‘r’)
(‘I’, ‘z’)
(‘p’,’r’,’o’,’g’,’r’,’a’,’m’,’I’,’z’)
Slicing can be best visualized by considering the index to be between the elements as shown
below. So if we want to access a range, we need the index that will slice the portion from the
tuple.
Changing a Tuple
This means that elements of a tuple cannot be changed once they have been assigned. But, if
the element is itself a mutable data type like a list, its nested items can be changed. We can
also assign a tuple to different values (reassignment).
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple)
Output:
(4,2,3, [9,5])
(‘p’,’r’,’o’,’g’,’r’,’a’,’m’,’I’,’z’)
We can use + operator to combine two tuples. This is called concatenation. We can
also repeat the elements in a tuple for a given number of times using the * operator.
Both + and * operations result in a new tuple.
# Concatenation
# Output: (1, 2, 3, 4, 5, 6)
print((1, 2, 3) + (4, 5, 6))
# Repeat
# Output: ('Repeat', 'Repeat', 'Repeat')
print(("Repeat",) * 3)
Output:
(1,2,3,4,5,6)
(‘repeat,’repeat’,’repeat’)
Deleting a Tuple
As discussed above, we cannot change the elements in a tuple. It means that we cannot
delete or remove items from a tuple. Deleting a tuple entirely, however, is possible using the
keyword del.
# Deleting tuples
my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
Methods that add items or remove items are not available with tuple. Only the following two
methods are available. Some examples of Python tuple methods:
print(my_tuple.count('p')) # Output: 2
print(my_tuple.index('l')) # Output: 3
Output
2
3
We can test if an item exists in a tuple or not, using the keyword in.
# Membership test in tuple
my_tuple = ('a', 'p', 'p', 'l', 'e',)
# In operation
print('a' in my_tuple)
print('b' in my_tuple)
# Not in operation
print('g' not in my_tuple)
Output:
True
False
True
2. Iterating Through a Tuple
Strings in Python
A string is a sequence of characters. A character is simply a symbol. For example, the English
language has 26 characters. Computers do not deal with characters, they deal with numbers
(binary). Even though you may see characters on your screen, internally it is stored and
manipulated as a combination of 0s and 1s.
This conversion of character to a number is called encoding, and the reverse process is
decoding. ASCII and Unicode are some of the popular encodings used.
In Python, a string is a sequence of Unicode characters. Unicode was introduced to include
every character in all languages and bring uniformity in encoding.
String in Python
Strings can be created by enclosing characters inside a single quote or double-quotes. Even
triple quotes can be used in Python but generally used to represent multiline strings and
docstrings.
# defining strings in Python
# all of the following are equivalent
my_string = 'Hello'
print(my_string)
my_string = "Hello"
print(my_string)
my_string = '''Hello'''
print(my_string)
Output:
Hello
Hello
Hello
Hello, Welcome to the world of python
#first character
print('str[0] = ', str[0])
#last character
print('str[-1] = ', str[-1])
Output:
str = programiz
str[0] = p
str[-1] = z
str[1:5] = rogr
str[5:-2] = am
If we try to access an index out of the range or use numbers other than an integer, we will get
errors.
# using +
print('str1 + str2 = ', str1 + str2)
# using *
print('str1 * 3 =', str1 * 3)
When we run the above program, we get the following output:
We can iterate through a string using a for loop. Here is an example to count the number of 'l's
in a string.
# Iterating through a string
count = 0
for letter in 'Hello World':
if(letter == 'l'):
count += 1
print(count,'letters found')
When we run the above program, we get the following output:
3 letters found
String Membership Test
We can test if a substring exists within a string or not, using the keyword in.
>>> 'a' in 'program'
True
>>> 'at' not in 'battle'
False
Built-in functions to Work with Python
Various built-in functions that work with sequence work with strings as well. Some of the
commonly used ones are enumerate() and len(). The enumerate() function returns an enumerate
object. It contains the index and value of all the items in the string as pairs. This can be useful
for iteration. Similarly, len() returns the length (number of characters) of the string.
str = 'cold'
# enumerate()
list_enumerate = list(enumerate(str))
print('list(enumerate(str) = ', list_enumerate)
#character count
print('len(str) = ', len(str))
When we run the above program, we get the following output:
Escape Sequence
If we want to print a text like He said, "What's there?", we can neither use single quotes nor
double quotes. This will result in a SyntaxError as the text itself contains both single and double
quotes.
>>> print("He said, "What's there?"")
...
SyntaxError: invalid syntax
>>> print('He said, "What's there?"')
...
SyntaxError: invalid syntax
One way to get around this problem is to use triple quotes. Alternatively, we can use escape
sequences. An escape sequence starts with a backslash and is interpreted differently. If we
use a single quote to represent a string, all the single quotes inside the string must be
escaped. Similar is the case with double quotes. Here is how it can be done to represent the
above text.
>>> print("C:\\Python32\\Lib")
C:\Python32\Lib
Sometimes we may wish to ignore the escape sequences inside a string. To do this we can
place r or R in front of the string. This will imply that it is a raw string and any escape sequence
inside it will be ignored.
>>> print("This is \x61 \ngood example")
This is a
good example
>>> print(r"This is \x61 \ngood example")
This is \x61 \ngood example
The format() Method for Formatting Strings
The format() method that is available with the string object is very versatile and powerful in
formatting strings. Format strings contain curly braces {} as placeholders or replacement
fields which get replaced.
We can use positional arguments or keyword arguments to specify the order.
# default(implicit) order
default_order = "{}, {} and {}".format('John','Bill','Sean')
print('\n--- Default Order ---')
print(default_order)
We can even format strings like the old sprintf() style used in C programming language. We use
the % operator to accomplish this.
>>> x = 12.3456789
>>> print('The value of x is %3.2f' %x)
The value of x is 12.35
>>> print('The value of x is %3.4f' %x)
The value of x is 12.3457
Common Python String Methods
There are numerous methods available with the string object. The format() method that we
mentioned above is one of them. Some of the commonly used methods
are lower(), upper(), join(), split(), find(), replace() etc. Here is a complete list of all the built-in
methods to work with strings in Python.
>>> "PrOgRaMiZ".lower()
'programiz'
>>> "PrOgRaMiZ".upper()
'PROGRAMIZ'
>>> "This will split all words into a list".split()
['This', 'will', 'split', 'all', 'words', 'into', 'a', 'list']
>>> ' '.join(['This', 'will', 'join', 'all', 'words', 'into', 'a', 'string'])
'This will join all words into a string'
>>> 'Happy New Year'.find('ew')
7
>>> 'Happy New Year'.replace('Happy','Brilliant')
'Brilliant New Year'
capitalize() Returns a copy of the string with its >>> mystring = "hello python"
first character capitalized and the >>>
rest lowercased. print(mystring.capitalize())
Hello python
Center(width, [fillchar]) Returns the string centered in a string >>> mystring = "Hello"
of length width. Padding can be done >>> x = mystring.center(12,
using the specified fillchar (the "-")
default padding uses an ASCII >>> print(x)
space). The original string is returned ---Hello----
if width is less than or equal to len(s)
Count(sub, [start], [end]) Returns the number of non- >>> mystr = "Hello Python"
overlapping occurrences of substring >>> print(mystr.count("o"))
(sub) in the range [start, end]. 2
Optional arguments startand end are >>> print(mystr.count("th"))
interpreted as in slice notation. 1
>>> print(mystr.count("l"))
2
>>> print(mystr.count("h"))
1
>>> print(mystr.count("H"))
1
>>> print(mystr.count("hH"))
0
endswith(suffix, [start], [end]) Returns True if the string ends with >>> mystr = "Python"
the specified suffix, otherwise it >>>
returns False. print(mystr.endswith("y"))
False
>>>
print(mystr.endswith("hon"))
True
Expandtabs(tabsize=8) Returns a copy of the string where all >>> mystr = "1\t2\t3"
tab characters are replaced by one or >>> print(mystr)
more spaces, depending on the 123
current column and the given tab >>>
size. print(mystr.expandtabs())
123
>>>
print(mystr.expandtabs(tabsi
ze=15))
12
3
>>>
print(mystr.expandtabs(tabsi
ze=2))
123
Find(sub, [start], [end]) Returns the lowest index in the string >>> mystring = "Python"
where substring sub is found within >>>
the slice s[start:end]. print(mystring.find("P"))
0
>>>
print(mystring.find("on"))
4
Index(sub, [start], [end]) Searches the string for a specified >>> mystr = "HelloPython"
value and returns the position of >>> print(mystr.index("P"))
where it was found 5
>>>
print(mystr.index("hon"))
8
>>> print(mystr.index("o"))
4
lstrip([chars]) Returns a left trim version of the >>> a = " Hello "
string >>> print(a.lstrip(), "!")
Hello
maketrans(x[, y[, z]]) Returns a translation table to be used >>> frm = "SecretCode"
in translations >>> to = "4203040540"
>>> trans_table =
str.maketrans(frm,to)
>>> sec_code = "Secret
Code".translate(trans_table)
>>> print(sec_code)
400304 0540
replace(old, new[,count]) Returns a string where a specified >>> mystr = "Hello Python.
value is replaced with a specified Hello Java. Hello C++."
value >>>
print(mystr.replace("Hello",
"Bye"))
Bye Python. Bye Java. Bye
C++.
>>>
print(mystr.replace("Hello",
"Hell", 2))
Hell Python. Hell Java.
Hello C++.
rfind(sub[, start[,end]]) Searches the string for a specified >>> mystr = "Hello-Python"
value and returns the last position of >>> print(mystr.rfind("P"))
where it was found 6
>>> print(mystr.rfind("-"))
5
>>> print(mystr.rfind("z"))
-1
rindex(sub[, start[,end]]) Searches the string for a specified >>> mystr = "Hello-Python"
value and returns the last position of >>> print(mystr.rindex("P"))
where it was found 6
>>> print(mystr.rindex("-"))
5
>>> print(mystr.rindex("z"))
Traceback (most recent call
last):
File "<pyshell#253>", line
1, in <module>
print(mystr.rindex("z"))
ValueError: substring not
found
rjust(width[,fillchar]) Returns the string right justified in a >>> mystr = "Hello Python"
string of length width. >>> mystr1 = mystr.rjust(20,
"-")
>>> print(mystr1)
--------Hello Python
rpartition(sep) Returns a tuple where the string is >>> mystr = "Hello Python"
parted into three parts >>>
print(mystr.rpartition("."))
('', '', 'Hello Python')
>>> print(mystr.rpartition("
"))
('Hello', ' ', 'Python')
rsplit(sep=None, maxsplit=-1) Splits the string at the specified >>> mystr = "Hello Python"
separator, and returns a list >>> print(mystr.rsplit())
['Hello', 'Python']
>>> mystr = "Hello-Python-
Hello"
>>>
print(mystr.rsplit(sep="-",
maxsplit=1))
['Hello-Python', 'Hello']
rstrip([chars]) Returns a right trim version of the >>> mystr = "Hello Python"
string >>> print(mystr.rstrip(),
"!")
Hello Python !
>>> mystr = "------------
Hello Python-----------"
>>> print(mystr.rstrip(), "-
")
------------Hello Python----
------- -
>>> print(mystr.rstrip(),
"_")
------------Hello Python----
------- _
split(sep=None, maxsplit=-1) Splits the string at the specified >>> mystr = "Hello Python"
separator, and returns a list >>> print(mystr.split())
['Hello', 'Python']
>>> mystr1="Hello,,Python"
>>> print(mystr1.split(","))
['Hello', '', 'Python']
splitlines([keepends]) Splits the string at line breaks and >>> mystr = "Hello:\n\n
returns a list Python\r\nJava\nC++\n"
>>>
print(mystr.splitlines())
['Hello:', '', ' Python',
'Java', 'C++']
>>>
print(mystr.splitlines(keepe
nds=True))
['Hello:\n', '\n', '
Python\r\n', 'Java\n',
'C++\n']
startswith(prefix[,start[, end]]) Returns true if the string starts with >>> mystr = "Hello Python"
the specified value >>>
print(mystr.startswith("P"))
False
>>>
print(mystr.startswith("H"))
True
>>>
print(mystr.startswith("Hell
"))
True
strip([chars]) Returns a trimmed version of the >>> mystr = "
string Hello Python
"
>>> print(mystr.strip(),
"!")
Hello Python !
>>> print(mystr.strip(), "
")
Hello Python
swapcase() Swaps cases, lower case becomes >>> mystr = "Hello PYthon"
upper case and vice versa >>> print(mystr.swapcase())
hELLO python
title() Converts the first character of each >>> mystr = "Hello PYthon"
word to upper case >>> print(mystr.title())
Hello Python
>>> mystr = "HELLO JAVA"
>>> print(mystr.title())
Hello Java
upper() Converts a string into upper case >>> mystr = "hello Python"
>>> print(mystr.upper())
HELLO PYTHON
Dictionaries in Python
Python dictionary is an unordered collection of items. Each item of a dictionary has
a key/value pair. Dictionaries are optimized to retrieve values when the key is known.
Creating Python Dictionary
Creating a dictionary is as simple as placing items inside curly braces {} separated by commas.
An item has a key and a corresponding value that is expressed as a pair (key: value). While the
values can be of any data type and can repeat, keys must be of immutable type
(string, number or tuple with immutable elements) and must be unique.
# empty dictionary
my_dict = {}
# using dict()
my_dict = dict({1:'apple', 2:'ball'})
While indexing is used with other data types to access values, a dictionary uses keys. Keys can
be used either inside square brackets [] or with the get() method. If we use the square
brackets [], KeyError is raised in case a key is not found in the dictionary. On the other hand,
the get() method returns None if the key is not found.
# get vs [] for retrieving elements
my_dict = {'name': 'Jack', 'age': 26}
# Output: Jack
print(my_dict['name'])
# Output: 26
print(my_dict.get('age'))
# KeyError
print(my_dict['address'])
Output
Jack
26
None
Traceback (most recent call last):
File "<string>", line 15, in <module>
print(my_dict['address'])
KeyError: 'address'
# update value
my_dict['age'] = 27
# add item
my_dict['address'] = 'Downtown'
We can remove a particular item in a dictionary by using the pop() method. This method
removes an item with the provided key and returns the value.
The popitem() method can be used to remove and return an arbitrary (key, value) item pair from
the dictionary. All the items can be removed at once, using the clear() method.
We can also use the del keyword to remove individual items or the entire dictionary itself.
# Removing elements from a dictionary
# create a dictionary
squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
# Output: {1: 1, 2: 4, 3: 9}
print(squares)
# Output: {}
print(squares)
# Throws Error
print(squares)
Output
16
{1: 1, 2: 4, 3: 9, 5: 25}
(5, 25)
{1: 1, 2: 4, 3: 9}
{}
Traceback (most recent call last):
File "<string>", line 30, in <module>
print(squares)
NameError: name 'squares' is not defined
# Dictionary Methods
marks = {}.fromkeys(['Math', 'English', 'Science'], 0)
print(squares)
Output
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
This code is equivalent to
squares = {}
for x in range(6):
squares[x] = x*x
print(squares)
Output
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
A dictionary comprehension can optionally contain more for or if statements.
An optional if statement can filter out items to form the new dictionary.
Here are some examples to make a dictionary with only odd items.
print(odd_squares)
Output
{1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
# Output: True
print(1 in squares)
# Output: True
print(2 not in squares)
Built-in functions like all(), any(), len(), cmp(), sorted(), etc. are commonly used with dictionaries to
perform different tasks.
Function Description
all() Return True if all keys of the dictionary are True (or if the dictionary is empty).
any() Return True if any key of the dictionary is true. If the dictionary is empty, return
False.
len() Return the length (the number of items) in the dictionary.
cmp() Compares items of two dictionaries. (Not available in Python 3)
sorted() Return a new sorted list of keys in the dictionary.
Here are some examples that use built-in functions to work with a dictionary.
# Output: False
print(all(squares))
# Output: True
print(any(squares))
# Output: 6
print(len(squares))
# Output: [0, 1, 3, 5, 7, 9]
print(sorted(squares))
Output
False
True
6
[0, 1, 3, 5, 7, 9]
Sets in Python
A set is an unordered collection of items. Every set element is unique (no duplicates) and
must be immutable (cannot be changed). However, a set itself is mutable. We can add or
remove items from it.
Sets can also be used to perform mathematical set operations like union, intersection,
symmetric difference, etc.
Empty curly braces {} will make an empty dictionary in Python. To make a set without any
elements, we use the set() function without any argument.
# Distinguish set and dictionary while creating empty set
# initialize a with {}
a = {}
Sets are mutable. However, since they are unordered, indexing has no meaning. We cannot
access or change an element of a set using indexing or slicing. Set data type does not support
it.
We can add a single element using the add() method, and multiple elements using
the update() method. The update() method can take tuples, lists, strings or other sets as its
argument. In all cases, duplicates are avoided.
# initialize my_set
my_set = {1, 3}
print(my_set)
# add an element
my_set.add(2)
print(my_set)
# Output: {1, 2, 3}
# initialize my_set
my_set = {1, 3, 4, 5, 6}
print(my_set)
# discard an element
# Output: {1, 3, 5, 6}
my_set.discard(4)
print(my_set)
# remove an element
# Output: {1, 3, 5}
my_set.remove(6)
print(my_set)
# discard an element
# not present in my_set
# Output: {1, 3, 5}
my_set.discard(2)
print(my_set)
# remove an element
# not present in my_set
# you will get an error.
# Output: KeyError
my_set.remove(2)
Output
{1, 3, 4, 5, 6}
{1, 3, 5, 6}
{1, 3, 5}
{1, 3, 5}
Similarly, we can remove and return an item using the pop() method. Since set is an unordered
data type, there is no way of determining which item will be popped. It is completely arbitrary.
We can also remove all the items from a set using the clear() method.
print(my_set.pop())
# Output: random element
my_set.pop()
print(my_set)
my_set.clear()
print(my_set)
# Output: set()
print(my_set)
Output
{'H', 'l', 'r', 'W', 'o', 'd', 'e'}
H
{'r', 'W', 'o', 'd', 'e'}
set()
Python Set Operations
Sets can be used to carry out mathematical set operations like union, intersection, difference
and symmetric difference. We can do this with operators or methods.
Let us consider the following two sets for the following operations.
>>> A = {1, 2, 3, 4, 5}
>>> B = {4, 5, 6, 7, 8}
Set Union
# use | operator
print(A | B)
Output
{1, 2, 3, 4, 5, 6, 7, 8}
Try the following examples on Python shell.
Output
{4, 5}
Try the following examples on Python shell.
Set Difference
# use - operator on B
>>> B - A
{8, 6, 7}
# use ^ operator
print(A ^ B)
Output
{1, 2, 3, 6, 7, 8}
Try the following examples on Python shell.
Method Description
add() Adds an element to the set
clear() Removes all elements from the set
copy() Returns a copy of the set
difference() Returns the difference of two or more sets as a new set
difference_update() Removes all elements of another set from this set
discard() Removes an element from the set if it is a member. (Do
nothing if the element is not in set)
intersection() Returns the intersection of two sets as a new set
intersection_update() Updates the set with the intersection of itself and another
isdisjoint() Returns True if two sets have a null intersection
issubset() Returns True if another set contains this set
issuperset() Returns True if this set contains another set
pop() Removes and returns an arbitrary set element. Raises
KeyError if the set is empty
remove() Removes an element from the set. If the element is not a
member, raises a KeyError
symmetric_difference() Returns the symmetric difference of two sets as a new set
symmetric_difference_update() Updates a set with the symmetric difference of itself and
another
union() Returns the union of sets in a new set
update() Updates the set with the union of itself and others
Function Description
all() Returns True if all elements of the set are true (or if the set is empty).
any() Returns True if any element of the set is true. If the set is empty, returns
False.
enumerate() Returns an enumerate object. It contains the index and value for all the items
of the set as a pair.
len() Returns the length (the number of items) in the set.
max() Returns the largest item in the set.
min() Returns the smallest item in the set.
sorted() Returns a new sorted list from elements in the set(does not sort the set
itself).
sum() Returns the sum of all elements in the set.
Python Frozenset
Frozenset is a new class that has the characteristics of a set, but its elements cannot be
changed once assigned. While tuples are immutable lists, frozensets are immutable sets. Sets
being mutable are unhashable, so they can't be used as dictionary keys. On the other hand,
frozensets are hashable and can be used as keys to a dictionary. Frozensets can be created
using the frozenset() function.
This data type supports methods
like copy(), difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference() and union().
Being immutable, it does not have methods that add or remove elements.
# Frozensets
# initialize A and B
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])
Try these examples on Python shell.
>>> A.isdisjoint(B)
False
>>> A.difference(B)
frozenset({1, 2})
>>> A | B
frozenset({1, 2, 3, 4, 5, 6})
>>> A.add(3)
...
AttributeError: 'frozenset' object has no attribute 'add'
Function Arguments
You can call a function by using the following types of formal arguments −
Required arguments
Keyword arguments
Default arguments
Variable-length arguments
Required arguments
Required arguments are the arguments passed to a function in correct positional order. Here,
the number of arguments in the function call should match exactly with the function
definition.
To call the function printme(), you definitely need to pass one argument, otherwise it gives a
syntax error as follows −
# Function definition is here
def printme( str ):
"This prints a passed string into this function"
print str
return;
Keyword arguments
Keyword arguments are related to the function calls. When you use keyword arguments in a
function call, the caller identifies the arguments by the parameter name.
This allows you to skip arguments or place them out of order because the Python interpreter
is able to use the keywords provided to match the values with parameters. You can also
make keyword calls to the printme() function in the following ways −
Default arguments
A default argument is an argument that assumes a default value if a value is not provided in
the function call for that argument. The following example gives an idea on default
arguments, it prints default age if it is not passed −
# Function definition is here
def printinfo( name, age = 35 ):
"This prints a passed info into this function"
print "Name: ", name
print "Age ", age
return;
Variable-length arguments
You may need to process a function for more arguments than you specified while defining
the function. These arguments are called variable-length arguments and are not named in the
function definition, unlike required and default arguments.
Syntax for a function with non-keyword variable arguments is this −
def functionname([formal_args,] *var_args_tuple ):
"function_docstring"
function_suite
return [expression]
An asterisk (*) is placed before the variable name that holds the values of all nonkeyword
variable arguments. This tuple remains empty if no additional arguments are specified during
the function call. Following is a simple example −
# Function definition is here
def printinfo( arg1, *vartuple ):
"This prints a variable passed arguments"
print "Output is: "
print arg1
for var in vartuple:
print var
return;
On the other hand, binary mode returns bytes and this is the mode to be used when dealing
with non-text files like images or executable files.
Mode Description
R Opens a file for reading. (default)
W Opens a file for writing. Creates a new file if it does not exist or truncates the file if
it exists.
X Opens a file for exclusive creation. If the file already exists, the operation fails.
A Opens a file for appending at the end of the file without truncating it. Creates a
new file if it does not exist.
T Opens in text mode. (default)
B Opens in binary mode.
+ Opens a file for updating (reading and writing)
f = open("test.txt") # equivalent to 'r' or 'rt'
f = open("test.txt",'w') # write in text mode
f = open("img.bmp",'r+b') # read and write in binary mode
Unlike other languages, the character a does not imply the number 97 until it is encoded
using ASCII (or other equivalent encodings).
Moreover, the default encoding is platform dependent. In windows, it is cp1252 but utf-8 in Linux.
So, we must not also rely on the default encoding or else our code will behave differently in
different platforms.
Hence, when working with files in text mode, it is highly recommended to specify the encoding
type.
This program will create a new file named test.txt in the current directory if it does not exist. If it
does exist, it is overwritten.
We must include the newline characters ourselves to distinguish the different lines.
Reading Files in Python
To read a file in Python, we must open the file in reading r mode.
There are various methods available for this purpose. We can use the read(size) method to read
in the size number of data. If the size parameter is not specified, it reads and returns up to the
end of the file.
We can read the text.txt file we wrote in the above section in the following way:
>>> f = open("test.txt",'r',encoding = 'utf-8')
>>> f.read(4) # read the first 4 data
'This'
We can read a file line-by-line using a for loop. This is both efficient and fast.
>>> for line in f:
... print(line, end = '')
...
This is my first file
This file
contains three lines
In this program, the lines in the file itself include a newline character \n. So, we use the end
parameter of the print() function to avoid two newlines when printing.
Alternatively, we can use the readline() method to read individual lines of a file. This method
reads a file till the newline, including the newline character.
>>> f.readline()
'This is my first file\n'
>>> f.readline()
'This file\n'
>>> f.readline()
'contains three lines\n'
>>> f.readline()
''
Lastly, the readlines() method returns a list of remaining lines of the entire file. All these reading
methods return empty values when the end of file (EOF) is reached.
>>> f.readlines()
['This is my first file\n', 'This file\n', 'contains three lines\n']
UNIT-III
Python OOPs Concepts
Like other general-purpose programming languages, Python is also an object-oriented
language since its beginning. It allows us to develop applications using an Object-Oriented
approach. In Python, we can easily create and use classes and objects.
An object-oriented paradigm is to design the program using classes and objects. The object is
related to real-word entities such as book, house, pencil, etc. The oops concept focuses on
writing the reusable code. It is a widespread technique to solve the problem by creating
objects.
Major principles of object-oriented programming system are given below.
o Class
o Object
o Method
o Inheritance
o Polymorphism
o Data Abstraction
o Encapsulation
Class
The class can be defined as a collection of objects. It is a logical entity that has some specific
attributes and methods. For example: if you have an employee class, then it should contain an
attribute and method, i.e. an email id, name, age, salary, etc.
class ClassName:
<statement-1>
.
.
<statement-N>
Object
The object is an entity that has state and behavior. It may be any real-world object like the
mouse, keyboard, chair, table, pen, etc.
Everything in Python is an object, and almost everything has attributes and methods. All
functions have a built-in attribute __doc__, which returns the docstring defined in the function
source code.
When we define a class, it needs to create an object to allocate the memory. Consider the
following example.
Example:
class car:
def __init__(self,modelname, year):
self.modelname = modelname
self.year = year
def display(self):
print(self.modelname,self.year)
c1 = car("Toyota", 2016)
c1.display()
Output:
Toyota 2016
In the above example, we have created the class named car, and it has two attributes
modelname and year. We have created a c1 object to access the class attribute. The c1 object
will allocate memory for these values. We will learn more about class and object in the next
tutorial.
Method
The method is a function that is associated with an object. In Python, a method is not unique
to class instances. Any object type can have methods.
Inheritance
Inheritance is the most important aspect of object-oriented programming, which simulates the
real-world concept of inheritance. It specifies that the child object acquires all the properties
and behaviors of the parent object.
By using inheritance, we can create a class which uses all the properties and behavior of
another class. The new class is known as a derived class or child class, and the one whose
properties are acquired is known as a base class or parent class.
It provides the re-usability of the code.
Polymorphism
Polymorphism contains two words "poly" and "morphs". Poly means many, and morph means
shape. By polymorphism, we understand that one task can be performed in different ways. For
example - you have a class animal, and all animals speak. But they speak differently. Here, the
"speak" behavior is polymorphic in a sense and depends on the animal. So, the abstract
"animal" concept does not actually "speak", but specific animals (like dogs and cats) have a
concrete implementation of the action "speak".
Encapsulation
Encapsulation is also an essential aspect of object-oriented programming. It is used to restrict
access to methods and variables. In encapsulation, code and data are wrapped together
within a single unit from being modified by accident.
Data Abstraction
Data abstraction and encapsulation both are often used as synonyms. Both are nearly
synonyms because data abstraction is achieved through encapsulation.
Abstraction is used to hide internal details and show only functionalities. Abstracting
something means to give names to things so that the name captures the core of what a
function or a whole program does.
Object-oriented vs. Procedure-oriented Programming languages
The difference between object-oriented and procedure-oriented programming is given below:
Index Object-oriented Programming Procedural Programming
3. It simulates the real world entity. So real- It doesn't simulate the real world. It
world problems can be easily solved works on step by step instructions
through oops. divided into small parts called
functions.
def display(self):
print("ID: %d \nName: %s" % (self.id, self.name))
# Creating a emp instance of Employee class
emp = Employee()
# Deleting the property of object
del emp.id
# Deleting the object itself
del emp
emp.display()
It will through the Attribute error because we have deleted the object emp.
Python Constructor
A constructor is a special type of method (function) which is used to initialize the instance
members of the class.
In C++ or Java, the constructor has the same name as its class, but it treats constructor
differently in Python. It is used to create an object.
Constructors can be of two types.
1. Parameterized Constructor
2. Non-parameterized Constructor
Constructor definition is executed when we create the object of this class. Constructors also
verify that there are enough resources for the object to perform any start-up task.
Creating the constructor in python
In Python, the method the __init__() simulates the constructor of the class. This method is
called when the class is instantiated. It accepts the self-keyword as a first argument which
allows accessing the attributes or method of the class.
We can pass any number of arguments at the time of creating the class object, depending
upon the __init__() definition. It is mostly used to initialize the class attributes. Every class
must have a constructor, even if it simply relies on the default constructor.
Consider the following example to initialize the Employee class attributes.
Example
class Employee:
def __init__(self, name, id):
self.id = id
self.name = name
def display(self):
print("ID: %d \nName: %s" % (self.id, self.name))
Output:
ID: 101
Name: John
ID: 102
Name: David
Counting the number of objects of a class
The constructor is called automatically when we create the object of the class. Consider the
following example.
Example
class Student:
count = 0
def __init__(self):
Student.count = Student.count + 1
s1=Student()
s2=Student()
s3=Student()
print("The number of students:",Student.count)
Output:
The number of students: 3
Python Non-Parameterized Constructor
The non-parameterized constructor uses when we do not want to manipulate the value or the
constructor that has only self as an argument. Consider the following example.
Example
class Student:
# Constructor - non parameterized
def __init__(self):
print("This is non parametrized constructor")
def show(self,name):
print("Hello",name)
student = Student()
student.show("John")
def display(self):
print(self.roll_num,self.name)
st = Student()
st.display()
Output:
101 Joseph
More than One Constructor in Single class
Let's have a look at another scenario, what happen if we declare the two same constructors in
the class.
Example
class Student:
def __init__(self):
print("The First Constructor")
def __init__(self):
print("The second contructor")
st = Student()
Output:
SN Function Description
Example
class Student:
def __init__(self, name, id, age):
self.name = name
self.id = id
self.age = age
# this will give an error since the attribute age has been deleted
print(s.age)
Output:
John
23
True
AttributeError: 'Student' object has no attribute 'age'
Built-in class attributes
Along with the other attributes, a Python class also contains some built-in class attributes
which provide information about the class.
The built-in class attributes are given in the below table.
SN Attribute Description
1 __dict__ It provides the dictionary containing the information about the class
namespace.
Example
class Student:
def __init__(self,name,id,age):
self.name = name;
self.id = id;
self.age = age
def display_details(self):
print("Name:%s, ID:%d, age:%d"%(self.name,self.id))
s = Student("John",101,22)
print(s.__doc__)
print(s.__dict__)
print(s.__module__)
Output:
None
{'name': 'John', 'id': 101, 'age': 22}
__main__
Python Operator Overloading
Python operators work for built-in classes. But the same operator behaves differently with
different types. For example, the + operator will perform arithmetic addition on two numbers,
merge two lists, or concatenate two strings.
This feature in Python that allows the same operator to have different meaning according to
the context is called operator overloading.
So what happens when we use them with objects of a user-defined class? Let us consider the
following class, which tries to simulate a point in 2-D coordinate system.
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
p1 = Point(1, 2)
p2 = Point(2, 3)
print(p1+p2)
Output
Traceback (most recent call last):
File "<string>", line 9, in <module>
print(p1+p2)
TypeError: unsupported operand type(s) for +: 'Point' and 'Point'
Here, we can see that a TypeError was raised, since Python didn't know how to add
two Point objects together.
However, we can achieve this task in Python through operator overloading. But first, let's get a
notion about special functions.
def __str__(self):
return "({0},{1})".format(self.x,self.y)
Now let's try the print() function again.
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
return "({0}, {1})".format(self.x, self.y)
p1 = Point(2, 3)
print(p1)
Output
(2, 3)
That's better. Turns out, that this same method is invoked when we use the built-in
function str() or format().
>>> str(p1)
'(2,3)'
>>> format(p1)
'(2,3)'
So, when you use str(p1) or format(p1), Python internally calls the p1.__str__() method. Hence the
name, special functions.
Now let's go back to operator overloading.
Overloading the + Operator
To overload the + operator, we will need to implement __add__() function in the class. With great
power comes great responsibility. We can do whatever we like, inside this function. But it is
more sensible to return a Point object of the coordinate sum.
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
return "({0},{1})".format(self.x, self.y)
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
return "({0},{1})".format(self.x, self.y)
p1 = Point(1, 2)
p2 = Point(2, 3)
print(p1+p2)
Output
(3,5)
What actually happens is that, when you use p1 + p2, Python calls p1.__add__(p2) which in turn
is Point.__add__(p1,p2). After this, the addition operation is carried out the way we specified.
Inheritance
Inheritance is an important aspect of the object-oriented paradigm. Inheritance provides code
reusability to the program because we can use an existing class to create a new class instead
of creating it from scratch.
In inheritance, the child class acquires the properties and can access all the data members
and functions defined in the parent class. A child class can also provide its specific
implementation to the functions of the parent class. In this section of the tutorial, we will
discuss inheritance in detail.
In python, a derived class can inherit base class by just mentioning the base in the bracket
after the derived class name. Consider the following syntax to inherit a base class into the
derived class.
Syntax
class derived-class(base class):
<class-suite>
A class can inherit multiple classes by mentioning all of them inside the bracket. Consider the
following syntax.
Syntax
class derive-class(<base class 1>, <base class 2>, ..... <base class n>):
<class - suite>
Example 1
class Animal:
def speak(self):
print("Animal Speaking")
#child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print("dog barking")
d = Dog()
d.bark()
d.speak()
Output:
dog barking
Animal Speaking
Syntax
class class1:
<class-suite>
class class2(class1):
<class suite>
class class3(class2):
<class suite>
.
.
Example
class Animal:
def speak(self):
print("Animal Speaking")
#The child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print("dog barking")
#The child class Dogchild inherits another child class Dog
class DogChild(Dog):
def eat(self):
print("Eating bread...")
d = DogChild()
d.bark()
d.speak()
d.eat()
Output:
dog barking
Animal Speaking
Eating bread...
Python provides us the flexibility to inherit multiple base classes in the child class.
Syntax
class Base1:
<class-suite>
class Base2:
<class-suite>
.
.
.
class BaseN:
<class-suite>
Example
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(d.Summation(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
Output:
30
200
0.5
The issubclass(sub, sup) method is used to check the relationships between the specified
classes. It returns true if the first class is the subclass of the second class, and false
otherwise.
Example
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(issubclass(Derived,Calculation2))
print(issubclass(Calculation1,Calculation2))
Output:
True
False
The isinstance (obj, class) method
The isinstance() method is used to check the relationship between the objects and classes. It
returns true if the first parameter, i.e., obj is the instance of the second parameter, i.e., class.
Example
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(isinstance(d,Derived))
Output:
True
Method Overriding
We can provide some specific implementation of the parent class method in our child class.
When the parent class method is defined in the child class with some specific implementation,
then the concept is called method overriding. We may need to perform method overriding in
the scenario where the different definition of a parent class method is needed in the child
class.
Consider the following example to perform method overriding in python.
Example
class Animal:
def speak(self):
print("speaking")
class Dog(Animal):
def speak(self):
print("Barking")
d = Dog()
d.speak()
Output: Barking
Data abstraction in python
Abstraction is an important aspect of object-oriented programming. In python, we can also
perform data hiding by adding the double underscore (___) as a prefix to the attribute which is
to be hidden. After this, the attribute will not be visible outside of the class through the object.
Consider the following example.
Example
class Employee:
__count = 0;
def __init__(self):
Employee.__count = Employee.__count+1
def display(self):
print("The number of employees",Employee.__count)
emp = Employee()
emp2 = Employee()
try:
print(emp.__count)
finally:
emp.display()
Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint
you get while you are still learning Python:
>>> while True print('Hello world')
File "<stdin>", line 1
while True print('Hello world')
^
SyntaxError: invalid syntax
The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest point
in the line where the error was detected. The error is caused by (or at least detected at) the
token preceding the arrow: in the example, the error is detected at the function print(), since a
colon (':') is missing before it. File name and line number are printed so you know where to
look in case the input came from a script.
Exceptions
First, the try clause (the statement(s) between the try and except keywords) is executed.
If no exception occurs, the except clause is skipped and execution of the try statement
is finished.
If an exception occurs during execution of the try clause, the rest of the clause is
skipped. Then if its type matches the exception named after the except keyword, the
except clause is executed, and then execution continues after the try statement.
If an exception occurs which does not match the exception named in the except clause,
it is passed on to outer try statements; if no handler is found, it is an unhandled
exception and execution stops with a message as shown above.
A try statement may have more than one except clause, to specify handlers for different
exceptions. At most one handler will be executed. Handlers only handle exceptions that occur
in the corresponding try clause, not in other handlers of the same try statement. An except
clause may name multiple exceptions as a parenthesized tuple, for example:
A class in an except clause is compatible with an exception if it is the same class or a base
class thereof (but not the other way around — an except clause listing a derived class is not
compatible with a base class). For example, the following code will print B, C, D in that order:
class B(Exception):
pass
class C(B):
pass
class D(C):
pass
The try … except statement has an optional else clause, which, when present, must follow all
except clauses. It is useful for code that must be executed if the try clause does not raise an
exception. For example:
>>> try:
... raise Exception('spam', 'eggs')
... except Exception as inst:
... print(type(inst)) # the exception instance
... print(inst.args) # arguments stored in .args
... print(inst) # __str__ allows args to be printed directly,
... # but may be overridden in exception subclasses
... x, y = inst.args # unpack args
... print('x =', x)
... print('y =', y)
...
<class 'Exception'>
('spam', 'eggs')
('spam', 'eggs')
x = spam
y = eggs
If an exception has arguments, they are printed as the last part (‘detail’) of the message for
unhandled exceptions.
Exception handlers don’t just handle exceptions if they occur immediately in the try clause, but
also if they occur inside functions that are called (even indirectly) in the try clause. For
example:
>>> def this_fails():
... x = 1/0
...
>>> try:
... this_fails()
... except ZeroDivisionError as err:
... print('Handling run-time error:', err)
...
Handling run-time error: division by zero
Raising Exceptions
The raise statement allows the programmer to force a specified exception to occur. For
example:
Exception Chaining
The raise statement allows an optional from which enables chaining exceptions. For example:
# exc must be exception instance or None.
raise RuntimeError from exc
This can be useful when you are transforming exceptions. For example:
>>> def func():
... raise IOError
...
>>> try:
... func()
... except IOError as exc:
... raise RuntimeError('Failed to open database') from exc
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 2, in func
OSError
The above exception was the direct cause of the following exception:
>>> try:
... open('database.sqlite')
... except OSError:
... raise RuntimeError from None
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
RuntimeError
User-defined Exceptions
Programs may name their own exceptions by creating a new exception class (see Classes for
more about Python classes). Exceptions should typically be derived from the Exception class,
either directly or indirectly.
Exception classes can be defined which do anything any other class can do, but are usually
kept simple, often only offering a number of attributes that allow information about the error
to be extracted by handlers for the exception. When creating a module that can raise several
distinct errors, a common practice is to create a base class for exceptions defined by that
module, and subclass that to create specific exception classes for different error conditions:
class Error(Exception):
"""Base class for exceptions in this module."""
pass
class InputError(Error):
"""Exception raised for errors in the input.
Attributes:
expression -- input expression in which the error occurred
message -- explanation of the error
"""
class TransitionError(Error):
"""Raised when an operation attempts a state transition that's not
allowed.
Attributes:
previous -- state at beginning of transition
next -- attempted new state
message -- explanation of why the specific transition is not allowed
"""
Most exceptions are defined with names that end in “Error”, similar to the naming of the
standard exceptions.
Many standard modules define their own exceptions to report errors that may occur in
functions they define. More information on classes is presented in chapter Classes.
Defining Clean-up Actions
The try statement has another optional clause which is intended to define clean-up actions
that must be executed under all circumstances. For example:
>>> try:
... raise KeyboardInterrupt
... finally:
... print('Goodbye, world!')
...
Goodbye, world!
KeyboardInterrupt
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
If a finally clause is present, the finally clause will execute as the last task before
the try statement completes. The finally clause runs whether or not the try statement
produces an exception. The following points discuss more complex cases when an exception
occurs:
If an exception occurs during execution of the try clause, the exception may be handled
by an except clause. If the exception is not handled by an except clause, the exception
is re-raised after the finally clause has been executed.
An exception could occur during execution of an except or else clause. Again, the
exception is re-raised after the finally clause has been executed.
If the finally clause executes a break, continue or return statement, exceptions are not
re-raised.
If the try statement reaches a break, continue or return statement, the finally clause will
execute just prior to the break, continue or return statement’s execution.
If a finally clause includes a return statement, the returned value will be the one from
the finally clause’s return statement, not the value from
the try clause’s return statement.
For example:
After the statement is executed, the file f is always closed, even if a problem was encountered
while processing the lines. Objects which, like files, provide predefined clean-up actions will
indicate this in their documentation.
Python Modules
A python module can be defined as a python program file which contains a python code
including python functions, class, or variables. In other words, we can say that our python
code file saved with the extension (.py) is treated as the module. We may have a runnable
code inside the python module.
Modules in Python provides us the flexibility to organize the code in a logical way. To use the
functionality of one module into another, we must have to import the specific module.
Example
In this example, we will create a module named as file.py which contains a function func that
contains a code to print some message on the console. Balcony
Let's create the module named as file.py.
#displayMsg prints a message to the name being passed.
def displayMsg(name)
print("Hi "+name);
Here, we need to include this module into our main module to call the method displayMsg()
defined in the module named file.
calculation.py:
Main.py:
import calculation;
#it will import only the summation() from calculation.py
a = int(input("Enter the first number"))
b = int(input("Enter the second number"))
print("Sum = ",summation(a,b)) #we do not need to specify the module name while accessing su
mmation()
Output:
Example
import json
List = dir(json)
print(List)
Output:
['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__cached__', '__doc__',
'__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__',
'_default_decoder', '_default_encoder', 'decoder', 'dump', 'dumps', 'encoder', 'load', 'loads',
'scanner']
Python packages
The packages in python facilitate the developer with the application development environment
by providing a hierarchical directory structure where a package contains sub-packages,
modules, and sub-modules. The packages are used to categorize the application level code
efficiently.
Let's create a package named Employees in your home directory. Consider the following steps.
1. Create a directory with name Employees on path /home.
2. Create a python source file with name ITEmployees.py on the path /home/Employees.
ITEmployees.py
def getITNames():
List = ["John", "David", "Nick", "Martin"]
return List;
3. Similarly, create one more python file with name BPOEmployees.py and create a function
getBPONames().
4. Now, the directory Employees which we have created in the first step contains two python
modules. To make this directory a package, we need to include one more file here, that is
__init__.py which contains the import statements of the modules defined in this directory.
__init__.py
from ITEmployees import getITNames
from BPOEmployees import getBPONames
5. Now, the directory Employees has become the package containing two python modules.
Here we must notice that we must have to create __init__.py inside a directory to convert this
directory to a package.
6. To use the modules defined inside the package Employees, we must have to import this in
our python source file. Let's create a simple python source file at our home directory (/home)
which uses the modules defined in this package.
Test.py
import Employees
print(Employees.getNames())
Output:
['John', 'David', 'Nick', 'Martin']
We can have sub-packages inside the packages. We can nest the packages up to any level
depending upon the application requirements.
The following image shows the directory structure of an application Library management
system which contains three sub-packages as Admin, Librarian, and Student. The sub-
packages contain the python modules.
Namespaces in Python
In python we deal with variables, functions, libraries and modules etc. There is a chance the
name of the variable you are going to use is already existing as name of another variable or as
the name of another function or another method. In such scenario, we need to learn about
how all these names are managed by a python program. This is the concept of namespace.
Following are the three categories of namespace
Local Namespace: All the names of the functions and variables declared by a program
are held in this namespace. This namespace exists as long as the program runs. < /p>
Global Namespace: This namespace holds all the names of functions and other
variables that are included in the modules being used in the python program. It
encompasses all the names that are part of the Local namespace.
Built-in Namespace: This is the highest level of namespace which is available with
default names available as part of the python interpreter that is loaded as the
programing environment. It encompasses Global Namespace which in turn
encompasses the local namespace.
Scope in Python
The namespace has a lifetime when it is available. That is also called the scope. Also the
scope will depend on the coding region where the variable or object is located. You can see in
the below program how the variables declared in an inner loop are available to the outer loop
but not vice-versa. Also please note how the name of the outer function also becomes part of
a global variable.
Example
prog_var = 'Hello'
def outer_func():
outer_var = 'x'
def inner_func():
inner_var = 'y'
print(dir(), ' Local Variable in Inner function')
inner_func()
print(dir(), 'Local variables in outer function')
outer_func()
print(dir(), 'Global variables ')
Running the above code gives us the following result –
Output
['inner_var'] Local Variable in Inner function
['inner_func', 'outer_var'] Local variables in outer function
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__',
'__package__'
Recursion in Python
What is recursion?
Recursion is the process of defining something in terms of itself. A physical world example
would be to place two parallel mirrors facing each other. Any object in between them would be
reflected recursively.
Python Recursive Function
In Python, we know that a function can call other functions. It is even possible for the function
to call itself. These types of construct are termed as recursive functions.
The following image shows the working of a recursive function called recurse.
Disadvantages of Recursion
1. Sometimes the logic behind recursion is hard to follow through.
2. Recursive calls are expensive (inefficient) as they take up a lot of memory and time.
3. Recursive functions are hard to debug.
4.
Recursion vs Iteration
Since Python does not store anything about previous iteration steps, iteration is quite
faster and memory-efficient than recursion. In practice, almost all iterations can be performed
by recursions and vice-versa. Some tasks can be executed by recursion simpler than iteration
due to repeatedly calling the same function. On the other hand, some tasks can be executed
by iteration in an elegant way rather than recursion. In terms of time complexity and memory
constraints, iteration is preferred over recursion. Both recursion and ‘while’ loops in iteration
may result in the dangerous infinite calls situation. If the limiting criteria are not met, a while
loop or a recursive function will never converge and lead to a break in program execution.
Since recursion is executed by defining a function, this function can be called whenever
required anywhere in the program. Iterative codes must be constructed at the place
requirement. Nevertheless, an iterative code set can be generalized by declaring inside a
typical Python function (not a recursive function).
The following examples will give a better understanding of recursive and iterative
programming approaches.
Factorial of an Integer
Calculating factorial is a popular use case to understand iteration and recursion. For instance,
we wish to calculate the factorial of 10. It can be determined as 1*2*3*4*5*6*7*8*9*10 =
3628800. This can be viewed as 10 subproblems of multiplying an incrementing integer to a
final result.
# using a for loop
n = 10
result = 1
for i in range(1,n+1):
result *= i
print(result)
A range function is implemented in a ‘for’ loop since it requires a sequence to iterate over.
Range function supplies values iteratively from 1 through 10, one at a time. It stops iteration
when the range function stops supplying values(i.e., at 10).
In a ‘while’ loop, an iterator i is introduced and incremented through every loop. While loop
stops iterating when the value of i exceeds the integer 10.
# using recursion
def Factorial(n):
# declare a base case (a limiting criteria)
if n == 1:
return 1
# continue with general case
else:
return n * Factorial(n-1)
print(Factorial(10))
A recursive function, named Factorial(), is defined with the limiting criteria of n=1. It first
attempts to find the factorial of 10. Factorial(10) is broken down into 10 * Factorial(9). Further,
Factorial(9) is broken down into 9 * Factorial(8), and so on. When Factorial(1) is called, it
stops the recursion.
UNIT-IV
Python Tkinter
Python provides the standard library Tkinter for creating the graphical user interface for
desktop based applications. Developing desktop based applications with python Tkinter is not
a complex task. An empty Tkinter top-level window can be created by using the following
steps.
1. import the Tkinter module.
2. Create the main application window.
3. Add the widgets like labels, buttons, frames, etc. to the window.
4. Call the main event loop so that the actions can take place on the user's computer
screen.
Example:
# !/usr/bin/python3
from tkinter import *
#creating the application main window.
top = Tk()
#Entering the event main loop
top.mainloop()
Output:
Tkinter widgets
There are various widgets like button, canvas, check button, entry, etc. that are used to build
the python GUI applications.
SN Widget Description
1 Button The Button is used to add various kinds of buttons to the python
application.
2 Canvas The canvas widget is used to draw the canvas on the window.
4 Entry The entry widget is used to display the single-line text field to the
user. It is commonly used to accept user values.
7 ListBox The ListBox widget is used to display a list of options to the user.
8 Menubutton The Menubutton is used to display the menu items to the user.
10 Message The Message widget is used to display the message-box to the user.
13 Scrollbar It provides the scrollbar to the user so that the user can scroll the
window up and down.
Output:
Output:
Example
# !/usr/bin/python3
from tkinter import *
top = Tk()
top.geometry("400x250")
name = Label(top, text = "Name").place(x = 30,y = 50)
email = Label(top, text = "Email").place(x = 30, y = 90)
password = Label(top, text = "Password").place(x = 30, y = 130)
e1 = Entry(top).place(x = 80, y = 50)
e2 = Entry(top).place(x = 80, y = 90)
e3 = Entry(top).place(x = 95, y = 130)
top.mainloop()
Output:
As an example, let's assign an event handler to a mouse click event on a label that will print a
message when activated:
from tkinter import *
window = Tk()
window.mainloop()
The above code will create a window with a single label with the text "click me", when any of
the mouse buttons are clicked (left click, right click or middle mouse button click) the
mouseClick() function will be called and will print "mouse clicked" to the console.
Event types
There are several event types available with Tkinter, including:
KeyPress: Activated when a keyboard button has been pressed, the Key event can also be
used for this.
KeyRelease : Activated when a keyboard button is released.
Button : Activated when a mouse button has been clicked.
ButtonRelease : Activated when a mouse button has been released.
Motion : Activated when the mouse cursor moves across the designated widget.
Enter : Activated when the mouse cursor enters the designated widget.
Leave : Activated when the mouse cursor leaves the designated widget.
MouseWheel : Activated when the mouse wheel is scrolled.
FocusIn : Activated when the designated widget gains focus through user input such as the
mouse clicking on it.
FocusOut : Activated when the designated widget loses focus.
Configure : Activated when the designated widget's configurations have changes such as its
width being adjusted by the user or its border being adjusted.
Event modifiers
An event modifier can alter the circumstances in which an event's handler is activated, for
example, some modifiers will require another button to be depressed while the event occurs.
Control : Requires that the control button is being pressed while the event is occurring.
Alt : Requires that the alt button is being pressed while the event is occurring.
Shift : Requires that the shift button is being pressed while the event is occurring.
Lock : Requires that caps lock is activated when the event occurs.
Double : Requires that the given event happens twice in quick succession (such as a double
click)
Triple : Requires that the given event happens three times in quick succession
Quadruple : Requires that the given event happens four times in quick succession
As an example, let's create an event handler that only activates on a double click:
Event details
The detail section of the event string allows us to specify a more specific event such as only a
certain key on the keyboard being pressed or only a certain mouse being being pressed.
When using Button or ButtonRelease
We can give a numeric detail from 1 to 5
Which represents the specific mouse button you wish to have the handler trigger from.
When using KeyPress or KeyRelease
We can give the ASCII value of the specific key we wish to trigger the event.
As an example, let's create an event handler that only activates on the double click of the left
mouse button.
As an example, let's create an event handler that prints the x and y coordinates of a mouse
click event to the console.
def mouseClick( event ):
print( "mouse clicked at x=" + event.x + " y=" + event.y )
label.bind( "<Button>", mouseClick )
output:
"mouse clicked at x=45 y=23"
Python GUI
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the
GUI methods, tkinter is the most commonly used method. It is a standard Python interface
to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way
to create the GUI applications. Creating a GUI using tkinter is an easy task.
To create a tkinter app:
1. Importing the module – tkinter
2. Create the main window (container)
3. Add any number of widgets to the main window
4. Apply the event Trigger on the widgets.
Importing tkinter is same as importing any other module in the Python code. Note that the
name of the module in Python 2.x is ‘Tkinter’ and in Python 3.x it is ‘tkinter’.
import tkinter
There are two main methods used which the user needs to remember while creating the
Python application with GUI.
1. Tk(screenName=None, baseName=None, className=’Tk’, useTk=1): To create a main
window, tkinter offers a method ‘Tk(screenName=None, baseName=None,
className=’Tk’, useTk=1)’. To change the name of the window, you can change the
className to the desired one. The basic code used to create the main window of the
application is:
1. mainloop(): There is a method known by the name mainloop() is used when your
application is ready to run. mainloop() is an infinite loop used to run the application, wait
for an event to occur and process the event as long as the window is not closed.
m.mainloop()
import tkinter
m = tkinter.Tk()
'''
widgets are added here
'''
m.mainloop()
tkinter also offers access to the geometric configuration of the widgets which can organize
the widgets in the parent windows. There are mainly three geometry manager classes class.
1. pack() method:It organizes the widgets in blocks before placing in the parent widget.
2. grid() method:It organizes the widgets in grid (table-like structure) before placing in the
parent widget.
3. place() method:It organizes the widgets by placing them on specific positions directed by
the programmer.
There are a number of widgets which you can put in your tkinter application. Some of the
major widgets are explained below:
Button:To add a button in your application, this widget is used.
The general syntax is:
w=Button(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the Buttons. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
activebackground: to set the background color when button is under the cursor.
activeforeground: to set the foreground color when button is under the cursor.
bg: to set he normal background color.
command: to call a function.
font: to set the font on the button label.
image: to set the image on the button.
width: to set the width of the button.
height: to set the height of the button.
import tkinter as tk
r = tk.Tk()
r.title('Counting Seconds')
button = tk.Button(r, text='Stop', width=25, command=r.destroy)
button.pack()
r.mainloop()
Canvas: It is used to draw pictures and other complex layout like graphics, text and widgets.
The general syntax is:
w = Canvas(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
bd: to set the border width in pixels.
bg: to set the normal background color.
cursor: to set the cursor used in the canvas.
highlightcolor: to set the color shown in the focus highlight.
width: to set the width of the widget.
height: to set the height of the widget.
Entry:It is used to input the single line text entry from the user.. For multi-line text input, Text
widget is used.
The general syntax is:
w=Entry(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
bd: to set the border width in pixels.
bg: to set the normal background color.
cursor: to set the cursor used.
command: to call a function.
highlightcolor: to set the color shown in the focus highlight.
width: to set the width of the button.
height: to set the height of the button.
Frame: It acts as a container to hold the widgets. It is used for grouping and organizing the
widgets. The general syntax is:
w = Frame(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
highlightcolor: To set the color of the focus highlight when widget has to be focused.
bd: to set the border width in pixels.
bg: to set the normal background color.
cursor: to set the cursor used.
width: to set the width of the widget.
height: to set the height of the widget.
root = Tk()
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
redbutton = Button(frame, text = 'Red', fg ='red')
redbutton.pack( side = LEFT)
greenbutton = Button(frame, text = 'Brown', fg='brown')
greenbutton.pack( side = LEFT )
bluebutton = Button(frame, text ='Blue', fg ='blue')
bluebutton.pack( side = LEFT )
blackbutton = Button(bottomframe, text ='Black', fg ='black')
blackbutton.pack( side = BOTTOM)
root.mainloop()
Label: It refers to the display box where you can put any text or image which can be updated
any time as per the code.
The general syntax is:
w=Label(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
bg: to set he normal background color.
bg to set he normal background color.
command: to call a function.
font: to set the font on the button label.
image: to set the image on the button.
width: to set the width of the button.
height” to set the height of the button.
Listbox: It offers a list to the user from which the user can accept any number of options.
The general syntax is:
w = Listbox(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
highlightcolor: To set the color of the focus highlight when widget has to be focused.
bg: to set he normal background color.
bd: to set the border width in pixels.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
top = Tk()
Lb = Listbox(top)
Lb.insert(1, 'Python')
Lb.insert(2, 'Java')
Lb.insert(3, 'C++')
Lb.insert(4, 'Any other')
Lb.pack()
top.mainloop()
Text: To edit a multi-line text and format the way it has to be displayed.
The general syntax is:
w =Text(master, option=value)
There are number of options which are used to change the format of the text. Number of
options can be passed as parameters separated by commas. Some of them are listed below.
highlightcolor: To set the color of the focus highlight when widget has to be focused.
insertbackground: To set the background of the widget.
bg: to set he normal background color.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
Python API
Python API tutorial, we’ll learn how to retrieve data for data science projects. There are
millions of APIs online which provide access to data. Websites like Reddit, Twitter,
and Facebook all offer certain data through their APIs.
To use an API, you make a request to a remote web server, and retrieve the data you need.
But why use an API instead of a static CSV dataset you can download from the web? APIs are
useful in the following cases:
The data is changing quickly. An example of this is stock price data. It doesn’t really
make sense to regenerate a dataset and download it every minute — this will take a lot
of bandwidth, and be pretty slow.
You want a small piece of a much larger set of data. Reddit comments are one
example. What if you want to just pull your own comments on Reddit? It doesn’t make
much sense to download the entire Reddit database, then filter just your own comments.
There is repeated computation involved. Spotify has an API that can tell you the genre
of a piece of music. You could theoretically create your own classifier, and use it to
compute music categories, but you’ll never have as much data as Spotify does.
What is an API?
An API, or Application Programming Interface, is a server that you can use to retrieve and send
data to using code. APIs are most commonly used to retrieve data, and that will be the focus
of this beginner tutorial.
When we want to receive data from an API, we need to make a request. Requests are used all
over the web. For instance, when you visited this blog post, your web browser made a request
to the Dataquest web server, which responded with the content of this web page.
API requests work in exactly the same way – you make a request to an API server for data,
and it responds to your request.
Making API Requests in Python
In order to work with APIs in Python, we need tools that will make those requests. In Python,
the most common library for making requests and working with APIs is the requests library.
The requests library isn’t part of the standard Python library, so you’ll need to install it to get
started.
If you use pip to manage your Python packages, you can install requests using the following
command:
pip install requests
If you use conda, the command you’ll need is:
conda install requests
Once you’ve installed the library, you’ll need to import it. Let’s start with that important step:
import requests
Now that we’ve installed and imported the requests library, let’s start using it.
To make a ‘GET’ request, we’ll use the requests.get() function, which requires one argument
— the URL we want to make the request to. We’ll start by making a request to an API endpoint
that doesn’t exist, so we can see what that response code looks like.
response = requests.get("https://api.open-notify.org/this-api-doesnt-exist")
The get() function returns a response object. We can use the response.status_code attribute to
receive the status code for our request:
print(response.status_code)
404
The ‘404’ status code might be familiar to you — it’s the status code that a server returns if it
can’t find the file we requested. In this case, we asked for this-api-doesnt-exist which (surprise,
surprise) didn’t exist!
200: Everything went okay, and the result has been returned (if any).
301: The server is redirecting you to a different endpoint. This can happen when a
company switches domain names, or an endpoint name is changed.
400: The server thinks you made a bad request. This can happen when you don’t send
along the right data, among other things.
401: The server thinks you’re not authenticated. Many APIs require login ccredentials, so
this happens when you don’t send the right credentials to access an API.
403: The resource you’re trying to access is forbidden: you don’t have the right
permissions to see it.
404: The resource you tried to access wasn’t found on the server.
A Regular Expression (RegEx) is a sequence of characters that defines a search pattern. For
example,
^a...s$
The above code defines a RegEx pattern. The pattern is: any five letter string starting
with a and ending with s.
A pattern defined using RegEx can be used to match against a string.
Expression String Matched?
abs No match
alias Match
^a...s$ abyss Match
Alias No match
An abacus No match
Python has a module named re to work with RegEx. Here's an example:
import re
pattern = '^a...s$'
test_string = 'abyss'
result = re.match(pattern, test_string)
if result:
print("Search successful.")
else:
print("Search unsuccessful.")
Here, we used re.match() function to search pattern within the test_string. The method returns a
match object if the search is successful. If not, it returns None.
There are other several functions defined in the re module to work with RegEx. Before we
explore that, let's learn about regular expressions themselves.
MetaCharacters
Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here's
a list of metacharacters:
[] . ^ $ * + ? {} () \ |
[] - Square brackets
Period
A period matches any single character (except newline '\n').
Expression String Matched?
.. a No match
ac 1 match
acd 1 match
acde 2 matches (contains 4 characters)
^ - Caret
The caret symbol ^ is used to check if a string starts with a certain character.
Expression String Matched?
^a a 1 match
abc 1 match
bac No match
^ab abc 1 match
acb No match (starts with a but not followed by b)
$ - Dollar
The dollar symbol $ is used to check if a string ends with a certain character.
Expression String Matched?
a$ a 1 match
formula 1 match
cab No match
* - Star
The star symbol * matches zero or more occurrences of the pattern left to it.
Expression String Matched?
ma*n mn 1 match
man 1 match
maaan 1 match
main No match (a is not followed by n)
woman 1 match
+ - Plus
The plus symbol + matches one or more occurrences of the pattern left to it.
Expression String Matched?
ma+n mn No match (no a character)
man 1 match
maaan 1 match
main No match (a is not followed by n)
woman 1 match
? - Question Mark
The question mark symbol ? matches zero or one occurrence of the pattern left to it.
Expression String Matched?
ma?n mn 1 match
man 1 match
maaan No match (more than one a character)
main No match (a is not followed by n)
woman 1 match
{} - Braces
Consider this code: {n,m}. This means at least n, and at most m repetitions of the pattern left to
it.
Expression String Matched?
a{2,3} abc dat No match
abc daat 1 match (at daat)
aabc daaat 2 matches (at aabc and daaat)
aabc daaaat 2 matches (at aabc and daaaat)
Let's try one more example. This RegEx [0-9]{2, 4} matches at least 2 digits but not more than 4
digits
Expression String Matched?
[0-9]{2,4} ab123csde 1 match (match at ab123csde)
12 and 345673 3 matches (12, 3456, 73)
1 and 2 No match
| - Alternation
Parentheses () is used to group sub-patterns. For example, (a|b|c)xz match any string that
matches either a or b or c followed by xz
Expression String Matched?
(a|b|c)xz ab xz No match
abxz 1 match (match at abxz)
axz cabxz 2 matches (at axzbc cabxz)
\ - Backslash
Backlash \ is used to escape various characters including all metacharacters. For example,
\$a match if a string contains $ followed by a. Here, $ is not interpreted by a RegEx engine in a
special way.
If you are unsure if a character has special meaning or not, you can put \ in front of it. This
makes sure the character is not treated in a special way.
Special Sequences
Special sequences make commonly used patterns easier to write. Here's a list of special
sequences:
\A - Matches if the specified characters are at the start of a string.
Expression String Matched?
\Athe the sun Match
In the sun No match
\b - Matches if the specified characters are at the beginning or end of a word.
Expression String Matched?
\bfoo football Match
a football Match
afootball No match
foo\b the foo Match
the afoo test Match
the afootest No match
\B - Opposite of \b. Matches if the specified characters are not at the beginning or end of a
word.
Expression String Matched?
\Bfoo football No match
a football No match
afootball Match
foo\B the foo No match
the afoo test No match
the afootest Match
\d - Matches any decimal digit. Equivalent to [0-9]
Expression String Matched?
\d 12abc3 3 matches (at 12abc3)
Python No match
\D - Matches any non-decimal digit. Equivalent to [^0-9]
Expression String Matched?
\D 1ab34"50 3 matches (at 1ab34"50)
1345 No match
\s - Matches where a string contains any whitespace character. Equivalent to [ \t\n\r\f\v].
Expression String Matched?
\s Python RegEx 1 match
PythonRegEx No match
\S - Matches where a string contains any non-whitespace character. Equivalent to
[^ \t\n\r\f\v].
Python RegEx
Python has a module named re to work with regular expressions. To use it, we need to import
the module.
import re
The module defines several functions and constants to work with RegEx.
re.findall()
The re.findall() method returns a list of strings containing all matches.
Example 1: re.findall()
re.split()
The re.split method splits the string where there is a match and returns a list of strings where
the splits have occurred.
Example 2: re.split()
import re
string = 'Twelve:12 Eighty nine:89.'
pattern = '\d+'
result = re.split(pattern, string)
print(result)
import re
# Output: abc12de23f456
If the pattern is not found, re.sub() returns the original string.
You can pass count as a fourth parameter to the re.sub() method. If omited, it results to 0. This
will replace all occurrences.
import re
# multiline string
string = 'abc 12\
de 23 \n f45 6'
# matches all whitespace characters
pattern = '\s+'
replace = ''
new_string = re.sub(r'\s+', replace, string, 1)
print(new_string)
# Output:
# abc12de 23
# f45 6
re.subn()
The re.subn() is similar to re.sub() except it returns a tuple of 2 items containing the new string
and the number of substitutions made.
Example 4: re.subn()
# multiline string
string = 'abc 12\
de 23 \n f45 6'
# matches all whitespace characters
pattern = '\s+'
# empty string
replace = ''
new_string = re.subn(pattern, replace, string)
print(new_string)
# Output: ('abc12de23f456', 4)
re.search()
The re.search() method takes two arguments: a pattern and a string. The method looks for the
first location where the RegEx pattern produces a match with the string.
If the search is successful, re.search() returns a match object; if not, it returns None.
match = re.search(pattern, str)
Example 5: re.search()
import re
match.group()
The group() method returns the part of the string where there is a match.
Example 6: Match object
import re
# Output: 801 35
Here, match variable contains a match object.
Our pattern (\d{3}) (\d{2}) has two subgroups (\d{3}) and (\d{2}). You can get the part of the string
of these parenthesized subgroups. Here's how:
>>> match.group(1)
'801'
>>> match.group(2)
'35'
>>> match.group(1, 2)
('801', '35')
>>> match.groups()
('801', '35')
match.start(), match.end() and match.span()
The start() function returns the index of the start of the matched substring.
Similarly, end() returns the end index of the matched substring.
>>> match.start()
2
>>> match.end()
8
The span() function returns a tuple containing start and end index of the matched part.
>>> match.span()
(2, 8)
match.re and match.string
>>> match.string
'39801 356, 2102 1111'
Using r prefix before RegEx
When r or R prefix is used before a regular expression, it means raw string. For example, '\n' is
a new line whereas r'\n' means two characters: a backslash \ followed by n. Backlash \ is used
to escape various characters including all metacharacters. However, using r prefix
makes \ treat as a normal character.
Example 7: Raw string using r prefix
import re
It is platform independent.
the open and closed connections of the database, to avoid further exceptions and
Python database APIs are compatible with various databases, so it is very easy to
Connection objects
Connection objects create a connection with the database and these are further used for
different transactions. These connection objects are also used as representatives of the
database session.
You can use a connection object for calling methods like commit(), rollback() and close() as
shown below:
>>>cur = conn.cursor() //creates new cursor object for executing SQL statements
>>>conn.commit() //Commits the transactions
>>>conn.rollback() //Roll back the transactions
>>>conn.close() //closes the connection
>>>conn.callproc(proc,param) //call stored procedure for execution
>>>conn.getsource(proc) //fetches stored procedure code