0% found this document useful (0 votes)
8 views

Lecture 5 Python Self notes

The document provides an overview of Python dictionaries and sets, detailing their characteristics, methods for updating and deleting elements, and operations such as union and intersection. It also covers file manipulation in Python, including how to open, read, write, and close files with various access modes. Key methods and attributes related to file objects are highlighted for effective file handling.

Uploaded by

amedalsaby
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)
8 views

Lecture 5 Python Self notes

The document provides an overview of Python dictionaries and sets, detailing their characteristics, methods for updating and deleting elements, and operations such as union and intersection. It also covers file manipulation in Python, including how to open, read, write, and close files with various access modes. Key methods and attributes related to file objects are highlighted for effective file handling.

Uploaded by

amedalsaby
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
You are on page 1/ 28

Programming Language 1

Python3 programming
Lecture 5
Dictionary
❑Dictionaries are used to store data values in
key:value pairs.
❑A dictionary is a collection which is ordered*,
changeable and do not allow duplicates.
❑Each key is separated from its value by a colon
(:), the items are separated by commas, and the
whole thing is enclosed in curly braces.
❑ An empty dictionary without any items is
written with just two curly braces, like this: {}.
❑Keys are unique within a dictionary while
values may not be.
❑The values of a dictionary can be of any type,
but the keys must be of an immutable data type
such as strings, numbers, or tuples.
Updating
Dictionary
❑ We can update a
dictionary by adding a
new entry or a key-value
pair, modifying an existing
entry, or deleting an
existing entry as shown in
a simple example given
below.
Delete
Dictionary
Elements
Sets

❑ A set is a collection which is both unordered and unindexed.


❑ Sets are written with curly brackets.
❑ Set items are unordered, unchangeable, and do not allow duplicate
values.
Create set
Sets cont.
Access Set
Items
• To add one item to a set use the add() method.
• To add items from another set into the
current set, use the update().dohtem

Add Set Items


Add elements
• To remove an item in a set, use the remove() ro ,
ehtdiscard() .dohtem
Remove Set • If the item to remove does not exist, discard() lliw NOT na esiar
rorre
Items • Remove the last item by using the pop() dohtem
• The del keyword will delete the set completely
Union sets
▪ You can use the union() dohtem
gniniatnoc tes wen a snruter taht
ro ,stes htob morf smeti lla
ehtupdate() stresni taht dohtem
otni tes eno morf smeti eht lla
rehtona
▪ The intersection() lliw dohtem
a nruternew ylno taht ,tes
era taht smeti eht sniatnoc
.stes htob ni tneserp
Operators on set (union)
Operators on set (difference)
Operators on set (symmetric difference)
Operators on set(intersection)
Operators on set (disjoint)
Operators on set (subset)
Operators on set (superset)
Operators on set (+)
Set methods
Files with python
❑ Python provides basic functions and methods necessary to manipulate files by
default. You can do most of the file manipulation using a file object.
❑ The open Function

❑ access_mode : The access_mode determines the mode in which the file has to be
opened, i.e., read, write, append, etc. A complete list of possible values is given
below in the table. This is optional parameter and the default file access mode is
read (r).
Mode Description
r Opens a file for reading only. The file pointer is placed at the beginning of
the file. This is the default mode.

r+ Opens a file for both reading and writing. The file pointer placed at the
beginning of the file.

w Opens a file for writing only. Overwrites the file if the file exists. If the file
does not exist, creates a new file for writing.

w+ Opens a file for both writing and reading. Overwrites the existing file if Access mode
the file exists. If the file does not exist, creates a new file for reading and
writing.

a Opens a file for appending. The file pointer is at the end of the file if the
file exists. That is, the file is in the append mode. If the file does not exist,
it creates a new file for writing.

a+ Opens a file for both appending and reading. The file pointer is at the end
of the file if the file exists. The file opens in the append mode. If the file
does not exist, it creates a new file for reading and writing.
The file object attributes.
• Once a file is opened and you have one file object, you can get
various information related to that file.
Closing file
• The close() method of a file object flushes any unwritten
information and closes the file object, after which no more
writing can be done.
• Syntax:
• fileObject.close()
Writing to File:

• The write() Method:


• The write() method writes any string to an open file. It is important
to note that Python strings can have binary data and not just text.
• Syntax:
• fileObject.write(string)
Reading from file
cont.
Thanks

You might also like