introduction to python - running notes

57
PYTHON INTRODUCTION [Type the abstract of the document here. The abstract is typically a short summary of the contents of the document. Type the abstract of the document here. The abstract is typically a short summary of the contents of the document.] Reference: sololearn.com

Upload: rajkumar-rampelli

Post on 12-Apr-2017

67 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Introduction to Python - Running Notes

Python introduction

[Type the abstract of the document here. The abstract is typically a short summary of the contents of the document. Type the abstract of the

document here. The abstract is typically a short summary of the contents of the document.]

Reference: sololearn.com

Page 2: Introduction to Python - Running Notes

PYTHON

Table of ContentsPython Introduction....................................................................................................................................6

Python versions.......................................................................................................................................6

Python console........................................................................................................................................6

Python functions.........................................................................................................................................6

Python errors...............................................................................................................................................6

Python floats...............................................................................................................................................7

Python numerical operations......................................................................................................................7

__main__.....................................................................................................................................................8

Command line arguments...........................................................................................................................8

sys module...............................................................................................................................................8

getopt module.........................................................................................................................................9

getopt.getopt method.........................................................................................................................9

Exception getopt.GetoptError.............................................................................................................9

Example..........................................................................................................................................10

Strings........................................................................................................................................................11

String formatting...................................................................................................................................11

String operations...................................................................................................................................12

Variables....................................................................................................................................................12

Variable names:.....................................................................................................................................12

del statement........................................................................................................................................13

Special operators.......................................................................................................................................13

if statements.............................................................................................................................................13

elif (short form of else if).......................................................................................................................13

Boolean logic.........................................................................................................................................14

While loops................................................................................................................................................14

infinite loop...........................................................................................................................................14

break......................................................................................................................................................14

continue................................................................................................................................................14

Page 3: Introduction to Python - Running Notes

operator precedence.................................................................................................................................14

More on else statements...........................................................................................................................15

Else with loops.......................................................................................................................................15

Else with try/except...............................................................................................................................15

Lists............................................................................................................................................................15

Empty list...............................................................................................................................................16

Nested lists............................................................................................................................................16

Errors.....................................................................................................................................................16

List operations.......................................................................................................................................16

Assignment........................................................................................................................................16

Addition and Multiplication...............................................................................................................16

in operator.........................................................................................................................................16

not operator......................................................................................................................................17

Loops.................................................................................................................................................17

List slices................................................................................................................................................17

Negative indexing..............................................................................................................................17

List comprehensions..............................................................................................................................18

List functions.........................................................................................................................................18

Sets............................................................................................................................................................19

Mathematical operations - union/intersection.....................................................................................19

Functions and modules..............................................................................................................................20

Arguments.............................................................................................................................................20

Return values.........................................................................................................................................21

Docstrings..............................................................................................................................................21

Modules.................................................................................................................................................21

Import certain functions....................................................................................................................21

Standard Library........................................................................................................................................21

Exception Handling....................................................................................................................................22

Raising exception...................................................................................................................................23

Assertions..................................................................................................................................................23

Files handling.............................................................................................................................................23

Opening files - open()............................................................................................................................23

Page 4: Introduction to Python - Running Notes

Reading Files - read().............................................................................................................................24

Writing Files - write().............................................................................................................................24

with statement......................................................................................................................................24

None..........................................................................................................................................................25

Dictionaries................................................................................................................................................25

Dictionary Functions..............................................................................................................................26

Search................................................................................................................................................26

get()...................................................................................................................................................26

Tuples........................................................................................................................................................26

Tuple unpacking....................................................................................................................................26

Variable*...............................................................................................................................................27

Ternary operator.......................................................................................................................................27

Higher order functions..............................................................................................................................27

Lambdas................................................................................................................................................27

map and filter........................................................................................................................................28

Generators................................................................................................................................................28

Decorators.................................................................................................................................................29

Object Oriented Programming..................................................................................................................30

Classes...................................................................................................................................................30

__init__..................................................................................................................................................30

Methods................................................................................................................................................30

Inheritance............................................................................................................................................30

Super function.......................................................................................................................................31

Magic Methods......................................................................................................................................31

Operator overloading............................................................................................................................31

Object Lifecycle.....................................................................................................................................31

Garbage collection.................................................................................................................................32

Data Hiding............................................................................................................................................32

Class methods........................................................................................................................................32

Static methods...................................................................................................................................32

Properties..............................................................................................................................................33

Regular Expressions...................................................................................................................................34

Page 5: Introduction to Python - Running Notes

Matching pattern...................................................................................................................................34

re.search().............................................................................................................................................34

re.sub() - search & replace.....................................................................................................................35

Metacharacters.....................................................................................................................................35

. (dot) character.................................................................................................................................35

^ and $ - start and end.......................................................................................................................35

Character classes...................................................................................................................................35

^ character.........................................................................................................................................36

More Metacharacters............................................................................................................................36

* character.........................................................................................................................................36

+ character.........................................................................................................................................36

? character.........................................................................................................................................36

Curly braces.......................................................................................................................................36

Groups...................................................................................................................................................36

| character.........................................................................................................................................37

Named groups...................................................................................................................................37

Special Sequences.....................................................................................................................................37

\number - Backslash..............................................................................................................................37

\d and \D sequence...............................................................................................................................38

\s and \S sequence................................................................................................................................38

\w and \W sequence.............................................................................................................................38

\A Sequence..........................................................................................................................................38

\b sequence...........................................................................................................................................38

Email extraction.........................................................................................................................................38

Pythonicness & Packaging.........................................................................................................................39

PEP- Python Enhancement Proposals........................................................................................................39

Packaging...................................................................................................................................................39

Setup.py.................................................................................................................................................40

Packaging for Users...............................................................................................................................40

Function arguments...................................................................................................................................40

*args......................................................................................................................................................40

**kwargs – keyword arguments............................................................................................................41

Page 6: Introduction to Python - Running Notes

Major 3rd party libraries.............................................................................................................................41

Django...................................................................................................................................................41

CherryPy and Flask................................................................................................................................41

BeautifulSoup........................................................................................................................................41

Matplotlib..............................................................................................................................................41

NumPy...................................................................................................................................................41

SciPy......................................................................................................................................................42

Panda3D................................................................................................................................................42

List Vs Dictionary Vs Tuples Vs Sets...........................................................................................................42

itertools - standard library.........................................................................................................................43

Python Vs C................................................................................................................................................43

Web scraping: webbrowser library............................................................................................................44

Webbrowser.open()..............................................................................................................................44

webbrowser.open_new(url)..................................................................................................................44

webbrowser.open_new_tab(url)...........................................................................................................44

webbrowser.get([name]).......................................................................................................................44

Page 7: Introduction to Python - Running Notes

Python !!!

Python IntroductionPython is a high level programming language like C/C++/Java etc., its applications including web programming, scripting, scientific computing and artificial intelligence.

python is processed at runtime by the interpreter. No need to compile python programs before executing them unlike C programs, where compilation step is mandatory before executing it.

An Interpreter is a program that runs scripts written in an interpreted language such as Python.

Python source files have an extension of .py

Python versionsThere are three major versions of python which are 1.x, 2.x and 3.x. Both 2.x and 3.x are currently used.

Python consoleThe python console is a program that allows you to enter one line of python code, repeatedly executes that line, and displays the output. This is known as a REPL - Read-Eval-Print-Loop.

To close a console, type in quit() or exit() and press enter.

Python functionsPython function Usageprint("Hello") or print('Hello') Prints the message, produces the outputquit() or exit() Close the consoleinput("Enter something please:") Prompts the use for input, and returns what they

enter as a string with contents automatically escaped.Enter something please: user enters\ncharacter!

o/p -> user enters\\ncharacter!int() Converts given number in string format to integer

int("2") + int("3") = 5Similarly float() and str()del variable_name del statement used to remove a variable from

program.

Python errorsType of error ExampleZeroDivisionError: division by zero 11 / 0

Page 8: Introduction to Python - Running Notes

TypeError: unsupported operand type(s) 1 + "2"For +:'int' and 'str'

TypeError: can't multiply sequence by non-int of type 'str' or 'float'

"s" * "s"9.0 * "r"

SyntaxError: invalid syntax >>>123abc = 71. When variable names don't follow rules.2. When program uses ++ or -- operators.

NameError: name 'var_name" is not defined When program tries to access undefined variable or a function which is not defined

In Lists:IndexError:

when try to access out of bound values.things="hai"print(things[4])

In Lists:TypeError:

when try to access integer or float types using index.Num = 123print(Num(2))

In Lists:ValueError:

If the item is not in the list and trying to get its index.

ImportError When trying to access unavailable moduleKeyError Trying to index a key that isn't part of the

dictionary returns a KeyError.Dictionary's TypeError When mutable objects are used as keysRuntimeError: maximum recursion depth exceeded

Often occur when forgot to implement the base case of resusion.

AttributeError Trying to access an attribute of an instance the isn't defined causes this error.

Python floatsA float can be added to an integer because Python silently converts the integer to a float. However, this implicit conversion is the exception rather than the rule in Python. You have to convert values manually if you want to operate on them.

Python numerical operationsOperator Usage+ , - , * , / Addition, subtraction, multiplication, division

1 + 2 = 31 + 2.2 = 3.22 * 3 = 62 * 3.2 = 6.44/2 = 2.05/2 = 2.5

** Exponentiation2**5 = 329**(1/2) = 3.0

Page 9: Introduction to Python - Running Notes

// Quotient (Floor division)20//6 = 3

% Remainder (modulo operator)1.25%0.5 = 0.25

Inplace operators+= , -=, *=, %= and /=

a += 3 -> a = a + 3

__main__When a python interpreter reads a source file, it executes all of the code it finds in the file. Before executing the code, it defines a few special variables.

For example, if the python interpreter is running that module (source file) as the main program, it sets the special __name__ variable to have avalue “__main__”. If the file being imported from another module, __name__ will be set to the module’s name.

If __name__ == “__main__”:print(“This is a script”)

elseprint(“Imported”)

Command line arguments

sys moduleThe Python sys module provides access to any command-line arguments via the sys.argv. This serves two purposes −

sys.argv is the list of command-line arguments.

len(sys.argv) is the number of command-line arguments.

Here sys.argv[0] is the program ie. script name.

Consider the following script test.py −

#!/usr/bin/python

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'

print 'Argument List:', str(sys.argv)

Page 10: Introduction to Python - Running Notes

Now run above script as follows −

$ python test.py arg1 arg2 arg3

This produce following result −

Number of arguments: 4 arguments.

Argument List: ['test.py', 'arg1', 'arg2', 'arg3']

getopt modulePython provides a getopt module that helps you parse command-line options and arguments.

Python provided a getopt module that helps you parse command-line options and arguments. This module provides two functions and an exception to enable command line argument parsing.

getopt.getopt methodThis method parses command line options and parameter list. Following is simple syntax for this method −

getopt.getopt(args, options, [long_options])

Here is the detail of the parameters −

args: This is the argument list to be parsed.

options: This is the string of option letters that the script wants to recognize, with options that require an argument should be followed by a colon (:).

long_options: This is optional parameter and if specified, must be a list of strings with the names of the long options, which should be supported. Long options, which require an argument should be followed by an equal sign ('='). To accept only long options, options should be an empty string.

This method returns value consisting of two elements: the first is a list of (option, value) pairs. The second is the list of program arguments left after the option list was stripped.

Each option-and-value pair returned has the option as its first element, prefixed with a hyphen for short options (e.g., '-x') or two hyphens for long options (e.g., '--long-option').

Exception getopt.GetoptErrorThis is raised when an unrecognized option is found in the argument list or when an option requiring an argument is given none.

The argument to the exception is a string indicating the cause of the error. The attributes msg and opt give the error message and related option.

Page 11: Introduction to Python - Running Notes

ExampleConsider we want to pass two file names through command line and we also want to give an option to check the usage of the script. Usage of the script is as follows −

usage: test.py -i <inputfile> -o <outputfile>

Here is the following script to test.py −

#!/usr/bin/python

import sys, getopt

def main(argv):

inputfile = ''

outputfile = ''

try:

opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])

except getopt.GetoptError:

print 'test.py -i <inputfile> -o <outputfile>'

sys.exit(2)

for opt, arg in opts:

if opt == '-h':

print 'test.py -i <inputfile> -o <outputfile>'

sys.exit()

elif opt in ("-i", "--ifile"):

inputfile = arg

elif opt in ("-o", "--ofile"):

outputfile = arg

print 'Input file is "', inputfile

print 'Output file is "', outputfile

if __name__ == "__main__":

main(sys.argv[1:])

Page 12: Introduction to Python - Running Notes

Now, run above script as follows −

$ test.py -h

usage: test.py -i <inputfile> -o <outputfile>

$ test.py -i BMP -o

usage: test.py -i <inputfile> -o <outputfile>

$ test.py -i inputfile

Input file is " inputfile

Output file is "

StringsA string is created by entering text between two single or double quotation marks.-> 'Hello World' or "Hello World"

Some characters can't be included in string directly, example of these characters are single quote and double quotes, tabs, arbitrary unicode characters.

Put a backslash before these characters if you add these special characters in your string.

"Ram\'s brother name is \"Raj\"" --> Ram's brother name is Raj

\n - represents new line.

String formattingProvides a more powerful way to embed non-strings within strings.

String formatting uses a string's format method to substitute a number of arguments in the string.

nums = [4, 5, 6]msg = "Numbers: {0} {1} {2}".format(nums[0], nums[2], nums[3])print(msg) -> Numbers: 4 5 6

String formatting can also be done with named argumets.

a = "{x}, {y}".format(x=5, y=12)print(a) -> 5, 12

Page 13: Introduction to Python - Running Notes

String operationsConcatenation "spam"+"eggs" = spameggs

"spam"+","+"eggs" = spam,eggs"2"+"2" = 221 + "2" = TypeError (Refer Python errors table)

Multiplication "spam"*3 = spamspamspam4 * "3" = 3333"s" * "r" = TypeError"s" * 7.0 = TypeError

Replace "hello me".replace("me", "world") -> hello worldStartswith "This is car".startswith("This") -> TrueEndswithUpper() "I am a boy".upper() -> I AM A BOYLower()Split() "spam, eggs, ham".split(",")

VariablesIn python, variables don't have specific types, so you can assign a string to a variable, and later assign an integer to the same variable.

>>> a = 100>>>print(a)100>>>a = "Raj">>>print(a)'Raj'

Variable names: The only characters that are allowed are letter, numbers and underscores. Also, they can't start with numbers.

Not following these rules results in SyntaxError error.

Python is a case sensitive programming language. Thus, Lastname and lastname are two different variable names in Python.

Trying to reference a variable that has not assigned in the program causes an error (NameError).>>>a = "raj">>>araj>>>bNameError: name 'b' is not defined.>>>del a

Page 14: Introduction to Python - Running Notes

del statementdel statement used to remove a variable from program.

Special operatorsPython doesn't support special operators ++ and --. But, it supports inplace operators (refer operation table above)

Interpreter throws syntax error if program uses ++ or -- operators.

Example: File "./prog.py", line 12 a++ ^SyntaxError: invalid syntax

if statementsuse if statements to run code when certain condition holds.

if expression:statements

Python uses indentation (white spaces at the beginning of a line) to delimit blocks of code. Other languages, such as, C, use curly braces to accomplish this. But, in Python indentation is mandatory; program won't work without it. As you can see, the statements in the if should be indented.

elif (short form of else if)the elif (short for else if) statement is a shortcut to use when chaining if and else statements.

if expression: statementelse: if expression: statement

can be modified as below

if expression: statementelif expression: statement

Page 15: Introduction to Python - Running Notes

Boolean logicBoolean logic is used to make more complicated conditions for it statements that rely on more than one condition.

Python's Boolean operators are and, or, and not.

While loopswhile condition: statement1 statement2

infinite loopwhile 1==1: print("in the loop")

breakTo end a while loop prematurely, the break statement can be used.

i = 0while 1==1: print(i) i = i +1 if i >= 5: printf("breaking") break

continueUnlike break, continue jumps back to the top of the loop, rather than stopping it.

operator precedence== has higher precedence than or.

Python's order of operations is the same as that of normal mathematics.

the following table lists all of Python's operators, from highest precedence to lowest.

Operator Description** Exponentiation~ + - Complement, unary plus/minus* / % // Multiplication, division. modulo and floor division+ - Addition/subtraction>> << Right and left bitwise shift& Bitwise AND

Page 16: Introduction to Python - Running Notes

^ | Bitwise exclusive OR and regular OR<= < > >= Comparisons= %= /= //= -= += *= **= Assignment operatorsis is not Identity operatorsin not in Membership operatornot or and Logical operators

More on else statements

Else with loopsUsing else with for and while loops, the code within it is called if the loop finished normally (when a break statement doesn’t cause an exit from the loop)

For I in range(10):if (i==999):

break

Else:print(“Unbroken”)

Else with try/exceptThe else statement can also be used with try/except statements. In this case, the code within it is only executed if no error occurs in the try statement.

Try:print(1)

except ZeroDivisionError:print(2)

else:print(3)

o/P:13

ListsLists are one type of object in Python. They are used to store an indexed list of items.

A list is created using square brackets with commas separating items. Item in the list can be accesses by using its index in square brackets. A comma can be placed after last item also, its valid only.

words = ["Hello", "world", "!"]print(words[0])

Page 17: Introduction to Python - Running Notes

print(words[1])print(words[2])

Empty listAn empty list is created with an empty pair of square brackets.

empty_list = []print(empty_list) --> o/p: []

Nested listsLists can also include different item types and also be nested within other lists.

num = 3things = ["string", 0, [1, 2, num], 4.56]print(things[1])print(things[2])print(things[2][2])

ErrorsIndexError: when try to access out of bound values. print(things[4])

TypeError: when try to access integer or float types using index.

No error will be seen for string type. Example, str = "Hello world", print(str[0]), o/P: H

List operations

AssignmentThe item at certain index in a list can be reassigned.

Addition and MultiplicationLists can be added and multiplies in the same way as strings

nums = [1, 2, 3]print(nums + [4, 5, 6]) -> [1,2,3,4,5,6]print(nums * 3) -> [1,2,3,1,2,3,1,2,3]

List and strings are similar in many ways - strings can be thought of as lists of characters that can't be changed.

in operatorTo check if an item is in list, the in operator can be used. It returns True if the item occurs one or more times in the list, and False if it doesn't.

Page 18: Introduction to Python - Running Notes

words = ["spam", "eggs", "spam", "sause"]print("spam" in words) -> Trueprint("tomo" in words) -> False

The in operator is also used to determine whether or not a string is substring of another string.

not operatorTo check if an item is not in a list.

print(not 4 in nums) -> Trueprint(4 not in nums) -> True

Loopswords = ["hello", "world", "spam"]for word in words: print(word)o/p: helloworldspam

for i in range(3): print("hi")o/p: hi hi hi

List slicesProvides more advanced way of retrieving values from a list.

if the first number in a slice is omitted, it is taken to be the start of the list. If the 2nd number is omitted, it is taken to be the end.

squares = [0, 1, 4, 9, 16, 25]print(squares[1:4]) -> [1, 4, 9]print(squares[0:1]) -> [0]print(squares[:3]) -> [0, 1, 4]print(squares[4:]) -> [16, 25]

List slices can also have a 3rd number, representing the step, to include only alternate values in the slice.

print(squares[2:5:2]) -> [4, 16]

Negative indexingNegative values can be used in list slicing (and normal list indexing).

When negative values are used for the first and 2nd values in a slice (or a normal index), they count from the end of the list.

Page 19: Introduction to Python - Running Notes

print(squares[1:-1]) -> [1, 4, 9, 16]

If Negative value is used for the step, the slice is done backwards. Using [ : : -1] as slice is a common and idiomatic way to reverse a list.

List comprehensionsList comprehensions are a useful way of quickly creating lists whose contents obey a simple rule.

cubes = [i**3 for i in range(5)]print(cubes) -> [0, 1, 8, 27, 64]

A list comprehension can also contain an if statement to enforce a condition on values in the list.

evens = [i**2 for i in range(10) if i**2 % 2 == 0]print(evens) --> [0, 4, 16, 36, 64]

Trying to create a list in a very extensive range will result in a MemoryError.

even = [2*i for i in range(10**1000)] -> MemoryError.

This issue is solved by generators.

List functionsFunction Descriptionlist.append(item): is a method of list class and hence can be accessed using dot before.

Adds an item to the end of an existing list.Nums = [1, 2, 3]Nums.append(4)Print(nums) -> [1, 2, 3, 4]

len() Get the number of items in a list.print(len(nums)) -> 4

list.insert(index, item) Allows to insert a new item at any position in the list.Nums.insert(1, 10)print(nums) -> [1, 10, 2, 3, 4]

list.index(item) Finds the first occurrence of a list item and returns this index.If the item isn't in the list, it raises a ValueError.print(nums.index(10)) -> 1print(nums.index(20)) -> ValueError: 20 is not in the list

max(list) Returns the list item with the maximum valuemin(list) Returns the list item with the minimum valuelist.count(item) Returns a count of how many times an item occurs

in a list.list.remove(item) Removes an item from a listlist.reverse() Reverses items in listlist(range(count)) : Creates a sequential list of Nums = list(range(5))

Page 20: Introduction to Python - Running Notes

numbers. range by itself creates a range object, and this must be converted to a list.

print(nums) -> [0, 1, 2, 3, 4]

list(range(first, end)): Produces a list from the first to the end

Nums = list(range(2, 7))print(nums) -> [2, 3, 4, 5, 6]

list(range(first, end, interval)) Nums = list(range(2, 7, 2))print(nums) -> [2, 4, 6]

all and any Take a list as argument and return True if all or any (respectively) of their arguments evaluate to True.Nums = [55, 44, 33]If all([i>5 for i in nums]): print("all are larger than 5")If any([i%2 == 0 for i in nums]): Print("atleast one is even")

Enumerate() Used to iterate through the values and indices of list simultaneously.For v in enumerate(nums): Print(v)o/p: (0, 55)(1, 44)(2, 33)

SetsSets are data structures, similar to lists or dictionaries.

They are created using curly braces, or the set function.

num_set = {1, 2, 3, 4}word_set = set(["spam", "eggs"])print(3 in num_set) -> Trueprint("spam" not in word_set) -> False

To create an empty set, you must use set(), as {} creates an empty dictionary.

Sets are unordered, which means that they can't be indexed.

They cannot contain duplicate elements. Due to the way they are stored, its faster to check whether an item is part of a set, rather than part of a list.

Instead of using append() to add to a set, use add()

The method remove() removes a specific element from a set, pop() removes an arbitrary element.

Mathematical operations - union/intersectionSets can be combined using mathematical operations.

Page 21: Introduction to Python - Running Notes

union operator |intersection operator &difference operator -symmetric difference operator ^

first = {1, 2, 3, 4}sec = {4, 5, 6}print(first|sec) -> {1, 2, 3, 4, 5, 6}print(first&sec) -> {4}print(first-sec) -> {1, 2, 3}print(first^sec) -> {1, 2, 3, 5, 6}

Functions and modulescreate a function by using the def statement and must be defined before they are called, else you would see NameError.

Functions can be assigned and reassigned to variables and later referenced by those values

def my_func(): print("I am in function")my_func()func2 = my_func()func2()

ArgumentsFunctions can also be used as arguments of other functions

def my_func(word): print(word + "!")my_func("Hello")

def sum(x, y): print(x+y)sum(5, 8)

def add(x, y):return x+y

def do_twice(func, x, y):return func(func(x, y), func(x, y))

a = 5b = 10print(do_twice(add, a, b)

Page 22: Introduction to Python - Running Notes

Return valuesdef max(x, y): if x >= y: return x else: return yz = max(8, 5)print(z)

DocstringsUnlike conventional comments, docstrings are retained throughout the runtime of the program. This allows the programmed to inspect these comments at run time.

ModulesModules are pieces of code that other people have written to fulfill common tasks, such as generating random numbers etc.

Trying to import a module that isn't available causes an ImportError.

Usage: add import module_name at the top of your code. And, access functions/variable using module_name.var

Example:import randomfor i in range(5):

val = random.randint(1, 6)print(val)

Import certain functionsfrom module_name import var

for math import piprint(pi)

for math import pi, sqrtprint(sqrt(25))

Import a module under a different name using as keyword

from math import sqrt as square_rootprint(square_root(100))

Standard Library3 types of modules

Page 23: Introduction to Python - Running Notes

1. those you write yourself2. those you install from external sources3. those are pre-installed with python --> Standard library

Tasks that can be done by using standard library includestring parsingdata serializationtesting | debuggingmanipulating dates, emailscommand line arguments and so on.

Python's extensive standard library is one of its main strengths as a language.

Exception HandlingExceptions occur when something goes wrong, due to incorrect code or input.

When an exception occurs, the program immediately stops.

Handle exceptions: use try and except statements. Useful when dealing with user input.

The try block contains code that might throw an exception. If that exception occurs, the code in the try block stops being executed, and the code in the except block is run. If no error occurs, the code in the except block doesn't run.

try:num1 = 7num2 = 0print(num1/num2)

except ZeroDivisionError:print("An error occurred due to zero division")

except (ValueError, TypeError):print("Error occurred")

An except statement without any exception specified will catch all errors.

try:print(7/0)

except:print("error occurred")

finally:print("This code will run no matters what")

The finally statement always runs after try/except blocks even in case of exception.

Page 24: Introduction to Python - Running Notes

Raising exceptionraise statement can be used to raise exception and specify the type of exception raised. You can pass argument for details.

print(1)raise ValueError print(2)raise NameError("Invalid name found")

AssertionsAn Assertion is a sanity-check that you can turn on or turn off when you have finished testing the program.

An expression is tested, and if the result comes up false, and exception is raised.

Programmers often place assertions at the start of a function to check for valid input, and after a function call to check for valid output.

print(1)assert 2 + 2 == 4print(2)assert 1 + 1 == 3print(3)

o/p:12AssertionError

The assert can take a seconds argument that is passed to the AssertionError raised if the assertion fails.

temp = -10assert (temp >= 0), "Colder than absolute Zero!"

o/p: AssertionError: Colder than absolute zero!

Files handling

Opening files - open()myfle = open("filename.txt")

The argument of the open() function is the path to the file. You can specify the mode used to open a file by 2nd argument.

Page 25: Introduction to Python - Running Notes

r : Open file in read mode, which is the default.w : Write mode, for re-writing the contents of the filea : Append mode, for adding new content to the end of the fileb : Open file in a binary mode, used for non-text files (image/sound files)

myfile = open("filename.txt", "a");

myfile.close()

Reading Files - read()cont = myfile.read()print(cont) -> will print all the contents of the file.

Provide number of bytes that read from file by passing value to the read()

print(myfile.read(10)) -> 10 bytes read

After all contents in a file have been read, any attempt to read further from that file will return an empty string, because you are trying to read from the end of the file.

To retrieve each line in a file, use readlines function. It return a list in which each element is a line in the file.

file = open("filename.txt", "r")print(file.readline())file.close()

o/p:['line1\n', 'line2\n', 'line3']

Writing Files - write()write() - writes a string to the file. "w" mode will create a file if not exist. If exist, the contents of the file will be deleted.

write() returns the number of bytes written to the file, if successful.

file = open("new.txt", "w")file.write("Writting to the file")file.close()file = open("new.txt", "r")print(file.read())file.close()

with statementIt is mandatory to close the file after read/writing the file even in case of exception occur.

Page 26: Introduction to Python - Running Notes

An alternative way of doing this is using with statement. The file is automatically closed at the end of the with statement, even if exception occur within it.

with open("new.txt") as f:print(f.read())

Nonerepresents absence of the value.

The None object is returned by any function that doesn't explicitly return anything else.

def myfunc():printf("Hi")

var = myfunc()print(var)o/p:HiNone

DictionariesDictionaries are data structures used to map arbitrary keys to values. {"Key": Value}.

Empty dictionary is {}

Each element in a dictionary is represented by key:value pair

Dictionary keys can be assigned to different values like lists.

Trying to index a key that isn't part of the dictionary returns a KeyError.

primary = { "red": [255, 0, 0], "green": [0, 255, 0], "blue": [0, 0, 255],}print(primary["red"]) --> [255, 0, 0]primary["white"] = [200, 140, 100]primary["blue"] = [0, 0, 245]print(primary["yellow"]) --> KeyError: "yellow"

Dictionary can store any type of data as Value.

Only immutable objects can be used as keys to dictionaries.

Page 27: Introduction to Python - Running Notes

List and dictionaries are mutable objects and hence can't be used as keys in the dictionary.

bad_dict = {[1, 2, 3]: "one two three",}TypeError: unhashable type: list

Dictionary Functions

SearchUse in and not in

nums = {1: "one", 2: "two", 3: "three",}print(1 in nums) -> Trueprint(1 not in nums) -> Falseprint(4 not in nums) -> True

get()Used to get the values by its index, but if the key is not found, it returns another specific value(2nd argument) instead (None by default).

pairs = {1: "apple", "orange": [2, 3], True: False, None: "True",}print(pairs.get("orange")) -> [2, 3]print(pairs.get(7)) -> Noneprint(pairs.get(1234, "Not found")) -> Not found

TuplesSimilar to lists, except that they are immutable (they can't be changed). Also, these are created using parenthesis, rather than square brackets.

words = () -> empty tuple

words = ("spam", "eggs")orwords = "spam", "eggs"

Tuple unpackingIt allows you to assign each item in an iterable to a variable.

Numbers = (1, 2 ,3)a, b, c = numbersprint(a) -> 1print(b) -> 2

Page 28: Introduction to Python - Running Notes

Variable*A variable that is prefaced with an asterisk(*) takes all values from the iterable that are left over from the other variables

A, b, *c, d = [1, 2, 3, 4 ,5 ,6, 7]print(a) -> 1print(b) -> 2print(c) ->[3, 4, 5, 6]print(d) -> 7

Ternary operatorIt takes three arguments

Status = 1msg = “Logout” if status ==1 else “Login”print(msg) -> Logout

Higher order functions

LambdasWhen functions are created using lambda syntax then the functions are not assigned to any variable unlike functions created normally using def assigns it to a variable automatically.

Strings and integers can be created on the fly, without assigning them to a variable. Similarly, functions can be created without assigning them to any variable using lambda keyword.

def my_func(f, arg):return f(arg)

my_func(lambda x: 2*x*x, 5)

Lambda functions get their name from lambda calculus.

Lambda functions are not powerful as named functions. They can only do things that require a single expression - usually equivalent to a single line of code.

#named functiondef polynomial(x):

return x**2 + 5*x + 4print(polynomial(-4))

Page 29: Introduction to Python - Running Notes

#lambdaprint(lambda x: x**2 + 5*x + 4, -4)

Lambda functions can be assigned to variables, and used like a normal functions

double = lambda x: x * 2print(double(4))

map and filtermap and filter operate on lists (or similar objects called itterables)

Maps Lambdadef add_five(x):

return x+5nums = [1, 2, 3, 4, 5]result = list(map(add_five, nums))print(result) -> [6, 7, 8, 9, 10]

Nums = [1, 2, 3, 4, 5]result = list(map(lambda x: x+5, nums))print(result) -> [6, 7, 8, 9, 10]

The function filter filters an iterable by removing items that don't match a predicate

res = list(filter(lambda x: x%2 ==0, nums))print(res) -> [2, 4]

GeneratorsGenerators are a type of iterable, like lists or tuples.

Unlike lists, they don't allow indexing with arbitrary indices, but they can still be iterated through with for loops.

Generators can be created using functions and the yield statement.

def countdown():i=5while i>0:

yield ii- = 1

for i in countdown():print(i)

o/p: 5 4 3 2 1

Yield statement is used to define a generator, replacing the return of a function to provide a result to its called without destroying local variables.

Page 30: Introduction to Python - Running Notes

Due to the fact that they yield one item at a time, generators don't have the memory restrictions of lists. In fact, they can be infinite.

In short, generators allows you to declare a function that behaves like an iterator i.e. it can be used in a for loop.

def infinite_sevens():while True:

yield 7for i in infinite_sevens():

print(i)

Finite generators can be converted into lists by passing them as arguments to the list function. Using generators results in improved performance, which is the results of the lazy generation of values, which translates to lower memory usage. Furthermore, we do not need to wait until all the elements have been generated before we start to use them.

def numbers(x):for i in range(x):

if i%2 == 0:yield i

print(list(numbers(11))) -> o/P: [0, 2, 4, 6, 8, 10]

DecoratorsProvides a way to modify functions using other functions.

This is ideal when you need to extend the functionality of functions that you don't want to modify.

def decor(func):def wrap():

print("=====")func()print("=====")

return wrapdef print_text():

print("Hello")decorated = decor(print_text)decorated()

def print_text():print("hello")

print_text = decor(print_text)

@decordef print_text(): print("Hello")

Page 31: Introduction to Python - Running Notes

Object Oriented Programming

Classes3 paradigms of programming1. Imperative - using statements, loops, and functions as subroutines2. Functional - using pure functions, higher order functions, and recursion3. Object Oriented programming (OOP)

Objects are created using classes, which are actually the focal point of OOP.

A Class can be described as an object's blueprint, description, or definition. Same class can be used for creating multiple different objects.

class Cat:def __init__(self, color, legs):

self.color = colorself.legs = legs

def bark(self):print("woof")

felix = Cat("ginger", 4)rover = Cat("dog-colored", 4)stumpy = Cat("brown", 3)

__init__Called when an instance (object) of the class is created, using the class name as function.

All methods must have self as their first parameter, although it isn't explicitly passed, python adds the self argument to the list for you; you do not need to include it when you call the methods.

The __init__ method is called the class constructor.

MethodsDefined to add functionality to them.

InheritanceInheritance provides a way to share functionality b/w calsses.

class Animal:def __init__(self, name, color):

self.name = nameself.color = color

class Cat(Animal):def purr(self):

print("Purr..")

Page 32: Introduction to Python - Running Notes

class Dog(Animal):def bark(self):

print("woof")super().purr()

class Red_dog(Dog):def sec_method(self):

print("second method")

A class that inherits from another class is called a subclass.

A class that is inherited from is called a superclass.

If a class inherits from another with the same attributes or methods, it overrides them.

Super functionThe function super is a useful inheritance related function that refers to the parent class.

Magic MethodsThese are special methods which a double underscores at the beginning and end of their names. They are also known as dunders.

One common use of them is operator overloading.

Operator overloading__add__ for + __truediv__ for / __floordiv__ for // __mod__ for %__sub__ for - __and__ for & __pow__ for ** __xor__ for ^ __or__ for |

class Vector2D:def __init__(self, x, y):

self.x = xself.y = y

def __add__(self, other):return Vector2D(self.x + other.x, self.y + other.y)

first = Vector2D(5, 7)sec = Vector2D(12, 9)result = first + sec

__lt__ for < __le__ for <= __eq__ for == __ne__ for != __gt__ for > __ge__ for >=

Object LifecycleMade up of creation, manipulation and destruction.

Page 33: Introduction to Python - Running Notes

Garbage collectionThe process of deleting objects when they are no longer needed is called garbage collection.

When an object's reference count reaches Zero, python automatically deletes it.

Data HidingA key part of OOP is encapsulation, which involves packaging of related variables and functions into single easy-to-use object - an instance of a class.

A related concept is data hiding, which states that implementation details of a class should be hidden, and a clean standard interface be presented for those who want to use the class.

This will be done using private methods attributes in other programming language.

In python, there are no ways of enforcing a method or attribute be strictly private.

Weekly private methods and attributes have a single underscore at the beginning.

Strongly private methods and attributes have a double underscore at the beginning.

Class methodsClass methods are called by a class, which is passed to the cls parameter of the method.

class Rectangle:def __init__(self, width, height):

self.width = widthself.height = height

def calculate_area(self):return self.width * self.height

@classmethoddef new_square(cls, side_length):

return cls(side_length, side_length)square = Rectangle.new_square(5)print(sqaure.calculate_area())

Note: Technically, the parameters self and cls are just conventions; they could be changed to anything else.

Static methodsThese are similar to class methods, except they don't receive any additional arguments; they are identical to normal functions that belongs to a class.

They are marked with the staticmethod

class Pizza:def __init__(self, toppings):

Page 34: Introduction to Python - Running Notes

self.toppings = toppings@staticmethoddef validate_topping(topping):

if topping == "pineapple":raise ValueError("No Pineaaple")

else:return True

ingredients = ["cheese", "onions", "spam"]if all(Pizza.validate_topping(i) for i in ingredients):

pizza = Pizza(ingredients)

Static methods behave like plain functions, except for the fact that you can call them from an instance of the class.

PropertiesProperties provide a way of customizing access to instance attributes.

They are created by putting the property decorator above a method, which means when the instance attribute with the same name as the method is accessed, the method will be called instead.

One common use of a property is to make an attribute read-only.

class Pizza:def __init__(self, toppings):

self.toppings = toppings@propertydef pineapple_allowed(self):

return False@pineapple_allowed.setterdef pineapple_allowed(sefl, value):

if value:pwd = input("Enter password")if pwd == "admin"

self._pineapple_allowed = valueelse

raise ValueError("Alert..Intruder")pizza = Pizza(["cheese", "tomoto"])print(pizza.pineapple_allowed)pizza.pineapple_allowed = True

o/p: FalseAttributeError: can't set attribute --> If setter is not defined

Page 35: Introduction to Python - Running Notes

Enter password: adminTrue

Properties can also be set by defining setter/getter functions.

The setter function sets the corresponding propery's value.

The getter gets the value.

Regular ExpressionsRegular Expressions are powerful tool for various kinds of string manipulation.

Matching patternStandard library is re.

Need to import it and call the below function.

Import rere.match(“spam”, “eggsspam”) -> Not foundre.search(“spam”, “eggsspam”) -> Foundre.findall(“spam”, “eggsspam”) -> [‘spam’]

Search() Finds a match of a pattern anywhere in the stringMatch() Looks at the beginning of the string onlyFindall() Returns a list of all substrings that match a patternFinditer() Does the same thing as findall() except it returns

an iterator, rather than a list

re.search()The search returns an object with several methods that give details about it. These methods include group() which returns string matched, start() and end() which return the start and ending positions of the match, and span() which returns the start and end positions as a tuple.

Import repattern = r”pam”match = re.search(pattern, “eggspamsausage”)if match:

print(match.group())print(match.start())print(match.end())print(match.span())

Page 36: Introduction to Python - Running Notes

o/p:pam47(4, 7)

re.sub() - search & replacesub() method replaces all occurrences of the pattern in string with replace string, substituting all occurrences, unless max provided.

re.sub(pattern, repl, string, max=0)

import restr = “My name is David. Hi David”pattern = r”David”newstr = re.sub(pattern, “Raj”, str)print(newstr) -> My name is Raj. Hi Raj.

MetacharactersAllows us to create regular expression to represent concepts like “one or more” repetitions of a vowel.

. (dot) characterThis matches any character, other than a new line.

Import repattern = r”gr.y”if re.match(pattern, “grey”):

print(“Match1”)

^ and $ - start and endThis matches the start and end of a string.

Patter = r”^gr.y$”re.match(pattern, “grey”) -> Truere.match(pattern, “spamgrey”) -> False

Character classesProvide a way to match only one of a specific set of characters.

A character class is created by putting the characters it matches inside square brackets.

Pattern = r”[aeiou]re.search(pattern, “grey”) - > Truere.search(pattern, “myths”) - > False

Page 37: Introduction to Python - Running Notes

Supports range of characters also.

[a-z] -> matches any lowercase alphabetic character[G-P] -> matches any uppercase character from G to P[0-9][A-Za-z]

^ characterPlace a ^ at the start of a character class to invert it.

Pattern = r”[^A-Z]”re.search(“Hi Raj”) -> Truere.search(“HI RAJ”) -> False

More Metacharacters

* characterZero or more repetitions

+ characterOne or more repetitions

? characterZero or one repetitions

Curly bracesUsed to represent the number of repetitions b/w two numbers.

Pattern = r”9{1, 3}$”Re.match(pattern, “9”) -> Truere.match(pattern, “999”) -> Truere.match(pattern, “9999”) -> False

GroupsA group can be created by surrounding part of a regular expression with parenthesis.

This means that a group can be given as an argument to metacharacters such as * and ?

Pattern = r”egg(spam)*”re.match(pattern, egg) -> Truere.match(pattern, spam) -> False

The contents of groups in a match can be accessed using the group function.

A call of group(0) or group() returns the whole match.

A call of group(n), where n is the greater than 0, returns the nth group from the left.

Page 38: Introduction to Python - Running Notes

Pattern = r”a(bc)(de)(f(g)h)I”match = re.match(pattern, “abcdefghijklmnop”)if match:

Print(match.group()) -> abcdefghiprint(match.group(0)) -> abcdefghiprint(match.group(1)) -> bcprint(match.group(2)) -> deprint(match.groups()) -> (‘bc’, ‘de’, ‘fgh’, ‘g’)

| characterMeans or.

Pattern = r”gr(a|e)y”re.match(pattern, “grey”) -> True

Named groupsNamed groups have format (?P<name>…), where name is the name of the group, … is the content.

They behave exactly same as the normal groups except they can be accessed by group(name) in addition to its number.

Pattern = r”(?P<first>abc)(?:def)(ghi)”match = re.match(pattern, “abcdefghi”)if match:

Print(match.group(“first”)) -> abcprint(match.groups()) -> (‘abc’, ‘ghi’)

Non-capturing groups have the format (?:…). They are not accessible by the group methods, so they can be added to an existing regular expression without breaking the numbering. See above.

Special Sequences

\number - BackslashThis matches the expression of the group of that number. \1 to \99 allowed.

Pattern = r”(.+) \1”re.match(pattern, “word word”) -> Truere.match(pattern, “abc cde”) -> False

Note: “(.+) \1” is not the same as “(.+) (.+)”. Because, \1 refers to the first group’s subexpression, which is the matched expression itself, and not the regular expression pattern.

Page 39: Introduction to Python - Running Notes

\d and \D sequence\d – matches the digits.\D – matches anything that isn’t a digit

Pattern = r”(\D+\d)”re.match(pattern, “Hi 999”) -> True

\s and \S sequence\s – matches the white spaces.\S – matches anything that isn’t a white space

Pattern = r”(\S+\s)”re.match(pattern, “Hi ”) -> True

\w and \W sequence\d – matches the word characters.\D – matches anything that isn’t a word character

\A SequenceThe sequences \A and \Z match the beginning and end of a string, respectively.

\b sequenceIt matches the empty string b/w \w and \W characters, or \w characters and the beginning or end of the string.

Informally, it represents the boundary b/w words.

Pattern = r”\b(cat)\b”re.search(pattern, “The cat sat!” -> Truere.search(pattern, “we scattered”) -> False

“\b(cat)\b” basically matches the word “cat” surrounded by word boundaries.

Email extractionTo extract email like [email protected] from text, use below pattern

Pattern = r”([\w\.-]+)@([\w\.-])(\.[\w\.]+)”

[\w\.-] -> matches one or more word character, dot or dash

Str = “Please contact [email protected] for assistance”match = re.search(pattern, str)if match:

Page 40: Introduction to Python - Running Notes

print(match.group())o/p: [email protected]

Pythonicness & PackagingImport this module prints the Zen of Python when imported.

There are 20 principles in the Zen of Python, but only 19 lines of test. The 20 th principle is a matter of opinion, but our interpretation is that the blank line means “Use white spaces”

PEP- Python Enhancement ProposalsPEP are suggestions for improvements to the language, made by experienced Python developers.

PEP8 is a style guide on the subject of writing readable code.

- Modules should have short, all lowercase names- Class names should be in CapWords style- Most variables and function names should be lowercase with underscores- Constants should be CAPS WITH UNDERSCORES- Names that would clash with pythin keyword should have a trailing underscore.- Lines shouldn’t be longer than 80 characters- “from module import *” should be avoided- There should be only be one statement per line

PackagingRefers to putting modules you have written in a standard format, so that other programmers can install and use them with ease. This involves use of the modules setuptools and disutils.

Place all the files you want to put in a library in the same parent directory. This directory should also contain a file called __init__.py, which can be blank but must be present in the directory.

This directory goes into the other directory which will have readme and license, as well as an important file called setup.py

Sololearn/License.txtReadme.txtSetup.pySololearn/

__init__.pysololearn.pysololearn2.py

Page 41: Introduction to Python - Running Notes

Setup.pyThis contains information necessary to assemble the package so it can be uploaded to PyPI and istalled with pip

From disutils.core import setup

Setup(name=”Sololearn”,version=’0.1dev’,packages=[‘sololearn’,],license=’MIT’,

long_description=open(‘README.txt’).read(),)

To build a source distributionpython setup.py sdist

To build a binary distributionpython setup.py bdist

To upload packagepython setup.py registerpythin setup.py sdist upload

Install a package with python setup.py install

Packaging for UsersPy2exe tool can be used to package a python script, along with libraries it requires, into a single executable.PyInstaller and cx_Freeze serve the same purpose.

Function argumentsPython allows to have function with varying number of arguments.

*argsUsing *args as a function parameter enables you to pass an arbitrary number of arguments to that function. The arguments are then accessible as the tuple args in the body of the function.

Def function(named_arg, *arg):print(named_arg)

print(args)

Function(1, 2, 3, 4, 5)

Page 42: Introduction to Python - Running Notes

o/p:1(2, 3, 4, 5)

The parameter *args must come after the names parameters to a function. The name args is just a convention; you can choose to use another.

**kwargs – keyword argumentsThis allows you to handle named arguments that you have not defined in advance.

The keyword arguments returns a dictionary, in which the keys are the argument names, and the values are the argument values.

Def my_func(x, y=7, *args, **kwargs):print(kwargs)

My_func(2, 3, 4, 5, 6, a=7, b=8)

o/P:{‘a’: 7, ‘b’: 8}

The arguments returned by the **kwargs are not included in *args.

Major 3rd party libraries

DjangoThe most frequently used web framework written in Python, it powers websites that include Instagram and Dicqus.

CherryPy and FlaskAre also popular web frameworks

BeautifulSoupIt is very useful, and leads to better results for scrapping data from websites.

MatplotlibAllows you to create a graph based on data in python

NumPyIt allows for the use of multidimentional arrays that are much faster than native python solution of nested lists.

Page 43: Introduction to Python - Running Notes

SciPyContains numerous extensions to the functionality of NumPy

Panda3DFor 3D games

Pygame for 2D games

List Vs Dictionary Vs Tuples Vs SetsLists Sets Dictionaries TuplesList = [10, 12, 15] Set = {1, 23, 34}

Print(set) -> {1, 23,24}Set = {1, 1}print(set) -> {1}

Dict = {"Ram": 26, "mary": 24}

Words = ("spam", "egss")OrWords = "spam", "eggs"

Access: print(list[0]) Print(set).Set elements can't be indexed.

print(dict["ram"]) Print(words[0])

List[0] = 100 Set elements can't be indexed. So, use add()Set.add(7)

Dict["Ram"] = 27 Words[0] = "care" -> TypeError

Mutable Mutable Mutable Immutable - Values can't be changed once assigned

List = [] Set = set() Dict = {} Words = ()Faster than lists but they can't be changed.

Slicing can be doneprint(list[1:2]) -> [12]

Not done. Slicing can also be done on tuples

Can't contain duplicate elements.

Use lists if you have a collection of data that doesn't need random access.Use lists when you need a simple, iterable collection that is modified frequently.

- Membership testing and the elimination of duplicate entries.- when you need uniqueness for the elements.

- When you need a logical association b/w key:value pair.- when you need fast lookup for your data, based on a custom key.- when your data is being constantly modified.

Use tuples when your data cannot change.A tuple is used in comibnation with a dictionary, for example, a tuple might represent a key, because its immutable.

Page 44: Introduction to Python - Running Notes

itertools - standard libraryThe module itertools is a standard library. It produces infinite iterators.

count(): counts up infinitely from a valuecycle(): infinitely iterates through an iterable (a list or a string)repeat(): repeats an objects, either infinitely or a specified number of times.

from itertools import countfor i in count(3):

print(i)if i >= 11:

break

Python Vs CC language Python languageSpecial operators (++ and --) worksI.e. a++; --a;

It doesn't support ++ and --. Throws Syntax error.

Each statement in C ends with semicolon ; No use of ; here.Curly braces are used to delimit blocks of codeIf (condition){Statement1;Statement2;}

It uses white spaces for this purposeIf condition: Statement1 Statement2Example:If 10 > 5: print("10 is greater than 5") if 10 > 9: print("10 is greater than 9")else: print("5 is greater than 10")

Compiling the program before execution is mandatory

Python program directly executed by using interpreter. No compiler here.

Boolean operators are && and || and ! Boolean operators are and, or and notUses // for one line commentUses /* */ for multiple line comment

Uses # for one line commentUses """ """ for multi line comments (Docstrings).

Uses #include to import standard library functions#include<stdio.h>

Uses import keyword to include standard library functionsImport math

Void assert(int expression)Expression -This can be a variable or any C expression. If expression evaluates to TRUE, assert() does nothing. If expression evaluates to FALSE, assert() displays an error message on stderr(standard error stream to display error

assert expressionExampleassert 1 + 1 == 3

Page 45: Introduction to Python - Running Notes

messages and diagnostics) and aborts program execution.NULL None - represents absence of valueDon't have automatic memory management. Automatic Garbage collection.

Web scraping: webbrowser libraryhttps://automatetheboringstuff.com/chapter11/

Web scraping is the term for using a program to download and process content from the Web. For example, Google runs many web scraping programs to index web pages for its search engine.

webbrowser. Comes with Python and opens a browser to a specific page.

Webbrowser.open()webbrowser.open(url, new=0, autoraise=True)

Display url using the default browser. If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible. If autoraise is True, the window is raised if possible (note that under many window managers this will occur regardless of the setting of this variable).

Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program. However, this is neither supported nor portable.

webbrowser.open_new(url)Open url in a new window of the default browser, if possible, otherwise, open url in the only browser window.

webbrowser.open_new_tab(url)Open url in a new page (“tab”) of the default browser, if possible, otherwise equivalent to open_new().

webbrowser.get([name])Return a controller object for the browser type name. If name is empty, return a controller for a default browser appropriate to the caller’s environment.

import webbrowserurl = 'http://idiotinside.com'# Open URL in new browser windowwebbrowser.open_new(url) # opens in default browser# Opens in safari browserwebbrowser.get('safari').open_new(url)# Open URL in a new tab

Page 46: Introduction to Python - Running Notes

webbrowser.open_new_tab(url) # opens in default browser# Opens in safari browserwebbrowser.get('safari').open_new_tab(url)

Type Name Class Name Notes

'mozilla' Mozilla('mozilla')

'firefox' Mozilla('mozilla')

'netscape' Mozilla('netscape')

'galeon' Galeon('galeon')

'epiphany' Galeon('epiphany')

'skipstone' BackgroundBrowser('skipstone')

'kfmclient' Konqueror() (1)

'konqueror' Konqueror() (1)

'kfm' Konqueror() (1)

'mosaic' BackgroundBrowser('mosaic')

'opera' Opera()

'grail' Grail()

'links' GenericBrowser('links')

'elinks' Elinks('elinks')

'lynx' GenericBrowser('lynx')

'w3m' GenericBrowser('w3m')

'windows-default' WindowsDefault (2)

'macosx' MacOSX('default') (3)

Page 47: Introduction to Python - Running Notes

Type Name Class Name Notes

'safari' MacOSX('safari') (3)

'google-chrome' Chrome('google-chrome') (4)

'chrome' Chrome('chrome') (4)

'chromium' Chromium('chromium') (4)

'chromium-browser' Chromium('chromium-browser') (4)