0% found this document useful (0 votes)
2 views8 pages

Python Module 4 Notes

Uploaded by

simpleshivu6361
Copyright
© All Rights Reserved
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% found this document useful (0 votes)
2 views8 pages

Python Module 4 Notes

Uploaded by

simpleshivu6361
Copyright
© All Rights Reserved
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

lOMoAR cPSD| 19616291

Introduction to Python Programming BCSPLC215B

Bapuji Educational Association ®


Bapuji Institute of Engineering and Technology, Davangere–577 004
Post Box No. :325, Davanagere -577 004, Karnataka, India.
An Autonomous Institute Affiliated to Visvesvaraya Technological University, Belagavi, Karnataka.
Approved by AlCTE, New Delhi | Accredited by NAAC with 'A' grade and NBA (UG Programmes)
Recognized by UGC,New Delhi under 2(F) and 12(B)

Introduction to Python Programming


(BPLCK105B/205B)

MODULE – 4
STUDY MATERIAL

SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.


lOMoAR cPSD| 19616291

Introduction to Python Programming BCSPLC215B

CHAPTER 7: FILES

7.1 About files


A file is a named location on secondary storage (hard disk, SSD, etc.) used to store data
permanently.
Advantages
• Data remains even after program execution ends.
• Large amounts of information can be stored.
• Data can be shared between programs.
Types of Files
1. Text Files – Store characters (e.g., .txt, .csv)
2. Binary Files – Store data in binary format (e.g., images, audio, videos)

7.2 Writing Files


Use the open() function with three types of modes.

Example 1 (write mode) 3 types of Modes

Output:

Example 2 (read mode)

Output:

SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.


lOMoAR cPSD| 19616291

Introduction to Python Programming BCSPLC215B

Example 3 (append mode)

[Link] already existed with some text inside it as shown in the above example
Output:

7.3 Reading a File Line-at-a-Time

Each iteration reads one line from the file.


Output:

7.4 Turning a File into a List of Lines

Each line becomes an element of the list.

Output:

SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.


lOMoAR cPSD| 19616291

Introduction to Python Programming BCSPLC215B

7.5 Reading the Whole File at Once

read() loads the complete file into memory.

Output:

7.6 Working with Binary Files


Binary files store data as bytes.

Writing Binary Data

Reading Binary Data

Output:

7.7 Directories
Files on non-volatile storage media are organized by a set of rules known as a file system.
A directory (folder) contains files and subdirectories.
Create Directory List Files Remove Directory

SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.


lOMoAR cPSD| 19616291

Introduction to Python Programming BCSPLC215B

7.8 Fetching Something from the Web


An example that copies the contents at some web URL to a local file.

This fetches the first 100 contents from a web page.

CHAPTER 8: MODULES

A module is a file containing Python definitions and statements intended for use in
other Python programs. Many Python modules come with Python as part of the
standard library.

8.1 Random Numbers Module


The random module generates random values.
Example (Picking balls from bags, throwing dice, shuffling a pack of cards)

Generating 5 numbers choosing between 1-12.

Picking Balls from a Bag

Throwing a Die 5 times

SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.


lOMoAR cPSD| 19616291

Introduction to Python Programming BCSPLC215B

Picking Cards from a Deck

8.2 The time module


Used for date and time operations.

This program demonstrates how to use the time module to compare the execution
speed of:
1. A user-defined summation function (do_my_sum)
2. Python's built-in sum() function

Output:

Observation: Both methods produce the same answer.


However Built-in sum() is faster.

SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.


lOMoAR cPSD| 19616291

Introduction to Python Programming BCSPLC215B

8.3 The math module


Provides mathematical functions.

8.4 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.
1. Save the file as [Link]

2. In another file [Link] or interpreter, first import seqtools

The use of modules makes it possible to break up very large programs into manageable
sized parts, and to keep related parts together.

8.5 Mutable versus immutable and aliasing


Some datatypes in Python are mutable. This means their contents can be changed after
they have been created. Lists and dictionaries are good examples of mutable datatypes.

Example showing that lists are mutable

SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.


lOMoAR cPSD| 19616291

Introduction to Python Programming BCSPLC215B

Example showing that tuples are immutable

Example showing aliasing


Aliasing occurs when two or more variables refer to the same object in memory.

This happens, because both list_one and list_two refer to the same memory address
containing the actual list. So when one is changed, another will also get changed.

We can check if both are pointing to same address by using id() builtin function.

If you do not want both to point a same location, rather create another copy.

However, this will not work for nested lists because of the same reason.

SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.

You might also like