0% found this document useful (0 votes)
9 views48 pages

IPP Module 2

This document provides an overview of Python's built-in data types, focusing on Lists, Tuples, Sets, and Dictionaries. It explains their characteristics, methods for accessing and modifying items, and how to manage collections of data effectively. The content is structured to assist learners in understanding the functionalities and applications of these data types in Python programming.

Uploaded by

GAJANAN M NAIK
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)
9 views48 pages

IPP Module 2

This document provides an overview of Python's built-in data types, focusing on Lists, Tuples, Sets, and Dictionaries. It explains their characteristics, methods for accessing and modifying items, and how to manage collections of data effectively. The content is structured to assist learners in understanding the functionalities and applications of these data types in Python programming.

Uploaded by

GAJANAN M NAIK
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/ 48

BPLCK105B/205B : Introduction to Python Programming

Module - 2
Lists: The List Data Type, Working with Lists, Augmented Assignment Operators, Methods, Example Program: Magic 8 Ball with a
List, List-like Types: Strings and Tuples, References,

Dictionaries and Structuring Data: The Dictionary Data Type, Pretty Printing, Using Data Structures to Model Real-World Things,
List
Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store collections of data, the other
3 are Tuple, Set, and Dictionary, all with different qualities and usage.
Lists are created using square brackets:
List Items
List items are ordered, changeable, and allow duplicate values. List items are
indexed, the first item has index [0], the second item has index [1] etc.

Ordered
When we say that lists are ordered, it means that the items have a defined order, and
that order will not change. If you add new items to a list, the new items will be placed at
the end of the list.

Changeable
The list is changeable, meaning that we can change, add, and remove items in a list after
it has been created.

Allow Duplicates
Since lists are indexed, lists can have items with the same value:
List Length
To determine how many items a list has, use the len() function:
List Items - Data Types
List items can be of any data type:

String, int and boolean data types:

A list can contain different data types:


type()
From Python's perspective, lists are defined as objects with the data type 'list'

The list() Constructor


It is also possible to use the list() constructor when creating a new list.
Python Collections (Arrays)
There are four collection data types in the Python
programming language:
•List is a collection which is ordered and changeable.
Allows duplicate members.
•Tuple is a collection which is ordered and unchangeable.
Allows duplicate members.
•Set is a collection which is unordered, unchangeable*,
and unindexed. No duplicate members.
•Dictionary is a collection which is ordered** and
changeable. No duplicate members.
Access Items
List items are indexed and you can access them by referring to the index
number:

Negative Indexing
Negative indexing means start from the end, -1 refers to the last item, -2 refers to the second last item etc.
Print the last item of the list:
Range of Indexes
You can specify a range of indexes by specifying where to start and where to end
the range.
When specifying a range, the return value will be a new list with the specified
items.
Range of Negative Indexes
Specify negative indexes if you want to start the search from the end of the list:

This example returns the items from "orange" (-4) to, but NOT including "mango" (-
1):
Check if Item Exists
To determine if a specified item is present in a list use the in keyword:

Change Item Value


To change the value of a specific item, refer to the index number:
Change a Range of Item Values
To change the value of items within a specific range, define a list with the new
values, and refer to the range of index numbers where you want to insert the
new values:
Insert Items
To insert a new list item, without replacing any of the existing values, we can use
the insert() method.
The insert() method inserts an item at the specified index:
Python - Add List Items

Append Items
To add an item to the end of the list, use the append() method:

Insert Items
To insert a list item at a specified index, use the insert() method. The insert() method inserts an
item at the specified index:
Extend List
To append elements from another list to the current list, use the extend() method.

Add Any Iterable


The extend() method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries etc.).
Remove Specified Item
The remove() method removes the specified item.

If there are more than one item with the specified value, the remove() method removes the first occurance:
Remove Specified Index
The pop() method removes the specified index.

If you do not specify the index, the pop() method removes the last item.

The del keyword also removes the specified index:

The del keyword can also delete the list completely.


Delete the entire list:

Clear the List


The clear() method empties the list. The list still remains, but it has no content.
Python Tuples

Tuples are used to store multiple items in a single variable.


Tuple is one of 4 built-in data types in Python used to store collections of data, the
other 3 are List, Set, and Dictionary, all with different qualities and usage.
A tuple is a collection which is ordered and unchangeable. Tuples are written with
round brackets.

Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.
Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.
Access Tuple Items
You can access tuple items by referring to the index number, inside square brackets:

Change Tuple Values


Once a tuple is created, you cannot change its values. Tuples are unchangeable,
or immutable as it also is called. But there is a workaround. You can convert the tuple
into a list, change the list, and convert the list back into a tuple.
Add Items
Since tuples are immutable, they do not have a built-in append() method, but there are other
ways to add items to a tuple.

Add tuple to a tuple. You are allowed to add tuples to tuples, so if you want to add
one item, (or many), create a new tuple with the item(s), and add it to the existing
tuple:
Loop Through a Tuple
You can loop through the tuple items by using a for loop.

Join Two Tuples


To join two or more tuples you can use the + operator:
Multiply Tuples
If you want to multiply the content of a tuple a given number of times, you can use the * operator:
Set
Sets are used to store multiple items in a single variable.
Set is one of 4 built-in data types in Python used to store collections of data, the
other 3 are List, Tuple, and Dictionary, all with different qualities and usage.
A set is a collection which is unordered, unchangeable*, and unindexed.
Duplicates Not Allowed
Sets cannot have two items with the same value.

Note: The values True and 1 are considered the same value in sets, and are treated as duplicates:

Note: The values False and 0 are considered the same value in sets, and are treated as duplicates:
To determine how many items a set has, use the len() function.

Access Items
You cannot access items in a set by referring to an index or a key.
But you can loop through the set items using a for loop, or ask if a specified value is
present in a set, by using the in keyword.
Check if "banana" is present in the set:

Change Items
Once a set is created, you cannot change its items, but you can add new items.
Add Items
Once a set is created, you cannot change its items, but you can add new items.
To add one item to a set use the add() method.
Add Sets
To add items from another set into the current set, use the update() method.

Add Any Iterable


The object in the update() method does not have to be a set, it can be any iterable object (tuples, lists, dictionaries
etc.).
Remove Item
To remove an item in a set, use the remove(), or the discard() method.

Remove "banana" by using the discard() method:


You can also use the pop() method to remove an item, but this
method will remove a random item, so you cannot be sure
what item that gets removed.
The return value of the pop() method is the removed item.
The clear() method empties the set:
The del keyword will delete the set completely:
Loop Items
You can loop through the set items by using a for loop:

Join Two Sets


There are several ways to join two or more sets in Python.
You can use the union() method that returns a new set containing all items from both
sets, or the update() method that inserts all the items from one set into another:
Python Dictionaries

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.
As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier,
dictionaries are unordered.
Dictionaries are written with curly brackets, and have keys and values:
Dictionary Items
Dictionary items are ordered, changeable, and does not allow duplicates.
Dictionary items are presented in key:value pairs, and can be referred to by
using the key name.

Ordered or Unordered?
As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries
are unordered.
When we say that dictionaries are ordered, it means that the items have a defined order,
and that order will not change.
Unordered means that the items does not have a defined order, you cannot
refer to an item by using an index.
Duplicates Not Allowed
Dictionaries cannot have two items with the same key:

Dictionary Length
To determine how many items a dictionary has, use the len() function:
Accessing Items
You can access the items of a dictionary by referring to its key name, inside
square brackets:
Change Values
You can change the value of a specific item by referring to its key name:
Update Dictionary
The update() method will update the dictionary with the items from the given argument.
The argument must be a dictionary, or an iterable object with key:value pairs.
Adding Items
Adding an item to the dictionary is done by using a new index
key and assigning a value to it:

Removing Items
There are several methods to remove items from a dictionary:
The popitem() method removes the last inserted item (in versions before
3.7, a random item is removed instead):

The del keyword removes the item with the specified key name:
Loop Through a Dictionary
You can loop through a dictionary by using a for loop.
When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to
return the values as well.

You can also use the values() method to return values of a dictionary:
You can use the keys() method to return the keys of a dictionary:

Loop through both keys and values, by using the items() method:
Copy a Dictionary
You cannot copy a dictionary simply by typing dict2 = dict1,
because: dict2 will only be a reference to dict1, and changes made
in dict1 will automatically also be made in dict2.
There are ways to make a copy, one way is to use the built-in Dictionary
method copy().

Make a copy of a dictionary with


the dict() function:

You might also like