





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to importing modules and functions in python. It explains the concept of modules, the difference between importing a whole module and an individual function, and provides examples of importing functions from various python modules such as math, string, and sys. It also covers the use of the python library webpage for finding available functionality.
Typology: Exams
1 / 9
This page cannot be seen from the preview
Don't miss anything!






LIN 392, Spring 2009 Working with Corpora Katrin Erk
When you start the Python interpreter, not all Python functionality is available right away. This is the case, for example, for the function math.log() >>> math.log(5) Traceback (most recent call last): File "", line 1, in? NameError: name 'math' is not defined You need to import it to be able to use it: >>> import math >>> math.log(5)
We have just seen a function call to math.log(5) What does the dot mean? It is not the same as when you access a method of an object: mylist = [1, 2] mylist.append(3) mylist.append() accesses a method available for all objects of type list. math is the name of a module.
There are two possibilities: 1.! Either import a whole module. Then all functions in the module are accessible as . import math math.log(92) 2.! Or import an individual function from a module. Then that function is accessible without the module name: from math import log log(92) But the other functions of the module are not imported.
page you find information on the ‘string’ module (which is different from the builtin data type str), and even further down information on the ‘math’ and ‘random’ modules.
First look up the methods of the builtin data type str. (This is different from the module string!) Find answers for the following questions: 1.! How can you turn a string into all lowercase characters? (For example, convert “For” to “for”.) 2.! If you have a list of strings, for example [“this”, “is”, “a”, “split”, “sentence”], how can you combine them into a single string joined by whitespace, in our example “this is a split sentence”? 3.! How can you remove all occurrences of fullstop, question mark, or comma from the end of a string? 4.! How can you count how many occurrences of the letter ‘e’ a string contains?
Now look up the module string. Among other things, it contains the following predefined variables: >>> import string >>> string.punctuation '!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~' >>> string.digits '0123456789' >>> string.letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX YZ’ How can you use this to remove punctuation from the beginning and end of a word?
import math math.exp(3) # e^3 math.log(5) # logarithm to base e math.log(5, 2) # logarithm to base 2:
math.sqrt(4) # square root Look up the available functionality on the Python Library webpage: What other functions does the math module offer?
sys.argv is a list of strings.
import sys print “I got the following arguments:” for index in range(len(sys.argv)): print index, sys.argv[index], type(sys.argv[index]) sys.argv[0] contains the name of your program. sys.argv[1] and following contain command line arguments. All arguments are strings.
Use test.py from the command line: !! python test_argv.py firstarg secondarg thirdarg !! python test_argv.py x y z a b c
Choose a number, or a list element, at random: import random mylist = [1, 2, 3, 4]
some_element = random.choice(mylist)
some_number = random.random()
You can use os.path to check whether a file with a given name exists.You can use the same module to split a path into the filename and directory path. >>> import os.path >>> filename = raw_input("Please enter a filename: ") Please enter a filename: /Users/katrinerk/Desktop/meananno.pdf >>> os.path.exists(filename) True >>> os.path.basename(filename) 'meananno.pdf' >>> os.path.dirname(filename) '/Users/katrinerk/Desktop' The function os.path.exists() returns True if the given file exists. os.path.basename(filename) returns the name of the file without directoy path, and os.path.dirname(filename) the directory path.