Python Book
Python Book
Fundamental:
Programming is important for speeding up the input and output processes in a machine.
Programming is important to automate, collect, manage, calculate, analyze the processing of
data and information accurately.
Programming is important to create software and applications that help computer and mobile
users in daily life. Due to all these reasons, it’s really important to learn how to use
programming languages in our daily life.
Java, javascript, C#, C++, PHP, Python, Swift, SQL, Ruby, etc. programming languages are
the reasons behind the innovations in information technologies. If today we are seeing
robots, artificial intelligence, machine learning, bitcoins, the blockchain, IOT (Internet of
Things), Cloud Computing, etc. new technology and products in IT industry, then it’s
because of programming languages.
What is a program?
A program is a group of logical, mathematical, systematical and managed functions grouped
together to perform a specific task. Just like there are various programs in the marriage such
as Shanti, Barat Prasthan, Lagan, vadhu pravesh, reception, dham etc.
What is a function?
Groups of classes are called a function. A function requires specific classes. For example, if
you want to create a function to play songs then you need to create specific classes under this
programs in which play, pause, stop and etc. are the classes. Now in a marriage example,
shanti program contains various function such as mama swagat, batna lepan, lok sangit, sehra
bandi are various functions inside the Shanti program. That’s why small and big functions
are important in a programming language to create software.
Language Types
Machine and assembly languages
A machine language consists of the numeric codes for the operations that a particular
computer can execute directly. The codes are strings of 0s and 1s, or binary digits (“bits”),
which are frequently converted both from and to hexadecimal (base 16) for human viewing
and modification. Machine language instructions typically use some bits to represent
operations, such as addition, and some to represent operands, or perhaps the location of the
next instruction. Machine language is difficult to read and write, since it does not resemble
conventional mathematical notation or human language, and its codes vary from computer to
computer.
Assembly language is one level above machine language. It uses short mnemonic codes for
instructions and allows the programmer to introduce names for blocks of memory that hold
data. One might thus write “add pay, total” instead of “0110101100101000” for an
instruction that adds two numbers. Assembly language is designed to be easily translated into
machine language. Although blocks of data may be referred to by name instead of by their
machine addresses, assembly language does not provide more sophisticated means of
organizing complex information.
Computers are a balanced mix of software and hardware. Hardware is just a piece of
mechanical device and its functions are being controlled by a compatible software.
Hardware understands instructions in the form of electronic charge, which is the counterpart
of binary language in software programming. Binary language has only two alphabets, 0
and 1. To instruct, the hardware codes must be written in binary format, which is simply a
series of 1s and 0s. It would be a difficult and cumbersome task for computer programmers
to write such codes, which is why we have compilers to write such codes.
Preprocessor
A preprocessor, generally considered as a part of compiler, is a tool that produces input for
compilers. It deals with macro-processing, augmentation, file inclusion, language extension,
etc.
Interpreter
An interpreter, like a compiler, translates high-level language into low-level machine
language. The difference lies in the way they read the source code or input. A compiler
reads the whole source code at once, creates tokens, checks semantics, generates
intermediate code, executes the whole program and may involve many passes. In contrast,
an interpreter reads a statement from the input, converts it to an intermediate code, executes
it, then takes the next statement in sequence. If an error occurs, an interpreter stops
execution and reports it. whereas a compiler reads the whole program even if it encounters
several errors.
Assembler
An assembler translates assembly language programs into machine code.The output of an
assembler is called an object file, which contains a combination of machine instructions as
well as the data required to place these instructions in memory.
Linker
Linker is a computer program that links and merges various object files together in order to
make an executable file. All these files might have been compiled by separate assemblers.
The major task of a linker is to search and locate referenced module/routines in a program
and to determine the memory location where these codes will be loaded, making the
program instruction to have absolute references.
Loader
Loader is a part of operating system and is responsible for loading executable files into
memory and execute them. It calculates the size of a program (instructions and data) and
creates memory space for it. It initializes various registers to initiate execution.
Cross-compiler
A compiler that runs on platform (A) and is capable of generating executable code for
platform (B) is called a cross-compiler.
Source-to-source Compiler
A compiler that takes the source code of one programming language and translates it into
the source code of another programming language is called a source-to-source compiler.
What is Software Testing?
Software testing is defined as an activity to check whether the actual results match the
expected results and to ensure that the software system is Defect free. It involves execution
of a software component or system component to evaluate one or more properties of interest.
Software testing also helps to identify errors, gaps or missing requirements in contrary to the
actual requirements. It can be either done manually or using automated tools. Some prefer
saying Software testing as a White Box and Black Box Testing.
Types of Testing:-
1. Unit Testing
It focuses on smallest unit of software design. In this we test an individual unit or group of
inter related units.It is often done by programmer by using sample input and observing its
corresponding outputs.
2. Integration Testing
The objective is to take unit tested components and build a program structure that has been
dictated by design.Integration testing is testing in which a group of components are
combined to produce output.
3. Regression Testing
Every time new module is added leads to changes in program. This type of testing make sure
that whole component works properly even after adding components to the complete
program.
4. Alpha Testing
This is a type of validation testing.It is a type of acceptance testing which is done before the
product is released to customers.
5. Beta Testing
The beta test is conducted at one or more customer sites by the end-user of the software. This
version is released for the limited number of users for testing in real time environment
6. System Testing
In this software is tested such that it works fine for different operating system.It is covered
under the black box testing technique. In this we just focus on required input and output
without focusing on internal working.
Debugging
Debugging, in computer programming and engineering, is a multistep process that involves
identifying a problem, isolating the source of the problem, and then either correcting the
problem or determining a way to work around it. The final step of debugging is to test the
correction or workaround and make sure it works.
Documentation
In computer hardware and software product development, documentation is the information
that describes the product to its users. It consists of the product technical manuals and online
information (including online versions of the technical manuals and help facility
descriptions). The term is also sometimes used to mean the source information about the
product contained in design documents, detailed code comments, white papers, and
blackboard session notes.
The term is derived from the idea that engineers and programmers "document" their products
in formal writing. The earliest computer users were sometimes simply handed the engineers'
or programmers' "documentation." As the product audience grew, it became necessary to add
professional technical writers and editors to the process. Today, IBM and other companies
look at developing product information based on what users actually need to do when using
the product. In this task-oriented view, product information can be divided into and
sometimes physically organized into these task categories: evaluating, planning for, setting
up or installing, customizing, administering, using, and maintaining the product.
Definition of Algorithm
Problem-solving in terms of computers, what does it mean? We know that a computer can
solve any computational problem, but how does it work? Before solving a problem one
should always know what to do and how to do it, and what types of steps should be carried
out etcetera. Therefore, an algorithm is the series of steps that must be executed to solve a
problem.
Features of an Algorithm:
• As we know that an algorithm takes some inputs, execute some finite number of steps
and gives an output. So, the certain step involved in the algorithm must be executable.
• It must generate some result.
• After a specific period, it must cease to run.
Definition of Flowchart
We have discussed the term algorithm in the previous definition. Now, what is a flowchart?
It is nothing but a manner of representing an algorithm. It is also known as the flow diagram,
which illustrates a process or a detailed series of steps needed to produce a specific output. A
flow chart is comprised of the different symbols and control lines to connect those symbols.
Each symbol specifies distinct functions. It is extremely useful in programming because it
simplifies the complicated algorithm and converts it into the understandable pictorial
representation.
• Rounded rectangle: Show the starting and ending point of the process.
• Diamond: Renders the decision point generally in the form of Yes/No or True/False.
• Control flow lines: Describes the flow and control of the data.
Parallelogram: Represents the Inputs given to the process or an Output generated by the
process.
It is used for:
A compiler reads the program and translates it completely before the program starts running.
In this context, the high-level program is called the source code, and the translated program
is called the object code or the executable. Once a program is compiled, you can execute it
repeatedly without further translation.
Alternatively, you can store code in a file and use the interpreter to execute the contents of
the file, which is called a script. By convention, Python scripts have names that end with
.py.
To execute the script, you have to tell the interpreter the name of the file. If you have a script
named dinsdale.py and you are working in a UNIX command window, you type python
dinsdale.py.
Or by creating a python file on the server, using the .py file extension, and running it in the
Command Line:
Where in other programming languages the indentation in code is for readability only, the
indentation in Python is very important.
if 5 > 2:
print("Five is greater than two!")
Python Variables
In Python variables are created the moment you assign a value to it:
Variables in Python:
x =5
y = "Hello, World!"
Comments
Python has commenting capability for the purpose of in-code documentation.
Comments start with a #, and Python will render the rest of the line as a comment:
Example
Comments in Python:
#This is a comment.
print("Hello, World!")
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume). Rules for Python variables:
Example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Global Variables
Variables that are created outside of a function (as in all of the examples above) are known
as global variables. Global variables can be used by everyone, both inside of functions and
outside.
Example
Create a variable outside of a function, and use it inside the function
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
To create a global variable inside a function, you can use the global keyword.
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Variable names and keywords
Programmers generally choose names for their variables that are meaningful—they
document what the variable is used for.
Variable names can be arbitrarily long. They can contain both letters and numbers, but they
have to begin with a letter. It is legal to use uppercase letters, but it is a good idea to begin
variable names with a lowercase letter.
The underscore character, _, can appear in a name. It is often used in names with multiple
words, such as my_name or airspeed_of_unladen_swallow.
If you give a variable an illegal name, you get a syntax error:
# Integer number
num = 100
print(num)
print("Data Type of variable num is", type(num))
Long – Long data type is deprecated in Python 3 because there is no need for it, since the
integer has no upper limit, there is no point in having a data type that allows larger upper
limit than integers.
Float – Values with decimal points are the float values, there is no need to specify the data type in
Python. It is automatically inferred based on the value we are assigning to a variable. For example
here fnum is a float data type.
# float number
fnum = 34.45
print(fnum)
print("Data Type of variable fnum is", type(fnum))
Complex Number – Numbers with real and imaginary parts are known as complex numbers.
Unlike other programming language such as Java, Python is able to identify these complex
numbers with the values. In the following example when we print the type of the variable
cnum, it prints as complex number.
# complex number
cnum = 3 + 4j
print(cnum)
print("Data Type of variable cnum is", type(cnum))
Strings in Python are either enclosed with single quotes or double quotes. In the following
example we have demonstrated two strings one with the double quotes and other string s2
with the single quotes. To read more about strings, refer this article: Python Strings.
s = "This is a String"
s2 = 'This is also a String'
# tuple of integers
t1 = (1, 2, 3, 4, 5)
# prints entire tuple
print(t1)
# tuple of strings
t2 = ("hi", "hello", "bye")
# loop through tuple elements
for s in t2:
print (s)
A list is enclosed with square brackets and elements are separated by commas. To read more
about Lists, refer this guide: Python Lists
# list of integers
lis1 = (1, 2, 3, 4, 5)
# prints entire list
print(lis1)
# list of strings
lis2 = ("Apple", "Orange", "Banana")
# loop through tuple elements
for x in lis2:
print (x)
The keys in a dictionary doesn’t necessarily to be a single data type, as you can see in the
following example that we have 1 integer key and two string keys.
# Dictionary example
# Set Example
myset = {"hi", 2, "bye", "Hello World"}
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Arithmetic Operators
Comparison Operators
Assignment Operators
Bitwise Operators
Logical Operators
Membership Operators
Python If Statement
If statements are control flow statements which helps us to run a particular code only
when a certain condition is satisfied. For example, you want to print a message on the
screen only when a condition is true then you can use if statement to accomplish this in
programming.
if condition:
block_of_code
if condition:
block_of_code_1
elif condition_2:
block_of_code_2
elif condition_3:
block_of_code_3
..
..
..
else:
block_of_code_n
1. There can be multiple ‘elif’ blocks, however there is only ‘else’ block is
allowed.
2. Out of all these blocks only one block_of_code gets executed. If the condition is
true then the code inside ‘if’ gets executed, if condition is false then the next
condition(associated with elif) is evaluated and so on. If none of the conditions
is true then the code inside ‘else’ gets executed.
Example
num = 1122
if 9 < num < 99:
print("Two digit number")
elif 99 < num < 999:
print("Three digit number")
elif 999 < num < 9999:
print("Four digit number")
else:
print("number is <= 9 or >= 9999")
num = -99
if num > 0:
print("Positive Number")
else:
print("Negative Number")
#nested if
if -99<=num:
print("Two digit Negative Number")
Function range()
In the above example, we have iterated over a list using for loop. However we can also use a
range() function in for loop to iterate over numbers defined by range().
range(start, stop): generates a set of whole numbers starting from start to stop-1.
For example:
range(5, 9) is equivalent to [5, 6, 7, 8]
range(start, stop, step_size): The default step_size is 1 which is why when we didn’t
specify the step_size, the numbers generated are having difference of 1. However by
specifying step_size we can generate numbers having the difference of step_size.
For example:
range(1, 10, 2) is equivalent to [1, 3, 5, 7, 9]
num = 1
# loop will repeat itself as long as
# num < 10 remains true
while num < 10:
print(num)
#incrementing the value of num
num = num + 3
while True:
print("hello")
Example 2:
num = 1
while num<5:
print(num)
This will print ‘1’ indefinitely because inside loop we are not updating the value of num, so
the value of num will always remain 1 and the condition num < 5 will always return true.
i=1
j=5
while i < 4:
while j < 8:
print(i, ",", j)
j=j+1
i=i+1
while loop with else block
We can have a ‘else’ block associated with while loop. The ‘else’ block is optional. It
executes only after the loop finished execution.
num = 10
while num > 6:
print(num)
num = num-1
else:
print("loop is finished")
Continue
What is Tick?
Time intervals are floating-point numbers in units of seconds. Particular instants in time are
expressed in seconds since 12:00am, January 1, 1970(epoch).
There is a popular time module available in Python which provides functions for working
with times, and for converting between representations. The function time.time() returns the
current system time in ticks since 12:00am, January 1, 1970(epoch).
Example
import time; # This is required to include time module.
ticks = time.time()
print "Number of ticks since 12:00am, January 1, 1970:", ticks
What is TimeTuple?
Many of Python's time functions handle time as a tuple of 9 numbers, as shown below −
Getting current time
To translate a time instant from a seconds since the epoch floating-point value into a time-
tuple, pass the floating-point value to a function (e.g., localtime) that returns a time-tuple
with all nine items valid.
import time;
localtime = time.localtime(time.time())
print "Local current time :", localtime
This would produce the following result, which could be formatted in any other
presentable form –
cal = calendar.month(2008, 1)
print "Here is the calendar:"
print cal
Python - Functions
A function is a block of organized, reusable code that is used to perform a single, related
action. Functions provide better modularity for your application and a high degree of code
reusing.
As you already know, Python gives you many built-in functions like print(), etc. but you can
also create your own functions. These functions are called user-defined functions.\
Why functions?
• It may not be clear why it is worth the trouble to divide a program into functions. There are
several reasons:
• Creating a new function gives you an opportunity to name a group of statements, which
makes your program easier to read and debug.
• Functions can make a program smaller by eliminating repetitive code. Later, if you make a
change, you only have to make it in one place.
• Dividing a long program into functions allows you to debug the parts one at a time and then
assemble them into a working whole.
• Well-designed functions are often useful for many programs. Once you write and debug
one, you can reuse it.
Defining a Function
You can define functions to provide the required functionality. Here are simple rules to
define a function in Python.
• Function blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).
• Any input parameters or arguments should be placed within these parentheses. You
can also define parameters inside these parentheses.
• The first statement of a function can be an optional statement - the documentation
string of the function or docstring.
• The code block within every function starts with a colon (:) and is indented.
• The statement return [expression] exits a function, optionally passing back an
expression to the caller. A return statement with no arguments is the same as return
None.
Syntax
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
By default, parameters have a positional behavior and you need to inform them in the
same order that they were defined.
Calling a Function
Defining a function only gives it a name, specifies the parameters that are to be included in
the function and structures the blocks of code.
Once the basic structure of a function is finalized, you can execute it by calling it from
another function or directly from the Python prompt. Following is the example to call
printme() function −
# Function definition is here
def printme( str ):
"This prints a passed string into this function"
print str
return;
Here, we are maintaining reference of the passed object and appending values in the
same object. So, this would produce the following result –
Function Arguments
You can call a function by using the following types of formal arguments −
• Required arguments
• Keyword arguments
• Default arguments
• Variable-length arguments
Required arguments
Required arguments are the arguments passed to a function in correct positional order. Here,
the number of arguments in the function call should match exactly with the function
definition.
To call the function printme(), you definitely need to pass one argument, otherwise it gives
a syntax error as follows −
# Function definition is here
def printme( str ):
"This prints a passed string into this function"
print str
return;
Recursion
It is legal for one function to call another; it is also legal for a function to call itself. It may
not be obvious why that is a good thing, but it turns out to be one of the most magical things
a program can do. For example, look at the following function:
def countdown(n):
if n <= 0:
print 'Blastoff!'
else:
print n
countdown(n-1)
If n is 0 or negative, it outputs the word, “Blastoff!” Otherwise, it outputs n and then calls a
function named countdown—itself—passing n-1 as an argument.
What happens if we call this function like this?
>>> countdown(3)
The execution of countdown begins with n=3, and since n is greater than 0, it outputs the
value 3, and then calls itself...
As another example, we can write a function that prints a string n times.
var1[0]: H
var2[1:5]: ytho
Updating Strings
You can "update" an existing string by (re)assigning a variable to another string. The new
value can be related to its previous value or to a completely different string altogether. For
example −
var1 = 'Hello World!'
print "Updated String :- ", var1[:6] + 'Python'
Escape Characters
Following table is a list of escape or non-printable characters that can be represented with
backslash notation.
An escape character gets interpreted; in a single quoted as well as double quoted strings.
\a Bell or alert
\b Backspace
\cx Control-x
\C-x Control-x
\e Escape
\f Formfeed
\M-\C-x Meta-Control-x
\n Newline
\r Carriage return
\s Space
\t Tab
\v Vertical tab
\x Character x
* Repetition - Creates new strings, concatenating multiple copies a*2 will give -
of the same string HelloHello
[] Slice - Gives the character from the given index a[1] will give e
[:] Range Slice - Gives the characters from the given range a[1:4] will give ell
not in Membership - Returns true if a character does not exist in the M not in a will give
given string 1
r/R Raw String - Suppresses actual meaning of Escape characters. print r'\n' prints \n
The syntax for raw strings is exactly the same as for normal and print R'\n'prints
strings with the exception of the raw string operator, the letter \n
"r," which precedes the quotation marks. The "r" can be
lowercase (r) or uppercase (R) and must be placed immediately
preceding the first quote mark.
% Format - Performs String formatting See at next section
1 capitalize()
2 center(width, fillchar)
Returns a space-padded string with the original string centered to a total of width columns.
Counts how many times str occurs in string or in a substring of string if starting index beg and
ending index end are given.
4 decode(encoding='UTF-8',errors='strict')
Decodes the string using the codec registered for encoding. encoding defaults to the default string
encoding.
5 encode(encoding='UTF-8',errors='strict')
Returns encoded string version of string; on error, default is to raise a ValueError unless errors is
given with 'ignore' or 'replace'.
Determines if string or a substring of string (if starting index beg and ending index end are given)
ends with suffix; returns true if so and false otherwise.
7 expandtabs(tabsize=8)
Expands tabs in string to multiple spaces; defaults to 8 spaces per tab if tabsize not provided.
Determine if str occurs in string or in a substring of string if starting index beg and ending index
end are given returns index if found and -1 otherwise.
Returns true if string has at least 1 character and all characters are alphanumeric and false
otherwise.
11 isalpha()
Returns true if string has at least 1 character and all characters are alphabetic and false otherwise.
12 isdigit()
13 islower()
Returns true if string has at least 1 cased character and all cased characters are in lowercase and
false otherwise.
14 isnumeric()
Returns true if a unicode string contains only numeric characters and false otherwise.
15 isspace()
Returns true if string contains only whitespace characters and false otherwise.
16 istitle()
17 isupper()
Returns true if string has at least one cased character and all cased characters are in uppercase
and false otherwise.
18 join(seq)
Merges (concatenates) the string representations of elements in sequence seq into a string, with
separator string.
19 len(string)
20 ljust(width[, fillchar])
Returns a space-padded string with the original string left-justified to a total of width columns.
21 lower()
Converts all uppercase letters in string to lowercase.
22 lstrip()
23 maketrans()
24 max(str)
25 min(str)
Replaces all occurrences of old in string with new or at most max occurrences if max given.
27 rfind(str, beg=0,end=len(string))
29 rjust(width,[, fillchar])
Returns a space-padded string with the original string right-justified to a total of width columns.
30 rstrip()
31 split(str="", num=string.count(str))
Splits string according to delimiter str (space if not provided) and returns list of substrings; split
into at most num substrings if given.
32 splitlines( num=string.count('\n'))
Splits string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs removed.
33 startswith(str, beg=0,end=len(string))
Determines if string or a substring of string (if starting index beg and ending index end are given)
starts with substring str; returns true if so and false otherwise.
34 strip([chars])
35 swapcase()
36 title()
Returns "titlecased" version of string, that is, all words begin with uppercase and the rest are
lowercase.
37 translate(table, deletechars="")
Translates string according to translation table str(256 chars), removing those in the del string.
38 upper()
39 zfill (width)
Returns original string leftpadded with zeros to a total of width characters; intended for numbers,
zfill() retains any sign given (less one zero).
40 isdecimal()
Returns true if a unicode string contains only decimal characters and false otherwise.
Python - Lists
The most basic data structure in Python is the sequence. Each element of a sequence is
assigned a number - its position or index. The first index is zero, the second index is one,
and so forth.
Python has six built-in types of sequences, but the most common ones are lists and tuples,
which we would see in this tutorial.
There are certain things you can do with all sequence types. These operations include
indexing, slicing, adding, multiplying, and checking for membership. In addition, Python
has built-in functions for finding the length of a sequence and for finding its largest and
smallest elements.
A list is a sequence
Like a string, a list is a sequence of values. In a string, the values are characters; in a list,
they can be any type. The values in a list are called elements or sometimes items. There are
several ways to create a new list; the simplest is to enclose the elements in square brackets ([
and ]):
[10, 20, 30, 40]
['crunchy frog', 'ram bladder', 'lark vomit']
The first example is a list of four integers. The second is a list of three strings. The elements
of a list don’t have to be the same type. The following list contains a string, a float, an
integer, and (lo!) another list:
['spam', 2.0, 5, [10, 20]]
A list within another list is nested. A list that contains no elements is called an empty list;
you can create one with empty brackets, [].
As you might expect, you can assign list values to variables:
Unlike strings, lists are mutable. When the bracket operator appears on the left side of an
assignment, it identifies the element of the list that will be assigned.
Python Lists
The list is a most versatile datatype available in Python which can be written as a list of
comma-separated values (items) between square brackets. Important thing about a list is that
items in a list need not be of the same type.
Creating a list is as simple as putting different comma-separated values between square
brackets. For example −
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]
Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and so
on.
Accessing Values in Lists
To access values in lists, use the square brackets for slicing along with the index or indices
to obtain value available at that index. For example −
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
Updating Lists
You can update single or multiple elements of lists by giving the slice on the left-hand side
of the assignment operator, and you can add to elements in a list with the append() method.
For example −
list = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "
print list[2]
list[2] = 2001;
print "New value available at index 2 : "
print list[2]
append() method is discussed in subsequent section.
When the above code is executed, it produces the following result −
Value available at index 2 :
1997
New value available at index 2 :
2001
1 cmp(list1, list2)
2 len(list)
3 max(list)
4 min(list)
5 list(seq)
1 list.append(obj)
2 list.count(obj)
3 list.extend(seq)
Appends the contents of seq to list
4 list.index(obj)
5 list.insert(index, obj)
6 list.pop(obj=list[-1])
7 list.remove(obj)
8 list.reverse()
9 list.sort([func])
Python - Tuples
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The
differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples
use parentheses, whereas lists use square brackets.
Creating a tuple is as simple as putting different comma-separated values. Optionally you
can put these comma-separated values between parentheses also. For example −
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
tup1 = ();
To write a tuple containing a single value you have to include a comma, even though there is
only one value –
tup1 = (50,);
Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so
on.
Accessing Values in Tuples
To access values in tuple, use the square brackets for slicing along with the index or indices
to obtain value available at that index. For example −
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print "tup1[0]: ", tup1[0];
print "tup2[1:5]: ", tup2[1:5];
tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]
Updating Tuples
Tuples are immutable which means you cannot update or change the values of tuple
elements. You are able to take portions of existing tuples to create new tuples as the
following example demonstrates −
tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');
This produces the following result. Note an exception raised, this is because after del
tup tuple does not exist any more –
('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
File "test.py", line 9, in <module>
print tup;
NameError: name 'tup' is not defined
1 cmp(tuple1, tuple2)
2 len(tuple)
3 max(tuple)
4 min(tuple)
5 tuple(seq)
Python - Dictionary
Each key is separated from its value by a colon (:), the items are separated by commas, and
the whole thing is enclosed in curly braces. An empty dictionary without any items is
written with just two curly braces, like this: {}.
Keys are unique within a dictionary while values may not be. The values of a dictionary can
be of any type, but the keys must be of an immutable data type such as strings, numbers, or
tuples.
Accessing Values in Dictionary
To access dictionary elements, you can use the familiar square brackets along with the key
to obtain its value. Following is a simple example −
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']
When the above code is executed, it produces the following result −
dict['Name']: Zara
dict['Age']: 7
If we attempt to access a data item with a key, which is not part of the dictionary, we get an
error as follows −
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print "dict['Alice']: ", dict['Alice']
When the above code is executed, it produces the following result −
dict['Alice']:
Traceback (most recent call last):
File "test.py", line 4, in <module>
print "dict['Alice']: ", dict['Alice'];
KeyError: 'Alice'
Updating Dictionary
You can update a dictionary by adding a new entry or a key-value pair, modifying an
existing entry, or deleting an existing entry as shown below in the simple example –
1 cmp(dict1, dict2)
2 len(dict)
Gives the total length of the dictionary. This would be equal to the number of items in the
dictionary.
3 str(dict)
4 type(variable)
Returns the type of the passed variable. If passed variable is dictionary, then it would return a
dictionary type.
1 dict.clear()
3 dict.fromkeys()
Create a new dictionary with keys from seq and values set to value.
4 dict.get(key, default=None)
5 dict.has_key(key)
6 dict.items()
7 dict.keys()
8 dict.setdefault(key, default=None)
Similar to get(), but will set dict[key]=default if key is not already in dict
9 dict.update(dict2)
10 dict.values()
Python - Modules
A module allows you to logically organize your Python code. Grouping related code into a
module makes the code easier to understand and use. A module is a Python object with
arbitrarily named attributes that you can bind and reference.
Simply, a module is a file consisting of Python code. A module can define functions, classes
and variables. A module can also include runnable code.
Example
The Python code for a module named aname normally resides in a file named aname.py.
Here's an example of a simple module, support.py
def print_func( par ):
print "Hello : ", par
return
Python provides two ways to import modules; we have already seen one:
If you import math, you get a module object named math. The module object contains
constants like pi and functions like sin and exp.
But if you try to access pi directly, you get an error.
>>> print pi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'pi' is not defined
Or you can use the star operator to import everything from the module:
>>> from math import *
>>> cos(pi)
-1.0
File is a named location on disk to store related information. It is used to permanently store
data in a non-volatile memory (e.g. hard disk).
Since, random access memory (RAM) is volatile which loses its data when computer is
turned off, we use files for future use of the data.
When we want to read from or write to a file we need to open it first. When we are done, it
needs to be closed, so that resources that are tied with the file are freed.
1. Open a file
2. Read or write (perform operation)
3. Close the file
Python too supports file handling and allows users to handle files i.e., to read and write files,
along with many other file handling options, to operate on files. The concept of file handling
has stretched over various other languages, but the implementation is either complicated or
lengthy, but alike other concepts of Python, this concept here is also easy and short. Python
treats file differently as text or binary and this is important. Each line of code includes a
sequence of characters and they form text file. Each line of a file is terminated with a special
character, called the EOL or End of Line characters like comma {,} or newline character. It
ends the current line and tells the interpreter a new one has begun. Let’s start with Reading
and Writing files.
Working of open() function
We use open () function in Python to open a file in read or write mode. As explained above,
open ( ) will return a file object. To return a file object we use open() function along with
two arguments, that accepts file name and the mode, whether to read or write. So, the syntax
being: open(filename, mode).
Mode Description
Open a file for writing. Creates a new file if it does not exist or truncates the file if it
'w' exists.
'x' Open a file for exclusive creation. If the file already exists, the operation fails.
Open for appending at the end of the file without truncating it. Creates a new file if it
'a' does not exist.
One must keep in mind that the mode argument is not mandatory. If not passed, then Python
will assume it to be “ r ” by default.
Another way to read a file is to call a certain number of characters like in the following
code the interpreter will read the first five characters of stored data and return it as a
string:
The close() command terminates all the resources in use and frees the system of this
particular program.
Working of append() mode
# Python code to illustrate append() mode
file = open('file.txt','a')
file.write("This will add this line")
file.close()
Here is the complete list of methods in text mode with a brief description.
Method Description
close() Close an open file. It has no effect if the file is already closed.
Read atmost n characters form the file. Reads till end of file if it is
read(n) negative or None.
Read and return one line from the file. Reads in at most n bytes if
readline(n=-1) specified.
Resize the file stream to size bytes. If size is not specified, resize to
truncate(size=None) current location.
writable() Returns True if the file stream can be written to.
NumPy in Python
What is NumPy?
NumPy is a general-purpose array-processing package. It provides a high-performance
multidimensional array object, and tools for working with these arrays.
It is the fundamental package for scientific computing with Python. It contains various
features including these important ones:
• A powerful N-dimensional array object
• Sophisticated (broadcasting) functions
• Tools for integrating C/C++ and Fortran code
• Useful linear algebra, Fourier transform, and random number capabilities
Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional
container of generic data.
Arbitrary data-types can be defined using Numpy which allows NumPy to seamlessly and
speedily integrate with a wide variety of databases.
Installation:
• Mac and Linux users can install NumPy via pip command:
pip install numpy
• Windows does not have any package manager analogous to that in linux or mac.
Please download the pre-built windows installer for NumPy from here (according to
your system configuration and Python version).
And then install the packages manually.
Numpy Once
import numpy as np
np.zeros((2,2))
Output:
array([[0., 0.],
[0., 0.]])
Numpy
Numpy is the most basic and a powerful package for working with data in python.
If you are going to work on data analysis or machine learning projects, then having a
solid understanding of numpy is nearly mandatory.
Because other packages for data analysis (like pandas) is built on top of numpy and the
scikit-learn package which is used to build machine learning applications works heavily
with numpy as well.
At the core, numpy provides the excellent ndarray objects, short for n -dimensional
arrays.
In a ‘ndarray’ object, aka ‘array’, you can store multiple items of the same data type. It
is the facilities around the array object that makes numpy so convenient for performing
math and data manipulations.
You might wonder, ‘I can store numbers and other objects in a python list itself and do
all sorts of computations and manipulations through list comprehensions, for -loops etc.
What do I need a numpy array for?’
Well, there are very significant advantages of using numpy arrays overs lists.
import numpy as np
list1 = [0,1,2,3,4]
arr1d = np.array(list1)
print(type(arr1d))
arr1d
The key difference between an array and a list is, arrays are designed to handle
vectorized operations while a python list is not.
That means, if you apply a function it is performed on every item in the array, rather
than on the whole array object.
Let’s suppose you want to add the number 2 to every item in the list. The intuitive way
to do it is something like this:
list1 + 2 # error
That was not possible with a list. But you can do that on a ndarray.
# Add 2 to each element of arr1d
arr1d + 2
Another characteristic is that, once a numpy array is created, you cannot increase its
size. To do so, you will have to create a new array. But such a behavior of extending
the size is natural in a list.
So, that’s about 1d array. You can also pass a list of lists to create a matrix like a 2d
array.
arr2d = np.array(list2)
arr2d
You may also specify the datatype by setting the dtype argument. Some of the most
commonly used numpy dtypes are: 'float' , 'int' , 'bool' , 'str' and 'object' .
To control the memory allocations you may choose to use one of ‘float32’, ‘float64’,
‘int8’, ‘int16’ or ‘int32’.
arr2d_f
The decimal point after each number is indicative of the float datatype. You can also
convert it to a different datatype using the astype method.
arr2d_f.astype('int')
arr2d_f.astype('int').astype('str')
#> dtype='U21')
A numpy array must have all items to be of the same data type, unlike lists. This is
another significant difference.
However, if you are uncertain about what datatype your array will hold or if you want
to hold characters and numbers in the same array, you can set the dtype as 'object' .
arr2d_b
arr1d_obj
Finally, you can always convert an array back to a python list using tolist() .
arr1d_obj.tolist()
Let’s consider the array, arr2d. Since it was created from a list of lists, it has 2
dimensions that can be shown as rows and columns, like in a matrix.
Had I created one from a list of list of lists, it would have 3 dimensions, as in a cu be.
And so on.
Let’s suppose you were handed a numpy vector that you didn’t create yourself. What
are the things you would want to explore in order to know about that array?
# shape
# dtype
# size
# ndim
#> Size: 12
You can extract specific portions on an array using indexing starting with 0, something
similar to how you would do with python lists.
But unlike lists, numpy arrays can optionally accept as many parameters in the square
brackets as there is number of dimensions.
arr2[:2, :2]
A boolean index array is of the same shape as the array-to-be-filtered and it contains
only True and False values. The values corresponding to True positions are retained in
the output.
b = arr2 > 4
arr2[b]
arr2[::-1, ]
arr2[::-1, ::-1]
# Replace nan and inf with -1. Don't use arr2 == np.nan
arr2[missing_bool] = -1
arr2
However, if you want to compute the minimum values row wise or column wise, use
the np.amin version instead.
# Row wise and column wise min
Computing the minimum row-wise is fine. But what if you want to do some other
computation/function row-wise? It can be done using the np.apply_over_axis which
you will see in the upcoming topic.
# Cumulative Sum
np.cumsum(arr2)
#> array([ 1., 3., 6., 10., 13., 12., 11., 17., 22., 28., 35., 43.])
That means, if you make any changes to the new array, it will reflect in the parent array
as well.
So to avoid disturbing the parent array, you need to make a copy of it using copy(). All
numpy arrays come with the copy() method.
arr2a = arr2[:2,:2]
arr2
arr2
First, let’s reshape the arr2 array from 3×4 to 4×3 shape.
arr2.reshape(4, 3)
The difference between ravel and flatten is, the new array created using ravel is
actually a reference to the parent array. So, any changes to the new array will affect the
parent as well. But is memory efficient since it does not create a copy.
# Flatten it to a 1d array
arr2.flatten()
#> array([ 100., 2., 3., 4., 3., -1., -1., 6., 5., 6., 7., 8.])
b1 = arr2.flatten()
arr2
b2 = arr2.ravel()
arr2
print(np.arange(5))
# 0 to 9
print(np.arange(0, 10))
# 0 to 9 with step of 2
# 10 to 1, decreasing order
print(np.arange(10, 0, -1))
#> [0 1 2 3 4]
#> [0 1 2 3 4 5 6 7 8 9]
#> [0 2 4 6 8]
#> [10 9 8 7 6 5 4 3 2 1]
You can set the starting and end positions using np.arange. But if you are focussed on
the number of items in the array you will have to manually calculate the appropriate
step value.
Say, you want to create an array of exactly 10 numbers between 1 and 50, Can you
compute what would be the step value?
#> array([ 1, 6, 11, 17, 22, 28, 33, 39, 44, 50])
Notice since I explicitly forced the dtype to be int, the numbers are not equally spaced
because of the rounding.
Similar to np.linspace, there is also np.logspace which rises in a logarithmic scale. In
np.logspace, the given start value is actually base^start and ends with base^stop, with a
default based value of 10.
# Limit the number of digits after the decimal to 2
np.set_printoptions(precision=2)
The np.zeros and np.ones functions lets you create arrays of desired shape where all
the items are either 0’s or 1’s.
np.zeros([2,2])
np.ones([2,2])
a = [1,2,3]
#> Tile: [1 2 3 1 2 3]
#> Repeat: [1 1 2 2 3 3]
print(np.random.rand(2,2))
print(np.random.randn(2,2))
print(np.random.random())
print(np.random.choice(['a', 'e', 'i', 'o', 'u'], size=10, p=[0.3, .1, 0.1, 0.4, 0.1])) # picks more
o's
#> [[4 0]
#> [8 7]]
#> 0.08737272424956832
#> ['i' 'a' 'e' 'e' 'a' 'u' 'o' 'e' 'i' 'u']
#> ['o' 'a' 'e' 'a' 'a' 'o' 'o' 'o' 'a' 'o']
Now, everytime you run any of the above functions, you get a different set of random
numbers.
If you want to repeat the same set of random numbers every time, you need to set the
seed or the random state. The see can be any value. The only requirement is you must
set the seed to the same value every time you want to generate the same set of random
numbers.
rn = np.random.RandomState(100)
print(rn.rand(2,2))
np.random.seed(100)
print(np.random.rand(2,2))
np.random.seed(100)
print(arr_rand)
#> [8 8 3 7 7 0 4 2 5 2]
#> Counts : [1 2 1 1 1 2 2]