Python: Importing Modules and Functions, Exams of Linguistics

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

Pre 2010

Uploaded on 08/30/2009

koofers-user-vzs
koofers-user-vzs 🇺🇸

5

(1)

9 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LIN 392, Spring 2009
Working with Corpora
Katrin Erk
Python: modules,
and more methods
Importing additional functionality
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 "<stdin>", 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)
1.6094379124341003
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Python: Importing Modules and Functions and more Exams Linguistics in PDF only on Docsity!

LIN 392, Spring 2009 Working with Corpora Katrin Erk

Python: modules,

and more methods

Importing additional functionality

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)

Another dot notation

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.

Importing 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.

The Python library Further down on the

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.

String methods

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?

The string module

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?

The math module

import math math.exp(3) # e^3 math.log(5) # logarithm to base e math.log(5, 2) # logarithm to base 2:

optional second argument is the base.

math.sqrt(4) # square root Look up the available functionality on the Python Library webpage: What other functions does the math module offer?

The sys module

sys.argv is a list of strings.

test_argv.py:

testing command line arguments

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.

The sys module

Use test.py from the command line: !! python test_argv.py firstarg secondarg thirdarg !! python test_argv.py x y z a b c

The module random

Choose a number, or a list element, at random: import random mylist = [1, 2, 3, 4]

choose list element at random

some_element = random.choice(mylist)

generate random number 0.0 <= number < 1.

some_number = random.random()

The module os.path

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.