SlideShare une entreprise Scribd logo
1  sur  90
Shell Programming & Scripting Languages
Dr. K. Sasidhar
UNIT – IV Contents
 Introduction to python language
 python-syntax
 statements
 functions, Built-in-functions and Methods
 Modules in python
 Exception Handling
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.
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
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
 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?
 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?
 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?
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.
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
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 code
Hello.java
byte code
Hello.class
interpret
outputsource code
Hello.py
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
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
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
15
Math commands (import math)
 Python has useful commands for performing calculations.
Command name Description
abs(value) absolute value
ceil(value) rounds up
cos(value) cosine, in radians
floor(value) rounds down
log(value) logarithm, base e
log10(value) logarithm, base 10
max(value1, value2) larger of two values
min(value1, value2) smaller of two values
round(value) nearest whole number
sin(value) sine, in radians
sqrt(value) square root
Constant Description
e 2.7182818...
pi 3.1415926...
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
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
print
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
Programming Basics
 Programs are composed of modules
 Modules contain statements
 Statements contain expressions
 Expressions create and process objects
Built-in Objects
 Make programs easy to write
 Components of extensions
 More efficient
 Standard part of language
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)
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
Python Script
Type 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)
Core data types & Built-in Objects in Python
Object type Example literals / creation
Numbers 1234, 3.145, 3+4j
Strings ‘anil’, ‘ax01c’
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, None
Program unit types Functions, modules, classes
Implementation types Compiled code, stacktracebacks
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’)
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
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]
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.
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’]
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]
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]
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.
Control statements
if condition
if expression:
statement
if-else statement
if expression:
statement
else:
statement
elif statement
if expression1:
statement
elif expression2:
statement
elif expression3:
statement
else:
statement
Looping Statements
 while loop
 while expression:
 statements
Example:
count = 0
while (count < 10):
print ‘Count is:', count
count = count + 1
for loop
for var in sequence:
statements
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
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)
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 ([]).
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
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()) )
Working with calendar namespace in python
 import calendar
 cal = calendar.month(2016, 3)
 print "Here is the calendar:"
 print cal;
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
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
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)
Command Line arguments in Python
 import sys
 print ('Number of arguments:', len(sys.argv), 'arguments.‘)
 print 'Argument List:', str(sys.argv)
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.
 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
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");
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.
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]]
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]
Function Arguments
 Required arguments
 Keyword arguments
 Default arguments
 Variable-length arguments
Required arguments
 Required arguments are the arguments passed to a
function in correct positional order. Here, the number of
arguments in the function call should match exactly with
the function definition.
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.
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");
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:
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" );
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.
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 );
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.
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))
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 )
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.
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;
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):")
 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")
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
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
File open modes
Mode 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.
File access modes
Mode 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.
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.
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.
 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
 fo = open(“python.txt", "r+")
 str = fo.read(10);
 print "Read String is : ", str
 # Close opend file
 fo.close()
Closing, reading and writing files
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.
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()
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" )
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")
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.
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>
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)
Example program
def division(x,y):
try:
return x/y
except ZeroDivisionError:
print("division by zero")
 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
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.
Example for try .. finally
 def division(x,y):
 try:
 return x/y
 except ZeroDivisionError:
 print("division by zero")
 finally:
 print("it executes compulsory")
Built-in Exceptions
Built-in Exceptions
Built-in Exceptions
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
 def result(marks ):
 if marks < 40:
 raise “fail!", marks

Contenu connexe

Tendances

Programming in Computational Biology
Programming in Computational BiologyProgramming in Computational Biology
Programming in Computational Biology
AtreyiB
 

Tendances (19)

web programming Unit VI PPT by Bhavsingh Maloth
web programming Unit VI PPT  by Bhavsingh Malothweb programming Unit VI PPT  by Bhavsingh Maloth
web programming Unit VI PPT by Bhavsingh Maloth
 
Python
PythonPython
Python
 
programming with python ppt
programming with python pptprogramming with python ppt
programming with python ppt
 
Data file handling
Data file handlingData file handling
Data file handling
 
Biopython
BiopythonBiopython
Biopython
 
Programming in Computational Biology
Programming in Computational BiologyProgramming in Computational Biology
Programming in Computational Biology
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
 
Unit VI
Unit VI Unit VI
Unit VI
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streams
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Linux basics
Linux basicsLinux basics
Linux basics
 
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
 
An introduction to Python for absolute beginners
An introduction to Python for absolute beginnersAn introduction to Python for absolute beginners
An introduction to Python for absolute beginners
 
intro unix/linux 10
intro unix/linux 10intro unix/linux 10
intro unix/linux 10
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
 
Presentation new
Presentation newPresentation new
Presentation new
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Maloth
 

Similaire à Spsl iv unit final

Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptx
usvirat1805
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
IruolagbePius
 

Similaire à Spsl iv unit final (20)

Python fundamentals
Python fundamentalsPython fundamentals
Python fundamentals
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
علم البيانات - Data Sience
علم البيانات - Data Sience علم البيانات - Data Sience
علم البيانات - Data Sience
 
Help with Pyhon Programming Homework
Help with Pyhon Programming HomeworkHelp with Pyhon Programming Homework
Help with Pyhon Programming Homework
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfpython-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
 
PYTHON PROGRAMMING NOTES RKREDDY.pdf
PYTHON PROGRAMMING NOTES RKREDDY.pdfPYTHON PROGRAMMING NOTES RKREDDY.pdf
PYTHON PROGRAMMING NOTES RKREDDY.pdf
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data science
 
00 C hello world.pptx
00 C hello world.pptx00 C hello world.pptx
00 C hello world.pptx
 
Python programming workshop session 1
Python programming workshop session 1Python programming workshop session 1
Python programming workshop session 1
 
Interview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdfInterview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdf
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptx
 
Python Programming | JNTUK | UNIT 1 | Lecture 3
Python Programming | JNTUK | UNIT 1 | Lecture 3Python Programming | JNTUK | UNIT 1 | Lecture 3
Python Programming | JNTUK | UNIT 1 | Lecture 3
 
Python for Physical Science.pdf
Python for Physical Science.pdfPython for Physical Science.pdf
Python for Physical Science.pdf
 
Revision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdfRevision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdf
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
 
Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
 
Chapter 1 Class 12 Computer Science Unit 1
Chapter 1 Class 12 Computer Science Unit 1Chapter 1 Class 12 Computer Science Unit 1
Chapter 1 Class 12 Computer Science Unit 1
 

Plus de Sasidhar Kothuru (18)

Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 
Spsl II unit
Spsl   II unitSpsl   II unit
Spsl II unit
 
Jdbc sasidhar
Jdbc  sasidharJdbc  sasidhar
Jdbc sasidhar
 
Servlets
ServletsServlets
Servlets
 
Servers names
Servers namesServers names
Servers names
 
Servers names
Servers namesServers names
Servers names
 
Web Technologies -- Servlets 4 unit slides
Web Technologies -- Servlets   4 unit slidesWeb Technologies -- Servlets   4 unit slides
Web Technologies -- Servlets 4 unit slides
 
Xml quiz 3 unit
Xml quiz   3 unitXml quiz   3 unit
Xml quiz 3 unit
 
Web technologies quiz questions - unit1,2
Web technologies quiz questions  - unit1,2Web technologies quiz questions  - unit1,2
Web technologies quiz questions - unit1,2
 
Web technologies 4th unit quiz
Web technologies 4th unit quizWeb technologies 4th unit quiz
Web technologies 4th unit quiz
 
Xml quiz 3 unit
Xml quiz   3 unitXml quiz   3 unit
Xml quiz 3 unit
 
Web technologies quiz questions - unit1,2
Web technologies quiz questions  - unit1,2Web technologies quiz questions  - unit1,2
Web technologies quiz questions - unit1,2
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
Xml sasidhar
Xml  sasidharXml  sasidhar
Xml sasidhar
 
Java script -23jan2015
Java script -23jan2015Java script -23jan2015
Java script -23jan2015
 
HTML By K.Sasidhar
HTML By K.SasidharHTML By K.Sasidhar
HTML By K.Sasidhar
 

Dernier

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Dernier (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Spsl iv unit final

  • 1. Shell Programming & Scripting Languages Dr. K. Sasidhar
  • 2. UNIT – IV Contents  Introduction to python language  python-syntax  statements  functions, Built-in-functions and Methods  Modules in python  Exception Handling
  • 3. 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.
  • 4. 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
  • 5. 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
  • 6.  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?
  • 7.  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?
  • 8.  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?
  • 9. 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.
  • 10. 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
  • 11. 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 code Hello.java byte code Hello.class interpret outputsource code Hello.py
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 15 Math commands (import math)  Python has useful commands for performing calculations. Command name Description abs(value) absolute value ceil(value) rounds up cos(value) cosine, in radians floor(value) rounds down log(value) logarithm, base e log10(value) logarithm, base 10 max(value1, value2) larger of two values min(value1, value2) smaller of two values round(value) nearest whole number sin(value) sine, in radians sqrt(value) square root Constant Description e 2.7182818... pi 3.1415926...
  • 16. 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
  • 17. 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 print
  • 18. 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
  • 19. Programming Basics  Programs are composed of modules  Modules contain statements  Statements contain expressions  Expressions create and process objects
  • 20. Built-in Objects  Make programs easy to write  Components of extensions  More efficient  Standard part of language
  • 21. 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)
  • 22. 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
  • 23. Python Script Type 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)
  • 24. Core data types & Built-in Objects in Python Object type Example literals / creation Numbers 1234, 3.145, 3+4j Strings ‘anil’, ‘ax01c’ 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, None Program unit types Functions, modules, classes Implementation types Compiled code, stacktracebacks
  • 25. 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’)
  • 26. 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
  • 27. 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]
  • 28. 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.
  • 29. 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’]
  • 30. 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]
  • 31. 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]
  • 32. 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.
  • 33. Control statements if condition if expression: statement if-else statement if expression: statement else: statement elif statement if expression1: statement elif expression2: statement elif expression3: statement else: statement
  • 34. Looping Statements  while loop  while expression:  statements Example: count = 0 while (count < 10): print ‘Count is:', count count = count + 1
  • 35. for loop for var in sequence: statements
  • 36. 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
  • 37. 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)
  • 38. 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 ([]).
  • 39. 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
  • 40. 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()) )
  • 41. Working with calendar namespace in python  import calendar  cal = calendar.month(2016, 3)  print "Here is the calendar:"  print cal;
  • 42. 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
  • 43. 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
  • 44. 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)
  • 45. Command Line arguments in Python  import sys  print ('Number of arguments:', len(sys.argv), 'arguments.‘)  print 'Argument List:', str(sys.argv)
  • 46. 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.
  • 47.  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
  • 48. 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");
  • 49. 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.
  • 50. 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]]
  • 51. 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]
  • 52. Function Arguments  Required arguments  Keyword arguments  Default arguments  Variable-length arguments
  • 53. 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.
  • 54. 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.
  • 55. 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");
  • 56. 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:
  • 57. 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" );
  • 58. 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.
  • 59. 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 );
  • 60. 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.
  • 61. 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))
  • 62. 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 )
  • 63. 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.
  • 64. 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;
  • 65. 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):")
  • 66.  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")
  • 67. 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
  • 68. 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
  • 69. File open modes Mode 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.
  • 70. File access modes Mode 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.
  • 71. 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.
  • 72. 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.
  • 73.  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
  • 74.  fo = open(“python.txt", "r+")  str = fo.read(10);  print "Read String is : ", str  # Close opend file  fo.close() Closing, reading and writing files
  • 75. 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.
  • 76. 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()
  • 77. 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" )
  • 78. 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")
  • 79. 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.
  • 80. 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>
  • 81. 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)
  • 82. Example program def division(x,y): try: return x/y except ZeroDivisionError: print("division by zero")
  • 83.  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
  • 84. 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.
  • 85. Example for try .. finally  def division(x,y):  try:  return x/y  except ZeroDivisionError:  print("division by zero")  finally:  print("it executes compulsory")
  • 89. 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
  • 90.  def result(marks ):  if marks < 40:  raise “fail!", marks