Module and Strings
Module and Strings
CSC 301
What are Modules?
Money = 2000
def AddMoney():
Money = Money + 1
print Money
AddMoney()
print Money
The dir( ) Function:
• The dir() built-in function returns a sorted list of strings containing the names
defined by a module.
• The list contains the names of all the modules, variables, and functions that are
defined in a module.
• Example:
import math
content = dir(math)
print (content);
• This would produce following result:
['__doc__', '__name__', '__package__', 'acos',
'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh',
'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e',
'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial',
'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot',
'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10',
'log1p', 'modf', 'pi', 'pow', 'radians', 'sin',
'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
The globals() and locals() Functions:
• The globals() and locals() functions can be used to return the names in the global
and local namespaces depending on the location from where they are called.
• If locals() is called from within a function, it will return all the names that can be
accessed locally from that function.
• If globals() is called from within a function, it will return all the names that can
be accessed globally from that function.
• The return type of both these functions is dictionary. Therefore, names can be
extracted using the keys() function.
The reload() Function:
• When the module is imported into a script, the code in the top-level portion of a module is executed only
once.
• Therefore, if you want to reexecute the top-level code in a module, you can use the reload() function.
The reload() function imports a previously imported module again.
• Syntax:
The syntax of the reload() function is this:
reload(module_name)
Here module_name is the name of the module you want to reload and not the string containing the
module name. For example to re-load hello module, do the following:
reload(hello)
Packages in Python:
Example:
Consider a file Pots.py available in Phone directory. This file has following
line of source code:
def Pots():
print "I'm Pots Phone"
Similar way we have another two files having different functions with the
same name as above:
Phone/Isdn.py file having function Isdn()
Phone/G3.py file having function G3()
Now create one more file __init__.py in Phone directory :
Phone/__init__.py
To make all of your functions available when you've imported Phone, you
need to put explicit import statements in __init__.py as follows:
from Pots import Pots
from Isdn import Isdn
from G3 import G3
• http://docs.python.org/tutorial/modules.html
Strings
CSC 301
String Data Type >>> str1 = "Hello”
• A string is a sequence of >>> str2 = 'there'
>>> bob = str1 + str2
characters >>> print bob
• A string literal uses quotes Hellothere
'Hello' or “Hello” >>> str3 = '123'
>>> str3 = str3 + 1
• For strings, + means Traceback (most recent call last):
“concatenate” File "<stdin>", line 1, in
• When a string contains <module>TypeError: cannot
numbers, it is still a string concatenate 'str' and 'int' objects
>>> x = int(str3) + 1
• We can convert numbers in a >>> print x
string into a number using 124
int() >>>
Reading and Converting
>>> name = input('Enter:')
• We prefer to read data Enter:Chuck
in using strings and then >>> print name
parse and convert the Chuck
data as we need >>> apple = input('Enter:')
Enter:100
• This gives us more
>>> x = apple – 10
control over error Traceback (most recent call last):
situations and/or bad File "<stdin>", line 1, in
user input <module>TypeError: unsupported
operand type(s) for -: 'str' and 'int'
• Raw input numbers
>>> x = int(apple) – 10
must be converted from >>> print x
strings 90
Looking Inside Strings
A function is some
>>> fruit = 'banana'
stored code that we
>>> x = len(fruit)
use. A function takes
>>> print (x)
some input and
6
produces an output.
'banana' len() 6
(a string) function (a number)
Len Function
A function is some
>>> fruit = 'banana'
stored code that we
>>> x = len(fruit)
use. A function takes
>>> print (x)
some input and
6
produces an output.
def len(inp):
blah
'banana' blah 6
for x in y: (a number)
(a string) blah
blah
Looping Through Strings
print letter
Slicing Strings
String Concatenation
if word == 'banana':
print 'All right, bananas.'
http://docs.python.org/lib/string-methods.html
http://docs.python.org/lib/string-methods.html
String Library
http://docs.python.org/lib/string-methods.html
Searching a String
• We use the find()
function to search for a b a n a n a
substring within another
0 1 2 3 4 5
string
• find() finds the first >>> fruit = 'banana'
occurance of the >>> pos = fruit.find('na')
>>> print (pos)
substring 2
• If the substring is not >>> aa = fruit.find('z')
found, find() returns -1 >>> print (aa)
-1
• Remember that string
position starts at zero
Making everything UPPER CASE
• String type
• Read/Convert
• Indexing strings []
• Slicing strings [2:4]
• Looping through strings with for and while
• Concatenating strings with +
• String operations