Day 02 (Module # 2C - Part-1)
Day 02 (Module # 2C - Part-1)
Email: [email protected]
Module # 2C (Part-1) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
Data Structures
Data structures are basically just that - they are structures which can hold
some data together. In other words, they are used to store a collection of related
data.
There are four built-in data structures in Python - list, tuple, dictionary and set.
Objects
We have not talked about objects yet, but in reality, you've been using them all along.
The secret to Python is that everything is an object. That's right: everything, including
functions and integers.
An object is simply a collection of data stored somewhere in your computer's memory.
What makes an object special in a programming language is the ability for it to store
information and perform actions. We've already seen an object's ability to store data (for
example, a = 3 means that a is an integer object and stores the information 3).
When we use a variable i and assign a value to it, say integer 5 to it, you can think of
it as creating an object (i.e. instance) i of class (i.e. type) int. In fact, you can
read help(int) to understand this better.
Objects are given a set of functions as defined by their class, which acts as a blueprint--
telling the objects what they can and can't do or information it can and can't hold. When
we talk about functions within a class (or objects), we call them methods.
List:
A list is a data structure that holds an ordered collection of items i.e. you can store
a sequence of items in a list. This is easy to imagine if you can think of a shopping list
where you have a list of items to buy, except that you probably have each item on a
separate line in your shopping list whereas in Python you put commas in between
them.
Email: [email protected]
Module # 2C (Part-1) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
The list of items should be enclosed in square brackets so that Python understands
that you are specifying a list. Once you have created a list, you can add, remove or
search for items in the list. Since we can add and remove items, we say that a list is
a mutable data type i.e. this type can be altered.
Example:
# This is my shopping list
shoplist = ['apple', 'mango', 'carrot', 'banana']
Email: [email protected]
Module # 2C (Part-1) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
Output:
$ python ds_using_list.py
Tuple
Tuples are used to hold together multiple objects. Think of them as similar to lists, but
without the extensive functionality that the list class gives you. One major feature of
tuples is that they are immutable like strings i.e. you cannot modify tuples.
Tuples are defined by specifying items separated by commas within an optional pair
of parentheses.
Tuples are usually used in cases where a statement or a user-defined function can
safely assume that the collection of values (i.e. the tuple of values used) will not
change.
Email: [email protected]
Module # 2C (Part-1) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
Example:
# I would recommend always using parentheses
# to indicate start and end of tuple
# even though parentheses are optional.
# Explicit is better than implicit.
zoo = ('python', 'elephant', 'penguin')
print('Number of animals in the zoo is', len(zoo))
Output:
$ python ds_using_tuple.py
Email: [email protected]
Module # 2C (Part-1) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
Dictionary
A dictionary is like an address-book where you can find the address or contact details
of a person by knowing only his/her name i.e. we associate keys (name)
with values (details). Note that the key must be unique just like you cannot find out
the correct information if you have two persons with the exact same name.
Note that you can use only immutable objects (like strings) for the keys of a dictionary
but you can use either immutable or mutable objects for the values of the dictionary.
This basically translates to say that you should use only simple objects for keys.
Pairs of keys and values are specified in a dictionary by using the notation d = {key1
: value1, key2 : value2 }. Notice that the key-value pairs are separated by a colon
and the pairs are separated themselves by commas and all this is enclosed in a pair
of curly braces
Example:
ab = {
'Swaroop': '[email protected]',
'Larry': '[email protected]',
'Matsumoto': '[email protected]',
'Spammer': '[email protected]'
}
Email: [email protected]
Module # 2C (Part-1) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
if 'Guido' in ab:
print("\nGuido's address is", ab['Guido'])
Output:
$ python ds_using_dict.py
Swaroop's address is [email protected]
Email: [email protected]
Module # 2C (Part-1) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
TASK:
Question # 1:
Ask the user for two integers, and print the addition, subtraction, multiplication, division,
and modulo of those numbers.
Question # 2:
Write a Python function that takes a list of words and returns the length of the longest
one.
Question # 3:
Write a Python function that can find the median among three numbers.
Question # 4:
Question # 5:
Write a Python function that takes two lists and returns True if they have at least one
common member.
Question # 6:
Write a Python program to reverse a tuple (in both string and numbers).
Question # 7:
Email: [email protected]