You are on page 1of 255

PYTHON NOTES BY JOBHUNTER TEAM

The Numeric array package

Lists look like arrays, and are versatile objects, but they are not very efficient at

fulfilling the functions one expects of the kind of arrays that appear in moderate to

large scale scientific computation. It would be possible to write a 2D matrix as a list

of lists, and even implement matrix multiplication and vector addition for such an

object. However, it would be very inefficient, because lists provide for a very general

and mutable data structure. Python does have a native array data type, which is

somewhat more efficient, but is still not very good for scientific computing.

This is where the real power of the extensibility feature of Python comes in.

When the language is missing some feature some community really needs, the community

gets together and writes an extension module which fills the bill. Sometimes

this is a cooperative process. Sometimes it is an evolutionary process, with many

competing extensions co-existing until one comes to dominate. For scientific arrays,

the solution that has come to the fore is the Numeric module, which provides highly

efficient Matlab-style array objects.

Numeric is not written in Python. It is written in very highly optimized c++,

which is why it is so efficient. This does not mean the developers of Numeric had

to learn a lot about the internal structure of Python or spend much time at all

turning their compiled library of objects into commands that could be accessed
within Python. In fact, once such a library is developed, it can be turned into a

Python module more or less automatically using a preprocessor known as swig (see

swig.org for more details). Compiled FORTRAN libraries can be similarly spliced

into Python using pyfort or f2py. For this reason, a great variety of numerical

analysis libraries are already available as Python modules. Moreover, if you know

how to program in c, c++ or FORTRAN, you can very easily learn to build your own

customized Python modules. The general strategy is to do as little as possible at the

compiled level, building tools there that are very general and of broad applicability.

One seeks to isolate the computationally intensive work in a few compiled toolkits,

and build more complex models out of these building blocks at the Python level.

Numeric provides one of the most fundamental building blocks for scientific

programming in Python, and most other Python modules doing numerical analysis

or data analysis deal with Numeric arrays. The Numeric module is imported like

any other module, using import Numeric. Numeric arrays can have however many

dimensions you need.

The first step in using a Numeric array is to create it. Numeric provides

various ways to do this. To create an array, you need to specify its dimensions

and its datatype. Commonly used data types are default float (Numeric.Float,

usualy double precision), default complex (Numeric.Complex), and default integer

1.3. PROGRESSING IN PYTHON 21

(Numeric.Int, typically a 32-bit int). Numeric does not currently support 64-bit

integers unless they are the default integer type on the machine you are using. It

also does not support the unlimited precision Python integers, though other modules

are available which do. Dimensions are specified as a tuple or a list of integers. For

example, the dimensions of a 3 by 5 array are specified as (3,5) or [3,5]. If the


array is one-dimensional, you can just use an integer in place of a list or tuple, if

you wish.

One way to create an array in Python is to call a creation routine which makes

an array of the desired dimension and fills it in with default data of some particular

type. For example, the following lines create a 5 by 10 floating point array of zeroes,

a one-dimensional integer array of ones of length 100, and a 10 by 10 complex

identity matrix. You can see the values of an array, if it is not too big, by just

typing its name.

A = Numeric.zeros((5,10),Numeric.Float)

B = Numeric.ones(100,Numeric.Int)

C = Numeric.identity(10, Numeric.Complex)

A typical thing to do would be to create an array of zeroes, then fill in the values

you want in a loop, as in:

A = Numeric.zeros((5,10),Numeric.Float)

for i in range(5):

for j in range(10):

A[i,j] = .1*i*i + i*j/10.

This example also illustrates the way one refers to elements of an array in Python.

Python arrays are zero-based, i.e. A[0,0] is the first element in the case above.

The designers of Numeric provided a very versatile indexing handler, so in fact you

could equally well refer to the i,j element as A[i][j]. In essence, multidimensional

Numeric arrays act like a list of lists. We’ll return to this shortly, in our discussion of

array cross section notation. Another important thing to know about Numeric array

indexing is that it conforms to Python list usage with regard to negative numbers.

For example, if B is a one-dimensional array, B[-1] is the last element of the array,
B[-2] is the next to last, and so forth. Try this with a 2D array, to make sure you

understand the indexing convention.

An array can also be created from a list. You can let Numeric infer the data

type from the contents of the list, or you can specify it explicitly, in which case a

conversion is performed. Try the following statements, and see what kind of array

is produced:

22 CHAPTER 1. GETTING STARTED

A = Numeric.array([.1,.2,.3])

B = Numeric.array(range(20),Numeric.Complex)

C = Numeric.array( [ [1.,2.],[-2.,1.] ])

Another useful way to create an array is to define it in terms of a function

on the indices, so A[i,j] = f(i,j), f being some function you have defined. This

is probably the most common way of creating the kind of array used in scientific

computation without employing a loop. It will generally operate much faster than

a Python loop, especially for large arrays. The following illustrates how to create a

10 by 10 array from a function:

dx = .1

dy = .2

def f(i,j):

x = dx*i

y = dx*j

return x*x + y*y

A = Numeric.fromfunction(f,(10,10))

The two parameters dx and dy are provided to the function f as globals, because that

is the only way Numeric has provided to pass auxiliary information to the function
defining the array. There is an important subtlety in the use of fromfunction. The

parameters i and j look like integers, but in fact they are arrays of the same dimension

as the array being created. The above example works because the arithmetic

being done on the arguments is actually array arithmetic. This allows fromfunction

to call the function only a single time, rather than in a loop, and results in a much

faster computation. If you are using other functions or list indexing inside your

creation function, you must take this into account. For example, the following code

will work:

dx = .1

dy = .2

def f(i,j):

x = dx*i

y = dx*j

return Numeric.sin(x)*Numeric.sin(y)

A = Numeric.fromfunction(f,(10,10))

but if we used math.sin and math.cos instead, it would not work, since these

functions do not operate on and return Numeric arrays. Similarly, an expression like

myList[i] will not work inside the function f since i is not an integer, and so can’t

1.3. PROGRESSING IN PYTHON 23

be used as an index. There are workarounds, but generally speaking, fromfunction

does not work very gracefully with table look-up operations. If you are still confused

about just what i and j are, try putting a print statement in the function to print

out the arguments.

There are other ways to create arrays, but the above methods should take care

of any cases you are likely to encounter. We have already illustrated how to refer
to individual array elements, but it is worth knowing about some of the powerful

array cross section indexing features allowed by Numeric, which allow you to refer

to subarrays easily. Python uses the colon (:) as an identifier to build cross sections.

The colon by itself stands for the full range of values of the corresponding index. For

example, if A is a 5 by 5 array, the subarray A[:,0] is the one dimensional array with

elements A[0,0],A[1,0],...,A[4,0]. An index of the form m:n would denote the

range of values m,m+1,...,n-1, so that A[1:3,0] would be the array with elements

A[1,0],A[2,0]. If you leave off one of the endpoints, Python substitutes the first

array element for the starting point, or the final array element for the ending point.

If B were a 100 element array, for example, B[:25] would be the same as B[0:25]

and B[50:] would be the same as B[50:100]. Finally, you can specify a stride,

allowing you to pick off every k

th element. Thus, 0:10:3 represents the set of

indices 0,3,6,9. You can combine subarray indices for the various dimensions of

an array in any way you want, as in A[0:8:2,5:].

Now we come to the most powerful aspect of Numeric arrays, namely that one

can do arithmetic with them just as if they were scalars, avoiding the writing of

inefficient and cumbersome loops. The following statements illustrate the kind of

arithmentic operations that can be performed with arrays. Note that you will get

an error if you try to perform arithmetic on incompatibly sized arrays (e.g. adding

a 10 by 10 array to a 5 by 5 array).

A = Numeric.ones((10,10),Numeric.Float)

B = Numeric.identity(10,Numeric.Float)

C = (A-B)*10. #You can multiply by a scalar

C1 = (A-B)*B #You can multiply by an array


D = A/(A+2) #You can divide arrays.

E = C**5.2 #Exponentiation

The expressions can be as complicated as you want. Note that Numeric array

multiplication is element-by-element multiplication, rather than matrix multiplication

(and similarly for division). If you want matrix multiplication you use the

Numeric.dot(...) function, as in C = Numeric.dot(A,A). When the two arrays

are 1D, this reduces to the conventional vector dot product. There is also a function

Numeric.outerproduct which computes the outer product of two arrays. Numeric

24 CHAPTER 1. GETTING STARTED

does not provide a function for matrix inversion; that can be found in various other

linear algebra packages made to work on Numeric arrays.

Numeric also provides versions of the standard math functions, that work

on entire arrays. For example B = Numeric.sin(A) returns an array of the same

dimension as A, whose elements are the sin of the corresponding elements of A

Array arithmetic can be done between arrays of different types, if the operation

makes sense. The result is promoted to the higher of the two operands. For example,

adding an integer and a complex results in a complex, or adding a single precision

float to a double precision float yields a double precision float. The default float

type for Numeric arrays is double precision. All floating point scalars in Python are

also treated as double precision, so an operation involving a Python float constant

and a Numeric float array will yield a double precision array.

Cross sections can be used with array arithmetic to compute many useful

derived quantities without the need of writing loops. For example, an approximation

to the derivative of a function whose values at an array of points x are tabulated in

the array F (i.e. F[j] = f(x[j]) ) can be computed using:


#We assume the function f and the array x have been defined already

F = Numeric.array([f(x1) for x1 in x])

n = len(x)

dx = x[1:n]-x[0:(n-1)]

df = F[1:n]-F[0:(n-1)]

dfdx = df/dx

xmid = (x[1:n]+x[0:(n-1)])/2.

The final line defines the array of midpoints, where the derivative has been estimated.

Note the use of list comprehension to generate the array F. Array cross sections can

also be used to do matrix row and column operations efficiently. For example,

suppose A is an n by n matrix. Then a set of row and column reductions can be

done with the following 1D loop:

for i in range(n):

A[i] = A[i] - 2.*A[0]

A[:,i] = A[:,i] - 3.*A[:,0]

You can even do a fairly broad class of conditional operations on arrays, using

the Numeric.where function. This function takes three arguments. The first is a

conditional involving an array. The second is an array expression, to be evaluated

and returned if the conditional is satisfied. The third argument is the expression

to be returned if the conditional is not satisfied. For example, the following use of

Python Language Introduction

1.1

Python is a widely used general-purpose, high level programming language. It was initially designed by
Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for
emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of
code.

Python is a programming language that lets you work quickly and integrate systems more efficiently.

There are two major Python versions- Python 2 and Python 3. Both are quite different.

Beginning with Python programming:

1) Finding an Interpreter:

Before we start Python programming, we need to have an interpreter to interpret and run our
programs. There are certain online interpreters like https://ide.geeksforgeeks.org/, http://ideone.com/
or http://codepad.org/ that can be used to start Python without installing an interpreter.

Windows:There are many interpreters available freely to run Python scripts like IDLE ( Integrated
Development Environment ) which is installed when you install the python software from
http://python.org/

Linux:For Linux, Python comes bundled with the linux.

2) Writing first program:

Following is first program in Python

# Script Begins
print("GeeksQuiz")

# Scripts Ends

Let us analyze the script line by line.

Line 1 : [# Script Begins] In Python comments begin with #. So this statement is for readability of code
and ignored by Python interpretor.

Line 2 : [print(“GeeksQuiz”)] In a Python script to print something on the console print() function is used
– it simply prints out a line ( and also includes a newline unlike in C ). One difference between Python 2
and Python 3 is the print statement. In Python 2, the “print” statement is not a function, and therefore
can be invoked without a parenthesis. However, in Python 3, it is a function, and must be invoked with
parentheses.

Line 3 : [# Script Ends] This is just another comment like Line 1.

Python – The new generation Language

1.3

Python designed by Guido van Rossum at CWI has become a widely used general-purpose, high-level
programming language.

Prerequisites:

Knowledge of any programming language can be a plus.


Reason for increasing popularity

Emphasis on code readability, shorter codes, ease of writing

Programmers can express logical concepts in fewer lines of code in comparison to languages such as C++
or Java.

Python supports multiple programming paradigms, like object-oriented, imperative and functional
programming or procedural.

There exists inbuilt functions for almost all of the frequently used concepts.

Philosophy is “Simplicity is the best”.

LANGUAGE FEATURES

Interpreted

There are no separate compilation and execution steps like C and C++.

Directly runthe program from the source code.

Internally, Python converts the source code into an intermediate form called bytecodes which is then
translated into native language of specific computer to run it.

No need to worry about linking and loading with libraries, etc.

Platform Independent

Python programs can be developed and executed on multiple operating system platforms.

Python can be used on Linux, Windows, Macintosh, Solaris and many more.

Free and Open Source; Redistributable

High-level Language

In Python, no need to take care about low-level details such as managing the memory used by the
program.

Simple

Closer to English language;Easy to Learn

More emphasis on the solution to the problem rather than the syntax
Embeddable

Python can be used within C/C++ program to give scripting capabilities for the program’s users.

Robust:

Exceptional handling features

Memory management techniques in built

Rich Library Support

The Python Standard Library is vary vast.

Known as the “batteries included” philosophy of Python ;It can help do various things involving regular
expressions, documentation generation, unit testing, threading, databases, web browsers, CGI, email,
XML, HTML, WAV files, cryptography, GUI and many more.

Besides the standard library, there are various other high-quality libraries such as the Python Imaging
Library which is an amazingly simple image manipulation library.

Python vs JAVA

Python Java

Dynamically Typed1.No need to declare anything. An assignment statement binds a name to an object,
and the object can be of any type.2.No type casting required when using container objects
Statically Typed 1.All variable names (along with their types) must be explicitly declared.
Attempting to assign an object of the wrong type to a variable name triggers a type exception.2.Type
casting is required when using container objects.

Concise Express much in limited words VerboseContains more words

Compact Less Compact

Uses Indentation for structuring code Uses braces for structuring code

The classical Hello World program illustrating the relative verbosity of a Java Program and Python
Program

Java Code

public class HelloWorld

{
public static void main (String[] args)

System.out.println("Hello, world!");

Run on IDE

Python Code

print "Hello, world!"

Run on IDE

print("Hello, world!") # Python version 3

Run on IDE

Similarity with Java

Require some form of runtime on your system (JVM/Python runtime)

Can probably be compiled to executables without the runtime (this is situational, none of them are
designed to work this way)

Currently, there are two versions of Python available Python 2 and Python 3. Many beginners must be
wondering with which version of Python they should start. My answer to this question is usually
something along the lines “just go with the version your favourite tutorial is written in, and check out
the differences later on.”

Softwares making use of Python

Python has been successfully embedded in a number of software products as a scripting language.

GNU Debugger uses Python as a pretty printer to show complex structures such as C++ containers.
Python has also been used in artificial intelligence

Python is often used for natural language processing tasks.

Current Applications of Python

A number of Linux distributions use installers written in Python example in Ubuntu we have the Ubiquity

Python has seen extensive use in the information security industry, including in exploit development.

Raspberry Pi– single board computer uses Python as its principal user-programming language.

Python is now being used Game Development areas also.

Pros:

Ease of use

Multi-paradigm Approach

Cons:

Slow speed of execution compared to C,C++

Absence from mobile computing and browsers

For the C,C++ programmers switching to python can be irritating as the language requires proper
indentation of code. Certain variable names commonly used like sum are functions in python. So C, C++
programmers have to look out for these.

Industrial Importance

Most of the companies are now looking for candidates who know about Python Programming. Those
having the knowledge of python may have more chances of impressing the interviewing panel. So I
would suggest that beginners should start learning python and excel in it.

Division operator
If we are porting our code or executing the python 3.x code in python 2.x, it can be dangerous if integer
division changes go unnoticed (since it doesn’t raise any error). It is preferred to use the floating value
(like 7.0/5 or 7/5.0) to get the expected result when porting our code.

print 7 / 5

print -7 / 5

'''

Output in Python 2.x

-2

Output in Python 3.x :

1.4

-1.4

# Refer below link for details

# https://www.geeksforgeeks.org/division-operator-in-python/

'''

Run on IDE

print function

This is the most well known change. In this the print function in Python 2.x is replaced by print() function
in Python 3.x,i.e, to print in Python 3.x an extra pair of parenthesis is required.

print 'Hello, Geeks' # Python 3.x doesn't support

print('Hope You like these facts')


'''

Output in Python 2.x :

Hello, Geeks

Hope You like these facts

Output in Python 3.x :

File "a.py", line 1

print 'Hello, Geeks'

SyntaxError: invalid syntax

Refer below link for details

https://www.geeksforgeeks.org/g-fact-25-print-single-multiple-variable-python/

'''

Run on IDE

As we can see, if we use parenthesis in python 2.x then there is no issue but if we don’t use parenthesis
in python 3.x, we get SyntaxError.

Unicode:

In Python 2, implicit str type is ASCII. But in Python 3.x implicit str type is Unicode.

print(type('default string '))

print(type(b'string with b '))

'''
Output in Python 2.x (Bytes is same as str)

<type 'str'>

<type 'str'>

Output in Python 3.x (Bytes and str are different)

<class 'str'>

<class 'bytes'>

'''

Run on IDE

Python 2.x also supports Unicode

print(type('default string '))

print(type(u'string with b '))

'''

Output in Python 2.x (Unicode and str are different)

<type 'str'>

<type 'unicode'>

Output in Python 3.x (Unicode and str are same)

<class 'str'>

<class 'str'>

'''

Run on IDE

xrange:
xrange() of Python 2.x doesn’t exist in Python 3.x. In Python 2.x, range returns a list i.e. range(3) returns
[0, 1, 2] while xrange returns a xrange object i. e., xrange(3) returns iterator object which work similar to
Java iterator and generates number when needed.

If we need to iterate over the same sequence multiple times, we prefer range() as range provides a
static list. xrange() reconstructs the sequence every time. xrange() doesn’t support slices and other list
methods. The advantage of xrange() is, it saves memory when task is to iterate over a large range.

In Python 3.x, the range function now does what xrange does in Python 2.x, so to keep our code
portable, we might want to stick to using range instead. So Python 3.x’s range function is xrange from
Python 2.x.

for x in xrange(1, 5):

print(x),

for x in range(1, 5):

print(x),

'''

Output in Python 2.x

12341234

Output in Python 3.x

NameError: name 'xrange' is not defined

'''

Run on IDE

Error Handling:
There is a small change in error handling in both versions. In python 3.x, ‘as’ keyword is required.

try:

trying_to_check_error

except NameError, err:

print err, 'Error Caused' # Would not work in Python 3.x

'''

Output in Python 2.x:

name 'trying_to_check_error' is not defined Error Caused

Output in Python 3.x :

File "a.py", line 3

except NameError, err:

SyntaxError: invalid syntax

'''

Run on IDE

try:

trying_to_check_error

except NameError as err: # 'as' is needed in Python 3.x

print (err, 'Error Caused')

'''

Output in Python 2.x:


(NameError("name 'trying_to_check_error' is not defined",), 'Error Caused')

Output in Python 3.x :

name 'trying_to_check_error' is not defined Error Caused

'''

Run on IDE

_future_module:

This is basically not a difference between two version, but useful thing to mention here. The idea of
__future__ module is to help in migration. We can use Python 3.x

If we are planning Python 3.x support in our 2.x code,we can ise_future_ imports it in our code.

For example, in below Python 2.x code, we use Python 3.x’s integer division behavior using __future__
module

# In below python 2.x code, division works

# same as Python 3.x because we use __future__

from __future__ import division

print 7 / 5

print -7 / 5

Run on IDE

Output :

1.4

-1.4
Another example where we use brackets in Python 2.x using __future__ module

from __future__ import print_function

print('GeeksforGeeks')

Run on IDE

Output :

GeeksforGeeks

More keywords:

16. try : This keyword is used for exception handling, used to catch the errors in the code using the
keyword except. Code in “try” block is checked, if there is any type of error, except block is executed.

17. except : As explained above, this works together with “try” to catch exceptions.

18. raise : Also used for exception handling to explicitly raise exceptions.

19. finally : No matter what is result of the “try” block, block termed “finally” is always executed.
Detailed article –Exception Handling in Python

20. for : This keyword is used to control flow and for looping.

21. while : Has a similar working like “for” , used to control flow and for looping.
22. pass : It is the null statement in python. Nothing happens when this is encountered. This is used to
prevent indentation errors and used as a placeholder

Detailed Article – for, while, pass

23. import : This statement is used to include a particular module into current program.

24. from : Generally used with import, from is used to import particular functionality from the module
imported.

25. as : This keyword is used to create the alias for the module imported. i.e giving a new name to the
imported module.. E.g import math as mymath.

Detailed Article – import, from and as

26. lambda : This keyword is used to make inline returning functions with no statements allowed
internally. Detailed Article – map, filter, lambda

27. return : This keyword is used to return from the function. Detailed article – Return values in Python.

28. yield : This keyword is used like return statement but is used to return a generator. Detailed Article –
yield keyword

29. with : This keyword is used to wrap the execution of block of code within methods defined by
context manager.This keyword is not used much in day to day programming.
30. in : This keyword is used to check if a container contains a value. This keyword is also used to loop
through the container.

31. is : This keyword is used to test object identity, i.e to check if both the objects take same memory
location or not.

# Python code to demonstrate working of

# in and is

# using "in" to check

if 's' in 'geeksforgeeks':

print ("s is part of geeksforgeeks")

else : print ("s is not part of geeksforgeeks")

# using "in" to loop through

for i in 'geeksforgeeks':

print (i,end=" ")

print ("\r")

# using is to check object identity

# string is immutable( cannot be changed once alloted)

# hence occupy same memory location

print (' ' is ' ')

# using is to check object identity


# dictionary is mutable( can be changed once alloted)

# hence occupy different memory location

print ({} is {})

Run on IDE

Output:

s is part of geeksforgeeks

geeksforgeeks

True

False

32. global : This keyword is used to define a variable inside the function to be of a global scope.

33. non-local : This keyword works similar to the global, but rather than global, this keyword declares a
variable to point to variable of outside enclosing function, in case of nested functions.

# Python code to demonstrate working of

# global and non local

#initializing variable globally

a = 10

# used to read the variable

def read():

print (a)

# changing the value of globally defined variable


def mod1():

global a

a=5

# changing value of only local variable

def mod2():

a = 15

# reading initial value of a

# prints 10

read()

# calling mod 1 function to modify value

# modifies value of global a to 5

mod1()

# reading modified value

# prints 5

read()

# calling mod 2 function to modify value

# modifies value of local a to 15, doesn't effect global value

mod2()

# reading modified value


# again prints 5

read()

# demonstrating non local

# inner loop changing the value of outer a

# prints 10

print ("Value of a using nonlocal is : ",end="")

def outer():

a=5

def inner():

nonlocal a

a = 10

inner()

print (a)

outer()

# demonstrating without non local

# inner loop not changing the value of outer a

# prints 5

print ("Value of a without using nonlocal is : ",end="")

def outer():

a=5

def inner():

a = 10
inner()

print (a)

outer()

Run on IDE

Output:

10

Value of a using nonlocal is : 10

Value of a without using nonlocal is : 5

Running your First Code in Python

Python programs are not compiled, rather they are interpreted. Now, let us move to writing a python
code and running it. Please make sure that python is installed on the system you are working. If it is not
installed, download it from here. We will be using python 2.7.

Making a Python file:

Python files are stored with the extension “.py”. Open text editor and save a file with the name
“hello.py”. Open it and write the following code:

print "Hello World"

# Notice that NO semi-colon is to be used

Run on IDE
Reading the file contents:

Linux System – Move to the directory from terminal where the created file (hello.py) is stored by using
the ‘cd’ command, and then type the following in the terminal :

python hello.py

Windows system – Open command prompt and move to the directory where the file is stored by using
the ‘cd’ command and then run the file by writing the file name as command.

Variables in Python

Variables need not be declared first in python. They can be used directly. Variables in python are case
sensitive as most of the other programming languages.

Example:

a=3

A=4

print a

print A

Run on IDE

The output is :

Expressions in Python

Arithmetic operations in python can be performed by using arithmetic operators and some of the in-
built functions.

a=2
b=3

c=a+b

print c

d=a*b

print d

Run on IDE

The output is :

Conditions in Python

Conditional output in python can be obtained by using if-else and elif (else if) statements.

a=3

b=9

if b % a == 0 :

print "b is divisible by a"

elif b + 1 == 10:

print "Increment in b produces 10"

else:

print "You are in else statement"

Run on IDE

The output is :

b is divisible by a
Functions in Python

A function in python is declared by the keyword ‘def’ before the name of the function. The return type
of the function need not be specified explicitly in python. The function can be invoked by writing the
function name followed by the parameter list in the brackets.

# Function for checking the divisibility

# Notice the indentation after function declaration

# and if and else statements

def checkDivisibility(a, b):

if a % b == 0 :

print "a is divisible by b"

else:

print "a is not divisible by b"

#Driver program to test the above function

checkDivisibility(4, 2)

Run on IDE

The output is :

a is divisible by b

What is the maximum possible value of an integer in Python ?

1.2

Consider below Python program.


# A Python program to demonstrate that we can store

# large numbers in Python

x = 10000000000000000000000000000000000000000000;

x=x+1

print (x)

Run on IDE

Output :

10000000000000000000000000000000000000000001

In Python, value of an integer is not restricted by the number of bits and can expand to the limit of the
available memory (Sources : this and this). Thus we never need any special arrangement for storing large
numbers (Imagine doing above arithmetic in C/C++).

As a side note, in Python 3, there is only one type “int” for all type of integers. In Python 2.7. there are
two separate types “int” (which is 32 bit) and “long int” that is same as “int” of Python 3.x, i.e., can store
arbitrarily large numbers.

# A Python program to show that there are two types in

# Python 2.7 : int and long int

# And in Python 3 there is only one type : int

x = 10

print(type(x))

x = 10000000000000000000000000000000000000000000

print(type(x))
Run on IDE

Output in Python 2.7 :

<type 'int'>

<type 'long'>

Output in Python 3 :

<type 'int'>

<type 'int'>

We may want to try more interesting programs like below :

# Printing 100 raise to power 100

print(100**100)

Transpose a matrix in Single line in Python

2.1

Transpose of a matrix is a task we all can perform very easily in python (Using a nested loop). But there
are some interesting ways to do the same in a single line.

In Python, we can implement a matrix as nested list (list inside a list). Each element is treated as a row of
the matrix. For example m = [[1, 2], [4, 5], [3, 6]] represents a matrix of 3 rows and 2 columns.

First element of the list – m[0] and element in first row, first column – m[0][0].
Using Nested List Comprehension: Nested list comprehension are used to iterate through each element
in the matrix.In the given example ,we iterate through each element of matrix (m) in column major
manner and assign the result to rez matrix which is the transpose of m.

m = [[1,2],[3,4],[5,6]]

for row in m :

print(row)

rez = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]

print("\n")

for row in rez:

print(row)

Run on IDE

Output:

[1, 2]

[3, 4]

[5, 6]

[1, 3, 5]

[2, 4, 6]

Using zip: Zip returns an iterator of tuples, where the i-th tuple contains the i-th element from each of
the argument sequences or iterables. In this example we unzip our array using * and then zip it to get
the transpose.

matrix=[(1,2,3),(4,5,6),(7,8,9),(10,11,12)]

for row in matrix:

print(row)
print("\n")

t_matrix = zip(*matrix)

for row in t_matrix:

print row

Run on IDE

Output:

(1, 2, 3)

(4, 5, 6)

(7, 8, 9)

(10, 11, 12)

(1, 4, 7, 10)

(2, 5, 8, 11)

(3, 6, 9, 12)

Note :- If you want your result in the form [[1,4,7,10][2,5,8,11][3,6,9,12]] , you can use
t_matrix=map(list, zip(*matrix)).

Using numpy: NumPy is a general-purpose array-processing package designed to efficiently manipulate


large multi-dimensional arrays. The transpose method returns a transposed view of the passed multi-
dimensional matrix.

# You need to install numpy in order to import it

# Numpy transpose returns similar result when

# applied on 1D matrix

import numpy

matrix=[[1,2,3],[4,5,6]]

print(matrix)
print("\n")

print(numpy.transpose(matrix))

Global and Local Variables in Python

1.5

Global variables are the one that are defined and declared outside a function and we need to use them
inside a function.

# This function uses global variable s

def f():

print s

# Global scope

s = "I love Geeksforgeeks"

f()

Run on IDE

Output:

I love Geeksforgeeks

If a variable with same name is defined inside the scope of function as well then it will print the value
given inside the function only and not the global value.
# This function has a variable with

# name same as s.

def f():

s = "Me too."

print s

# Global scope

s = "I love Geeksforgeeks"

f()

print s

Run on IDE

Output:

Me too.

I love Geeksforgeeks.

The variable s is defined as the string “I love Geeksforgeeks”, before we call the function f(). The only
statement in f() is the “print s” statement. As there is no local s, the value from the global s will be used.

The question is, what will happen, if we change the value of s inside of the function f()? Will it affect the
global s as well? We test it in the following piece of code:

def f():

print s

# This program will NOT show error


# if we comment below line.

s = "Me too."

print s

# Global scope

s = "I love Geeksforgeeks"

f()

print s

Run on IDE

Output:

Line 2: undefined: Error: local variable 's' referenced before assignment

To make the above program work, we need to use “global” keyword. We only need to use global
keyword in a function if we want to do assignments / change them. global is not needed for printing and
accessing. Why? Python “assumes” that we want a local variable due to the assignment to s inside of f(),
so the first print statement throws this error message. Any variable which is changed or created inside of
a function is local, if it hasn’t been declared as a global variable. To tell Python, that we want to use the
global variable, we have to use the keyword “global”, as can be seen in the following example:

# This function modifies global variable 's'

def f():

global s

print s

s = "Look for Geeksforgeeks Python Section"

print s
# Global Scope

s = "Python is great!"

f()

print s

Run on IDE

Now there is no ambiguity.

Output:

Python is great!

Look for Geeksforgeeks Python Section.

Look for Geeksforgeeks Python Section.

A good Example

a=1

# Uses global because there is no local 'a'

def f():

print 'Inside f() : ', a

# Variable 'a' is redefined as a local

def g():

a=2

print 'Inside g() : ',a

# Uses global keyword to modify global 'a'


def h():

global a

a=3

print 'Inside h() : ',a

# Global scope

print 'global : ',a

f()

print 'global : ',a

g()

print 'global : ',a

h()

print 'global : ',a

Run on IDE

Output:

global : 1

Inside f() : 1

global : 1

Inside g() : 2

global : 1

Inside h() : 3

global : 3
Partial Functions in Python

Partial functions allow us to fix a certain number of arguments of a function and generate a new
function.

Example:

from functools import partial

# A normal function

def f(a, b, c, x):

return 1000*a + 100*b + 10*c + x

# A partial function that calls f with

# a as 3, b as 1 and c as 4.

g = partial(f, 3, 1, 4)

# Calling g()

print(g(5))

Run on IDE

Output:

3145

In the example we have pre-filled our function with some constant values of a, b and c. And g() just takes
a single argument i.e. the variable x.
Another Example :

from functools import *

# A normal function

def add(a, b, c):

return 100*a + 10*b + c

# A partial function with b = 1 and c = 2

add_part = partial(add, c=2, b=1)

# Calling partial function

print(add_part(3))

Run on IDE

Output:

312

Partial functions can be used to derive specialized functions from general functions and therefore help
us to reuse our code.

This feature is similar to bind in C++.


Packing and Unpacking Arguments in Python

We use two operators * (for tuples) and ** (for dictionaries).

Background

Consider a situation where we have a function that receives four arguments. We want to make call to
this function and we have a list of size 4 with us that has all arguments for the function. If we simply pass
list to the function, the call doesn’t work.

# A Python program to demonstrate need

# of packing and unpacking

# A sample function that takes 4 arguments

# and prints them.

def fun(a, b, c, d):

print(a, b, c, d)

# Driver Code

my_list = [1, 2, 3, 4]

# This doesn't work

fun(my_list)

Run on IDE

Output :
TypeError: fun() takes exactly 4 arguments (1 given)

Unpacking

We can us * to unpack the list so that all elements of it can be passed as different parameters.

# A sample function that takes 4 arguments

# and prints the,

def fun(a, b, c, d):

print(a, b, c, d)

# Driver Code

my_list = [1, 2, 3, 4]

# Unpacking list into four arguments

fun(*my_list)

Run on IDE

Output :

(1, 2, 3, 4)

As another example, consider the built-in range() function that expects separate start and stop
arguments. If they are not available separately, write the function call with the *-operator to unpack the
arguments out of a list or tuple:

>>>

>>> range(3, 6) # normal call with separate arguments


[3, 4, 5]

>>> args = [3, 6]

>>> range(*args) # call with arguments unpacked from a list

[3, 4, 5]

Run on IDE

Packing

When we don’t know how many arguments need to be passed to a python function, we can use Packing
to pack all arguments in a tuple.

# A Python program to demonstrate use

# of packing

# This function uses packing to sum

# unknown number of arguments

def mySum(*args):

sum = 0

for i in range(0, len(args)):

sum = sum + args[i]

return sum

# Driver code

print(mySum(1, 2, 3, 4, 5))

print(mySum(10, 20))

Run on IDE

Output:
15

30

Python | end parameter in print()

1.2

By default python’s print() function ends with a newline. A programmer with C/C++ background may
wonder how to print without newline.

Python’s print() function comes with a parameter called ‘end’. By default, the value of this parameter is
‘\n’, i.e. the new line character. You can end a print statement with any character/string using this
parameter.

# This Python program must be run with


# Python 3 as it won't work with 2.7.

# ends the output with a <space>

print("Welcome to" , end = ' ')

print("GeeksforGeeks", end = ' ')

Run on IDE

Output :

Welcome to GeeksforGeeks

One more program to demonstrate working of end parameter.

# This Python program must be run with

# Python 3 as it won't work with 2.7.

# ends the output with '@'

print("Python" , end = '@')

print("GeeksforGeeks")

Run on IDE

Output :

Python@GeeksforGeeks
ype Conversion in Python

Python defines type conversion functions to directly convert one data type to another which is useful in
day to day and competitive programming. This article is aimed at providing the information about
certain conversion functions.

1. int(a,base) : This function converts any data type to integer. ‘Base’ specifies the base in which string is
if data type is string.

2. float() : This function is used to convert any data type to a floating point number

# Python code to demonstrate Type conversion

# using int(), float()

# initializing string

s = "10010"

# printing string converting to int base 2

c = int(s,2)

print ("After converting to integer base 2 : ", end="")

print (c)
# printing string converting to float

e = float(s)

print ("After converting to float : ", end="")

print (e)

Run on IDE

Output:

After converting to integer base 2 : 18

After converting to float : 10010.0

3. ord() : This function is used to convert a character to integer.

4. hex() : This function is to convert integer to hexadecimal string.

5. oct() : This function is to convert integer to octal string.

# Python code to demonstrate Type conversion

# using ord(), hex(), oct()

# initializing integer

s = '4'

# printing character converting to integer

c = ord(s)

print ("After converting character to integer : ",end="")


print (c)

# printing integer converting to hexadecimal string

c = hex(56)

print ("After converting 56 to hexadecimal string : ",end="")

print (c)

# printing integer converting to octal string

c = oct(56)

print ("After converting 56 to octal string : ",end="")

print (c)

Run on IDE

Output:

After converting character to integer : 52

After converting 56 to hexadecimal string : 0x38

After converting 56 to octal string : 0o70

6. tuple() : This function is used to convert to a tuple.

7. set() : This function returns the type after converting to set.

8. list() : This function is used to convert any data type to a list type.

# Python code to demonstrate Type conversion

# using tuple(), set(), list()


# initializing string

s = 'geeks'

# printing string converting to tuple

c = tuple(s)

print ("After converting string to tuple : ",end="")

print (c)

# printing string converting to set

c = set(s)

print ("After converting string to set : ",end="")

print (c)

# printing string converting to list

c = list(s)

print ("After converting string to list : ",end="")

print (c)

Run on IDE

Output:

After converting string to tuple : ('g', 'e', 'e', 'k', 's')

After converting string to set : {'k', 'e', 's', 'g'}

After converting string to list : ['g', 'e', 'e', 'k', 's']

9. dict() : This function is used to convert a tuple of order (key,value) into a dictionary.
10. str() : Used to convert integer into a string.

11. complex(real,imag) : : This function converts real numbers to complex(real,imag) number.

# Python code to demonstrate Type conversion

# using dict(), complex(), str()

# initializing integers

a=1

b=2

# initializing tuple

tup = (('a', 1) ,('f', 2), ('g', 3))

# printing integer converting to complex number

c = complex(1,2)

print ("After converting integer to complex number : ",end="")

print (c)

# printing integer converting to string

c = str(a)

print ("After converting integer to string : ",end="")

print (c)
# printing tuple converting to expression dictionary

c = dict(tup)

print ("After converting tuple to dictionary : ",end="")

print (c)

Run on IDE

Output:

After converting integer to complex number : (1+2j)

After converting integer to string : 1

After converting tuple to dictionary : {'a': 1, 'f': 2, 'g': 3}

Byte Objects vs String in Python

1.6

In Python 2, both str and bytes are the same typeByte objects whereas in Python 3 Byte objects, defined
in Python 3 are “sequence of bytes” and similar to “unicode” objects from Python 2. However, there are
many differences in strings and Byte objects. Some of them are depicted below:

Byte objects are sequence of Bytes, whereas Strings are sequence of characters.
Byte objects are in machine readable form internally, Strings are only in human readable form.

Since Byte objects are machine readable, they can be directly stored on the disk. Whereas, Strings need
encoding before which they can be stored on disk.

string vs byte in python

There are methods to convert a byte object to String and String to byte objects.

Encoding

PNG, JPEG, MP3, WAV, ASCII, UTF-8 etc are different forms of encodings. An encoding is a format to
represent audio, images, text, etc in bytes. Converting Strings to byte objects is termed as encoding. This
is necessary so that the text can be stored on disk using mapping using ASCII or UTF-8 encoding
techniques.

This task is achieved using encode(). It take encoding technique as argument. Default technique is “UTF-
8” technique.

# Python code to demonstate String encoding

# initialising a String

a = 'GeeksforGeeks'

# initialising a byte object

c = b'GeeksforGeeks'

# using encode() to encode the String


# encoded version of a is stored in d

# using ASCII mapping

d = a.encode('ASCII')

# checking if a is converted to bytes or not

if (d==c):

print ("Encoding successful")

else : print ("Encoding Unsuccessful")

Run on IDE

Output:

Encoding successful

Decoding

Similarly, Decoding is process to convert a Byte object to String. It is implemented using decode() . A
byte string can be decoded back into a character string, if you know which encoding was used to encode
it. Encoding and Decoding are inverse processes.

# Python code to demonstate Byte Decoding

# initialising a String

a = 'GeeksforGeeks'

# initialising a byte object

c = b'GeeksforGeeks'
# using decode() to decode the Byte object

# decoded version of c is stored in d

# using ASCII mapping

d = c.decode('ASCII')

# checking if c is converted to String or not

if (d==a):

print ("Decoding successful")

else : print ("Decoding Unsuccessful")

Run on IDE

Output:

Decoding successful
G-Fact 19 (Logical and Bitwise Not Operators on Boolean)

1.7

Most of the languages including C, C++, Java and Python provide the boolean type that can be either set
to False or True.

Consider below programs that use Logical Not (or !) operator on boolean.

# A Python program that uses Logical Not or ! on boolean

a = not True

b = not False

print a

print b

# Output: False

# True

Run on IDE

The outputs of above programs are as expected, but the outputs following programs may not be as
expected if we have not used Bitwise Not (or ~) operator before.

# A Python program that uses Bitwise Not or ~ on boolean

a = True
b = False

print ~a

print ~b

Run on IDE

Output:

-2

-1

Reason: The bitwise not operator ~ returns the complement of a number i.e., it switches each 1 to 0 and
each 0 to 1. Booleans True and False have values 1 and 0 respectively.

˜being the bitwise not operator,

The expression “˜True” returns bitwise inverse of 1.

The expression “˜False” returns bitwise inverse of 0.

Java doesn’t allow ~ operator to be applied on boolean values. For example, the below program
produces compiler error.

// A Java program that uses Bitwise Not or ~ on boolean

import java.io.*;

class GFG

public static void main (String[] args)


{

boolean a = true, b = false;

System.out.println(~a);

System.out.println(~b);

Run on IDE

Output:

6: error: bad operand type boolean for unary operator '~'

System.out.println(~a);

7: error: bad operand type boolean for unary operator '~'

System.out.println(~b);

2 errors

Conclusion:

“Logical not or !” is meant for boolean values and “bitwise not or ~” is for integers. Languages like C/C++
and python do auto promotion of boolean to integer type when an integer operator is applied. But Java
doesn’t do it.
Ternary Operator in Python

2.1

Ternary operators also known as conditional expressions are operators that evaluate something based
on a condition being true or false. It was added to Python in version 2.5.

It simply allows to test a condition in a single line replacing the multiline if-else making the code
compact.

Syntax :

[on_true] if [expression] else [on_false]

Simple Method to use ternary operator:

# Program to demonstrate conditional operator


a, b = 10, 20

# Copy value of a in min if a < b else copy b

min = a if a < b else b

print(min)

Run on IDE

Output:

10

Direct Method by using tuples, Dictionary and lambda

# Python program to demonstrate ternary operator

a, b = 10, 20

# Use tuple for selecting an item

print( (b, a) [a < b] )

# Use Dictionary for selecting an item

print({True: a, False: b} [a < b])

# lamda is more efficient than above two methods

# because in lambda we are assure that

# only one expression will be evaluated unlike in

# tuple and Dictionary

print((lambda: b, lambda: a)[a < b]())

Run on IDE
Output:

10

10

10

Ternary operator can be written as nested if-else:

# Python program to demonstrate nested ternary operator

a, b = 10, 20

print ("Both a and b are equal" if a == b else "a is greater than b"

if a > b else "b is greater than a")

Run on IDE

Above approach can be written as:

# Python program to demonstrate nested ternary operator

a, b = 10, 20

if a != b:

if a > b:

print("a is greater than b")

else:

print("b is greater than a")

else:

print("Both a and b are equal")

Run on IDE

Output: b is greater than a


Important Points:

First the given condition is evaluated (a < b), then either a or b is returned based on the Boolean value
returned by the condition

Order of the arguments in the operator is different from other languages like C/C++ (See C/C++ ternary
operators).

Conditional expressions have the lowest priority amongst all Python operations.

Method used prior to 2.5 when ternary operator was not present

In an expression like the one given below , the interpreter checks for the expression if this is true then
on_true is evaluated, else the on_false is evaluated.

Syntax :

'''When condition becomes true, expression [on_false]

is not executed and value of "True and [on_true]"

is returned. Else value of "False or [on_false]"

is returned.

Note that "True and x" is equal to x.

And "False or x" is equal to x. '''

[expression] and [on_true] or [on_false]

Example :

# Program to demonstrate conditional operator

a, b = 10, 20
# If a is less than b, then a is assigned

# else b is assigned (Note : it doesn't

# work if a is 0.

min = a < b and a or b

print(min)

Run on IDE

Output:

10

Note : The only drawback of this method is that on_true must not be zero or False. If this happens
on_false will be evaluated always. The reason for that is if expression is true, the interpreter will check
for the on_true, if that will be zero or false, that will force the interpreter to check for on_false to give
the final result of whole expression.

Increment and Decrement Operators in Python

1.4

If you’re familiar with Python, you would have known Increment and Decrement operators ( both pre
and post) are not allowed in it.

Python is designed to be consistent and readable. One common error by a novice programmer in
languages with ++ and -- operators is mixing up the differences (both in precedence and in return value)
between pre and post increment/decrement operators. Simple increment and decrement operators
aren’t needed as much as in other languages.

You don’t write things like :


for (int i = 0; i < 5; ++i)

In Python, instead we write it like

# A Sample Python program to show loop (unlike many

# other languages, it doesn't use ++)

for i in range(0, 5):

print(i)

Run on IDE

Output:

We can almost always avoid use of ++ and --. For example, x++ can be written as x += 1 and x-- can be
written as x -= 1.
Division Operators in Python

Consider below statements in Python 2.7

# A Python 2.7 program to demonstrate use of

# "/" for integers

print 5/2

print -5/2

Run on IDE

Output:

-3

First output is fine, but the second one may be surprising if we are coming Java/C++ world. In Python
2.7, the “/” operator works as a floor division for integer arguments. However, the operator / returns a
float value if one of the arguments is a float (this is similar to C++)

# A Python 2.7 program to demonstrate use of

# "/" for floating point numbers

print 5.0/2

print -5.0/2

Run on IDE

Output:

2.5
-2.5

The real floor division operator is “//”. It returns floor value for both integer and floating point
arguments.

# A Python 2.7 program to demonstrate use of

# "//" for both integers and floating points

print 5//2

print -5//2

print 5.0//2

print -5.0//2

Run on IDE

Output:

-3

2.0

-3.0

How about Python 3?

Here is another surprise, In Python 3, ‘/’ operator does floating point division for both int and float
arguments.

# A Python 3 program to demonstrate use of

# "/" for both integers and floating points

print (5/2)

print (-5/2)

print (5.0/2)
print (-5.0/2)

Output:

2.5

-2.5

2.5

-2.5

Any & All in Python

1.6

Any and All are two built ins provided in python used for successive And/Or.

Any

Returns true if any of the items is True. It returns False if empty or all are false. Any can be thought of as
a sequence of OR operations on the provided iterables.

It short circuit the execution i.e. stop the execution as soon as the result is known.
Syntax : any(list of iterables)

# Since all are false, false is returned

print (any([False, False, False, False]))

# Here the method will short-circuit at the

# second item (True) and will return True.

print (any([False, True, False, False]))

# Here the method will short-circuit at the

# first (True) and will return True.

print (any([True, False, False, False]))

Run on IDE

Output :

False

True

True

All

Returns true if all of the items are True (or if the iterable is empty). All can be thought of as a sequence
of AND operations on the provided iterables. It also short circuit the execution i.e. stop the execution as
soon as the result is known.

Syntax : all(list of iterables)


# Here all the iterables are True so all

# will return True and the same will be printed

print (all([True, True, True, True]))

# Here the method will short-circuit at the

# first item (False) and will return False.

print (all([False, True, True, False]))

# This statement will return False, as no

# True is found in the iterables

print (all([False, False, False]))

Run on IDE

Output :

True

False

False
Inplace vs Standard Operators in Python

Inplace Operators – Set 1, Set 2

Normal operators do the simple assigning job. On other hand, Inplace operators behave similar to
normal operators except that they act in a different manner in case of mutable and Immutable targets.

The _add_ method, does simple addition, takes two arguments, returns the sum and stores it in other
variable without modifying any of the argument.

On the other hand, _iadd_ method also takes two arguments, but it makes in-place change in 1st
argument passed by storing the sum in it. As object mutation is needed in this process, immutable
targets such as numbers, strings and tuples, shouldn’t have _iadd_ method.

Normal operator’s “add()” method, implements “a+b” and stores the result in the mentioned variable.

Inplace operator’s “iadd()” method, implements “a+=b” if it exists (i.e in case of immutable targets, it
doesn’t exist) and changes the value of passed argument. But if not, “a+b” is implemented.

In both the cases assignment is required to do to store the value.


Case 1 : Immutable Targets.

In Immutable targets, such as numbers, strings and tuples. Inplace operator behave same as normal
operators, i.e only assignment takes place, no modification is taken place in the passed arguments.

# Python code to demonstrate difference between

# Inplace and Normal operators in Immutable Targets

# importing operator to handle operator operations

import operator

# Initializing values

x=5

y=6

a=5

b=6

# using add() to add the arguments passed

z = operator.add(a,b)

# using iadd() to add the arguments passed

p = operator.iadd(x,y)

# printing the modified value

print ("Value after adding using normal operator : ",end="")

print (z)
# printing the modified value

print ("Value after adding using Inplace operator : ",end="")

print (p)

# printing value of first argument

# value is unchanged

print ("Value of first argument using normal operator : ",end="")

print (a)

# printing value of first argument

# value is unchanged

print ("Value of first argument using Inplace operator : ",end="")

print (x)

Run on IDE

Output:

Value after adding using normal operator : 11

Value after adding using Inplace operator : 11

Value of first argument using normal operator : 5

Value of first argument using Inplace operator : 5

Case 2 : Mutable Targets

The behaviour of Inplace operators in mutable targets, such as list and dictionaries, is different from
normal operators.The updation and assignment both are carried out in case of mutable targets.

# Python code to demonstrate difference between

# Inplace and Normal operators in mutable Targets


# importing operator to handle operator operations

import operator

# Initializing list

a = [1, 2, 4, 5]

# using add() to add the arguments passed

z = operator.add(a,[1, 2, 3])

# printing the modified value

print ("Value after adding using normal operator : ",end="")

print (z)

# printing value of first argument

# value is unchanged

print ("Value of first argument using normal operator : ",end="")

print (a)

# using iadd() to add the arguments passed

# performs a+=[1, 2, 3]

p = operator.iadd(a,[1, 2, 3])

# printing the modified value

print ("Value after adding using Inplace operator : ",end="")


print (p)

# printing value of first argument

# value is changed

print ("Value of first argument using Inplace operator : ",end="")

print (a)

Run on IDE

Output:

Value after adding using normal operator : [1, 2, 4, 5, 1, 2, 3]

Value of first argument using normal operator : [1, 2, 4, 5]

Value after adding using Inplace operator : [1, 2, 4, 5, 1, 2, 3]

Value of first argument using Inplace operator : [1, 2, 4, 5, 1, 2, 3]


Operator Functions in Python | Set 1

1.3

Python has predefined functions for many mathematical, logical, relational, bitwise etc operations under
the module “operator”. Some of the basic functions are covered in this article.

1. add(a, b) :- This functions returns addition of the given arguments.

Operation – a + b.

2. sub(a, b) :- This functions returns difference of the given arguments.

Operation – a – b.

3. mul(a, b) :- This functions returns product of the given arguments.

Operation – a * b.

# Python code to demonstrate working of

# add(), sub(), mul()

# importing operator module

import operator

# Initializing variables

a=4

b=3
# using add() to add two numbers

print ("The addition of numbers is :",end="");

print (operator.add(a, b))

# using sub() to subtract two numbers

print ("The difference of numbers is :",end="");

print (operator.sub(a, b))

# using mul() to multiply two numbers

print ("The product of numbers is :",end="");

print (operator.mul(a, b))

Run on IDE

Output:

The addition of numbers is :7

The difference of numbers is :1

The product of numbers is :12

4. truediv(a,b) :- This functions returns division of the given arguments.

Operation – a / b.

5. floordiv(a,b) :- This functions also returns division of the given arguments. But the value is floored
value i.e. returns greatest small integer.

Operation – a // b.

6. pow(a,b) :- This functions returns exponentiation of the given arguments.

Operation – a ** b.
7. mod(a,b) :- This functions returns modulus of the given arguments.

Operation – a % b.

# Python code to demonstrate working of

# truediv(), floordiv(), pow(), mod()

# importing operator module

import operator

# Initializing variables

a=5

b=2

# using truediv() to divide two numbers

print ("The true division of numbers is : ",end="");

print (operator.truediv(a,b))

# using floordiv() to divide two numbers

print ("The floor division of numbers is : ",end="");

print (operator.floordiv(a,b))

# using pow() to exponentiate two numbers

print ("The exponentiation of numbers is : ",end="");


print (operator.pow(a,b))

# using mod() to take modulus of two numbers

print ("The modulus of numbers is : ",end="");

print (operator.mod(a,b))

Run on IDE

Output:

The true division of numbers is : 2.5

The floor division of numbers is : 2

The exponentiation of numbers is : 25

The modulus of numbers is : 1

8. lt(a, b) :- This function is used to check if a is less than b or not. Returns true if a is less than b, else
returns false.

Operation – a < b.

9. le(a, b) :- This function is used to check if a is less than or equal to b or not. Returns true if a is less
than or equal to b, else returns false.

Operation – a <= b.

10. eq(a, b) :- This function is used to check if a is equal to b or not. Returns true if a is equal to b, else
returns false.

Operation – a == b.

# Python code to demonstrate working of

# lt(), le() and eq()


# importing operator module

import operator

# Initializing variables

a=3

b=3

# using lt() to check if a is less than b

if(operator.lt(a,b)):

print ("3 is less than 3")

else : print ("3 is not less than 3")

# using le() to check if a is less than or equal to b

if(operator.le(a,b)):

print ("3 is less than or equal to 3")

else : print ("3 is not less than or equal to 3")

# using eq() to check if a is equal to b

if (operator.eq(a,b)):

print ("3 is equal to 3")

else : print ("3 is not equal to 3")

Run on IDE

Output:
3 is not less than 3

3 is less than or equal to 3

3 is equal to 3

11. gt(a,b) :- This function is used to check if a is greater than b or not. Returns true if a is greater than b,
else returns false.

Operation – a > b.

12. ge(a,b) :- This function is used to check if a is greater than or equal to b or not. Returns true if a is
greater than or equal to b, else returns false.

Operation – a >= b.

13. ne(a,b) :- This function is used to check if a is not equal to b or is equal. Returns true if a is not equal
to b, else returns false.

Operation – a != b.

# Python code to demonstrate working of

# gt(), ge() and ne()

# importing operator module

import operator

# Initializing variables

a=4

b=3

# using gt() to check if a is greater than b


if (operator.gt(a,b)):

print ("4 is greater than 3")

else : print ("4 is not greater than 3")

# using ge() to check if a is greater than or equal to b

if (operator.ge(a,b)):

print ("4 is greater than or equal to 3")

else : print ("4 is not greater than or equal to 3")

# using ne() to check if a is not equal to b

if (operator.ne(a,b)):

print ("4 is not equal to 3")

else : print ("4 is equal to 3")

Run on IDE

Output:

4 is greater than 3

4 is greater than or equal to 3

4 is not equal to 3
Loops and Control Statements (continue, break and pass) in Python

1.4

Python programming language provides following types of loops to handle looping requirements.

While Loop

Syntax :

while expression:

statement(s)

In Python, all the statements indented by the same number of character spaces after a programming
construct are considered to be part of a single block of code. Python uses indentation as its method of
grouping statements.

# prints Hello Geek 3 Times

count = 0
while (count < 3):

count = count+1

print("Hello Geek")

Run on IDE

Output:

Hello Geek

Hello Geek

Hello Geek

See this for an example where while loop is used for iterators. As mentioned in the article, it is not
recommended to use while loop for iterators in python.

For in Loop

In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is similar to for
each loop in other languages.

Syntax:

for iterator_var in sequence:

statements(s)

It can be used to iterate over iterators and a range.

# Iterating over a list

print("List Iteration")
l = ["geeks", "for", "geeks"]

for i in l:

print(i)

# Iterating over a tuple (immutable)

print("\nTuple Iteration")

t = ("geeks", "for", "geeks")

for i in t:

print(i)

# Iterating over a String

print("\nString Iteration")

s = "Geeks"

for i in s :

print(i)

# Iterating over dictionary

print("\nDictionary Iteration")

d = dict()

d['xyz'] = 123

d['abc'] = 345

for i in d :

print("%s %d" %(i, d[i]))

Run on IDE

Output:
List Iteration

geeks

for

geeks

Tuple Iteration

geeks

for

geeks

String Iteration

Dictionary Iteration

xyz 123

abc 345

We can use for in loop for user defined iterators. See this for example.
Nested Loops

Python programming language allows to use one loop inside another loop. Following section shows few
examples to illustrate the concept.

Syntax:

for iterator_var in sequence:

for iterator_var in sequence:

statements(s)

statements(s)

The syntax for a nested while loop statement in Python programming language is as follows:

while expression:

while expression:

statement(s)

statement(s)

A final note on loop nesting is that we can put any type of loop inside of any other type of loop. For
example a for loop can be inside a while loop or vice versa.

from __future__ import print_function

for i in range(1, 5):

for j in range(i):

print(i, end=' ')

print()

Run on IDE

Output:
1

22

333

4444

Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope,
all automatic objects that were created in that scope are destroyed. Python supports the following
control statements.

Continue Statement

It returns the control to the beginning of the loop.

# Prints all letters except 'e' and 's'

for letter in 'geeksforgeeks':

if letter == 'e' or letter == 's':

continue

print 'Current Letter :', letter

var = 10

Run on IDE

Output:

Current Letter : g

Current Letter : k

Current Letter : f

Current Letter : o

Current Letter : r
Current Letter : g

Current Letter : k

Break Statement

It brings control out of the loop

for letter in 'geeksforgeeks':

# break the loop as soon it sees 'e'

# or 's'

if letter == 'e' or letter == 's':

break

print 'Current Letter :', letter

Run on IDE

Output:

Current Letter : e

Pass Statement

We use pass statement to write empty loops. Pass is also used for empty control statement, function
and classes.

# An empty loop

for letter in 'geeksforgeeks':

pass

print 'Last Letter :', letter

Run on IDE
Output:

Last Letter : s

Using Iterations in Python Effectively

1.8

Prerequisite : Iterators in Python

Following are different ways to use iterators.

C-style approach:

This approach requires prior knowledge of total number of iterations.

# A C-style way of accessing list elements

cars = ["Aston", "Audi", "McLaren"]

i=0

while (i < len(cars)):

print cars[i]

i += 1

Run on IDE

Output:

Aston
Audi

McLaren

Important Points:

This style of looping is rarely used by python programmers.

This 4-step approach creates no compactness with single-view looping construct.

This is also prone to errors in large-scale programs or designs.

There is no C-Style for loop in Python, i.e., a loop like for (int i=0; i<n; i++)

Use of for-in (or for each) style:

This style is used in python containing iterator of lists, dictonary, n dimensional-arrays etc. The iterator
fetches each component and prints data while looping. The iterator is automatically
incremented/decremented in this construct.

# Accessing items using for-in loop

cars = ["Aston", "Audi", "McLaren"]

for x in cars:

print x

Run on IDE

Output:

Aston

Audi

McLaren
See this for more examples of different data types.

Indexing using Range function: We can also use indexing using range() in Python.

# Accessing items using indexes and for-in

cars = ["Aston", "Audi", "McLaren"]

for i in range(len(cars)):

print cars[i]

Run on IDE

Output:

Aston

Audi

McLaren

Enumerate:

Enumerate is built-in python function that takes input as iterator, list etc and returns a tuple containing
index and data at that index in the iterator sequence. For example, enumerate(cars), returns a iterator
that will return (0, cars[0]), (1, cars[1]), (2, cars[2]), and so on.

# Accessing items using enumerate()

cars = ["Aston" , "Audi", "McLaren "]

for i, x in enumerate(cars):
print (x)

Run on IDE

Output :

Aston

Audi

McLaren

Below solution also works.

# Accessing items and indexes enumerate()

cars = ["Aston" , "Audi", "McLaren "]

for x in enumerate(cars):

print (x[0], x[1])

Run on IDE

Output :

(0, 'Aston')

(1, 'Audi')

(2, 'McLaren ')

We can also directly print returned value of enumerate() to see what it returns.

# Printing return value of enumerate()

cars = ["Aston" , "Audi", "McLaren "]


print enumerate(cars)

Run on IDE

Output :

[(0, 'Aston'), (1, 'Audi'), (2, 'McLaren ')]

Enumerate takes parameter start which is default set to zero. We can change this parameter to any
value we like. In the below code we have used start as 1.

# demonstrating use of start in enumerate

cars = ["Aston" , "Audi", "McLaren "]

for x in enumerate(cars, start=1):

print (x[0], x[1])

Run on IDE

Output :

(1, 'Aston')

(2, 'Audi')

(3, 'McLaren ')

enumerate() helps to embed solution for accessing each data item in iterator and fetching index of each
data item.

Looping extensions:
i) Two iterators for a single looping construct: In this case, a list and dictionary are to be used for each
iteration in a single looping block using enumerate function. Let us see example.

# Two separate lists

cars = ["Aston", "Audi", "McLaren"]

accessories = ["GPS kit", "Car repair-tool kit"]

# Single dictionary holds prices of cars and

# its accessories.

# First two items store prices of cars and

# next three items store prices of accessories.

prices = {1:"570000$", 2:"68000$", 3:"450000$",

4:"890000$", 5:"4500$"}

# Printing prices of cars

for index, c in enumerate(cars, start=1):

print "Car: %s Price: %s"%(c, prices[index])

# Printing prices of accessories

for index, a in enumerate(accessories,start=1):

print ("Accessory: %s Price: %s"\

%(a,prices[index+len(cars)]))

Run on IDE

Output:

Car: Aston Price: 570000$

Car: Audi Price: 68000$


Car: McLaren Price: 450000$

Accessory: GPS kit Price: 890000$

Accessory: Car repair-tool kit Price: 4500$

ii) zip function (Both iterators to be used in single looping construct):

This function is helpful to combine similar type iterators(list-list or dict- dict etc) data items at ith
position. It uses shortest length of these input iterators. Other items of larger length iterators are
skipped. In case of empty iterators it returns No output.

For example, the use of zip for two lists (iterators) helped to combine a single car and its required
accessory.

# Python program to demonstrate working of zip

# Two separate lists

cars = ["Aston", "Audi", "McLaren"]

accessories = ["GPS", "Car Repair Kit",

"Dolby sound kit"]

# Combining lists and printing

for c, a in zip(cars, accessories):

print "Car: %s, Accessory required: %s"\

%(c, a)

Run on IDE

Output:

Car: Aston, Accessory required: GPS

Car: Audi, Accessory required: Car Repair Kit


Car: McLaren, Accessory required: Dolby sound kit

The reverse of getting iterators from zip function is known as unzipping using “*” operator.

Use of enumerate function and zip function helps to achieve an effective extension of iteration logic in
python and solves many more sub-problems of a huge task or problem.

# Python program to demonstrate unzip (reverse

# of zip)using * with zip function

# Unzip lists

l1,l2 = zip(*[('Aston', 'GPS'),

('Audi', 'Car Repair'),

('McLaren', 'Dolby sound kit')

])

# Printing unzipped lists

print(l1)

print(l2)

Run on IDE

Output:

('Aston', 'Audi', 'McLaren')

('GPS', 'Car Repair', 'Dolby sound kit')


Counters in Python | Set 1 (Initialization and Updation)

What is counter?

Counter is a container included in the collections module.

What is Container?

Containers are objects that hold objects. They provide a way to access the contained objects and iterate
over them. Examples of built in containers are Tuple, list and dictionary. Others are included in
Collections module.

A Counter is a subclass of dict. Therefore it is an unordered collection where elements and their
respective count are stored as dictionary. This is equivalent to bag or multiset of other languages.

Syntax :

class collections.Counter([iterable-or-mapping])
Initialization :

The constructor of counter can be called in any one of the following ways :

With sequence of items

With dictionary containing keys and counts

With keyword arguments mapping string names to counts

Example of each type of initialization :

# A Python program to show different ways to create

# Counter

from collections import Counter

# With sequence of items

print Counter(['B','B','A','B','C','A','B','B','A','C'])

# with dictionary

print Counter({'A':3, 'B':5, 'C':2})

# with keyword arguments

print Counter(A=3, B=5, C=2)

Run on IDE

Output of all the three lines is same :

Counter({'B': 5, 'A': 3, 'C': 2})

Counter({'B': 5, 'A': 3, 'C': 2})


Counter({'B': 5, 'A': 3, 'C': 2})

Updation :

We can also create an empty counter in the following manner :

coun = collections.Counter()

And can be updated via update() method .Syntax for the same :

coun.update(Data)

# A Python program to demonstrate update()

from collections import Counter

coun = Counter()

coun.update([1, 2, 3, 1, 2, 1, 1, 2])

print(coun)

coun.update([1, 2, 4])

print(coun)

Run on IDE

Output :

Counter({1: 4, 2: 3, 3: 1})

Counter({1: 5, 2: 4, 3: 1, 4: 1})

Data can be provided in any of the three ways as mentioned in initialization and the counter’s data will
be increased not replaced.

Counts can be zero and negative also.

# Python program to demonstrate that counts in


# Counter can be 0 and negative

from collections import Counter

c1 = Counter(A=4, B=3, C=10)

c2 = Counter(A=10, B=3, C=4)

c1.subtract(c2)

print(c1)

Run on IDE

Output :

Counter({'c': 6, 'B': 0, 'A': -6})

We can use Counter to count distinct elements of a list or other collections.

# An example program where different list items are

# counted using counter

from collections import Counter

# Create a list

z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']

# Count distinct elements and print Counter aboject

print(Counter(z))

Run on IDE

Output:
Counter({'blue': 3, 'red': 2, 'yellow': 1})

Counters in Python | Set 2 (Accessing Counters)

1.8

Counters in Python | Set 1 (Initialization and Updation)

Once initialized, counters are accessed just like dictionaries. Also, it does not raise the KeyValue error (if
key is not present) instead the value’s count is shown as 0.

Example :

# Python program to demonstrate accessing of

# Counter elements

from collections import Counter

# Create a list

z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']

col_count = Counter(z)

print(col_count)

col = ['blue','red','yellow','green']
# Here green is not in col_count

# so count of green will be zero

for color in col:

print (color, col_count[color])

Run on IDE

Output:

Counter({'blue': 3, 'red': 2, 'yellow': 1})

blue 3

red 2

yellow 1

green 0

elements() :

The elements() method returns an iterator that produces all of the items known to the Counter.

Note : Elements with count <= 0 are not included.

Example :

# Python example to demonstrate elements() on

# Counter (gives back list)

from collections import Counter

coun = Counter(a=1, b=2, c=3)

print(coun)
print(list(coun.elements()))

Run on IDE

Output :

Counter({'c': 3, 'b': 2, 'a': 1})

['a', 'b', 'b', 'c', 'c', 'c']

most_common() :

most_common() is used to produce a sequence of the n most frequently encountered input values and
their respective counts.

# Python example to demonstrate most_elements() on

# Counter

from collections import Counter

coun = Counter(a=1, b=2, c=3, d=120, e=1, f=219)

# This prints 3 most frequent characters

for letter, count in coun.most_common(3):

print('%s: %d' % (letter, count))

Run on IDE

Output :

f: 219

d: 120
c: 3

Iterators in Python

2.1

Iterator in python is any python type that can be used with a ‘for in loop’. Python lists, tuples, dicts and
sets are all examples of inbuilt iterators. These types are iterators because they implement following
methods. In fact, any object that wants to be an iterator must implement following methods.
__iter__ method that is called on initialization of an iterator. This should return an object that has a next
or __next__ (in Python 3) method.

next ( __next__ in Python 3) The iterator next method should return the next value for the iterable.
When an iterator is used with a ‘for in’ loop, the for loop implicitly calls next() on the iterator object. This
method should raise a StopIteration to signal the end of the iteration.

Below is a simple Python program that creates iterator type that iterates from 10 to given limit. For
example, if limit is 15, then it prints 10 11 12 13 14 15. And if limit is 5, then it prints nothing.

# A simple Python program to demonstrate

# working of iterators using an example type

# that iterates from 10 to given value

# An iterable user defined type

class Test:

# Cosntructor

def __init__(self, limit):

self.limit = limit

# Called when iteration is initialized

def __iter__(self):

self.x = 10

return self

# To move to next element. In Python 3,

# we should replace next with __next__

def next(self):
# Store current value ofx

x = self.x

# Stop iteration if limit is reached

if x > self.limit:

raise StopIteration

# Else increment and return old value

self.x = x + 1;

return x

# Prints numbers from 10 to 15

for i in Test(15):

print(i)

# Prints nothing

for i in Test(5):

print(i)

Run on IDE

Output :

10

11

12
13

14

15

Examples of inbuilt iterator types.

# Sample built-in iterators

# Iterating over a list

print("List Iteration")

l = ["geeks", "for", "geeks"]

for i in l:

print(i)

# Iterating over a tuple (immutable)

print("\nTuple Iteration")

t = ("geeks", "for", "geeks")

for i in t:

print(i)

# Iterating over a String

print("\nString Iteration")

s = "Geeks"

for i in s :

print(i)
# Iterating over dictionary

print("\nDictionary Iteration")

d = dict()

d['xyz'] = 123

d['abc'] = 345

for i in d :

print("%s %d" %(i, d[i]))

Run on IDE

Output :

List Iteration

geeks

for

geeks

Tuple Iteration

geeks

for

geeks

String Iteration

k
s

Dictionary Iteration

xyz 123

abc 345

Iterator Functions in Python | Set 1

2.5

Perquisite: Iterators in Python


Python in its definition also allows some interesting and useful iterator functions for efficient looping
and making execution of the code faster. There are many build-in iterators in the module “itertools“.

This module implements a number of iterator building blocks.

Some useful Iterators :

1. accumulate(iter, func) :- This iterator takes two arguments, iterable target and the function which
would be followed at each iteration of value in target. If no function is passed, addition takes place by
default.If the input iterable is empty, the output iterable will also be empty.

2. chain(iter1, iter2..) :- This function is used to print all the values in iterable targets one after another
mentioned in its arguments.

# Python code to demonstrate the working of

# accumulate() and chain()

# importing "itertools" for iterator operations

import itertools

# importing "operator" for operator operations

import operator

# initializing list 1

li1 = [1, 4, 5, 7]

# initializing list 2

li2 = [1, 6, 5, 9]
# initializing list 3

li3 = [8, 10, 5, 4]

# using accumulate()

# prints the successive summation of elements

print ("The sum after each iteration is : ",end="")

print (list(itertools.accumulate(li1)))

# using accumulate()

# prints the successive multiplication of elements

print ("The product after each iteration is : ",end="")

print (list(itertools.accumulate(li1,operator.mul)))

# using chain() to print all elements of lists

print ("All values in mentioned chain are : ",end="")

print (list(itertools.chain(li1,li2,li3)))

Run on IDE

Output:

The sum after each iteration is : [1, 5, 10, 17]

The product after each iteration is : [1, 4, 20, 140]

All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]

3. chain.from_iterable() :- This function is implemented similarly as chain() but the argument here is a
list of lists or any other iterable container.
4. compress(iter, selector) :- This iterator selectively picks the values to print from the passed container
according to the boolean list value passed as other argument. The arguments corresponding to boolean
true are printed else all are skipped.

# Python code to demonstrate the working of

# chain.from_iterable() and compress()

# importing "itertools" for iterator operations

import itertools

# initializing list 1

li1 = [1, 4, 5, 7]

# initializing list 2

li2 = [1, 6, 5, 9]

# initializing list 3

li3 = [8, 10, 5, 4]

# intializing list of list

li4 = [li1, li2, li3]

# using chain.from_iterable() to print all elements of lists

print ("All values in mentioned chain are : ",end="")

print (list(itertools.chain.from_iterable(li4)))
# using compress() selectively print data values

print ("The compressed values in string are : ",end="")

print (list(itertools.compress('GEEKSFORGEEKS',[1,0,0,0,0,1,0,0,1,0,0,0,0])))

Run on IDE

Output:

All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]

The compressed values in string are : ['G', 'F', 'G']

5. dropwhile(func, seq) :- This iterator starts printing the characters only after the func. in argument
returns false for the first time.

6. filterfalse(func, seq) :- As the name suggests, this iterator prints only values that return false for the
passed function.

# Python code to demonstrate the working of

# dropwhile() and filterfalse()

# importing "itertools" for iterator operations

import itertools

# initializing list

li = [2, 4, 5, 7, 8]

# using dropwhile() to start displaying after condition is false

print ("The values after condition returns false : ",end="")

print (list(itertools.dropwhile(lambda x : x%2==0,li)))


# using filterfalse() to print false values

print ("The values that return false to function are : ",end="")

print (list(itertools.filterfalse(lambda x : x%2==0,li)))

Run on IDE

Output:

The values after condition returns false : [5, 7, 8]

The values that return false to function are : [5, 7]

Looping Techniques in Python

2.2

Python supports various looping techniques by certain inbuilt functions, in various sequential containers.
These methods are primarily verya useful in competitive programming and also in various project which
require a specific technique with loops maintaining the overall structure of code.
Where they are used ?

Different looping techniques are primarily useful in the places where we don’t need to actually
manipulate the structure and ordering of overall container, rather only print the elements for a single
use instance, no inplace change occurs in the container. This can also be used in instances to save time.

Different looping techniques using Python data structures are:

Using enumerate(): enumerate() is used to loop through the containers printing the index number along
with the value present in that particular index.

# python code to demonstrate working of enumerate()

for key, value in enumerate(['The', 'Big', 'Bang', 'Theory']):

print(key, value)

Run on IDE

Output :

0 The

1 Big

2 Bang

3 Theory

Using zip(): zip() is used to combine 2 similar containers(list-list or dict-dict) printing the values
sequentially. The loop exists only till the smaller container ends. The detailed explanation of zip() and
enumerate() can be found here.

# python code to demonstrate working of zip()

# initializing list
questions = ['name', 'colour', 'shape']

answers = ['apple', 'red', 'a circle']

# using zip() to combine two containers

# and print values

for question, answer in zip(questions, answers):

print('What is your {0}? I am {1}.'.format(question, answer))

Run on IDE

Output :

What is your name? I am apple.

What is your color? I am red.

What is your shape? I am a circle.

Using iteritem(): iteritems() is used to loop through the dictionary printing the dictionary key-value pair
sequentially.

Using items(): items() performs the similar task on dictionary as iteritems() but have certain
disadvantages when compared with iteritems().

It is very time consuming. Calling it on large dictionaries consumes quite a lot of time.

It takes a lot of memory. Sometimes takes double the memory when called on dictionary.

Example 1:

# python code to demonstrate working of iteritems(),items()

d = { "geeks" : "for", "only" : "geeks" }

# using iteritems to print the dictionary key-value pair

print ("The key value pair using iteritems is : ")


for i,j in d.iteritems():

print i,j

# using items to print the dictionary key-value pair

print ("The key value pair using items is : ")

for i,j in d.items():

print i,j

Run on IDE

Output:

The key value pair using iteritems is :

geeks for

only geeks

The key value pair using items is :

geeks for

only geeks

Example 2:

# python code to demonstrate working of items()

king = {'Akbar': 'The Great', 'Chandragupta': 'The Maurya',

'Modi' : 'The Changer'}

# using items to print the dictionary key-value pair

for key, value in king.items():

print(key, value)
Run on IDE

Output :

Akbar The Great

Modi The Changer

Chandragupta The Maurya

Using sorted(): sorted() is used to print the container is sorted order. It doesn’t sort the container, but
just prints the container in sorted order for 1 instance. Use of set() can be combined to remove
duplicate occurrences.

Example 1:

# python code to demonstrate working of sorted()

# initializing list

lis = [ 1 , 3, 5, 6, 2, 1, 3 ]

# using sorted() to print the list in sorted order

print ("The list in sorted order is : ")

for i in sorted(lis) :

print (i,end=" ")

print ("\r")

# using sorted() and set() to print the list in sorted order

# use of set() removes duplicates.

print ("The list in sorted order (without duplicates) is : ")

for i in sorted(set(lis)) :
print (i,end=" ")

Run on IDE

Output:

The list in sorted order is :

1123356

The list in sorted order (without duplicates) is :

12356

Example 2:

# python code to demonstrate working of sorted()

# initializing list

basket = ['guave', 'orange', 'apple', 'pear',

'guava', 'banana', 'grape']

# using sorted() and set() to print the list

# in sorted order

for fruit in sorted(set(basket)):

print(fruit)

Run on IDE

Output:

apple

banana

grape
guava

guave

orange

pear

Using reversed(): reversed() is used to print the values of container in the descending order as declared.

Example 1:

# python code to demonstrate working of reversed()

# initializing list

lis = [ 1 , 3, 5, 6, 2, 1, 3 ]

# using revered() to print the list in reversed order

print ("The list in reversed order is : ")

for i in reversed(lis) :

print (i,end=" ")

Run on IDE

Output:

The list in reversed order is :

3126531

Example 2:

# python code to demonstrate working of reversed()

# using reversed() to print in reverse order


for i in reversed(range(1, 10, 3)):

print (i)

Run on IDE

Output :

Advantage of using above techniques over for, while loop

These techniques are quick to use and reduces coding effort. for, while loops needs the entire structure
of container to be changed.

These Looping techniques do not require any structural changes to container. They have keywords
which present the exact purpose of usage. Whereas, no pre-predictions or guesses can be made in for,
while loop i.e not easily understandable the purpose at a glance.

Looping technique make the code more concise than using for, while loopings.
Patterns can be printed in python using simple for loops. First outer loop is used to handle number of
rows and Inner nested loop is used to handle the number of columns. Manipulating the print
statements, different number patterns, alphabet patterns or star patterns can be printed.

Some of the Patterns are shown in this article.

Simple pyramid pattern

# Python 3.x code to demonstrate star pattern

# Function to demonstrate printing pattern

def pypart(n):
# outer loop to handle number of rows

# n in this case

for i in range(0, n):

# inner loop to handle number of columns

# values changing acc. to outer loop

for j in range(0, i+1):

# printing stars

print("* ",end="")

# ending line after each row

print("\r")

# Driver Code

n=5

pypart(n)

Run on IDE

Output:

**

***

****
*****

After 180 degree rotation

# Python 3.x code to demonstrate star pattern

# Function to demonstrate printing pattern

def pypart2(n):

# number of spaces

k = 2*n - 2

# outer loop to handle number of rows

for i in range(0, n):

# inner loop to handle number spaces

# values changing acc. to requirement

for j in range(0, k):

print(end=" ")

# decrementing k after each loop

k=k-2

# inner loop to handle number of columns

# values changing acc. to outer loop

for j in range(0, i+1):


# printing stars

print("* ", end="")

# ending line after each row

print("\r")

# Driver Code

n=5

pypart2(n)

Run on IDE

Output:

**

***

****

*****

Printing Triangle

# Python 3.x code to demonstrate star pattern

# Function to demonstrate printing pattern triangle

def triangle(n):

# number of spaces

k = 2*n - 2
# outer loop to handle number of rows

for i in range(0, n):

# inner loop to handle number spaces

# values changing acc. to requirement

for j in range(0, k):

print(end=" ")

# decrementing k after each loop

k=k-1

# inner loop to handle number of columns

# values changing acc. to outer loop

for j in range(0, i+1):

# printing stars

print("* ", end="")

# ending line after each row

print("\r")

# Driver Code

n=5

triangle(n)
Run on IDE

Output:

**

***

****

*****

Number Pattern

# Python 3.x code to demonstrate star pattern

# Function to demonstrate printing pattern of numbers

def numpat(n):

# initialising starting number

num = 1

# outer loop to handle number of rows

for i in range(0, n):

# re assigning num

num = 1

# inner loop to handle number of columns

# values changing acc. to outer loop


for j in range(0, i+1):

# printing number

print(num, end=" ")

# incrementing number at each column

num = num + 1

# ending line after each row

print("\r")

# Driver code

n=5

numpat(n)

Run on IDE

Output:

12

123

1234

12345

Numbers without re assigning

# Python 3.x code to demonstrate star pattern


# Function to demonstrate printing pattern of numbers

def contnum(n):

# initializing starting number

num = 1

# outer loop to handle number of rows

for i in range(0, n):

# not re assigning num

# num = 1

# inner loop to handle number of columns

# values changing acc. to outer loop

for j in range(0, i+1):

# printing number

print(num, end=" ")

# incrementing number at each column

num = num + 1

# ending line after each row

print("\r")
n=5

# sending 5 as argument

# calling Function

contnum(n)

Run on IDE

Output:

23

456

7 8 9 10

11 12 13 14 15

Character Pattern

# Python 3.x code to demonstrate star pattern

# Function to demonstrate printing pattern of alphabets

def alphapat(n):

# initializing value corresponding to 'A'

# ASCII value

num = 65

# outer loop to handle number of rows

# 5 in this case
for i in range(0, n):

# inner loop to handle number of columns

# values changing acc. to outer loop

for j in range(0, i+1):

# explicitely converting to char

ch = chr(num)

# printing char value

print(ch, end=" ")

# incrementing number

num = num + 1

# ending line after each row

print("\r")

# Driver Code

n=5

alphapat(n)

Run on IDE

Output:

A
BB

CCC

DDDD

EEEEE

Continuous Character pattern

# Python code 3.x to demonstrate star pattern

# Function to demonstrate printing pattern of alphabets

def contalpha(n):

# initializing value corresponding to 'A'

# ASCII value

num = 65

# outer loop to handle number of rows

- for i in range(0, n):

# inner loop to handle number of columns

# values changing acc. to outer loop

for j in range(0, i+1):

# explicitely converting to char

ch = chr(num)

# printing char value


print(ch, end=" ")

# incrementing at each column

num = num +1

# ending line after each row

print("\r")

# Driver code

n=5

contalpha(n)

Run on IDE

Output:

BC

DEF

GHIJ

KLMNO
ange() vs xrange() in Python

1.3

range() and xrange() are two functions that could be used to iterate a certain number of times in for
loops in Python. In Python 3, there is no xrange , but the range function behaves like xrange in Python
2.If you want to write code that will run on both Python 2 and Python 3, you should use range().

range() – This returns a list of numbers created using range() function.

xrange() – This function returns the generator object that can be used to display numbers only by
looping. Only particular range is displayed on demand and hence called “lazy evaluation“.

Both are implemented in different ways and have different characteristics associated with them. The
points of comparisons are:

Return Type

Memory

Operation Usage

Speed

Return Type

range() returns – the list as return type.

xrange() returns – xrange() object.


# Python code to demonstrate range() vs xrange()

# on basis of return type

# initializing a with range()

a = range(1,10000)

# initializing a with xrange()

x = xrange(1,10000)

# testing the type of a

print ("The return type of range() is : ")

print (type(a))

# testing the type of x

print ("The return type of xrange() is : ")

print (type(x))

Run on IDE

Output:

The return type of range() is :

The return type of xrange() is :

Memory
The variable storing the range created by range() takes more memory as compared to variable storing
the range using xrange(). The basic reason for this is the return type of range() is list and xrange() is
xrange() object.

# Python code to demonstrate range() vs xrange()

# on basis of memory

import sys

# initializing a with range()

a = range(1,10000)

# initializing a with xrange()

x = xrange(1,10000)

# testing the size of a

# range() takes more memory

print ("The size allotted using range() is : ")

print (sys.getsizeof(a))

# testing the size of a

# range() takes less memory

print ("The size allotted using xrange() is : ")

print (sys.getsizeof(x))

Run on IDE

Output:
The size allotted using range() is :

80064

The size allotted using xrange() is :

40

Operations usage

As range() returns the list, all the operations that can be applied on the list can be used on it. On the
other hand, as xrange() returns the xrange object, operations associated to list cannot be applied on
them, hence a disadvantage.

# Python code to demonstrate range() vs xrange()

# on basis of operations usage

# initializing a with range()

a = range(1,6)

# initializing a with xrange()

x = xrange(1,6)

# testing usage of slice operation on range()

# prints without error

print ("The list after slicing using range is : ")

print (a[2:5])

# testing usage of slice operation on xrange()

# raises error
print ("The list after slicing using xrange is : ")

print (x[2:5])

Run on IDE

Error:

Traceback (most recent call last):

File "1f2d94c59aea6aed795b05a19e44474d.py", line 18, in

print (x[2:5])

TypeError: sequence index must be integer, not 'slice'

Output:

The list after slicing using range is :

[3, 4, 5]

The list after slicing using xrange is :

Speed

Because of the fact that xrange() evaluates only the generator object containing only the values that are
required by lazy evaluation, therefore is faster in implementation than range().

Important Points:

If you want to write code that will run on both Python 2 and Python 3, use range() as the xrange funtion
is deprecated in Python 3

range() is faster if iterating over the same sequence multiple times.

xrange() has to reconstruct the integer object every time, but range() will have real integer objects. (It
will always perform worse in terms of memory however)
Send mail with attachment from your Gmail account using Python

4.3

In last article, we have discussed the basics of sending a mail from a Gmail account without any subject
as well as without any attachment. Today, we will learn how to send mail with attachment and subject
using Python. Before moving on, it is highly recommended to learn how to send a simple mail using
Python and learn the basics working of ‘smtplib’ library of Python.

If you have read the previous article, you have gained the knowledge how a session is created and how
it works. Now, you need to learn to attach a file and subject to the mail. For that you need to import
some native libraries of Python. From these libraries, you need to import the tools used in our programs.

Steps to send mail with Attachments from Gmail account:

For adding an attachment, you need to import:


import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.base import MIMEBase

from email import encoders

These are some libraries which will make our work simple. These are the native libraries and you dont
need to import any external library for this.

Firstly, create an instance of MIMEMultipart, namely “msg” to begin with.

Mention the sender’s email id, receiver’s email id and the subject in the “From”, “To” and “Subject” key
of the created instance “msg”.

In a string, write the body of the message you want to send, namely body. Now, attach the body with
the instance msg using attach function.

Open the file you wish to attach in the “rb” mode. Then create an instance of MIMEBase with two
parameters. First one is ‘_maintype’ amd the other one is ‘_subtype’. This is the base class for all the
MIME-specific sub-classes of Message.

Note that ‘_maintype’ is the Content-Type major type (e.g. text or image), and ‘_subtype’ is the Content-
Type minor type (e.g. plain or gif or other media).

set_payload is used to change the payload the encoded form. Encode it in encode_base64. And finally
attach the file with the MIMEMultipart created instance msg.

After finishing up these steps, follow the instructions described in the previous article to create a
session, secure it and check the authenticity and then after sending the mail, terminate the session.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

# Python code to illustrate Sending mail with attachments

# from your Gmail account

# libraries to be imported

import smtplib
from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.base import MIMEBase

from email import encoders

fromaddr = "EMAIL address of the sender"

toaddr = "EMAIL address of the receiver"

# instance of MIMEMultipart

msg = MIMEMultipart()

# storing the senders email address

msg['From'] = fromaddr

# storing the receivers email address

msg['To'] = toaddr

# storing the subject

msg['Subject'] = "Subject of the Mail"

# string to store the body of the mail

body = "Body_of_the_mail"

# attach the body with the msg instance

msg.attach(MIMEText(body, 'plain'))
# open the file to be sent

filename = "File_name_with_extension"

attachment = open("Path of the file", "rb")

# instance of MIMEBase and named as p

p = MIMEBase('application', 'octet-stream')

# To change the payload into encoded form

p.set_payload((attachment).read())

# encode into base64

encoders.encode_base64(p)

p.add_header('Content-Disposition', "attachment; filename= %s" % filename)

# attach the instance 'p' to instance 'msg'

msg.attach(p)

# creates SMTP session

s = smtplib.SMTP('smtp.gmail.com', 587)

# start TLS for security

s.starttls()
# Authentication

s.login(fromaddr, "Password_of_the_sender")

# Converts the Multipart msg into a string

text = msg.as_string()

# sending the mail

s.sendmail(fromaddr, toaddr, text)

# terminating the session

s.quit()

Run on IDE

Important Points:

You can use loops to send mails to a number of people.

This code is simple to implement. But it will not work if you have enabled 2-step verification on your
gmail account. It is required to switch off the 2-step verification first.

Using this method, Gmail will always put your mail in the primary section and the mails sent will not be
Spam.
Python Desktop News Notifier in 20 lines

To get started with the Desktop News Notifier, we require two libraries: feedparser and notify2.

Give following command to to install feedparser:

sudo pip3 install feedparser

For installing notify2 in your terminal:

sudo pip3 install notify2

Feedparser wil parse the feed that we will get from the URL. We will use notify2 for the desktop
notification purpose. Other than these two libararies, we will use OS and time lib. Once you are done
with the installation import both libraries in the program. Here, in this example i have parsed the news
from the BBC UK, you can use any news feedparser URL. Let’s have a look at the program:

# Python program to illustrate

# desktop news notifier

import feedparser

import notify2

import os

import time

def parseFeed():

f = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")

ICON_PATH = os.getcwd() + "/icon.ico"

notify2.init('News Notify')

for newsitem in f['items']:

n = notify2.Notification(newsitem['title'],
newsitem['summary'],

icon=ICON_PATH

n.set_urgency(notify2.URGENCY_NORMAL)

n.show()

n.set_timeout(15000)

time.sleep(1200)

if _name_ = '_main_':

parseFeed()

Run on IDE

Screenshot of the news notification popup

Python Desktop News Notifier in 20 lines

Step by step Explanation of Code:

f = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")

Here feedparser will parse the news data from the feed URL. The parsed data will be in the form of
dictionary.

ICON_PATH = os.getcwd() + "/icon.ico"

If you want to set any icon in the notification then here we are setting the Icon path. This is optional.

notify2.init('News Notify')
Here we are initializing the notify2 using the init method of notify2. Initialize the D-Bus connection. Must
be called before you send any notifications, or retrieve server info or capabilities.

for newsitem in f['items']:

n = notify2.Notification(newsitem['title'],

newsitem['summary'],

icon=ICON_PATH

Looping from the parsed data to get the relevant information like news title, short summary and setting
the notification icon using the Notification method of the notify2 lib.

n.set_urgency(notify2.URGENCY_NORMAL)

Set the urgency level to one of URGENCY_LOW, URGENCY_NORMAL or URGENCY_CRITICAL

n.show()

This method will show the notification on the Desktop

n.set_timeout(15000)

Setting the time to keep the notification on the desktop (in milliseconds). I have set here as 15 seconds.

time.sleep(1200)

This will usually display the news notification every 20 mins. You can set the time as per your
requirement. You can find the full source code that is hosted on GitHub
Hangman Game in Python

Hangman Wiki:

The origins of Hangman are obscure meaning not discovered, but it seems to have arisen in Victorian
times, ” says Tony Augarde, author of The Oxford Guide to Word Games. The game is mentioned in Alice
Bertha Gomme’s “Traditional Games” in 1894 under the name “Birds, Beasts and Fishes.” The rules are
simple; a player writes down the first and last letters of a word and another player guesses the letters in
between. In other sources, [where?] the game is called “Gallows”, “The Game of Hangin”, or “Hanger”.

Implementation

This is a simple Hangman game using Python programming language. Beginners can use this as a small
project to boost their programming skills and understanding logic.

The Hangman program randomly selects a secret word from a list of secret words. The random module
will provide this ability, so line 1 in program imports it.

The Game: Here, a random word (a fruit name) is picked up from our collection and the player gets
limited chances to win the game.

When a letter in that word is guessed correctly, that letter position in the word is made visible. In this
way, all letters of the word are to be guessed before all the chances are over.

For convenience, we have given length of word + 2 chances. For example, word to be guessed is mango,
then user gets 5 + 2 = 7 chances, as mango is a five letter word.

# Python Program to illustrate

# Hangman Game

import random

from collections import Counter

someWords = '''apple banana mango strawberry

orange grape pineapple apricot lemon coconut watermelon


cherry papaya berry peach lychee muskmelon'''

someWords = someWords.split(' ')

# randomly choose a secret word from our "someWords" LIST.

word = random.choice(someWords)

if __name__ == '__main__':

print('Guess the word! HINT: word is a name of a fruit')

for i in word:

# For printing the empty spaces for letters of the word

print('_', end = ' ')

print()

playing = True

# list for storing the letters guessed by the player

letterGuessed = ''

chances = len(word) + 2

correct = 0

try:

while (chances != 0):

print()

chances -= 1
try:

guess = str(input('Enter a letter to guess: '))

except:

print('Enter only a letter!')

continue

# Validation of the guess

if not guess.isalpha():

print('Enter only a LETTER')

continue

elif len(guess) > 1:

print('Enter only a SINGLE letter')

continue

elif guess in letterGuessed:

print('You have already guessed that letter')

continue

# If letter is guessed correcly

if guess in word:

letterGuessed += guess

# Print the word

for char in word:

if char in letterGuessed:
print(char, end = ' ')

correct += 1

else:

print('_', end = ' ')

# If user has guessed all the letters

if (Counter(letterGuessed) == Counter(word)):

print()

print('Congratulations, You won!')

break

# If user has used all of his chances

if chances == 0:

print()

print('You lost! Try again..')

print('The word was {}'.format(word))

except KeyboardInterrupt:

print()

print('Bye! Try again.')

exit()

# print(letterGuessed)

Run on IDE

Note: Please run the program on your terminal.


omkarpathak@omkarpathak-Inspiron-3542:~/Documents/

Python-Programs$ python P37_HangmanGame.py

Guess the word! HINT: word is a name of a fruit

_____

Enter a letter to guess: m

__m__

Enter a letter to guess: o

__mo_

Enter a letter to guess: l

l_mo_

Enter a letter to guess: e

lemo_

Enter a letter to guess: n

lemon

Congratulations, You won!


Birthday Reminder Application in Python

This app helps in reminding birthdays and notifying you friend’s birthdays. This app uses Python and
Ubuntu notifications to notify users on every startup of the system.

# Python program For

# Birthday Reminder Application

# time module is must as reminder


# is set with the help of dates

import time

# os module is used to notify user

# using default "Ubuntu" notification bar

import os

# Birthday file is the one in which the actual birthdays

# and dates are present. This file can be

# manually edited or can be automated.

# For simplicity, we will edit it manually.

# Birthdays should be written in this file in

# the format: "MonthDay Name Surname" (Without Quotes)

birthdayFile = '/path/to/birthday/file'

def checkTodaysBirthdays():

fileName = open(birthdayFile, 'r')

today = time.strftime('%m%d')

flag = 0

for line in fileName:

if today in line:

line = line.split(' ')

flag =1

# line[1] contains Name and line[2] contains Surname


os.system('notify-send "Birthdays Today: ' + line[1]

+ ' ' + line[2] + '"')

if flag == 0:

os.system('notify-send "No Birthdays Today!"')

if __name__ == '__main__':

checkTodaysBirthdays()

Run on IDE

Adding the script to Startup

After writing the above code now it is the time to add this Python script to startup. This can be done in
Ubuntu as follows:

Firstly, we have to create an executable file for our reminder.py script

This can be done by typing the following command in the terminal

sudo chmod +x reminder.py, where reminder.py is our script file name

Now we have to transfer this file to the path where Linux searches for its default files:

Type this command in terminal:

sudo cp /path/to/our/reminder.py /usr/bin

. This will add our executable script to /usr/bin.

In global search, search for Startup Applications

Click on Add and Give a desired Name for your process

Type in the command. For example, our file name is reminder.py then type reminder.py in the command
field and Select Add
Speech Recognition in Python using Google Speech API
4.5

Speech Recognition is an important feature in several applications used such as home automation,
artificial intelligence, etc. This article aims to provide an introduction on how to make use of the
SpeechRecognition library of Python. This is useful as it can be used on microcontrollers such as
Raspberri Pis with the help of an external microphone.

Required Installations

The following must be installed:

Python Speech Recognition module:

sudo pip install SpeechRecognition

PyAudio: Use the following command for linux users

sudo apt-get install python-pyaudio python3-pyaudio

If the versions in the repositories are too old, install pyaudio using the following command

sudo apt-get install portaudio19-dev python-all-dev python3-all-dev &&

sudo pip install pyaudio

Use pip3 instead of pip for python3.

Windows users can install pyaudio by executing the following command in a terminal

pip install pyaudio

Speech Input Using a Microphone and Translation of Speech to Text

Configure Microphone (For external microphones): It is advisable to specify the microphone during the
program to avoid any glitches.
Type lsusb in the terminal. A list of connected devices will show up. The microphone name would look
like this

USB Device 0x46d:0x825: Audio (hw:1, 0)

Make a note of this as it will be used in the program.

Set Chunk Size: This basically involved specifying how many bytes of data we want to read at once.
Typically, this value is specified in powers of 2 such as 1024 or 2048

Set Sampling Rate: Sampling rate defines how often values are recorded for processing

Set Device ID to the selected microphone: In this step, we specify the device ID of the microphone that
we wish to use in order to avoid ambiguity in case there are multiple microphones. This also helps
debug, in the sense that, while running the program, we will know whether the specified microphone is
being recognized. During the program, we specify a parameter device_id. The program will say that
device_id could not be found if the microphone is not recognized.

Allow Adjusting for Ambient Noise: Since the surrounding noise varies, we must allow the program a
second or too to adjust the energy threshold of recording so it is adjusted according to the external
noise level.

Speech to text translation: This is done with the help of Google Speech Recognition. This requires an
active internet connection to work. However, there are certain offline Recognition systems such as
PocketSphinx, but have a very rigorous installation process that requires several dependencies. Google
Speech Recognition is one of the easiest to use.

The Above steps have been implemented below:

#Python 2.x program for Speech Recognition

import speech_recognition as sr

#enter the name of usb microphone that you found

#using lsusb

#the following name is only used as an example

mic_name = "USB Device 0x46d:0x825: Audio (hw:1, 0)"


#Sample rate is how often values are recorded

sample_rate = 48000

#Chunk is like a buffer. It stores 2048 samples (bytes of data)

#here.

#it is advisable to use powers of 2 such as 1024 or 2048

chunk_size = 2048

#Initialize the recognizer

r = sr.Recognizer()

#generate a list of all audio cards/microphones

mic_list = sr.Microphone.list_microphone_names()

#the following loop aims to set the device ID of the mic that

#we specifically want to use to avoid ambiguity.

for i, microphone_name in enumerate(mic_list):

if microphone_name == mic_name:

device_id = i

#use the microphone as source for input. Here, we also specify

#which device ID to specifically look for incase the microphone

#is not working, an error will pop up saying "device_id undefined"

with sr.Microphone(device_index = device_id, sample_rate = sample_rate,

chunk_size = chunk_size) as source:

#wait for a second to let the recognizer adjust the

#energy threshold based on the surrounding noise level


r.adjust_for_ambient_noise(source)

print "Say Something"

#listens for the user's input

audio = r.listen(source)

try:

text = r.recognize_google(audio)

print "you said: " + text

#error occurs when google could not understand what was said

except sr.UnknownValueError:

print("Google Speech Recognition could not understand audio")

except sr.RequestError as e:

print("Could not request results from Google

Speech Recognition service; {0}".format(e))

Run on IDE

Transcribe an Audio file to text

If we have an audio file that we want to translate to text, we simply have to replace the source with the
audio file instead of a microphone.

Place the audio file and the program in the same folder for convenience. This works for WAV, AIFF, of
FLAC files.

An implementation has been shown below


#Python 2.x program to transcribe an Audio file

import speech_recognition as sr

AUDIO_FILE = ("example.wav")

# use the audio file as the audio source

r = sr.Recognizer()

with sr.AudioFile(AUDIO_FILE) as source:

#reads the audio file. Here we use record instead of

#listen

audio = r.record(source)

try:

print("The audio file contains: " + r.recognize_google(audio))

except sr.UnknownValueError:

print("Google Speech Recognition could not understand audio")

except sr.RequestError as e:

print("Could not request results from Google Speech

Recognition service; {0}".format(e))

Run on IDE

Troubleshooting
The following problems are commonly encountered

Muted Microphone: This leads to input not being received. To check for this, you can use alsamixer.

It can be installed using

sudo apt-get install libasound2 alsa-utils alsa-oss

Type amixer. The output will look somewhat like this

Simple mixer control 'Master', 0

Capabilities: pvolume pswitch pswitch-joined

Playback channels: Front Left - Front Right

Limits: Playback 0 - 65536

Mono:

Front Left: Playback 41855 [64%] [on]

Front Right: Playback 65536 [100%] [on]

Simple mixer control 'Capture', 0

Capabilities: cvolume cswitch cswitch-joined

Capture channels: Front Left - Front Right

Limits: Capture 0 - 65536

Front Left: Capture 0 [0%] [off] #switched off

Front Right: Capture 0 [0%] [off]


Simple Chat Room using Python

This article demonstrates – How to set up a simple Chat Room server and allow multiple clients to
connect to it using a client-side script. The code uses the concept of sockets and threading.
Socket programming

Sockets can be thought of as endpoints in a communication channel that is bi-directional, and


establishes communication between a server and one or more clients. Here, we set up a socket on each
end and allow a client to interact with other clients via the server. The socket on the server side
associates itself with some hardware port on the server side. Any client that has a socket associated with
the same port can communicate with the server socket.

Multi-Threading

A thread is sub process that runs a set of commands individually of any other thread. So, every time a
user connects to the server, a separate thread is created for that user and communication from server
to client takes place along individual threads based on socket objects created for the sake of identity of
each client.

We will require two scripts to establish this chat room. One to keep the serving running, and another
that every client should run in order to connect to the server.

Server Side Script

The server side script will attempt to establish a socket and bind it to an IP address and port specified by
the user (windows users might have to make an exception for the specified port number in their firewall
settings, or can rather use a port that is already open). The script will then stay open and receive
connection requests, and will append respective socket objects to a list to keep track of active
connections. Every time a user connects,

a separate thread will be created for that user. In each thread, the server awaits a message, and sends
that message to other users currently on the chat. If the server encounters an error while trying to
receive a message from a particular thread, it will exit that thread.

Usage
This server can be set up on a local area network by choosing any on computer to be a server node, and
using that computer’s private IP address as the server IP address.

For example, if a local area network has a set of private IP addresses assigned ranging from 192.168.1.2
to 192.168.1.100, then any computer from these 99 nodes can act as a server, and the remaining nodes
may connect to the server node by using the server’s private IP address. Care must be taken to choose a
port that is currently not in usage. For example, port 22 is default for ssh, and port 80 is default for HTTP
protocols. So these two ports preferably, shouldnt be used or reconfigured to make them free for usage.

However, if the server is meant to be accessible beyond a local network, the public IP address would be
required for usage. This would require port forwarding in cases where a node from a local network
(node that isnt the router) wishes to host the server. In this case, we would require any requests that
come to the public IP addresses to be re routed towards our private IP address in our local network, and
would hence require port forwarding.

For more reading on port forwarding: link

To run the script, simply download it from the GitHub link specified at the bottom of the post, and save
it at a convenient location on your computer.

/* Both the server and client script can then be run

from the Command prompt (in Windows) or from bash

Terminal (Linux users) by simply typing

"python chat_server.py " or "python client.py ".

For example, */

python chat_server.py 192.168.55.13 8081

python client.py 192.168.55.13 8081

Below is the Server side script that must be run at all times to keep the chatroom running.

# Python program to implement server side of chat room.

import socket

import select
import sys

from thread import *

"""The first argument AF_INET is the address domain of the

socket. This is used when we have an Internet Domain with

any two hosts The second argument is the type of socket.

SOCK_STREAM means that data or characters are read in

a continuous flow."""

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# checks whether sufficient arguments have been provided

if len(sys.argv) != 3:

print "Correct usage: script, IP address, port number"

exit()

# takes the first argument from command prompt as IP address

IP_address = str(sys.argv[1])

# takes second argument from command prompt as port number

Port = int(sys.argv[2])

"""

binds the server to an entered IP address and at the

specified port number.


The client must be aware of these parameters

"""

server.bind((IP_address, Port))

"""

listens for 100 active connections. This number can be

increased as per convenience.

"""

server.listen(100)

list_of_clients = []

def clientthread(conn, addr):

# sends a message to the client whose user object is conn

conn.send("Welcome to this chatroom!")

while True:

try:

message = conn.recv(2048)

if message:

"""prints the message and address of the

user who just sent the message on the server

terminal"""
print "<" + addr[0] + "> " + message

# Calls broadcast function to send message to all

message_to_send = "<" + addr[0] + "> " + message

broadcast(message_to_send, conn)

else:

"""message may have no content if the connection

is broken, in this case we remove the connection"""

remove(conn)

except:

continue

"""Using the below function, we broadcast the message to all

clients who's object is not the same as the one sending

the message """

def broadcast(message, connection):

for clients in list_of_clients:

if clients!=connection:

try:

clients.send(message)

except:

clients.close()
# if the link is broken, we remove the client

remove(clients)

"""The following function simply removes the object

from the list that was created at the beginning of

the program"""

def remove(connection):

if connection in list_of_clients:

list_of_clients.remove(connection)

while True:

"""Accepts a connection request and stores two parameters,

conn which is a socket object for that user, and addr

which contains the IP address of the client that just

connected"""

conn, addr = server.accept()

"""Maintains a list of clients for ease of broadcasting

a message to all available people in the chatroom"""

list_of_clients.append(conn)

# prints the address of the user that just connected

print addr[0] + " connected"


# creates and individual thread for every user

# that connects

start_new_thread(clientthread,(conn,addr))

conn.close()

server.close()

Run on IDE

Client Side Script

The client side script will simply attempt to access the server socket created at the specified IP address
and port. Once it connects, it will continuously check as to whether the input comes from the server or
from the client, and accordingly redirects output. If the input is from the server, it displays the message
on the terminal. If the input is from the user, it sends the message that the users enters to the server for
it to be broadcasted to other users.

This is the client side script, that each user must use in order to connect to the server.

# Python program to implement client side of chat room.

import socket

import select

import sys

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

if len(sys.argv) != 3:

print "Correct usage: script, IP address, port number"

exit()

IP_address = str(sys.argv[1])
Port = int(sys.argv[2])

server.connect((IP_address, Port))

while True:

# maintains a list of possible input streams

sockets_list = [sys.stdin, server]

""" There are two possible input situations. Either the

user wants to give manual input to send to other people,

or the server is sending a message to be printed on the

screen. Select returns from sockets_list, the stream that

is reader for input. So for example, if the server wants

to send a message, then the if condition will hold true

below.If the user wants to send a message, the else

condition will evaluate as true"""

read_sockets,write_socket, error_socket = select.select(sockets_list,[],[])

for socks in read_sockets:

if socks == server:

message = socks.recv(2048)

print message

else:

message = sys.stdin.readline()

server.send(message)
sys.stdout.write("<You>")

sys.stdout.write(message)

sys.stdout.flush()

server.close()

Run on IDE

Output: In the picture given below, a server has been initialized on the left side of the terminal and a
client script on the right side of the terminal. (Splitting of terminal done using tmux, ‘sudo apt-get install
tmux’). For initialization purposes, you can see that whenever a message is sent by a user, the message
along with IP address is shown on the server side.

Screenshot from 2017-04-18 18-31-03

The below picture has a basic conversation between two people on the same server. Multiple clients can
connect to the server in the same way!

Screenshot from 2017-04-18 18-31-03

Link to download script: link


OpenCV Python Program to analyze an image using Histogram

3.5

In this article, image analysis using Matplotlib and OpenCV is discussed. Let’s first understand how to
experiment image data with various styles and how to represent with Histogram.

Prerequisites:

OpenCV

matplotlib

Importing image data

import matplotlib.pyplot as plt #importing matplotlib

The image should be used in a PNG file as matplotlib supports only PNG images. Here, It’s a 24-bit RGB
PNG image (8 bits for each of R, G, B) used in this example. Each inner list represents a pixel. Here, with
an RGB image, there are 3 values. For RGB images, matplotlib supports float32 and uint8 data types.
img = plt.imread('flower.png') #reads image data

c5

In Matplotlib, this is performed using the imshow() function. Here we have grabbed the plot object.

All about Histogram

Histogram is considered as a graph or plot which is related to frequency of pixels in an Gray Scale Image

with pixel values (ranging from 0 to 255). Grayscale image is an image in which the value of each pixel is
a single sample, that is, it carries only intensity information where pixel value varies from 0 to 255.
Images of this sort, also known as black-and-white, are composed exclusively of shades of gray, varying
from black at the weakest intensity to white at the strongest where Pixel can be considered as a every
point in an image.

How GrayScale Image looks like:

images

It quantifies the number of pixels for each intensity value considered. Before going through Histogram,
lets have a rough idea from this given example.

histogram_sample

Here, we get intuition about contrast, brightness, intensity distribution etc of that image. As we can see
the image and its histogram which is drawn for grayscale image, not color image.

Left region of histogram shows the amount of darker pixels in image and right region shows the amount
of brighter pixels.

Histogram creation using numpy array

To create a histogram of our image data, we use the hist() function.

plt.hist(n_img.ravel(), bins=256, range=(0.0, 1.0), fc='k', ec='k') #calculating histogram

c1
In our histogram, it looks like there’s distribution of intensity all over image Black and White pixels as
grayscale image.

From the histogram, we can conclude that dark region is more than brighter region.

Now, we will deal with an image which consist of intensity distribution of pixels where pixel value
varies. First, we need to calculate histogram using OpenCV in-built function.

Histogram Calculation

Here, we use cv2.calcHist()(in-built function in OpenCV) to find the histogram.

cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])

images : it is the source image of type uint8 or float32 represented as “[img]”.

channels : it is the index of channel for which we calculate histogram. For grayscale image, its value is [0]
and

color image, you can pass [0], [1] or [2] to calculate histogram of blue, green or red channel respectively.

mask : mask image. To find histogram of full image, it is given as “None”.

histSize : this represents our BIN count. For full scale, we pass [256].

ranges : this is our RANGE. Normally, it is [0,256].

For example:

# load an image in grayscale mode

img = cv2.imread('ex.jpg',0)

# calculate frequency of pixels in range 0-255


histg = cv2.calcHist([img],[0],None,[256],[0,256])

Then, we need to plot histogram to show the characteristics of an image.

Plotting Histograms

Analysis using Matplotlib:

# importing required libraries of opencv

import cv2

# importing library for plotting

from matplotlib import pyplot as plt

# reads an input image

img = cv2.imread('ex.jpg',0)

# find frequency of pixels in range 0-255

histr = cv2.calcHist([img],[0],None,[256],[0,256])

# show the plotting graph of an image

plt.plot(histr)

plt.show()

Illustration shows that each number of pixels of an image lie upon range of 0 to 255. In the second
example, it directly finds the histogram and plot it. We need not use calcHist(). See the code below:
import cv2

from matplotlib import pyplot as plt

img = cv2.imread('ex.jpg',0)

# alternative way to find histogram of an image

plt.hist(img.ravel(),256,[0,256])

plt.show()
Bloom Filters – Introduction and Python Implementation

Suppose you are creating an account on Geekbook, you want to enter a cool username, you entered it
and got a message, “Username is already taken”. You added your birth date along username, still no
luck. Now you have added your university roll number also, still got “Username is already taken”. It’s
really frustrating, isn’t it?

But have you ever thought how quickly Geekbook check availability of username by searching millions of
username registered with it. There are many ways to do this job –

Linear search : Bad idea!

Binary Search : Store all username alphabetically and compare entered username with middle one in list,
If it matched, then username is taken otherwise figure out , whether entered username will come
before or after middle one and if it will come after, neglect all the usernames before middle
one(inclusive). Now search after middle one and repeat this process until you got a match or search end
with no match.This technique is better and promising but still it requires multiple steps.

But, There must be something better!!

Bloom Filter is a data structure that can do this job.

For understanding bloom filters, you must know what is hashing. A hash function takes input and
outputs a unique identifier of fixed length which is used for identification of input.

What is Bloom Filter?


A Bloom filter is a space-efficient probabilistic data structure that is used to test whether an element is a
member of a set. For example, checking availability of username is set membership problem, where the
set is the list of all registered username. The price we pay for efficiency is that it is probabilistic in nature
that means, there might be some False Positive results. False positive means, it might tell that given
username is already taken but actually it’s not.

Interesting Properties of Bloom Filters

Unlike a standard hash table, a Bloom filter of a fixed size can represent a set with an arbitrarily large
number of elements.

Adding an element never fails. However, the false positive rate increases steadily as elements are added
until all bits in the filter are set to 1, at which point all queries yield a positive result.

Bloom filters never generate false negative result, i.e., telling you that a username doesn’t exist when it
actually exists.

Deleting elements from filter is not possible because, if we delete a single element by clearing bits at
indices generated by k hash functions, it might cause deletion of few other elements. Example – if we
delete “geeks” (in given example below) by clearing bit at 1, 4 and 7, we might end up deleting “nerd”
also Because bit at index 4 becomes 0 and bloom filter claims that “nerd” is not present.

Working of Bloom Filter

A empty bloom filter is a bit array of m bits, all set to zero, like this –

empty_bit_array

We need k number of hash functions to calculate the hashes for a given input. When we want to add an
item in the filter, the bits at k indices h1(x), h2(x), … hk(x) are set, where indices are calculated using
hash functions.

Example – Suppose we want to enter “geeks” in the filter, we are using 3 hash functions and a bit array
of length 10, all set to 0 initially. First we’ll calculate the hashes as following :

h1(“geeks”) % 10 = 1

h2(“geeks”) % 10 = 4

h3(“geeks”) % 10 = 7
Note: These outputs are random for explanation only.

Now we will set the bits at indices 1, 4 and 7 to 1

geeks

Again we want to enter “nerd”, similarly we’ll calculate hashes

h1(“nerd”) % 10 = 3

h2(“nerd”) % 10 = 5

h3(“nerd”) % 10 = 4

Set the bits at indices 3, 5 and 4 to 1

nerd

Now if we want to check “geeks” is present in filter or not. We’ll do the same process but this time in
reverse order. We calculate respective hashes using h1, h2 and h3 and check if all these indices are set
to 1 in the bit array. If all the bits are set then we can say that “geeks” is probably present. If any of the
bit at these indices are 0 then “geeks” is definitely not present.

False Positive in Bloom Filters

The question is why we said “probably present”, why this uncertainty. Let’s understand this with an
example. Suppose we want to check whether “cat” is present or not. We’ll calculate hashes using h1, h2
and h3

h1(“cat”) % 10 = 1

h2(“cat”) % 10 = 3

h3(“cat”) % 10 = 7

If we check the bit array, bits at these indices are set to 1 but we know that “cat” was never added to
the filter. Bit at index 1 and 7 was set when we added “geeks” and bit 3 was set we added “nerd”.

cat
So, because bits at calculated indices are already set by some other item, bloom filter erroneously claim
that “cat” is present and generating a false positive result. Depending on the application, it could be
huge downside or relatively okay.

We can control the probability of getting a false positive by controlling the size of the Bloom filter. More
space means fewer false positives. If we want decrease probability of false positive result, we have to
use more number of hash functions and larger bit array. This would add latency in addition of item and
checking membership.

Probability of False positivity: Let m be the size of bit array, k be the number of hash functions and n be
the number of expected elements to be inserted in the filter, then the probability of false positive p can
be calculated as:

P=\left ( 1-\left [ 1- \frac {1}{m} \right ]^{kn} \right )^k

Size of Bit Array: If expected number of elements n is known and desired false positive probability is p
then the size of bit array m can be calculated as :

m= -\frac {n\ln P}{(ln 2)^2}

Optimum number of hash functions: The number of hash functions k must be a positive integer. If m is
size of bit array and n is number of elements to be inserted, then k can be calculated as :

k= \frac {m}{n} ln 2

Space Efficiency

If we want to store large list of items in a set for purpose of set membership, we can store it in hashmap,
tries or simple array or linked list. All these methods require storing item itself, which is not very
memory efficient. For example, if we want to store “geeks” in hashmap we have to store actual string “
geeks” as a key value pair {some_key : ”geeks”}.

Bloom filters do not store the data item at all. As we have seen they use bit array which allow hash
collision. Without hash collision, it would not be compact.

Choice of Hash Function

The hash function used in bloom filters should be independent and uniformly distributed. They should
be fast as possible. Fast simple non cryptographic hashes which are independent enough include
murmur, FNV series of hash functions and Jenkins hashes.

Generating hash is major operation in bloom filters. Cryptographic hash functions provide stability and
guarantee but are expensive in calculation. With increase in number of hash functions k, bloom filter
become slow. All though non-cryptographic hash functions do not provide guarantee but provide major
performance improvement.

Basic implementation of Bloom Filter class in Python3. Save it as bloomfilter.py

# Python 3 program to build Bloom Filter

# Install mmh3 and bitarray 3rd party module first

# pip install mmh3

# pip install bitarray

import math

import mmh3

from bitarray import bitarray

class BloomFilter(object):

'''
Class for Bloom filter, using murmur3 hash function

'''

def __init__(self, items_count,fp_prob):

'''

items_count : int

Number of items expected to be stored in bloom filter

fp_prob : float

False Positive probability in decimal

'''

# False posible probability in decimal

self.fp_prob = fp_prob

# Size of bit array to use

self.size = self.get_size(items_count,fp_prob)

# number of hash functions to use

self.hash_count = self.get_hash_count(self.size,items_count)

# Bit array of given size

self.bit_array = bitarray(self.size)

# initialize all bits as 0

self.bit_array.setall(0)
def add(self, item):

'''

Add an item in the filter

'''

digests = []

for i in range(self.hash_count):

# create digest for given item.

# i work as seed to mmh3.hash() function

# With different seed, digest created is different

digest = mmh3.hash(item,i) % self.size

digests.append(digest)

# set the bit True in bit_array

self.bit_array[digest] = True

def check(self, item):

'''

Check for existence of an item in filter

'''

for i in range(self.hash_count):

digest = mmh3.hash(item,i) % self.size

if self.bit_array[digest] == False:

# if any of bit is False then,its not present


# in filter

# else there is probability that it exist

return False

return True

@classmethod

def get_size(self,n,p):

'''

Return the size of bit array(m) to used using

following formula

m = -(n * lg(p)) / (lg(2)^2)

n : int

number of items expected to be stored in filter

p : float

False Positive probability in decimal

'''

m = -(n * math.log(p))/(math.log(2)**2)

return int(m)

@classmethod

def get_hash_count(self, m, n):

'''

Return the hash function(k) to be used using

following formula

k = (m/n) * lg(2)
m : int

size of bit array

n : int

number of items expected to be stored in filter

'''

k = (m/n) * math.log(2)

return int(k)

Run on IDE

Lets test the bloom filter. Save this file as bloom_test.py

from bloomfilter import BloomFilter

from random import shuffle

n = 20 #no of items to add

p = 0.05 #false positive probability

bloomf = BloomFilter(n,p)

print("Size of bit array:{}".format(bloomf.size))

print("False positive Probability:{}".format(bloomf.fp_prob))

print("Number of hash functions:{}".format(bloomf.hash_count))

# words to be added

word_present = ['abound','abounds','abundance','abundant','accessable',

'bloom','blossom','bolster','bonny','bonus','bonuses',
'coherent','cohesive','colorful','comely','comfort',

'gems','generosity','generous','generously','genial']

# word not added

word_absent = ['bluff','cheater','hate','war','humanity',

'racism','hurt','nuke','gloomy','facebook',

'geeksforgeeks','twitter']

for item in word_present:

bloomf.add(item)

shuffle(word_present)

shuffle(word_absent)

test_words = word_present[:10] + word_absent

shuffle(test_words)

for word in test_words:

if bloomf.check(word):

if word in word_absent:

print("'{}' is a false positive!".format(word))

else:

print("'{}' is probably present!".format(word))

else:

print("'{}' is definitely not present!".format(word))

Run on IDE
Output

Size of bit array:124

False positive Probability:0.05

Number of hash functions:4

'war' is definitely not present!

'gloomy' is definitely not present!

'humanity' is definitely not present!

'abundant' is probably present!

'bloom' is probably present!

'coherent' is probably present!

'cohesive' is probably present!

'bluff' is definitely not present!

'bolster' is probably present!

'hate' is definitely not present!

'racism' is definitely not present!

'bonus' is probably present!

'abounds' is probably present!

'genial' is probably present!

'geeksforgeeks' is definitely not present!

'nuke' is definitely not present!

'hurt' is definitely not present!

'twitter' is a false positive!

'cheater' is definitely not present!

'generosity' is probably present!


'facebook' is definitely not present!

'abundance' is probably present!

Applications of Bloom filters

Medium uses bloom filters for recommending post to users by filtering post which have been seen by
user.

Quora implemented a shared bloom filter in the feed backend to filter out stories that people have seen
before.

The Google Chrome web browser used to use a Bloom filter to identify malicious URLs

Google BigTable, Apache HBase and Apache Cassandra, and Postgresql use Bloom filters to reduce the
disk lookups for non-existent rows or columns
Find the first non-repeating character from a stream of characters

3.5

Given a stream of characters, find the first non-repeating character from stream. You need to tell the
first non-repeating character in O(1) time at any moment.

If we follow the first approach discussed here, then we need to store the stream so that we can traverse
it one more time to find the first non-repeating character at any moment. If we use extended approach
discussed in the same post, we need to go through the count array every time first non-repeating
element is queried. We can find the first non-repeating character from stream at any moment without
traversing any array.

Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

The idea is to use a DLL (Doubly Linked List) to efficiently get the first non-repeating character from a
stream. The DLL contains all non-repeating characters in order, i.e., the head of DLL contains first non-
repeating character, the second node contains the second non-repeating and so on.

We also maintain two arrays: one array is to maintain characters that are already visited two or more
times, we call it repeated[], the other array is array of pointers to linked list nodes, we call it inDLL[]. The
size of both arrays is equal to alphabet size which is typically 256.

Create an empty DLL. Also create two arrays inDLL[] and repeated[] of size 256. inDLL is an array of
pointers to DLL nodes. repeated[] is a boolean array, repeated[x] is true if x is repeated two or more
times, otherwise false. inDLL[x] contains pointer to a DLL node if character x is present in DLL, otherwise
NULL.

Initialize all entries of inDLL[] as NULL and repeated[] as false.

To get the first non-repeating character, return character at head of DLL.

Following are steps to process a new character ‘x’ in stream.

If repeated[x] is true, ignore this character (x is already repeated two or more times in the stream)

If repeated[x] is false and inDLL[x] is NULL (x is seen first time). Append x to DLL and store address of
new DLL node in inDLL[x].

If repeated[x] is false and inDLL[x] is not NULL (x is seen second time). Get DLL node of x using inDLL[x]
and remove the node. Also, mark inDLL[x] as NULL and repeated[x] as true.

Note that appending a new node to DLL is O(1) operation if we maintain tail pointer. Removing a node
from DLL is also O(1). So both operations, addition of new character and finding first non-repeating
character take O(1) time.

# A Python program to find first non-repeating character from

# a stream of characters

MAX_CHAR = 256

def findFirstNonRepeating():

# inDLL[x] contains pointer to a DLL node if x is present

# in DLL. If x is not present, then inDLL[x] is NULL

inDLL = [] * MAX_CHAR

# repeated[x] is true if x is repeated two or more times.


# If x is not seen so far or x is seen only once. then

# repeated[x] is false

repeated = [False] * MAX_CHAR

# Let us consider following stream and see the process

stream = "geeksforgeeksandgeeksquizfor"

for i in xrange(len(stream)):

x = stream[i]

print "Reading " + x + " from stream"

# We process this character only if it has not occurred

# or occurred only once. repeated[x] is true if x is

# repeated twice or more.s

if not repeated[ord(x)]:

# If the character is not in DLL, then add this

# at the end of DLL

if not x in inDLL:

inDLL.append(x)

else:

inDLL.remove(x)

if len(inDLL) != 0:

print "First non-repeating character so far is ",

print str(inDLL[0])
# Driver program

findFirstNonRepeating()

# This code is contributed by BHAVYA JAIN

Run on IDE

Output:

Reading g from stream

First non-repeating character so far is g

Reading e from stream

First non-repeating character so far is g

Reading e from stream

First non-repeating character so far is g

Reading k from stream

First non-repeating character so far is g

Reading s from stream

First non-repeating character so far is g

Reading f from stream

First non-repeating character so far is g

Reading o from stream

First non-repeating character so far is g

Reading r from stream

First non-repeating character so far is g

Reading g from stream


First non-repeating character so far is k

Reading e from stream

First non-repeating character so far is k

Reading e from stream

First non-repeating character so far is k

Reading k from stream

First non-repeating character so far is s

Reading s from stream

First non-repeating character so far is f

Reading a from stream

First non-repeating character so far is f

Reading n from stream

First non-repeating character so far is f

Reading d from stream

First non-repeating character so far is f

Reading g from stream

First non-repeating character so far is f

Reading e from stream

First non-repeating character so far is f

Reading e from stream

First non-repeating character so far is f

Reading k from stream

First non-repeating character so far is f

Reading s from stream

First non-repeating character so far is f


Reading q from stream

First non-repeating character so far is f

Reading u from stream

First non-repeating character so far is f

Reading i from stream

First non-repeating character so far is f

Reading z from stream

First non-repeating character so far is f

Reading f from stream

First non-repeating character so far is o

Reading o from stream

First non-repeating character so far is r

Reading r from stream

First non-repeating character so far is a

Python?

Defining a Keyword
In programming, a keyword is a “reserved word” by the language which convey a special meaning to the
interpreter. It may be a command or a parameter. Keywords cannot be used as a variable name in the
program snippet.

Keywords in Python: Python language also reserves some of keywords that convey special meaning.
Knowledge of these is necessary part of learning this language. Below is list of keywords registered by
python .

False, elif, lambda,

None, else, nonlocal,

True, except, not,

and, finally, or,

as, for, pass,

assert, from, raise,

break, global, return,

class, if, try,

continue, import, while,

def, in, with,

del, is, yield,

How to check if a string is keyword?

Python in its language defines an inbuilt module “keyword” which handles certain operations related to
keywords. A function “iskeyword()” checks if a string is keyword or not. Returns true if a string is
keyword, else returns false.

#Python code to demonstrate working of iskeyword()

# importing "keyword" for keyword operations


import keyword

# initializing strings for testing

s = "for"

s1 = "geeksforgeeks"

s2 = "elif"

s3 = "elseif"

s4 = "nikhil"

s5 = "assert"

s6 = "shambhavi"

s7 = "True"

s8 = "False"

s9 = "akshat"

s10 = "akash"

s11 = "break"

s12 = "ashty"

s13 = "lambda"

s14 = "suman"

s15 = "try"

s16 = "vaishnavi"

# checking which are keywords

if keyword.iskeyword(s):

print ( s + " is a python keyword")

else : print ( s + " is not a python keyword")


if keyword.iskeyword(s1):

print ( s1 + " is a python keyword")

else : print ( s1 + " is not a python keyword")

if keyword.iskeyword(s2):

print ( s2 + " is a python keyword")

else : print ( s2 + " is not a python keyword")

if keyword.iskeyword(s3):

print ( s3 + " is a python keyword")

else : print ( s3 + " is not a python keyword")

if keyword.iskeyword(s4):

print ( s4 + " is a python keyword")

else : print ( s4 + " is not a python keyword")

if keyword.iskeyword(s5):

print ( s5 + " is a python keyword")

else : print ( s5 + " is not a python keyword")

if keyword.iskeyword(s6):

print ( s6 + " is a python keyword")

else : print ( s6 + " is not a python keyword")


if keyword.iskeyword(s7):

print ( s7 + " is a python keyword")

else : print ( s7 + " is not a python keyword")

if keyword.iskeyword(s8):

print ( s8 + " is a python keyword")

else : print ( s8 + " is not a python keyword")

if keyword.iskeyword(s9):

print ( s9 + " is a python keyword")

else : print ( s9 + " is not a python keyword")

if keyword.iskeyword(s10):

print ( s10 + " is a python keyword")

else : print ( s10 + " is not a python keyword")

if keyword.iskeyword(s11):

print ( s11 + " is a python keyword")

else : print ( s11 + " is not a python keyword")

if keyword.iskeyword(s12):

print ( s12 + " is a python keyword")

else : print ( s12 + " is not a python keyword")

if keyword.iskeyword(s13):
print ( s13 + " is a python keyword")

else : print ( s13 + " is not a python keyword")

if keyword.iskeyword(s14):

print ( s14 + " is a python keyword")

else : print ( s14 + " is not a python keyword")

if keyword.iskeyword(s15):

print ( s15 + " is a python keyword")

else : print ( s15 + " is not a python keyword")

if keyword.iskeyword(s16):

print ( s16 + " is a python keyword")

else : print ( s16 + " is not a python keyword")

Run on IDE

Output:

for is a python keyword

geeksforgeeks is not a python keyword

elif is a python keyword

elseif is not a python keyword

nikhil is not a python keyword

assert is a python keyword

shambhavi is not a python keyword

True is a python keyword


False is a python keyword

akshat is not a python keyword

akash is not a python keyword

break is a python keyword

ashty is not a python keyword

lambda is a python keyword

suman is not a python keyword

try is a python keyword

vaishnavi is not a python keyword

How to print list of all keywords

Sometimes, remembering all the keywords can be a difficult task while assigning variable names. Hence
a function “kwlist()” is provided in “keyword” module which prints all the 33 python keywords.

#Python code to demonstrate working of iskeyword()

# importing "keyword" for keyword operations

import keyword

# printing all keywords at once using "kwlist()"

print ("The list of keywords is : ")

print (keyword.kwlist)

Run on IDE

Output:

The list of keywords is :


['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class',

'continue', 'def', 'del', 'elif', 'else', 'except', 'finally',

'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',

'nonlocal', 'not', 'or', 'pass', 'raise', 'return',

'try', 'while', 'with', 'yield']

How to input multiple values from user in one line in Python?

1.5

For instance, in C we can do something like this:

// Reads two values in one line

scanf("%d %d", &x, &y)

Run on IDE

One solution is to use raw_input() two times.

x, y = raw_input(), raw_input()

Run on IDE

Another solution is to use split()


x, y = raw_input().split()

Run on IDE

Note that we don’t have to explicitly specify split(‘ ‘) because split() uses any whitespace characters as
delimiter as default.

One thing to note in above Python code is, both x and y would be of string. We can convert them to int
using another line

x, y = [int(x), int(y)]

# We can also use list comprehension

x, y = [int(x) for x in [x, y]]

Below is complete one line code to read two integer variables from standard input using split and list
comprehension

# Reads two numbers from input and typecasts them to int using

# list comprehension

x, y = [int(x) for x in raw_input().split()]

Run on IDE

Note that in Python 3, we use input() in place of raw_input().


Command Line Arguments

Till now, we have taken input in python using raw_input() or input() [for integers]. There is another
method that uses command line arguments. The command line arguments must be given whenever we
want to give the input before the start of the script, while on the other hand, raw_input() is used to get
the input while the python program / script is running.
For example, in the UNIX environment, the arguments ‘-a’ and ‘-l’ for the ‘ls’ command give different
results.

The command line arguments in python can be processed by using either ‘sys’ module or ‘argparse’
module.

# Python code to demonstrate the use of 'sys' module

# for command line arguments

import sys

# command line arguments are stored in the form

# of list in sys.argv

argumentList = sys.argv

print argumentList

# Print the name of file

print sys.argv[0]

# Print the first argument after the name of file

print sys.argv[1]

Run on IDE

OUTPUT :

['program1.py', 'test', '1']

program1.py
test

NOTE : The above code runs only on command line. We need to fire the below command given that the
program is saved as program1.py

python program1.py test 123

Please note the following points about the above program :

The sys.argv takes the command line arguments in the form of a list.

The first element in the list is the name of the file.

The arguments always come in the form of a string even if we type an integer in the argument list. We
need to use int() function to convert the string to integer.

We can use the command line arguments to write the programs which do frequently used tasks. For
example, we need to find factorial many times. We can keep the following program in a file named
factorial.py in our computer and get the output by simply writing the command for getting the factorial
of a number, say 5.

python factorial.py 5

import sys

from math import factorial

print factorial(int(sys.argv[1]))

Run on IDE
Variable Arguments

args(*) and kwargs(**)

Both ‘args’ and ‘kwargs’ are used to get arbitrary number of arguments in a function.

args will give us all the function parameters in the form of a list and kwargs will give us all the keyword
arguments except for those corresponding to formal parameter as dictionary.

# Python program to illustrate the use of args which

# multiplies all the values given to the function as parameter

def multiplyAll(*values):

mul = 1

# values(args) will be in the form of tuple

print values

print "Type = ", type(values)

# Multiplying the all the parameters and return the result

for i in values:

mul = mul * i

return mul
# Driver program to test above function

# Multiply two numbers using above function

ans = multiplyAll(1,2)

print "The multiplication of 1 and 2 is ", ans

# Multiply 5 numbers using above function

ans = multiplyAll(3, 4, 5, 6, 7)

print "The multiplication of 3 to 7 is ", ans

Run on IDE

OUTPUT :

(1, 2)

Type =

The multiplication of 1 and 2 is 2

(3, 4, 5, 6, 7)

Type =

The multiplication of 3 to 7 is 2520

Note that args are denoted by using a single star and kwargs are denoted by two stars before the formal
parameters in the function.

# Program to illustrate the use of kwargs in Python

# Function that print the details of a student

# The number of details per student may vary


def printDetails(**details):

# Variable 'details' contains the details in the

# form of dictionary

print "Parameter details contains"

print details

print "Type = ", type(details)

# Print first name

print "First Name = ", details['firstName']

# Print the department of student

print "Department = ", details['department']

print "" # Extra line break

# Driver program to test above function

# Calling the function with three arguments

printDetails(firstName = "Nikhil",

rollNumber = "007",

department = "CSE")

# Calling the function with two arguments

printDetails(firstName = "Abhay",
department = "ECE")

Run on IDE

Output:

Parameter details contains

{'department': 'CSE', 'rollNumber': '007', 'firstName': 'Nikhil'}

Type =

First Name = Nikhil

Department = CSE

Parameter details contains

{'department': 'ECE', 'firstName': 'Abhay'}

Type =

First Name = Abhay

Department = ECE

Please note that if you are using both args and kwargs in a function then the args parameter must
precede before the kwarg parameters.

Example :

# Function containing both args and kwargs

def cheeseshop(kind, *arguments, **keywords):

print "-- Do you have any", kind, "?"

print "-- I'm sorry, we're all out of", kind

for arg in arguments:

print arg
print "-" * 40

keys = sorted(keywords.keys())

for kw in keys:

print kw, ":", keywords[kw]

# Driver program to test above function

cheeseshop("Burger", "It's very funny, sir.",

"It's really very, VERY funny, sir.",

shopkeeper='Michael Palin',

client="John Cleese",

sketch="Cheese Shop Sketch")


Py-Facts – 10 interesting facts about Python

2.2

Python is one of the most popular programming languages nowadays on account of its code readability
and simplicity. All thanks to Guido Van Rossum, its creator.

I’ve compiled a list of 10 interesting Facts in the Python Language. Here they are:

1. There is actually a poem written by Tim Peters named as THE ZEN OF PYTHON which can be read by
just writing import this in the interpreter.

# Try to guess the result before you actually run it

import this

Run on IDE

Output:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.


Readability counts.

Special cases aren't special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to do it.

Although that way may not be obvious at first unless you're Dutch.

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it's a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea -- let's do more of those!

2. One can return multiple values in Python. Don’t believe ? See the below code snippet:

# Multiple Return Values in Python!

def func():

return 1, 2, 3, 4, 5

one, two, three, four, five = func()

print(one, two, three, four, five)

Run on IDE

Output:
(1, 2, 3, 4, 5)

3. One can use an “else” clause with a “for” loop in Python. It’s a special type of syntax that executes
only if the for loop exits naturally, without any break statements.

def func(array):

for num in array:

if num%2==0:

print(num)

break # Case1: Break is called, so 'else' wouldn't be executed.

else: # Case 2: 'else' executed since break is not called

print("No call for Break. Else is executed")

print("1st Case:")

a = [2]

func(a)

print("2nd Case:")

a = [1]

func(a)

Run on IDE

Output:

1st Case:

2nd Case:

No call for Break. Else is executed

4. In Python, everything is done by reference. It doesn’t support pointers.


5. Function Argument Unpacking is another awesome feature of Python. One can unpack a list or a
dictionary as function arguments using * and ** respectively. This is commonly known as the Splat
operator. Example here

def point(x, y):

print(x,y)

foo_list = (3, 4)

bar_dict = {'y': 3, 'x': 2}

point(*foo_list) # Unpacking Lists

point(**bar_dict) # Unpacking Dictionaries

Run on IDE

Output:

34

23

6. Want to find the index inside a for loop? Wrap an iterable with ‘enumerate’ and it will yield the item
along with its index. See this code snippet

# Know the index faster

vowels=['a','e','i','o','u']

for i, letter in enumerate(vowels):

print (i, letter)

Run on IDE

Output:
(0, 'a')

(1, 'e')

(2, 'i')

(3, 'o')

(4, 'u')

7. One can chain comparison operators in Python answer= 1<x<10 is executable in Python. More
examples here

# Chaining Comparison Operators

i = 5;

ans = 1 < i < 10

print(ans)

ans = 10 > i <= 9

print(ans)

ans = 5 == i

print(ans)

Run on IDE

Output:

True

True

True
8. We can’t define Infinities right? But wait! Not for Python. See this amazing example

# Positive Infinity

p_infinity = float('Inf')

if 99999999999999 > p_infinity:

print("The number is greater than Infinity!")

else:

print("Infinity is greatest")

# Negetive Infinity

n_infinity = float('-Inf')

if -99999999999999 < n_infinity:

print("The number is lesser than Negative Infinity!")

else:

print("Negative Infinity is least")

Run on IDE

Output:

Infinity is greatest

Negative Infinity is least

9. Instead of building a list with a loop, one can build it more concisely with a list comprehension. See
this code for more understanding.

# Simple List Append

a = []
for x in range(0,10):

a.append(x)

print(a)

# List Comprehension

print([x for x in a])

Run on IDE

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

10. Finally, Python’s special Slice Operator. It is a way to get items from lists, as well as change them. See
this code snippet

# Slice Operator

a = [1,2,3,4,5]

print(a[0:2]) # Choose elements [0-2), upper-bound noninclusive

print(a[0:-1]) # Choose all but the last

print(a[::-1]) # Reverse the list

print(a[::2]) # Skip by 2

print(a[::-2]) # Skip by -2 from the back


Run on IDE

Output:

[1, 2]

[1, 2, 3, 4]

[5, 4, 3, 2, 1]

[1, 3, 5]

[5, 3, 1]

Reading and Writing to text files in Python

1.6

Python provides inbuilt functions for creating, writing and reading files. There are two types of files that
can be handled in python, normal text files and binary files (written in binary language,0s and 1s).
Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of
Line), which is the new line character (‘\n’) in python by default.

Binary files: In this type of file, there is no terminator for a line and the data is stored after converting it
into machine understandable binary language.

In this article, we will be focusing on opening, closing, reading and writing data in a text file.

File Access Modes

Access modes govern the type of operations possible in the opened file. It refers to how the file will be
used once its opened. These modes also define the location of the File Handle in the file. File handle is
like a cursor, which defines from where the data has to be read or written in the file. There are 6 access
modes in python.

Read Only (‘r’) : Open text file for reading. The handle is positioned at the beginning of the file. If the file
does not exists, raises I/O error. This is also the default mode in which file is opened.

Read and Write (‘r+’) : Open the file for reading and writing. The handle is positioned at the beginning of
the file. Raises I/O error if the file does not exists.

Write Only (‘w’) : Open the file for writing. For existing file, the data is truncated and over-written. The
handle is positioned at the beginning of the file. Creates the file if the file does not exists.

Write and Read (‘w+’) : Open the file for reading and writing. For existing file, data is truncated and over-
written. The handle is positioned at the beginning of the file.

Append Only (‘a’) : Open the file for writing. The file is created if it does not exist. The handle is
positioned at the end of the file. The data being written will be inserted at the end, after the existing
data.

Append and Read (‘a+’) : Open the file for reading and writing. The file is created if it does not exist. The
handle is positioned at the end of the file. The data being written will be inserted at the end, after the
existing data.

Opening a File

It is done using the open() function. No module is required to be imported for this function.
File_object = open(r"File_Name","Access_Mode")

The file should exist in the same directory as the python program file else, full address of the file should
be written on place of filename.

Note: The r is placed before filename to prevent the characters in filename string to be treated as
special character. For example, if there is \temp in the file address, then \t is treated as the tab
character and error is raised of invalid address. The r makes the string raw, that is, it tells that the string
is without any special characters. The r can be ignored if the file is in same directory and address is not
being placed.

# Open function to open the file "MyFile1.txt"

# (same directory) in append mode and

file1 = open("MyFile.txt","a")

# store its reference in the variable file1

# and "MyFile2.txt" in D:\Text in file2

file2 = open(r"D:\Text\MyFile2.txt","w+")

Run on IDE

Here, file1 is created as object for MyFile1 and file2 as object for MyFile2

Closing a file

close() function closes the file and frees the memory space acquired by that file. It is used at the time
when the file is no longer needed or if it is to be opened in a different file mode.

File_object.close()

# Opening and Closing a file "MyFile.txt"

# for object name file1.


file1 = open("MyFile.txt","a")

file1.close()

Run on IDE

Writing to a file

There are two ways to write in a file.

write() : Inserts the string str1 in a single line in the text file.

File_object.write(str1)

writelines() : For a list of string elements, each string is inserted in the text file.Used to insert multiple
strings at a single time.

File_object.writelines(L) for L = [str1, str2, str3]

Reading from a file

There are three ways to read data from a text file.

read() : Returns the read bytes in form of a string. Reads n bytes, if no n specified, reads the entire file.

File_object.read([n])

readline() : Reads a line of the file and returns in form of a string.For specified n, reads at most n bytes.
However, does not reads more than one line, even if n exceeds the length of the line.

File_object.readline([n])

readlines() : Reads all the lines and return them as each line a string element in a list.

File_object.readlines()

Note: ‘\n’ is treated as a special character of two bytes

# Program to show various ways to read and


# write data in a file.

file1 = open("myfile.txt","w")

L = ["This is Delhi \n","This is Paris \n","This is London \n"]

# \n is placed to indicate EOL (End of Line)

file1.write("Hello \n")

file1.writelines(L)

file1.close() #to change file access modes

file1 = open("myfile.txt","r+")

print "Output of Read function is "

print file1.read()

print

# seek(n) takes the file handle to the nth

# bite from the beginning.

file1.seek(0)

print "Output of Readline function is "

print file1.readline()

print

file1.seek(0)
# To show difference between read and readline

print "Output of Read(9) function is "

print file1.read(9)

print

file1.seek(0)

print "Output of Readline(9) function is "

print file1.readline(9)

file1.seek(0)

# readlines function

print "Output of Readlines function is "

print file1.readlines()

print

file1.close()

Run on IDE

Output:

Output of Read function is

Hello

This is Delhi

This is Paris

This is London
Output of Readline function is

Hello

Output of Read(9) function is

Hello

Th

Output of Readline(9) function is

Hello

Output of Readlines function is

['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']

Appending to a file

# Python program to illustrate

# Append vs write mode

file1 = open("myfile.txt","w")

L = ["This is Delhi \n","This is Paris \n","This is London \n"]

file1.close()

# Append-adds at last

file1 = open("myfile.txt","a")#append mode


file1.write("Today \n")

file1.close()

file1 = open("myfile.txt","r")

print "Output of Readlines after appending"

print file1.readlines()

print

file1.close()

# Write-Overwrites

file1 = open("myfile.txt","w")#write mode

file1.write("Tomorrow \n")

file1.close()

file1 = open("myfile.txt","r")

print "Output of Readlines after writing"

print file1.readlines()

print

file1.close()

Run on IDE

Output:

Output of Readlines after appending

['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']
Output of Readlines after writing

['Tomorrow \n']

Related Article :

File Objects in Python

SQL using Python | Set 1

In this article, database connection with the python program is discussed. Connecting a program with a
database is considered a tough task in any programming language. It is used to connect the front-end of
your application with the back-end database. Python with its native builtin modules made this thing easy
too.

This needs the basic understanding of SQL.

Here, we are going to connect SQLite with Python. Python has a native library for SQLite. Let us explain
how it works.
To use SQLite, we must import sqlite3.

Then create a connection using connect() method and pass the name of the database you want to
access if there is a file with that name, it will open that file. Otherwise, Python will create a file with the
given name.

After this, a cursor object is called to be capable to send commands to the SQL. Cursor is a control
structure used to traverse and fetch the records of the database. Cursor has a major role in working with
Python. All the commands will be executed using cursor object only.

To create a table in the database, create an object and write the SQL command in it with being
commented. Example:- sql_comm = ”SQL statement”

And executing the command is very easy. Call the cursor method execute and pass the name of the sql
command as a parameter in it. Save a number of commands as the sql_comm and execute them. After
you perform all your activities, save the changes in the file by committing those changes and then lose
the connection.

# Python code to demonstrate table creation and

# insertions with SQL

# importing module

import sqlite3

# connecting to the database

connection = sqlite3.connect("myTable.db")

# cursor

crsr = connection.cursor()

# SQL command to create a table in the database

sql_command = """CREATE TABLE emp (

staff_number INTEGER PRIMARY KEY,


fname VARCHAR(20),

lname VARCHAR(30),

gender CHAR(1),

joining DATE);"""

# execute the statement

crsr.execute(sql_command)

# SQL command to insert the data in the table

sql_command = """INSERT INTO emp VALUES (23, "Rishabh", "Bansal", "M", "2014-03-28");"""

crsr.execute(sql_command)

# another SQL command to insert the data in the table

sql_command = """INSERT INTO emp VALUES (1, "Bill", "Gates", "M", "1980-10-28");"""

crsr.execute(sql_command)

# To save the changes in the files. Never skip this.

# If we skip this, nothing will be saved in the database.

connection.commit()

# close the connection

connection.close()

Run on IDE

In this section, we have discussed how to create a table and how to add new rows in the database.
Fetching the data from record is simple as the inserting them. The execute method uses the SQL
command of getting all the data from the table using “Select * from table_name” and all the table data
can be fetched in an object in the form of list of lists.

# Python code to demonstrate SQL to fetch data.

# importing the module

import sqlite3

# connect withe the myTable database

connection = sqlite3.connect("myTable.db")

# cursor object

crsr = connection.cursor()

# execute the command to fetch all the data from the table emp

crsr.execute("SELECT * FROM emp")

# store all the fetched data in the ans variable

ans= crsr.fetchall()

# loop to print all the data

for i in ans:

print(i)

Run on IDE
It should be noted that the database file that will be created will be in the same folder as that of the
python file. If we wish to change the path of the file, change the path while opening the file

PYTHON INTERVIEW QUESTION

Basic Python Interview Questions

Q1. What are the key features of Python?

Ans: These are the few key features of Python:

Python is an interpreted language. That means that, unlike languages like C and its variants, Python does
not need to be compiled before it is run. Other interpreted languages include PHP and Ruby.

Python is dynamically typed, this means that you don’t need to state the types of variables when you
declare them or anything like that. You can do things like x=111 and then x="I'm a string" without error

Python is well suited to object orientated programming in that it allows the definition of classes along
with composition and inheritance. Python does not have access specifiers (like C++’s public, private), the
justification for this point is given as “we are all adults here”

In Python, functions are first-class objects. This means that they can be assigned to variables, returned
from other functions and passed into functions. Classes are also first class objects

Writing Python code is quick but running it is often slower than compiled languages. Fortunately,Python
allows the inclusion of C based extensions so bottlenecks can be optimized away and often are. The
numpy package is a good example of this, it’s really quite quick because a lot of the number crunching it
does isn’t actually done by Python

Python finds use in many spheres – web applications, automation, scientific modelling, big data
applications and many more. It’s also often used as “glue” code to get other languages and components
to play nice.

Q2. What is the difference between deep and shallow copy?

Ans: Shallow copy is used when a new instance type gets created and it keeps the values that are copied
in the new instance. Shallow copy is used to copy the reference pointers just like it copies the values.
These references point to the original objects and the changes made in any member of the class will also
affect the original copy of it. Shallow copy allows faster execution of the program and it depends on the
size of the data that is used.

Deep copy is used to store the values that are already copied. Deep copy doesn’t copy the reference
pointers to the objects. It makes the reference to an object and the new object that is pointed by some
other object gets stored. The changes made in the original copy won’t affect any other copy that uses
the object. Deep copy makes execution of the program slower due to making certain copies for each
object that is been called.

Q3. What is the difference between list and tuples?

Ans: Lists are mutable i.e they can be edited. Syntax: list_1 = [10, ‘Chelsea’, 20]

Tuples are immutable (tuples are lists which can’t be edited). Syntax: tup_1 = (10, ‘Chelsea’ , 20)

Q4. How is Multithreading achieved in Python?

Ans:

Python has a multi-threading package but if you want to multi-thread to speed your code up.

Python has a construct called the Global Interpreter Lock (GIL). The GIL makes sure that only one of your
‘threads’ can execute at any one time. A thread acquires the GIL, does a little work, then passes the GIL
onto the next thread.

This happens very quickly so to the human eye it may seem like your threads are executing in parallel,
but they are really just taking turns using the same CPU core.

All this GIL passing adds overhead to execution. This means that if you want to make your code run
faster then using the threading package often isn’t a good idea.

Q5. How can the ternary operators be used in python?

Ans: The Ternary operator is the operator that is used to show the conditional statements. This consists
of the true or false values with a statement that has to be evaluated for it.
Syntax:

The Ternary operator will be given as:

[on_true] if [expression] else [on_false]x, y = 25, 50big = x if x < y else y

Example:

The expression gets evaluated like if x<y else y, in this case if x<y is true then the value is returned as
big=x and if it is incorrect then big=y will be sent as a result.

GET STARTED WITH PYTHON

Q6. How is memory managed in Python?

Ans:

Python memory is managed by Python private heap space. All Python objects and data structures are
located in a private heap. The programmer does not have an access to this private heap and interpreter
takes care of this Python private heap.

The allocation of Python heap space for Python objects is done by Python memory manager. The core
API gives access to some tools for the programmer to code.

Python also have an inbuilt garbage collector, which recycle all the unused memory and frees the
memory and makes it available to the heap space.

Q7. Explain Inheritance in Python with an example.

Ans: Inheritance allows One class to gain all the members(say attributes and methods) of another class.
Inheritance provides code reusability, makes it easier to create and maintain an application. The class
from which we are inheriting is called super-class and the class that is inherited is called a derived / child
class.

They are different types of inheritance supported by Python:


Single Inheritance – where a derived class acquires the members of a single super class.

Multi-level inheritance – a derived class d1 in inherited from base class base1, and d2 is inherited from
base2.

Hierarchical inheritance – from one base class you can inherit any number of child classes

Multiple inheritance – a derived class is inherited from more than one base class.

Q8. Explain what Flask is and its benefits?

Ans: Flask is a web micro framework for Python based on “Werkzeug, Jinja2 and good intentions” BSD
license. Werkzeug and Jinja2 are two of its dependencies. This means it will have little to no
dependencies on external libraries. It makes the framework light while there is little dependency to
update and less security bugs.

A session basically allows you to remember information from one request to another. In a flask, a
session uses a signed cookie so the user can look at the session contents and modify. The user can
modify the session if only it has the secret key Flask.secret_key.

Q9. What is the usage of help() and dir() function in Python?

Ans: Help() and dir() both functions are accessible from the Python interpreter and used for viewing a
consolidated dump of built-in functions.

Help() function: The help() function is used to display the documentation string and also facilitates you
to see the help related to modules, keywords, attributes, etc.

Dir() function: The dir() function is used to display the defined symbols.

Q10. Whenever Python exits, why isn’t all the memory de-allocated?

Ans:

Whenever Python exits, especially those Python modules which are having circular references to other
objects or the objects that are referenced from the global namespaces are not always de-allocated or
freed.
It is impossible to de-allocate those portions of memory that are reserved by the C library.

On exit, because of having its own efficient clean up mechanism, Python would try to de-
allocate/destroy every other object.

Q11. What is dictionary in Python?

Ans: The built-in datatypes in Python is called dictionary. It defines one-to-one relationship between
keys and values. Dictionaries contain pair of keys and their corresponding values. Dictionaries are
indexed by keys.

Let’s take an example:

The following example contains some keys. Country, Capital & PM. Their corresponding values are India,
Delhi and Modi respectively.

dict={'Country':'India','Capital':'Delhi','PM':'Modi'}

print dict[Country]

India

print dict[Capital]

Delhi

print dict[PM]

Modi

Q12. What is monkey patching in Python?

Ans: In Python, the term monkey patch only refers to dynamic modifications of a class or module at run-
time.
Consider the below example:

# m.py

class MyClass:

def f(self):

print "f()"

We can then run the monkey-patch testing like this:

import m

def monkey_f(self):

print "monkey_f()"

m.MyClass.f = monkey_f
obj = m.MyClass()

obj.f()

The output will be as below:

monkey_f()

As we can see, we did make some changes in the behavior of f() in MyClass using the function we
defined, monkey_f(), outside of the module m.

Q13. What does this mean: *args, **kwargs? And why would we use it?

Ans: We use *args when we aren’t sure how many arguments are going to be passed to a function, or if
we want to pass a stored list or tuple of arguments to a function. **kwargsis used when we don’t know
how many keyword arguments will be passed to a function, or it can be used to pass the values of a
dictionary as keyword arguments. The identifiers args and kwargs are a convention, you could also use
*bob and **billy but that would not be wise.

Q14. Write a one-liner that will count the number of capital letters in a file. Your code should work even
if the file is too big to fit in memory.

Ans: Let us first write a multiple line solution and then convert it to one liner code.

with open(SOME_LARGE_FILE) as fh:

count = 0

text = fh.read()
for character in text:

if character.isupper():

count += 1

We will now try to transform this into a single line.

count sum(1 for line in fh for character in line if character.isupper())

Q15. What are negative indexes and why are they used?

Ans: The sequences in Python are indexed and it consists of the positive as well as negative numbers.
The numbers that are positive uses ‘0’ that is uses as first index and ‘1’ as the second index and the
process goes on like that.

The index for the negative number starts from ‘-1’ that represents the last index in the sequence and ‘-2’
as the penultimate index and the sequence carries forward like the positive number.

The negative index is used to remove any new-line spaces from the string and allow the string to except
the last character that is given as S[:-1]. The negative index is also used to show the index to represent
the string in correct order.

Q16. How can you randomize the items of a list in place in Python?

Ans: Consider the example shown below:

from random import shuffle


x = ['Keep', 'The', 'Blue', 'Flag', 'Flying', 'High']

shuffle(x)

print(x)

The output of the following code is as below.

['Flying', 'Keep', 'Blue', 'High', 'The', 'Flag']

Q17. What is the process of compilation and linking in python?

Ans: The compiling and linking allows the new extensions to be compiled properly without any error and
the linking can be done only when it passes the compiled procedure. If the dynamic loading is used then
it depends on the style that is being provided with the system. The python interpreter can be used to
provide the dynamic loading of the configuration setup files and will rebuild the interpreter.

The steps that is required in this as:

Create a file with any name and in any language that is supported by the compiler of your system. For
example file.c or file.cpp

Place this file in the Modules/ directory of the distribution which is getting used.

Add a line in the file Setup.local that is present in the Modules/ directory.

Run the file using spam file.o

After successful run of this rebuild the interpreter by using the make command on the top-level
directory.

If the file is changed then run rebuildMakefile by using the command as ‘make Makefile’.

Q18. Write a sorting algorithm for a numerical dataset in Python.

Ans: The following code can be used to sort a list in Python:

2
3

list = ["1", "4", "0", "6", "9"]

list = [int(i) for i in list]

list.sort()

print (list)

Q19. Looking at the below code, write down the final values of A0, A1, …An.

A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))

A1 = range(10)A2 = sorted([i for i in A1 if i in A0])

A3 = sorted([A0[s] for s in A0])

A4 = [i for i in A1 if i in A3]

A5 = {i:i*i for i in A1}

A6 = [[i,i*i] for i in A1]

print(A0,A1,A2,A3,A4,A5,A6)

Ans: The following will be the final outputs of A0, A1, … A6

A0 = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4} # the order may vary

A1 = range(0, 10)
A2 = []

A3 = [1, 2, 3, 4, 5]

A4 = [1, 2, 3, 4, 5]

A5 = {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

A6 = [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]

Q20. Explain split(), sub(), subn() methods of “re” module in Python.

Ans: To modify the strings, Python’s “re” module is providing 3 methods. They are:

split() – uses a regex pattern to “split” a given string into a list.

sub() – finds all substrings where the regex pattern matches and then replace them with a different
string

subn() – it is similar to sub() and also returns the new string along with the no. of replacements.

Q21. How can you generate random numbers in Python?

Ans: Random module is the standard module that is used to generate the random number. The method
is defined as:

import random

random.random

The statement random.random() method return the floating point number that is in the range of [0, 1).
The function generates the random float numbers. The methods that are used with the random class are
the bound methods of the hidden instances. The instances of the Random can be done to show the
multi-threading programs that creates different instance of individual threads. The other random
generators that are used in this are:

randrange(a, b): it chooses an integer and define the range in-between [a, b). It returns the elements by
selecting it randomly from the range that is specified. It doesn’t build a range object.
uniform(a, b): it chooses a floating point number that is defined in the range of [a,b).Iyt returns the
floating point number

normalvariate(mean, sdev): it is used for the normal distribution where the mu is a mean and the sdev is
a sigma that is used for standard deviation.

The Random class that is used and instantiated creates an independent multiple random number
generators.

Q22. What is the difference between range & xrange?

Ans: For the most part, xrange and range are the exact same in terms of functionality. They both provide
a way to generate a list of integers for you to use, however you please. The only difference is that range
returns a Python list object and x range returns an xrange object.

This means that xrange doesn’t actually generate a static list at run-time like range does. It creates the
values as you need them with a special technique called yielding. This technique is used with a type of
object known as generators. That means that if you have a really gigantic range you’d like to generate a
list for, say one billion, xrange is the function to use.

This is especially true if you have a really memory sensitive system such as a cell phone that you are
working with, as range will use as much memory as it can to create your array of integers, which can
result in a Memory Error and crash your program. It’s a memory hungry beast.

Q23. What is pickling and unpickling?

Ans: Pickle module accepts any Python object and converts it into a string representation and dumps it
into a file by using dump function, this process is called pickling. While the process of retrieving original
Python objects from the stored string representation is called unpickling.

Django – Python Interview Questions

Q24. Mention the differences between Django, Pyramid and Flask.

Ans:

Flask is a “microframework” primarily build for a small application with simpler requirements. In flask,
you have to use external libraries. Flask is ready to use.
Pyramid is built for larger applications. It provides flexibility and lets the developer use the right tools for
their project. The developer can choose the database, URL structure, templating style and more.
Pyramid is heavy configurable.

Django can also used for larger applications just like Pyramid. It includes an ORM.

Q25. Discuss the Django architecture.

Ans: Django MVT Pattern:

Django Architecture - Python Interview Questions - EdurekaFigure: Python Interview Questions –


Django Architecture

The developer provides the Model, the view and the template then just maps it to a URL and Django
does the magic to serve it to the user.

Q26. Explain how you can set up the Database in Django.

Ans: You can use the command edit mysite/setting.py , it is a normal python module with module level
representing Django settings.

Django uses SQLite by default; it is easy for Django users as such it won’t require any other type of
installation. In the case your database choice is different that you have to the following keys in the
DATABASE ‘default’ item to match your database connection settings.

Engines: you can change database by using ‘django.db.backends.sqlite3’ , ‘django.db.backeneds.mysql’,


‘django.db.backends.postgresql_psycopg2’, ‘django.db.backends.oracle’ and so on

Name: The name of your database. In the case if you are using SQLite as your database, in that case
database will be a file on your computer, Name should be a full absolute path, including file name of
that file.

If you are not choosing SQLite as your database then settings like Password, Host, User, etc. must be
added.

Django uses SQLite as default database, it stores data as a single file in the filesystem. If you do have a
database server—PostgreSQL, MySQL, Oracle, MSSQL—and want to use it rather than SQLite, then use
your database’s administration tools to create a new database for your Django project. Either way, with
your (empty) database in place, all that remains is to tell Django how to use it. This is where your
project’s settings.py file comes in.

We will add the following lines of code to the setting.py file:

DATABASES = {

'default': {

'ENGINE' : 'django.db.backends.sqlite3',

'NAME' : os.path.join(BASE_DIR, 'db.sqlite3'),

GET PYTHON CERTIFIED TODAY

Q27. Give an example how you can write a VIEW in Django?

Ans: This is how we can use write a view in Django:

4
5

from django.http import HttpResponse

import datetime

def Current_datetime(request):

now = datetime.datetime.now()

html = "<html><body>It is now %s</body></html>" % now

return HttpResponse(html)

Returns the current date and time, as an HTML document

Q28. Mention what the Django templates consists of.

Ans: The template is a simple text file. It can create any text-based format like XML, CSV, HTML, etc. A
template contains variables that get replaced with values when the template is evaluated and tags (%
tag %) that controls the logic of the template.

Django Template - Python Interview Questions - EdurekaFigure: Python Interview Questions – Django
Template

Q29. Explain the use of session in Django framework?

Ans: Django provides session that lets you store and retrieve data on a per-site-visitor basis. Django
abstracts the process of sending and receiving cookies, by placing a session ID cookie on the client side,
and storing all the related data on the server side.

Django Framework - Python Interview Questions - EdurekaFigure: Python Interview Questions – Django
Framework
So the data itself is not stored client side. This is nice from a security perspective.

Q30. List out the inheritance styles in Django.

Ans: In Django, there is three possible inheritance styles:

Abstract Base Classes: This style is used when you only wants parent’s class to hold information that you
don’t want to type out for each child model.

Multi-table Inheritance: This style is used If you are sub-classing an existing model and need each model
to have its own database table.

Proxy models: You can use this model, If you only want to modify the Python level behavior of the
model, without changing the model’s fields.

Web Scraping – Python Interview Questions

Q31. How To Save An Image Locally Using Python Whose URL Address I Already Know?

Ans: We will use the following code to save an image locally from an URL address

import urllib.request

urllib.request.urlretrieve("URL", "local-filename.jpg")

Q32. How can you Get the Google cache age of any URL or web page?

Ans: Use the following URL format:

http://webcache.googleusercontent.com/search?q=cache:URLGOESHERE
Be sure to replace “URLGOESHERE” with the proper web address of the page or site whose cache you
want to retrieve and see the time for. For example, to check the Google Webcache age of edureka.co
you’d use the following URL:

http://webcache.googleusercontent.com/search?q=cache:edureka.co

Q33. You are required to scrap data from IMDb top 250 movies page. It should only have fields movie
name, year, and rating.

Ans: We will use the following lines of code:

10

11

12

13

14

15

16

17
18

19

from bs4 import BeautifulSoup

import requests

import sys

url = 'http://www.imdb.com/chart/top'

response = requests.get(url)

soup = BeautifulSoup(response.text)

tr = soup.findChildren("tr")

tr = iter(tr)

next(tr)

for movie in tr:

title = movie.find('td', {'class': 'titleColumn'} ).find('a').contents[0]

year = movie.find('td', {'class': 'titleColumn'} ).find('span', {'class': 'secondaryInfo'}).contents[0]

rating = movie.find('td', {'class': 'ratingColumn imdbRating'} ).find('strong').contents[0]

row = title + ' - ' + year + ' ' + ' ' + rating

print(row)

The above code will help scrap data from IMDb’s top 250 list

Data Analysis – Python Interview Questions

Q34. What is map function in Python?


Ans: map function executes the function given as the first argument on all the elements of the iterable
given as the second argument. If the function given takes in more than 1 arguments, then many
iterables are given. #Follow the link to know more similar functions.

Q35. How to get indices of N maximum values in a NumPy array?

Ans: We can get the indices of N maximum values in a NumPy array using the below code:

import numpy as np

arr = np.array([1, 3, 2, 4, 5])

print(arr.argsort()[-3:][::-1])

Output

[431]

Q36. How do you calculate percentiles with Python/ NumPy?

Ans: We can calculate percentiles with the following code

import numpy as np

a = np.array([1,2,3,4,5])

p = np.percentile(a, 50) #Returns 50th percentile, e.g. median


print(p)

Output

Q37. What advantages do NumPy arrays offer over (nested) Python lists?

Ans:

Python’s lists are efficient general-purpose containers. They support (fairly) efficient insertion, deletion,
appending, and concatenation, and Python’s list comprehensions make them easy to construct and
manipulate.

They have certain limitations: they don’t support “vectorized” operations like elementwise addition and
multiplication, and the fact that they can contain objects of differing types mean that Python must store
type information for every element, and must execute type dispatching code when operating on each
element.

NumPy is not just more efficient; it is also more convenient. You get a lot of vector and matrix
operations for free, which sometimes allow one to avoid unnecessary work. And they are also efficiently
implemented.

NumPy array is faster and You get a lot built in with NumPy, FFTs, convolutions, fast searching, basic
statistics, linear algebra, histograms, etc.

Q38. Explain the use of decorators.

Ans: Decorators in Python are used to modify or inject code in functions or classes. Using decorators,
you can wrap a class or function method call so that a piece of code can be executed before or after the
execution of the original code. Decorators can be used to check for permissions, modify or track the
arguments passed to a method, logging the calls to a specific method, etc.

Q39. What is the difference between NumPy and SciPy?

Ans:

In an ideal world, NumPy would contain nothing but the array data type and the most basic operations:
indexing, sorting, reshaping, basic elementwise functions, et cetera.
All numerical code would reside in SciPy. However, one of NumPy’s important goals is compatibility, so
NumPy tries to retain all features supported by either of its predecessors.

Thus NumPy contains some linear algebra functions, even though these more properly belong in SciPy.
In any case, SciPy contains more fully-featured versions of the linear algebra modules, as well as many
other numerical algorithms.

If you are doing scientific computing with python, you should probably install both NumPy and SciPy.
Most new features belong in SciPy rather than NumPy.

Q40. How do you make 3D plots/visualizations using NumPy/SciPy?

Ans: Like 2D plotting, 3D graphics is beyond the scope of NumPy and SciPy, but just as in the 2D case,
packages exist that integrate with NumPy. Matplotlib provides basic 3D plotting in the mplot3d
subpackage, whereas Mayavi provides a wide range of high-quality 3D visualization features, utilizing
the powerful VTK engine.

LEARN PYTHON FROM EXPERTS

Multiple Choice Questions

Q41. Which of the following statements create a dictionary? (Multiple Correct Answers Possible)

a) d = {}

b) d = {“john”:40, “peter”:45}

c) d = {40:”john”, 45:”peter”}

d) d = (40:”john”, 45:”50”)

Answer: b, c & d.

Dictionaries are created by specifying keys and values.

Q42. Which one of these is floor division?

a) /

b) //
c) %

d) None of the mentioned

Answer: b) //

When both of the operands are integer then python chops out the fraction part and gives you the round
off value, to get the accurate answer use floor division. For ex, 5/2 = 2.5 but both of the operands are
integer so answer of this expression in python is 2. To get the 2.5 as the answer, use floor division
using //. So, 5//2 = 2.5

Q43. What is the maximum possible length of an identifier?

a) 31 characters

b) 63 characters

c) 79 characters

d) None of the above

Answer: d) None of the above

Identifiers can be of any length.

Q44. Why are local variable names beginning with an underscore discouraged?

a) they are used to indicate a private variables of a class

b) they confuse the interpreter

c) they are used to indicate global variables

d) they slow down execution

Answer: a) they are used to indicate a private variables of a class

As Python has no concept of private variables, leading underscores are used to indicate variables that
must not be accessed from outside the class.
Q45. Which of the following is an invalid statement?

a) abc = 1,000,000

b) a b c = 1000 2000 3000

c) a,b,c = 1000, 2000, 3000

d) a_b_c = 1,000,000

Answer: b) a b c = 1000 2000 3000

Spaces are not allowed in variable names.

Q46. What is the output of the following?

try:

if '1' != 1:

raise "someError"

else:

print("someError has not occured")

except "someError":

print ("someError has occured")


a) someError has occured

b) someError has not occured

c) invalid code

d) none of the above

Answer: c) invalid code

A new exception class must inherit from a BaseException. There is no such inheritance here.

Q47. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1] ?

a) Error

b) None

c) 25

d) 2

Answer: c) 25

The index -1 corresponds to the last index in the list.

Q48. To open a file c:\scores.txt for writing, we use

a) outfile = open(“c:\scores.txt”, “r”)

b) outfile = open(“c:\\scores.txt”, “w”)

c) outfile = open(file = “c:\scores.txt”, “r”)

d) outfile = open(file = “c:\\scores.txt”, “o”)

Answer: b) The location contains double slashes ( \\ ) and w is used to indicate that file is being written
to.

Q49. What is the output of the following?


1

f = None

for i in range (5):

with open("data.txt", "w") as f:

if i > 2:

break

print f.closed

a) True

b) False

c) None

d) Error

Answer: a) True

The WITH statement when used with open file guarantees that the file object is closed when the with
block exits.

Q50. When will the else part of try-except-else be executed?


a) always

b) when an exception occurs

c) when no exception occurs

d) when an exception occurs in to except block

Answer: c) when no exception occurs

The else part is executed when no exception occurs.

You might also like