This document provides an overview of the Python programming language. It discusses Python's history, features, and why it is a good programming language. Key points covered include:
- Python was created in the late 1980s and draws from many other languages.
- It is an open source, interpreted, object-oriented, and portable language with a large online community and library support.
- Python code is compiled to bytecode for performance. It has dynamic typing, automatic memory management, and is powerful yet easy to learn.
- The document reviews Python statements, expressions, variables, basic data types, functions, modules and exceptions. It provides examples of Python code.
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.
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
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]
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):")
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)
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