spsl iv unit final
TRANSCRIPT
![Page 1: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/1.jpg)
Shell Programming & Scripting Languages
Dr. K. Sasidhar
![Page 2: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/2.jpg)
UNIT – IV Contents Introduction to python language python-syntax statements functions, Built-in-functions and Methods Modules in python Exception Handling
![Page 3: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/3.jpg)
UNIT IV Outcomes From the IV unit Student can
Understand python programming features and its statements Write and execute programs with basic concepts, functions,
Modules and Exception Handling. Analyze the problem statements and can write solution in the
form of a python program. Arrives to a conclusion about programming languages
principles by comparing python with other Programming languages already he/she learnt in the previous semesters.
![Page 4: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/4.jpg)
Python
Python was developed by Guido van Rossum in the late
eighties at the National Research Institute for Mathematics
and Computer Science in Netherlands.
Python is derived from many other Programming
languages including, C, C++, Algol-68, Smalltalk, Unix
shell and other scripting languages
![Page 5: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/5.jpg)
Why Python? Python is object-oriented
Structure supports Encapsulation, polymorphism, Abstraction, multiple inheritance
It's open source, so downloading and installing Python is free and easy
Source code is easily accessible Online Python community is huge to support the Python
programmers It's portable Python runs on every major platform
![Page 6: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/6.jpg)
Python programs will run in the same manner, irrespective of platform
It's powerful Dynamic typing Built-in types and tools Library utilities Third party utilities (numeric, NumPy, SciPy) Automatic memory management Can design data structures, web applications, Database
programs, games etc…
Why Python?
![Page 7: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/7.jpg)
Python's library is very portable and cross-platform compatible on UNIX, Windows, and Macintosh.
Support for an interactive mode which allows interactive testing and debugging the code.
Python can be linked to components written in other languages easily
Linking to fast, compiled code is useful to computationally intensive problems
Python is good for code steering and for merging multiple programs
Why Python?
![Page 8: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/8.jpg)
WARP is implemented in a mixture of Python and Fortran Python programs are compiled automatically to an
intermediate form called bytecode, which the interpreter then reads
This gives Python the development speed of an interpreter without the performance loss inherent in purely interpreted languages
It's easy to learn also. Structure and syntax are pretty intuitive and easy to grasp
Why Python?
![Page 9: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/9.jpg)
Running Python
Unix: IDLE is the very first Unix IDE for Python. Windows: PythonWin is the first Windows interface for
Python and is an IDE with a GUI. Macintosh: The Macintosh version of Python along
with the IDLE IDE is available from the
main website, downloadable as either
MacBinary or BinHex'd files.
![Page 10: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/10.jpg)
10
source code: The sequence of instructions in a program. syntax: The set of legal structures and commands that can be used
in a particular programming language. output: The messages printed to the user by a program. console: The text box onto which output is printed. Some source code editors pop up the console as an external
window, and others contain their own console window.
Programming basics
![Page 11: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/11.jpg)
11
Compiling and interpreting
Many languages require you to compile (translate) program into a form that the machine understands.
Python is instead directly interpreted into machine instructions.
compile execute
outputsource codeHello.java
byte codeHello.class
interpret
outputsource codeHello.py
![Page 12: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/12.jpg)
Reserved Words
And exec Not Assert finally or Break for pass Class from
print Continue global raise def if return del import try
elif in while else is with except lambda yield
![Page 13: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/13.jpg)
13
Expressions expression: A data value or set of operations to compute a value.
Examples: 1 + 4 * 3 Arithmetic operators we will use: + - * / addition, subtraction/negation, multiplication, division % modulus, a.k.a. remainder ** exponentiation precedence: Order in which operations are computed. * / % ** have a higher precedence than + -
1 + 3 * 4 is 13
Parentheses can be used to force a certain order of evaluation.
(1 + 3) * 4 is 16
![Page 14: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/14.jpg)
14
Real numbers Python can also manipulate real numbers.
Examples: 6.022 -15.9997 42.0 2.143e17 The operators + - * / % ** ( ) all work for real numbers.
The / produces an exact answer: 15.0 / 2.0 is 7.5 The same rules of precedence also apply to real
numbers:Evaluate ( ) before * / % before + -
When integers and reals are mixed, the result is a real number. Example: 1 / 2.0 is 0.5
![Page 15: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/15.jpg)
15
Math commands (import math) Python has useful commands for performing calculations.
To use many of these commands, you must write the following at the top of your Python program:
from math import *
Command name Descriptionabs(value) absolute valueceil(value) rounds upcos(value) cosine, in radiansfloor(value) rounds downlog(value) logarithm, base elog10(value) logarithm, base 10max(value1, value2)
larger of two values
min(value1, value2)
smaller of two values
round(value) nearest whole numbersin(value) sine, in radianssqrt(value) square root
Constant
Description
e 2.7182818...
pi 3.1415926...
![Page 16: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/16.jpg)
16
Variables variable: A named piece of memory that can store
a value. Usage:
Compute an expression's result, store that result into a variable, and use that variable later in the program.
assignment statement: Stores a value into a variable.
Syntax: name = value Examples: x = 5 gpa = 3.14 x 5 gpa 3.14 A variable that has been given a value can be used in expressions. x + 4 is 9
![Page 17: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/17.jpg)
17
print : Produces text output on the console. Syntax: print "Message"
print Expression Prints the given text message or expression value on the
console, and moves the cursor down to the next line. print Item1, Item2, ..., ItemN Prints several messages and/or expressions on the same line. Example: print "Hello, world!" age = 45 print "You have", 65 - age, "years until retirement" Output: Hello, world! You have 20 years until retirement
![Page 18: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/18.jpg)
18
Reads a number from user input. You can assign (store) the result of input into a variable. Example Program:
import sys
age = int(input("How old are you? "))
print "Your age is", age
print (“No. of years for retirement”, 65-age) Output: How old are you? 53 Your age is 53 No. of years for retirement: 12
input statement
![Page 19: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/19.jpg)
Programming Basics Programs are composed of modules Modules contain statements Statements contain expressions Expressions create and process objects
![Page 20: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/20.jpg)
Built-in Objects Make programs easy to write Components of extensions More efficient Standard part of language
![Page 21: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/21.jpg)
Program statements You can run programs at command prompt or IDLE
Window print(“hello world!”)
Arithmetic operations: 5+4 or print(5+4) 5-4 or print(5-4) 5*4 or print(5*4) 5/4 or print(5/4) displays 1.25 5//4 or print(5//4) displays 1 5%4 or print(5%4)
![Page 22: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/22.jpg)
Program Statements
2**8 displays 256 Name=‘sasidhar’ Name displays sasidhar ‘sasidhar|’ * 8 displays sasidhar|sasidhar| for 8 times Import os os.getcwd() displays the current python version with its path
![Page 23: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/23.jpg)
Python ScriptType the following program in idle and save it
as script1.py
Import sys # Load a library module
print(sys.platform) Print(2**100)X=‘Sasidhar|’print(x*8)
![Page 24: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/24.jpg)
Core data types & Built-in Objects in Python
Object type Example literals / creation Numbers 1234, 3.145, 3+4j Strings ‘anil’, ‘a\x01c’
Lists [1, [2, ‘three’], 4]Dictionaries {‘food’: ‘spam’, ‘taste’:}Tuples (1, ‘anil’, 77, ‘ecm’)Files myfile = open(‘ecm’, ‘r’)Sets set(‘abc’), {‘a’, ‘b’, ‘c’} Other core types Booleans, types, NoneProgram unit types Functions, modules, classesImplementation types Compiled code, stacktracebacks
![Page 25: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/25.jpg)
Strings and its operations Strings are used to record textual information as
well as bytes. s=“anil” len(s) s[0] displays a s[4] or s[-1] displays l s[1:3] displays ni s + xyz anilxyz s.find(il) found at 2 s.replace(‘an’, ‘man’)
![Page 26: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/26.jpg)
String operations s.split splits based on delimeter s.upper() displays in capital letters. s.lower() displays in small case letters s.isalpha() tests the text. s.rstrip() removes the whitespace characters on
right side
![Page 27: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/27.jpg)
Lists (Data Structures) Lists are ordered collections of arbitrarily
typed objects and they have no fixed size. Lists are mutable. Lists supports all sequence of operations
similar to strings. The difference is the result will be displayed in lists.
Ex: L=[13, ‘anil’, 88] L[1] displays anil L[:-1] displays anil, 88 L + [4,5,6 ] displays [13,anil,88, 4,5,6]
![Page 28: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/28.jpg)
Operations on Lists L.append(‘kk’) adds element kk at the end
of the list L displays anilkk L.pop (2) deletes the 2nd element from
the list L.sort() sorts in ascending order L.reverse() in reverse order.
![Page 29: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/29.jpg)
List Operations len([1,2,3]) 3 [1,2,3] + [4,5,6] [1,2,3,4,5,6] [‘Anil’] * 4 [‘Anil’, ‘Anil’, ‘Anil’, ‘Anil’] str([1,2]) + “34” [1,2]34 [1,2] + list(“34”) [1,2,’3’,’4’]
![Page 30: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/30.jpg)
Nesting Nesting of lists is possible in python. Application is
matrix. A = [ [1,2,3], [4,5,6], [7,8,9]]
>>> A displays [[1,2,3], [4,5,6], [7,8,9] ] >>> A[1] displays [4,5,6]>>> A[1,2] displays [6]
![Page 31: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/31.jpg)
Comprehensions Useful to process the matrix like structures. Example1: to extract 2nd column from the
matrix: Col2= [ row[1] for row in A ] Col2 displays [2,5,8]
Example2: [row[1] + 1for row in A] Example3:
[row[1] for row in A if row[1]%2 ==0] [2,8]
![Page 32: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/32.jpg)
List built-in functions cmp(list1, list2) Compares elements of both lists. len(list) Gives the total length of the list max(list) Returns item from the list with max value. min(list) Returns item from the list with min value. list(seq) Converts a tuple into list.
![Page 33: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/33.jpg)
Control statementsif conditionif expression: statementif-else statementif expression: statement else: statement
elif statementif expression1: statementelif expression2: statementelif expression3: statementelse: statement
![Page 34: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/34.jpg)
Looping Statements while loop
while expression: statements
Example:count = 0 while (count < 10): print ‘Count is:', count count = count + 1
![Page 35: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/35.jpg)
for loop for var in sequence: statements
![Page 36: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/36.jpg)
Tuples tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) tinytuple = (123, 'john') print tuple # Prints complete list print tuple[0] # Prints first element of the list print tuple[1:3] # Prints elements starting from
2nd till 3rd print tuple[2:] # Prints elements starting from
3rd element print tinytuple * 2 # Prints list two times print tuple + tinytuple # Prints concatenated
lists
![Page 37: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/37.jpg)
Tuples vs. Lists Lists slower but more powerful than tuples
Lists can be modified, and they have lots of handy operations and mehtods
Tuples are immutable and have fewer features To convert between tuples and lists use the list()
and tuple() functions:li = list(tu)tu = tuple(li)
![Page 38: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/38.jpg)
Dictionaries Python's dictionaries are kind of hash table type. They work like associative arrays or hashes and consist of
key-value pairs. A dictionary key can be almost any Python type, but are
usually numbers or strings. Values, on the other hand, can be any arbitrary Python
object. Dictionaries are enclosed by curly braces ({ }) and values
can be assigned and accessed using square braces ([]).
![Page 39: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/39.jpg)
Dictionaries Example dict = { } dict['one'] = "This is one" dict[2] = "This is two" tinydict = {'name': 'john','code':6734, 'dept': 'sales'} print dict['one'] # Prints value for 'one' key print dict[2] # Prints value for 2 key print tinydict # Prints complete dictionary print tinydict.keys() # Prints only keys: name,code,dept print tinydict.values() # Prints all the values
![Page 40: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/40.jpg)
Working with Time namespace import time >>> ticks = time.time() >>> print("no.of ticks since 12am, march31, 2015:", ticks) Getting current time: import time; localtime = time.localtime(time.time()) print "Local current time :", localtime Formatted time: localtime =
time.asctime( time.localtime(time.time()) )
![Page 41: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/41.jpg)
Working with calendar namespace in python import calendar cal = calendar.month(2016, 3) print "Here is the calendar:" print cal;
![Page 42: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/42.jpg)
Python’s Statements Statement Role Example Assignment Creating references a, *b = 'good', 'bad', 'ugly' Calls and other expressions Running functions log.write("spam, ham") print calls Printing objects print('The Killer', joke) if/elif/else Selecting actions if "python" in text: print(text) for/else Sequence iteration for x in mylist: print(x) while/else General loops while X > Y: print('hello') Pass Empty placeholder while True: pass break Loop exit while True: if exittest(): break Continue Loop continue while True: if skiptest(): continue
![Page 43: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/43.jpg)
Python’s Statements … def Functions and methods def f(a, b, c=1, *d): print(a+b+c+d[0]) return Functions results def f(a, b, c=1, *d): return a+b+c+d[0] yield Generator functions def gen(n): for i in n: yield i*2 global Namespaces x = 'old' def function(): global x, y; x = 'new' import Module access import sys from Attribute access from sys import stdin class Building objects class Subclass(Superclass): staticData = [] def method(self): pass
![Page 44: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/44.jpg)
Statements of Python try/except/ finally Catching exceptions try: action() except: print('action error') raise Triggering exceptions raise EndSearch(location) assert Debugging checks assert X > Y, 'X too small' with/as Context managers (2.6+) with open('data') as myfile: process(myfile)
![Page 45: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/45.jpg)
Command Line arguments in Python import sys print ('Number of arguments:', len(sys.argv),
'arguments.‘) print 'Argument List:', str(sys.argv)
![Page 46: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/46.jpg)
Functions
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. 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.
![Page 47: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/47.jpg)
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.
def printme( str ): "This prints a passed string into this function" print str return
Functions
![Page 48: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/48.jpg)
Function calling # function definition def display(str): "this prints sasidhar“ # value for str
argument print(str); return; # function calling display("hi this is sasidhar"); display("second time sasidhar");
![Page 49: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/49.jpg)
Pass by reference All parameters (arguments) in the Python language are
passed by reference.
If you change the parameter that refers to within a function, the change also reflects back in the calling function.
![Page 50: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/50.jpg)
Example # function definition def changelist( mylist ): # "This changes a passed list into this function"
mylist.append([1,2,3,4]); Print( "Values inside the function: ", mylist) return #call changelist function mylist = [10,20,30]; changelist( mylist ); print( "Values outside the function: ", mylist)
Output: Values inside the function: [10, 20, 30, [1, 2, 3, 4]] Values outside the function: [10, 20, 30, [1, 2, 3, 4]]
![Page 51: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/51.jpg)
Example for passed by reference and overwritten inside the function. mylist = [1,2,3,4]; # global variable def changeme( mylist ): print "Values inside the function: ", mylist return # Now call changeme function mylist = [10,20,30]; changeme( mylist ); print "Values outside the function: ", mylist Output: Values inside the function: [10, 20, 30] Values outside the function: [10, 20, 30]
![Page 52: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/52.jpg)
Function Arguments Required arguments Keyword arguments Default arguments Variable-length arguments
![Page 53: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/53.jpg)
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.
![Page 54: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/54.jpg)
Keyword arguments Keyword arguments are related to the function calls.
When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.
This allows you to skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters.
![Page 55: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/55.jpg)
Example for keyword argument def printme( str ): "This prints a passed string into this function" print str; return; # Now call printme function printme( str = "My string");
![Page 56: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/56.jpg)
Default arguments A default argument is an argument that assumes a default
value if a value is not provided in the function call for that argument. The following example gives an idea on default arguments, it prints default age if it is not passed:
![Page 57: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/57.jpg)
Example def printinfo( name, age = 35 ): "This prints a passed info into this function" print "Name: ", name; print "Age ", age; return; # Now you can call printinfo function printinfo( age=50, name="miki" ); printinfo( name="miki" );
![Page 58: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/58.jpg)
Variable Length Arguments To process a function for more arguments than specified
while defining the function.
These arguments are called variable-length arguments
An asterisk (*) is placed before the variable name that
holds the values of all non-keyword variable arguments.
![Page 59: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/59.jpg)
Example for variable length arguments def printinfo( arg1, *vartuple ):
print "Output is: " print arg1 #“This prints variable passed arguments"
for var in vartuple: print (var) return;
# Now call printinfo function printinfo( 10 ); printinfo( 70, 60, 50 );
![Page 60: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/60.jpg)
Anonymous functions : lambda The functions that are not declared in the standard manner by
using the def keyword are called anonymous functions. The lambda keyword is used to create small anonymous
functions. Lambda forms can take any number of arguments but return just
one value in the form of an expression. They cannot contain commands or multiple expressions.
An anonymous function cannot be a direct call to print because lambda requires an expression.
Lambda functions have their own local namespace and cannot access variables other than those in their parameter list and those in the global namespace.
![Page 61: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/61.jpg)
Anonymous functions : lambda These functions are not like inline functions in
c++ Syntax: lambda [arg1
[,arg2,.....argn]] :expression
sum=lambda arg1, arg2: arg1+ arg2 print(" value of total:", sum(10,22)) print("value of total:", sum(20,20))
![Page 62: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/62.jpg)
Return statement def sum( arg1, arg2 ):
total = arg1 + arg2 print "Inside the function : ", total return total
total = sum( 10, 20 ); print( "Outside the function : ", total )
![Page 63: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/63.jpg)
Scope of variables Variables declared inside the function are
local and can be accessible with in that function
Variables declared outside the function are global and can be accessible from the calling or outside of the function.
![Page 64: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/64.jpg)
Module A module allows to logically organize the Python code.
Grouping related code into a module makes the code easier to understand and use. A module is an object with arbitrarily named attributes that you can bind and reference. A module can define functions, classes and variables.
# Import built-in module math import math content = dir(math) print content;
![Page 65: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/65.jpg)
Simple calculator Program Using functions """This function adds two numbers""" def add(x,y): return x + y def subtract(x, y): """This function subtracts two numbers""" return x - y def multiply(x, y): """This function multiplies two numbers""“ return x * y def divide(x, y): """This function divides two numbers""" return x / y # take input from the user print("Select
operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") choice = input("Enter choice(1/2/3/4):")
![Page 66: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/66.jpg)
num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) if choice == '1': print(num1,"+",num2,"=", add(num1,num2)) elif choice == '2': print(num1,"-",num2,"=", subtract(num1,num2)) elif choice == '3': print(num1,"*",num2,"=",
multiply(num1,num2)) elif choice == '4': print(num1,"/",num2,"=",
divide(num1,num2)) else: print("Invalid input")
![Page 67: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/67.jpg)
Files I/O Reading input from keyboard:
Input, raw_input The raw_input([prompt]) function reads one
line from standard input and returns it as a string (removing the trailing newline).
Example: str = raw_input("Enter your input: "); print "Received input is : ", str
![Page 68: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/68.jpg)
Opening files A file can be opened using open() function. Syntax:
file object = open(file_name [, access_mode][, buffering])
access mode = file has to be opened in read or write or append mode.
f1 = open(“ecm.txt", "wb") print "Name of the file: ", f1.name print "Closed or not : ", f1.closed print "Opening mode : ", f1.mode
![Page 69: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/69.jpg)
File open modesMode Description
r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
r+ Opens a file for both reading and writing. The file pointer is placed at the beginning of the file.
rb+ Opens a file for both reading and writing in binary format. The file pointer is placed at the beginning of the file.
![Page 70: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/70.jpg)
File access modesMode Description
w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
![Page 71: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/71.jpg)
Mode Description
wb+Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
![Page 72: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/72.jpg)
Closing, reading and writing files close() method of file object closes the file Syntax: fileObject.close(); write() and read() methods are used to read
and write files. fileObject.read([count]); Passed parameter is the number of bytes to be read from
the opened file. This method starts reading from the beginning of the file and if count is missing, then it tries to read as much as possible, maybe until the end of file.
![Page 73: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/73.jpg)
Write() is used to write the data into the file. fileObject.write(string); fo = open(“python.txt", "wb") fo.write( "Python is a good language.!\n"); # Close opend file fo.close()
Closing, reading and writing files
![Page 74: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/74.jpg)
fo = open(“python.txt", "r+") str = fo.read(10); print "Read String is : ", str # Close opend file fo.close()
Closing, reading and writing files
![Page 75: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/75.jpg)
File Positions
tell() method tells you the current position within the file seek(offset[, from]) method changes the current file position. The offset argument indicates the number of bytes to be
moved. The from argument specifies the reference position from
where the bytes are to be moved.
![Page 76: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/76.jpg)
Example # Open a file
fo = open(“ecm.txt", "r+") str = fo.read(10); print "Read String is : ", str
# Check current position position = fo.tell(); print "Current file position : ", position
# Reposition pointer at the beginning once again position = fo.seek(0, 0); str = fo.read(10); print "Again read String is : ", str
# Close opend file fo.close()
![Page 77: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/77.jpg)
Renaming and Deleting Files
rename() method takes two arguments, the current filename and the new filename.
os.rename(current_file_name, new_file_name) Example: import os # Rename a file from test1.txt to test2.txt os.rename( "test1.txt", "test2.txt" )
![Page 78: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/78.jpg)
remove() Method remove() method is used to delete files by supplying the
name of the file to be deleted as the argument. os.remove(file_name) Example: import os
# Delete file test2.txt os.remove("text2.txt")
![Page 79: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/79.jpg)
Exception Handling An exception is an event, that occurs during the execution of a
program and that disrupts the normal flow of the program Execution.
An exception is a Python object that represents an error. When an exception raises in python program, it must either
handle the exception immediately otherwise it terminates and quits.
Handling an Exception If any suspicious code that may raise an exception, then, place
the suspicious code in a try: block. After the try: block, include an except: statement, followed by
a block of code which handles the problem as elegantly as possible.
![Page 80: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/80.jpg)
try and except blocks try: <block of statements> #main code to run except <name1>: #handler for exception <block of statements> except <name2>,<data>: #handler for exception <block of statements> except (<name3>,<name4>): #handler for exception <block of statements> except: #handler for exception <block of statements> else: # optional, runs if no exception occurs <block of statements>
![Page 81: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/81.jpg)
Example >>>try: action() except NameError(): … except IndexError(): … except KeyError(): … except (AttributeError,TypeError,SyntaxError):… else: …. General catch-all clause: add empty except. It may catch unrelated to your code system
exceptions. It may catch exceptions meant for other handler
(system exit)
![Page 82: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/82.jpg)
Example programdef division(x,y): try: return x/y except ZeroDivisionError: print("division by zero")
![Page 83: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/83.jpg)
elseelse is used to verify if no exception occurred in trytry.
You can always eliminate elseelse by moving its logic at the end of the trytry block.
However, if “else statement” triggers exceptions, it would be misclassified as exception in try block.
try/else
![Page 84: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/84.jpg)
try/finally In try/finallytry/finally, finallyfinally block is always run
whether an exception occurs or not
try: <block of statements> finally: <block of statements>
Ensure some actions to be done in any case It can not be used in the trytry with exceptexcept and
elseelse.
![Page 85: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/85.jpg)
Example for try .. finally def division(x,y): try: return x/y except ZeroDivisionError: print("division by zero")
finally: print("it executes compulsory")
![Page 86: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/86.jpg)
Built-in Exceptions
![Page 87: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/87.jpg)
Built-in Exceptions
![Page 88: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/88.jpg)
Built-in Exceptions
![Page 89: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/89.jpg)
Raise raise triggers exceptions explicitly raise <name> raise <name>,<data> # provide data to handler raise #re-raise last exception >>>try: raise ‘zero’, (3,0) except ‘zero’: print “zero argument” except ‘zero’, data: print data Last form may be useful if you want to propagate
cought exception to another handler. Exception name: built-in name, string, user-defined
class
![Page 90: Spsl iv unit final](https://reader036.vdocuments.net/reader036/viewer/2022070522/58edf1b01a28ab800e8b4573/html5/thumbnails/90.jpg)
def result(marks ): if marks < 40: raise “fail!", marks