The document discusses various Python programming concepts including modules, namespaces, and file handling. It explains how to create and use modules, the importance of namespaces and scopes, and methods for reading and writing files, both text and binary. Additionally, it touches on algorithms such as linear and binary search, and the merging of sorted lists.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views40 pages
Python Unit 3
The document discusses various Python programming concepts including modules, namespaces, and file handling. It explains how to create and use modules, the importance of namespaces and scopes, and methods for reading and writing files, both text and binary. Additionally, it touches on algorithms such as linear and binary search, and the merging of sorted lists.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40
Unit 3
Modules
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
module A module is a file containing Python definitions and statements intended for use in other Python programs.
Random numbers
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
The time module The time module has a function called clock. Whenever clock is called, it returns a floating point number representing how many seconds have elapsed since your program started running. So our function runs about 57% slower than the built-in one. Generating and summing up ten million elements
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT The math module The math module contains the kinds of mathematical functions. Mathematical functions are “pure” functions
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Creating your own modules All we need to do to create our own modules is to save our script as a file with a .py extension.
Save as Hello.py
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Namespaces A namespace is a collection of identifiers that belong to a module, or to a function,
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Namespaces
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Namespaces Functions also have their own namespaces:
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Scope and lookup rules The scope of an identifier is the region of program code in which the identifier can be accessed, or used. There are three important scopes in Python: • Local scope refers to identifiers declared within a function. •Global scope refers to all the identifiers declared within the current module, or file. •Built-in scope refers to all the identifiers built into Python—those like range and min
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Scope and lookup rules
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Attributes and the dot operator Variables defined inside a module are called attributes of the module Attributes are accessed using the dot operator (.) Modules contain functions as well as attributes, and the dot operator is used to access them in the same way.
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Files
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Files While a program is running, its data is stored in random access memory (RAM). RAM is fast and inexpensive, but it is also volatile, which means that when the program ends, or the computer shuts down, data in RAM disappears. To make data available the next time the computer is turned on and the program is started, it has to be written to a non-volatile storage medium, such a hard drive, usb drive, or CD-RW. Data on non-volatile storage media is stored in named locations on the media called files
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Files Working with files is a lot like working with a notebook. To use a notebook, it has to be opened. When done, it has to be closed. While the notebook is open, it can either be read from or written to. In either case, the notebook holder knows where they are.
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Writing our first file
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Writing our first file Opening a file creates what we call a file handle. In this example, the variable myfile refers to the new handle object. The open function takes two arguments. The first is the name of the file, and the second is the mode.
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Reading a file line-at-a-time
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Turning a file into a list of lines It is often useful to fetch data from a disk file and turn it into a list of lines. Suppose we have a file containing our friends and their email addresses, one per line in the file. But we’d like the lines sorted into alphabetical order. A good plan is to read everything into a list of lines, then sort the list, and then write the sorted list back to another file:
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Turning a file into a list of lines
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Reading the whole file at once Another way of working with text files is to read the complete contents of the file into a string, and then to use our string- processing skills to work with the contents.
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Reading the whole file at once
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Your file paths may need to be explicitly named. In the above example, we’re assuming that the file somefile.txt is in the same directory as your Python source code. If this is not the case, you may need to provide a full or a relative path to the file.
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Your file paths may need to be explicitly named.
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Your file paths may need to be explicitly named.
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Working with binary files Files that hold photographs, videos, zip files, executable programs, etc. are called binary files. They’re not organized into lines, and cannot be opened with a normal text editor. Python works just as easily with binary files, but when we read from the file we’re going to get bytes back rather than a string. Here we’ll copy one binary file to another:
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Working with binary files
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Working with binary files
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Directories Files on non-volatile storage media are organized by a set of rules known as a file system. File systems are made up of files and directories, which are containers for both files and other directories. When we create a new file by opening it and writing, the new file goes in the current directory Similarly, when we open a file for reading , Python looks for it in the current directory. If we want to open a file somewhere else, we have to specify the path to the file, which is the name of the directory (or folder) where the file is located: Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT Directories
A Windows path might be
"c:/temp/words.txt" or "c:\\temp\\words.txt".
Because backslashes are used to escape things like newlines and tabs, we need to write two backslashes in a literal string to get one!
We cannot use / or \ as part of a filename; they are reserved as a delimiter between
directory and filenames. Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT What about fetching something from the web?
The urlretrieve function—just one call—could be used to download
any kind of content from the Internet.
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT Different example.
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Different example. Opening the remote url returns what we call a socket. This is a handle to our end of the connection between our program and the remote web server. We can call read, write, and close methods on the socket object in much the same way as we can work with a file handle.
Akshata S Bhayyar, Asst. Prof. Dept. of CSE, RIT
Algorithms Linear search Binary search merging two sorted lists.