Day 03 (Module # 2C - Part 2)
Day 03 (Module # 2C - Part 2)
Email: [email protected]
Module # 2C (Part-2) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
Sequence
Lists, tuples and strings are examples of sequences, but what are sequences and
what is so special about them?
The major features are membership tests, (i.e. the in and not in expressions)
and indexing operations, which allow us to fetch a particular item in the sequence
directly.
The three types of sequences mentioned above - lists, tuples and strings, also have
a slicing operation which allows us to retrieve a slice of the sequence i.e. a part of the
sequence.
# Slicing on a list #
print('Item 1 to 3 is', shoplist[1:3])
print('Item 2 to end is', shoplist[2:])
print('Item 1 to -1 is', shoplist[1:-1])
print('Item start to end is', shoplist[:])
# Slicing on a string #
print('characters 1 to 3 is', name[1:3])
print('characters 2 to end is', name[2:])
print('characters 1 to -1 is', name[1:-1])
print('characters start to end is', name[:])
Email: [email protected]
Module # 2C (Part-2) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
Output:
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Character 0 is s
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop
Set
Sets are unordered collections of simple objects. These are used when the existence
of an object in a collection is more important than the order or how many times it
occurs.
Using sets, you can test for membership, whether it is a subset of another set, find
the intersection between two sets, and so on.
Example:
>>> bri = set(['brazil', 'russia', 'india'])
>>> 'india' in bri
True
>>> 'usa' in bri
False
>>> bric = bri.copy()
>>> bric.add('china')
>>> bric.issuperset(bri)
True
Email: [email protected]
Module # 2C (Part-2) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
>>> bri.remove('russia')
>>> bri & bric # OR bri.intersection(bric)
{'brazil', 'india'}
References
When you create an object and assign it to a variable, the variable only refers to the
object and does not represent the object itself! That is, the variable name points to
that part of your computer's memory where the object is stored. This is
called binding the name to the object.
Generally, you don't need to be worried about this, but there is a subtle effect due to
references which you need to be aware of:
print('Simple Assignment')
shoplist = ['apple', 'mango', 'carrot', 'banana']
# mylist is just another name pointing to the same object!
mylist = shoplist
Email: [email protected]
Module # 2C (Part-2) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
Output:
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']
In fact, you've already been using a string method... the format method!
The strings that you use in programs are all objects of the class str. Some useful
methods of this class are demonstrated in the next example. For a complete list of
such methods, see help(str).
Example:
if name.startswith('Swa'):
print('Yes, the string starts with "Swa"')
if 'a' in name:
print('Yes, it contains the string "a"')
if name.find('war') != -1:
print('Yes, it contains the string "war"')
delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print(delimiter.join(mylist))
Email: [email protected]
Module # 2C (Part-2) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
Output:
$ python ds_str_methods.py
Modules:
You have seen how you can reuse code in your program by defining functions once.
What if you wanted to reuse a number of functions in other programs that you write?
As you might have guessed, the answer is modules.
There are various methods of writing modules, but the simplest way is to create a file
with a .py extension that contains functions and variables.
They are simply files that are imported into your main program. After importing a
module, you can use a module in much the same way you would an object: access
constants and functions using the dot-notation.
Example:
import math
Output:
pi is 3.141592653589793
cos(pi) is -1.0
Email: [email protected]
Module # 2C (Part-2) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez
TASK:
Question # 1:
Python comes with several standard modules that you can import into your program.
One of them is the math module, which you can use with import math. Use the
constants and functions found in the math module to perform the following actions:
Question # 2:
Modify the code below so that the phrase stored in my_string is converted to all lower
case letters and printed to the terminal. Hint: review the String Methods in the Python
Reference Guide to find a built-in method to do this for you.
Question # 3:
Write a Python function that takes a sequence of numbers and determines if all the
numbers are different from each other.
Email: [email protected]