Lecture Notes 12
Lecture Notes 12
CS 3377
1
Reading for Week 9-10
Chapter 12 of A Practical Guide to
Linux® Commands, Editors, and Shell
Programming, Third Edition. Mark G.
Sobell.
◼ Chapter 12: The Python Programming
Language
2
Agenda
Functions in Python
Python Project
3
Functions
def mysum(x,y):
w=x+y
print (w)
return w
This works both as code in file and on
evaluator
If you don’t use correct indentation in
the evaluator then you need to put
semicolon!
4
Reuse Functions
Syntax:
def NAME( LIST OF PARAMETERS):
STATEMENTS
STATEMENTS
5
Using a Function
6
Importance of Indentation
A function is made up of two main parts, the
Header, and the Body.
The function header consists of:
def funcName(param1,param2):
◼ def keyword, function name
◼ zero or more parameters, comma separated,
inside of parenthesis ()
◼ A colon :
The function body consists of all statements
in the block that directly follows the header.
A block is made up of statements that are at
the same indentation level.
7
Indentation leads to syntax error!
def findArea( ):
Radius = 10
pi = 3.1459
area = pi * Radius * Radius
print(area)
You canot mix indentation levels within
the same block!
The above code will result in a syntax
error!
Parametrize functions when possible to
make them more generic
8
findArea function better example
def findArea( Radius ):
pi = 3.1459
area = pi * Radius * Radius
print( area)
This function is parameterized
This function will PRINT the area to the
screen, but will NOT return the value
pointed to by the area variable.
def findArea( Radius ):
pi = 3.1459
area = pi * Radius * Radius
return area
9
Keywords and Scope
In Python, not all names are equal.
Some names are reserved by the system and are
already defined. Examples are things like: def,
print, if, else, while, for, in, and, or, not, return.
These names are built in keywords.
Names that are defined in a function are “local” to
that function.
Names that are defined outside of a function are
“global” to the module.
Local names overshadow global names when
inside the function that defined them.
If you want to access a global variable from inside
of a function, you should declare it “global”.
10
Global vs Local
myVariable = 7
myParam = 20
def func1(myParam):
myVariable = 20
print(myParam)
func1(5)
print(myVariable)
What gets printed?
11
Global vs Local
myVariable = 7
myParam = 20
def func1(myParam):
global myVariable
myVariable = 20
print(myParam)
func1(5)
print(myVariable)
12
Dictionaries in Python
Hash or associative array
>>> tel {'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> list(tel.keys())
['irv', 'guido', 'jack']
>>> sorted(tel.keys())
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
13
>>> 'jack' not in tel False
Random Numbers, Format
>>> import random
>>> random.random()
0.5543475553557455
>>> int(random.random()*100)
72
>>> import math
>>> print('The value of PI is approximately
{0:.3f}.'.format(math.pi))
The value of PI is approximately 3.142.
14
File IO
open() returns a file object, and is most
commonly used with two
arguments: open(filename, mode).
>>> f = open('workfile', 'w')
Modes, ‘r’, ‘w’, ‘a’, ‘r+’ (both read and write)
>>> f.read()
◼ 'This is the entire file.\n‘
>>> f.readline()
◼ 'This is the first line of the file.\n‘
>>> f.write('This is a test\n')
15
◼ 15 (number of characters written )
Python Course
http://www.python-course.eu/index.php
16