Python Notes by Jobhunter Team
Python Notes by Jobhunter Team
Python Notes by Jobhunter Team
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
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
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
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
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
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,
(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
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,
identity matrix. You can see the values of an array, if it is not too big, by just
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
A = Numeric.zeros((5,10),Numeric.Float)
for i in range(5):
for j in range(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
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:
A = Numeric.array([.1,.2,.3])
B = Numeric.array(range(20),Numeric.Complex)
C = Numeric.array( [ [1.,2.],[-2.,1.] ])
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
dx = .1
dy = .2
def f(i,j):
x = dx*i
y = dx*j
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
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
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
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,
indices 0,3,6,9. You can combine subarray indices for the various dimensions of
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)
E = C**5.2 #Exponentiation
The expressions can be as complicated as you want. Note that Numeric array
(and similarly for division). If you want matrix multiplication you use the
are 1D, this reduces to the conventional vector dot product. There is also a function
does not provide a function for matrix inversion; that can be found in various other
Numeric also provides versions of the standard math functions, that work
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,
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
Cross sections can be used with array arithmetic to compute many useful
derived quantities without the need of writing loops. For example, an approximation
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,
for i in range(n):
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
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
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.
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/
# Script Begins
print("GeeksQuiz")
# Scripts Ends
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.
1.3
Python designed by Guido van Rossum at CWI has become a widely used general-purpose, high-level
programming language.
Prerequisites:
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.
LANGUAGE FEATURES
Interpreted
There are no separate compilation and execution steps like C and C++.
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.
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.
High-level Language
In Python, no need to take care about low-level details such as managing the memory used by the
program.
Simple
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:
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.
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 static void main (String[] args)
System.out.println("Hello, world!");
Run on IDE
Python Code
Run on IDE
Run on IDE
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.”
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
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.
Pros:
Ease of use
Multi-paradigm Approach
Cons:
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
'''
-2
1.4
-1.4
# 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.
Hello, Geeks
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.
'''
Output in Python 2.x (Bytes is same as str)
<type 'str'>
<type 'str'>
<class 'str'>
<class 'bytes'>
'''
Run on IDE
'''
<type 'str'>
<type 'unicode'>
<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.
print(x),
print(x),
'''
12341234
'''
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
'''
'''
Run on IDE
try:
trying_to_check_error
'''
'''
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
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
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
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.
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.
# in and is
if 's' in 'geeksforgeeks':
for i in 'geeksforgeeks':
print ("\r")
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.
a = 10
def read():
print (a)
global a
a=5
def mod2():
a = 15
# prints 10
read()
mod1()
# prints 5
read()
mod2()
read()
# prints 10
def outer():
a=5
def inner():
nonlocal a
a = 10
inner()
print (a)
outer()
# prints 5
def outer():
a=5
def inner():
a = 10
inner()
print (a)
outer()
Run on IDE
Output:
10
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.
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:
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 :
elif b + 1 == 10:
else:
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.
if a % b == 0 :
else:
checkDivisibility(4, 2)
Run on IDE
The output is :
a is divisible by b
1.2
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.
x = 10
print(type(x))
x = 10000000000000000000000000000000000000000000
print(type(x))
Run on IDE
<type 'int'>
<type 'long'>
Output in Python 3 :
<type 'int'>
<type 'int'>
print(100**100)
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)
print("\n")
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)]
print(row)
print("\n")
t_matrix = zip(*matrix)
print row
Run on IDE
Output:
(1, 2, 3)
(4, 5, 6)
(7, 8, 9)
(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)).
# applied on 1D matrix
import numpy
matrix=[[1,2,3],[4,5,6]]
print(matrix)
print("\n")
print(numpy.transpose(matrix))
1.5
Global variables are the one that are defined and declared outside a function and we need to use them
inside a function.
def f():
print s
# Global scope
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
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
s = "Me too."
print s
# Global scope
f()
print s
Run on IDE
Output:
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:
def f():
global s
print s
print s
# Global Scope
s = "Python is great!"
f()
print s
Run on IDE
Output:
Python is great!
A good Example
a=1
def f():
def g():
a=2
global a
a=3
# Global scope
f()
g()
h()
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:
# A normal function
# 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 :
# A normal 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.
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.
print(a, b, c, d)
# Driver Code
my_list = [1, 2, 3, 4]
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.
print(a, b, c, d)
# Driver Code
my_list = [1, 2, 3, 4]
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:
>>>
[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.
# of packing
def mySum(*args):
sum = 0
return sum
# Driver code
print(mySum(1, 2, 3, 4, 5))
print(mySum(10, 20))
Run on IDE
Output:
15
30
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.
Run on IDE
Output :
Welcome to GeeksforGeeks
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
# initializing string
s = "10010"
c = int(s,2)
print (c)
# printing string converting to float
e = float(s)
print (e)
Run on IDE
Output:
# initializing integer
s = '4'
c = ord(s)
c = hex(56)
print (c)
c = oct(56)
print (c)
Run on IDE
Output:
8. list() : This function is used to convert any data type to a list type.
s = 'geeks'
c = tuple(s)
print (c)
c = set(s)
print (c)
c = list(s)
print (c)
Run on IDE
Output:
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.
# initializing integers
a=1
b=2
# initializing tuple
c = complex(1,2)
print (c)
c = str(a)
print (c)
# printing tuple converting to expression dictionary
c = dict(tup)
print (c)
Run on IDE
Output:
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.
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.
# initialising a String
a = 'GeeksforGeeks'
c = b'GeeksforGeeks'
d = a.encode('ASCII')
if (d==c):
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.
# initialising a String
a = 'GeeksforGeeks'
c = b'GeeksforGeeks'
# using decode() to decode the Byte object
d = c.decode('ASCII')
if (d==a):
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 = 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 = 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.
Java doesn’t allow ~ operator to be applied on boolean values. For example, the below program
produces compiler error.
import java.io.*;
class GFG
System.out.println(~a);
System.out.println(~b);
Run on IDE
Output:
System.out.println(~a);
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 :
print(min)
Run on IDE
Output:
10
a, b = 10, 20
Run on IDE
Output:
10
10
10
a, b = 10, 20
print ("Both a and b are equal" if a == b else "a is greater than b"
Run on IDE
a, b = 10, 20
if a != b:
if a > b:
else:
else:
Run on IDE
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 :
is returned.
Example :
a, b = 10, 20
# If a is less than b, then a is assigned
# work if a is 0.
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.
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.
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
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++)
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.
print 5//2
print -5//2
print 5.0//2
print -5.0//2
Run on IDE
Output:
-3
2.0
-3.0
Here is another surprise, In Python 3, ‘/’ operator does floating point division for both int and float
arguments.
print (5/2)
print (-5/2)
print (5.0/2)
print (-5.0/2)
Output:
2.5
-2.5
2.5
-2.5
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)
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.
Run on IDE
Output :
True
False
False
Inplace vs Standard Operators in Python
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 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.
import operator
# Initializing values
x=5
y=6
a=5
b=6
z = operator.add(a,b)
p = operator.iadd(x,y)
print (z)
# printing the modified value
print (p)
# value is unchanged
print (a)
# value is unchanged
print (x)
Run on IDE
Output:
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.
import operator
# Initializing list
a = [1, 2, 4, 5]
z = operator.add(a,[1, 2, 3])
print (z)
# value is unchanged
print (a)
# performs a+=[1, 2, 3]
p = operator.iadd(a,[1, 2, 3])
# value is changed
print (a)
Run on IDE
Output:
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.
Operation – a + b.
Operation – a – b.
Operation – a * b.
import operator
# Initializing variables
a=4
b=3
# using add() to add two numbers
Run on IDE
Output:
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.
Operation – a ** b.
7. mod(a,b) :- This functions returns modulus of the given arguments.
Operation – a % b.
import operator
# Initializing variables
a=5
b=2
print (operator.truediv(a,b))
print (operator.floordiv(a,b))
print (operator.mod(a,b))
Run on IDE
Output:
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.
import operator
# Initializing variables
a=3
b=3
if(operator.lt(a,b)):
if(operator.le(a,b)):
if (operator.eq(a,b)):
Run on IDE
Output:
3 is not less than 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.
import operator
# Initializing variables
a=4
b=3
if (operator.ge(a,b)):
if (operator.ne(a,b)):
Run on IDE
Output:
4 is greater than 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.
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:
statements(s)
print("List Iteration")
l = ["geeks", "for", "geeks"]
for i in l:
print(i)
print("\nTuple Iteration")
for i in t:
print(i)
print("\nString Iteration")
s = "Geeks"
for i in s :
print(i)
print("\nDictionary Iteration")
d = dict()
d['xyz'] = 123
d['abc'] = 345
for i in d :
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:
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.
for j in range(i):
print()
Run on IDE
Output:
1
22
333
4444
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
continue
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
# or 's'
break
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
pass
Run on IDE
Output:
Last Letter : s
1.8
C-style approach:
i=0
print cars[i]
i += 1
Run on IDE
Output:
Aston
Audi
McLaren
Important Points:
There is no C-Style for loop in Python, i.e., a loop like for (int i=0; i<n; i++)
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.
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.
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.
for i, x in enumerate(cars):
print (x)
Run on IDE
Output :
Aston
Audi
McLaren
for x in enumerate(cars):
Run on IDE
Output :
(0, 'Aston')
(1, 'Audi')
We can also directly print returned value of enumerate() to see what it returns.
Run on IDE
Output :
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.
Run on IDE
Output :
(1, 'Aston')
(2, 'Audi')
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.
# its accessories.
4:"890000$", 5:"4500$"}
%(a,prices[index+len(cars)]))
Run on IDE
Output:
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.
%(c, a)
Run on IDE
Output:
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.
# Unzip lists
])
print(l1)
print(l2)
Run on IDE
Output:
What is counter?
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 :
# Counter
print Counter(['B','B','A','B','C','A','B','B','A','C'])
# with dictionary
Run on IDE
Updation :
coun = collections.Counter()
And can be updated via update() method .Syntax for the same :
coun.update(Data)
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.
c1.subtract(c2)
print(c1)
Run on IDE
Output :
# Create a list
print(Counter(z))
Run on IDE
Output:
Counter({'blue': 3, 'red': 2, 'yellow': 1})
1.8
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 :
# Counter elements
# Create a list
col_count = Counter(z)
print(col_count)
col = ['blue','red','yellow','green']
# Here green is not in col_count
Run on IDE
Output:
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.
Example :
print(coun)
print(list(coun.elements()))
Run on IDE
Output :
most_common() :
most_common() is used to produce a sequence of the n most frequently encountered input values and
their respective counts.
# Counter
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.
class Test:
# Cosntructor
self.limit = limit
def __iter__(self):
self.x = 10
return self
def next(self):
# Store current value ofx
x = self.x
if x > self.limit:
raise StopIteration
self.x = x + 1;
return x
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
print("List Iteration")
for i in l:
print(i)
print("\nTuple Iteration")
for i in t:
print(i)
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 :
Run on IDE
Output :
List Iteration
geeks
for
geeks
Tuple Iteration
geeks
for
geeks
String Iteration
k
s
Dictionary Iteration
xyz 123
abc 345
2.5
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.
import itertools
import operator
# initializing list 1
li1 = [1, 4, 5, 7]
# initializing list 2
li2 = [1, 6, 5, 9]
# initializing list 3
# using accumulate()
print (list(itertools.accumulate(li1)))
# using accumulate()
print (list(itertools.accumulate(li1,operator.mul)))
print (list(itertools.chain(li1,li2,li3)))
Run on IDE
Output:
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.
import itertools
# initializing list 1
li1 = [1, 4, 5, 7]
# initializing list 2
li2 = [1, 6, 5, 9]
# initializing list 3
print (list(itertools.chain.from_iterable(li4)))
# using compress() selectively print data values
print (list(itertools.compress('GEEKSFORGEEKS',[1,0,0,0,0,1,0,0,1,0,0,0,0])))
Run on IDE
Output:
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.
import itertools
# initializing list
li = [2, 4, 5, 7, 8]
Run on IDE
Output:
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.
Using enumerate(): enumerate() is used to loop through the containers printing the index number along
with the value present in that particular index.
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.
# initializing list
questions = ['name', 'colour', 'shape']
Run on IDE
Output :
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:
print i,j
print i,j
Run on IDE
Output:
geeks for
only geeks
geeks for
only geeks
Example 2:
print(key, value)
Run on IDE
Output :
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:
# initializing list
lis = [ 1 , 3, 5, 6, 2, 1, 3 ]
for i in sorted(lis) :
print ("\r")
for i in sorted(set(lis)) :
print (i,end=" ")
Run on IDE
Output:
1123356
12356
Example 2:
# initializing list
# in sorted order
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:
# initializing list
lis = [ 1 , 3, 5, 6, 2, 1, 3 ]
for i in reversed(lis) :
Run on IDE
Output:
3126531
Example 2:
print (i)
Run on IDE
Output :
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.
def pypart(n):
# outer loop to handle number of rows
# n in this case
# printing stars
print("* ",end="")
print("\r")
# Driver Code
n=5
pypart(n)
Run on IDE
Output:
**
***
****
*****
def pypart2(n):
# number of spaces
k = 2*n - 2
print(end=" ")
k=k-2
print("\r")
# Driver Code
n=5
pypart2(n)
Run on IDE
Output:
**
***
****
*****
Printing Triangle
def triangle(n):
# number of spaces
k = 2*n - 2
# outer loop to handle number of rows
print(end=" ")
k=k-1
# printing stars
print("\r")
# Driver Code
n=5
triangle(n)
Run on IDE
Output:
**
***
****
*****
Number Pattern
def numpat(n):
num = 1
# re assigning num
num = 1
# printing number
num = num + 1
print("\r")
# Driver code
n=5
numpat(n)
Run on IDE
Output:
12
123
1234
12345
def contnum(n):
num = 1
# num = 1
# printing number
num = num + 1
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
def alphapat(n):
# ASCII value
num = 65
# 5 in this case
for i in range(0, n):
ch = chr(num)
# incrementing number
num = num + 1
print("\r")
# Driver Code
n=5
alphapat(n)
Run on IDE
Output:
A
BB
CCC
DDDD
EEEEE
def contalpha(n):
# ASCII value
num = 65
ch = chr(num)
num = num +1
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().
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
a = range(1,10000)
x = xrange(1,10000)
print (type(a))
print (type(x))
Run on IDE
Output:
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.
# on basis of memory
import sys
a = range(1,10000)
x = xrange(1,10000)
print (sys.getsizeof(a))
print (sys.getsizeof(x))
Run on IDE
Output:
The size allotted using range() is :
80064
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.
a = range(1,6)
x = xrange(1,6)
print (a[2:5])
# raises error
print ("The list after slicing using xrange is : ")
print (x[2:5])
Run on IDE
Error:
print (x[2:5])
Output:
[3, 4, 5]
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
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.
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.
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.
# libraries to be imported
import smtplib
from email.mime.multipart import MIMEMultipart
# instance of MIMEMultipart
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
body = "Body_of_the_mail"
msg.attach(MIMEText(body, 'plain'))
# open the file to be sent
filename = "File_name_with_extension"
p = MIMEBase('application', 'octet-stream')
p.set_payload((attachment).read())
encoders.encode_base64(p)
msg.attach(p)
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
# Authentication
s.login(fromaddr, "Password_of_the_sender")
text = msg.as_string()
s.quit()
Run on IDE
Important Points:
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.
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:
import feedparser
import notify2
import os
import time
def parseFeed():
f = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")
notify2.init('News Notify')
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
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.
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.
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)
n.show()
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.
# Hangman Game
import random
word = random.choice(someWords)
if __name__ == '__main__':
for i in word:
print()
playing = True
letterGuessed = ''
chances = len(word) + 2
correct = 0
try:
print()
chances -= 1
try:
except:
continue
if not guess.isalpha():
continue
continue
continue
if guess in word:
letterGuessed += guess
if char in letterGuessed:
print(char, end = ' ')
correct += 1
else:
if (Counter(letterGuessed) == Counter(word)):
print()
break
if chances == 0:
print()
except KeyboardInterrupt:
print()
exit()
# print(letterGuessed)
Run on IDE
_____
__m__
__mo_
l_mo_
lemo_
lemon
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.
import time
import os
birthdayFile = '/path/to/birthday/file'
def checkTodaysBirthdays():
today = time.strftime('%m%d')
flag = 0
if today in line:
flag =1
if flag == 0:
if __name__ == '__main__':
checkTodaysBirthdays()
Run on IDE
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:
Now we have to transfer this file to the path where Linux searches for its default files:
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
If the versions in the repositories are too old, install pyaudio using the following command
Windows users can install pyaudio by executing the following command in a terminal
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
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.
import speech_recognition as sr
#using lsusb
sample_rate = 48000
#here.
chunk_size = 2048
r = sr.Recognizer()
mic_list = sr.Microphone.list_microphone_names()
#the following loop aims to set the device ID of the mic that
if microphone_name == mic_name:
device_id = i
audio = r.listen(source)
try:
text = r.recognize_google(audio)
#error occurs when google could not understand what was said
except sr.UnknownValueError:
except sr.RequestError as e:
Run on IDE
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.
import speech_recognition as sr
AUDIO_FILE = ("example.wav")
r = sr.Recognizer()
#listen
audio = r.record(source)
try:
except sr.UnknownValueError:
except sr.RequestError as 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.
Mono:
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
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.
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.
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.
For example, */
Below is the Server side script that must be run at all times to keep the chatroom running.
import socket
import select
import sys
a continuous flow."""
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if len(sys.argv) != 3:
exit()
IP_address = str(sys.argv[1])
Port = int(sys.argv[2])
"""
"""
server.bind((IP_address, Port))
"""
"""
server.listen(100)
list_of_clients = []
while True:
try:
message = conn.recv(2048)
if message:
terminal"""
print "<" + addr[0] + "> " + message
broadcast(message_to_send, conn)
else:
remove(conn)
except:
continue
if clients!=connection:
try:
clients.send(message)
except:
clients.close()
# if the link is broken, we remove the client
remove(clients)
the program"""
def remove(connection):
if connection in list_of_clients:
list_of_clients.remove(connection)
while True:
connected"""
list_of_clients.append(conn)
# that connects
start_new_thread(clientthread,(conn,addr))
conn.close()
server.close()
Run on IDE
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.
import socket
import select
import sys
if len(sys.argv) != 3:
exit()
IP_address = str(sys.argv[1])
Port = int(sys.argv[2])
server.connect((IP_address, Port))
while True:
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.
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!
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
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.
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.
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.
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
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.
histSize : this represents our BIN count. For full scale, we pass [256].
For example:
img = cv2.imread('ex.jpg',0)
Plotting Histograms
import cv2
img = cv2.imread('ex.jpg',0)
histr = cv2.calcHist([img],[0],None,[256],[0,256])
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
img = cv2.imread('ex.jpg',0)
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 –
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.
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.
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.
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.
geeks
h1(“nerd”) % 10 = 3
h2(“nerd”) % 10 = 5
h3(“nerd”) % 10 = 4
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.
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:
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 :
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.
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.
import math
import mmh3
class BloomFilter(object):
'''
Class for Bloom filter, using murmur3 hash function
'''
'''
items_count : int
fp_prob : float
'''
self.fp_prob = fp_prob
self.size = self.get_size(items_count,fp_prob)
self.hash_count = self.get_hash_count(self.size,items_count)
self.bit_array = bitarray(self.size)
self.bit_array.setall(0)
def add(self, item):
'''
'''
digests = []
for i in range(self.hash_count):
digests.append(digest)
self.bit_array[digest] = True
'''
'''
for i in range(self.hash_count):
if self.bit_array[digest] == False:
return False
return True
@classmethod
def get_size(self,n,p):
'''
following formula
n : int
p : float
'''
m = -(n * math.log(p))/(math.log(2)**2)
return int(m)
@classmethod
'''
following formula
k = (m/n) * lg(2)
m : int
n : int
'''
k = (m/n) * math.log(2)
return int(k)
Run on IDE
bloomf = BloomFilter(n,p)
# 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_absent = ['bluff','cheater','hate','war','humanity',
'racism','hurt','nuke','gloomy','facebook',
'geeksforgeeks','twitter']
bloomf.add(item)
shuffle(word_present)
shuffle(word_absent)
shuffle(test_words)
if bloomf.check(word):
if word in word_absent:
else:
else:
Run on IDE
Output
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.
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.
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 stream of characters
MAX_CHAR = 256
def findFirstNonRepeating():
inDLL = [] * MAX_CHAR
# repeated[x] is false
stream = "geeksforgeeksandgeeksquizfor"
for i in xrange(len(stream)):
x = stream[i]
if not repeated[ord(x)]:
if not x in inDLL:
inDLL.append(x)
else:
inDLL.remove(x)
if len(inDLL) != 0:
print str(inDLL[0])
# Driver program
findFirstNonRepeating()
Run on IDE
Output:
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 .
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.
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"
if keyword.iskeyword(s):
if keyword.iskeyword(s2):
if keyword.iskeyword(s3):
if keyword.iskeyword(s4):
if keyword.iskeyword(s5):
if keyword.iskeyword(s6):
if keyword.iskeyword(s8):
if keyword.iskeyword(s9):
if keyword.iskeyword(s10):
if keyword.iskeyword(s11):
if keyword.iskeyword(s12):
if keyword.iskeyword(s13):
print ( s13 + " is a python keyword")
if keyword.iskeyword(s14):
if keyword.iskeyword(s15):
if keyword.iskeyword(s16):
Run on IDE
Output:
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.
import keyword
print (keyword.kwlist)
Run on IDE
Output:
1.5
Run on IDE
x, y = raw_input(), raw_input()
Run on IDE
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)]
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
Run on IDE
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.
import sys
# of list in sys.argv
argumentList = sys.argv
print argumentList
print sys.argv[0]
print sys.argv[1]
Run on IDE
OUTPUT :
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
The sys.argv takes the command line arguments in the form of a list.
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
print factorial(int(sys.argv[1]))
Run on IDE
Variable Arguments
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.
def multiplyAll(*values):
mul = 1
print values
for i in values:
mul = mul * i
return mul
# Driver program to test above function
ans = multiplyAll(1,2)
ans = multiplyAll(3, 4, 5, 6, 7)
Run on IDE
OUTPUT :
(1, 2)
Type =
(3, 4, 5, 6, 7)
Type =
Note that args are denoted by using a single star and kwargs are denoted by two stars before the formal
parameters in the function.
# form of dictionary
print details
printDetails(firstName = "Nikhil",
rollNumber = "007",
department = "CSE")
printDetails(firstName = "Abhay",
department = "ECE")
Run on IDE
Output:
Type =
Department = CSE
Type =
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 :
print arg
print "-" * 40
keys = sorted(keywords.keys())
for kw in keys:
shopkeeper='Michael Palin',
client="John Cleese",
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.
import this
Run on IDE
Output:
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.
2. One can return multiple values in Python. Don’t believe ? See the below code snippet:
def func():
return 1, 2, 3, 4, 5
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):
if num%2==0:
print(num)
print("1st Case:")
a = [2]
func(a)
print("2nd Case:")
a = [1]
func(a)
Run on IDE
Output:
1st Case:
2nd Case:
print(x,y)
foo_list = (3, 4)
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
vowels=['a','e','i','o','u']
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
i = 5;
print(ans)
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')
else:
print("Infinity is greatest")
# Negetive Infinity
n_infinity = float('-Inf')
else:
Run on IDE
Output:
Infinity is greatest
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.
a = []
for x in range(0,10):
a.append(x)
print(a)
# List Comprehension
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[::2]) # Skip by 2
Output:
[1, 2]
[1, 2, 3, 4]
[5, 4, 3, 2, 1]
[1, 3, 5]
[5, 3, 1]
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.
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.
file1 = open("MyFile.txt","a")
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()
file1.close()
Run on IDE
Writing to 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.
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()
file1 = open("myfile.txt","w")
file1.write("Hello \n")
file1.writelines(L)
file1 = open("myfile.txt","r+")
print file1.read()
file1.seek(0)
print file1.readline()
file1.seek(0)
# To show difference between read and readline
print file1.read(9)
file1.seek(0)
print file1.readline(9)
file1.seek(0)
# readlines function
print file1.readlines()
file1.close()
Run on IDE
Output:
Hello
This is Delhi
This is Paris
This is London
Output of Readline function is
Hello
Hello
Th
Hello
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
Appending to a file
file1 = open("myfile.txt","w")
file1.close()
# Append-adds at last
file1.close()
file1 = open("myfile.txt","r")
print file1.readlines()
file1.close()
# Write-Overwrites
file1.write("Tomorrow \n")
file1.close()
file1 = open("myfile.txt","r")
print file1.readlines()
file1.close()
Run on IDE
Output:
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']
Output of Readlines after writing
['Tomorrow \n']
Related Article :
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.
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.
# importing module
import sqlite3
connection = sqlite3.connect("myTable.db")
# cursor
crsr = connection.cursor()
lname VARCHAR(30),
gender CHAR(1),
joining DATE);"""
crsr.execute(sql_command)
sql_command = """INSERT INTO emp VALUES (23, "Rishabh", "Bansal", "M", "2014-03-28");"""
crsr.execute(sql_command)
sql_command = """INSERT INTO emp VALUES (1, "Bill", "Gates", "M", "1980-10-28");"""
crsr.execute(sql_command)
connection.commit()
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.
import sqlite3
connection = sqlite3.connect("myTable.db")
# cursor object
crsr = connection.cursor()
# execute the command to fetch all the data from the table emp
ans= crsr.fetchall()
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 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.
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.
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)
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.
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:
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.
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.
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.
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.
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.
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.
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.
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
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()"
import m
def monkey_f(self):
print "monkey_f()"
m.MyClass.f = monkey_f
obj = m.MyClass()
obj.f()
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.
count = 0
text = fh.read()
for character in text:
if character.isupper():
count += 1
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?
shuffle(x)
print(x)
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.
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.
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’.
2
3
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)))
A4 = [i for i in A1 if i in A3]
print(A0,A1,A2,A3,A4,A5,A6)
A1 = range(0, 10)
A2 = []
A3 = [1, 2, 3, 4, 5]
A4 = [1, 2, 3, 4, 5]
A6 = [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]
Ans: To modify the strings, Python’s “re” module is providing 3 methods. They are:
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.
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.
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.
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.
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.
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.
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.
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.
DATABASES = {
'default': {
'ENGINE' : 'django.db.backends.sqlite3',
4
5
import datetime
def Current_datetime(request):
now = datetime.datetime.now()
return HttpResponse(html)
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
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.
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.
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?
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.
10
11
12
13
14
15
16
17
18
19
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)
row = title + ' - ' + year + ' ' + ' ' + rating
print(row)
The above code will help scrap data from IMDb’s top 250 list
Ans: We can get the indices of N maximum values in a NumPy array using the below code:
import numpy as np
print(arr.argsort()[-3:][::-1])
Output
[431]
import numpy as np
a = np.array([1,2,3,4,5])
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.
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.
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.
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.
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.
a) /
b) //
c) %
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
a) 31 characters
b) 63 characters
c) 79 characters
Q44. Why are local variable names beginning with an underscore discouraged?
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
d) a_b_c = 1,000,000
try:
if '1' != 1:
raise "someError"
else:
except "someError":
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
Answer: b) The location contains double slashes ( \\ ) and w is used to indicate that file is being written
to.
f = None
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.