0% found this document useful (0 votes)
10 views7 pages

Day 03 (Module # 2C - Part 2)

This document is a guide on data structures and modules in Python, focusing on sequences like lists, tuples, and strings, as well as sets and their operations. It explains indexing, slicing, and the concept of references in Python, along with examples of string methods and module usage. Additionally, it includes tasks for practicing the use of the math module and string manipulation methods.

Uploaded by

Haya
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)
10 views7 pages

Day 03 (Module # 2C - Part 2)

This document is a guide on data structures and modules in Python, focusing on sequences like lists, tuples, and strings, as well as sets and their operations. It explains indexing, slicing, and the concept of references in Python, along with examples of string methods and module usage. Additionally, it includes tasks for practicing the use of the math module and string manipulation methods.

Uploaded by

Haya
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/ 7

Module # 2C (Part-2) Prepared by:

Data Structure and Modules in Python M. Wasiq Pervez

RASPBERRY PI COURSE GUIDE

thingsRoam Academy Contact: +92-308-1222240 academy.thingsroam.com

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.

shoplist = ['apple', 'mango', 'carrot', 'banana']


name = 'swaroop'

# Indexing or 'Subscription' operation #


print('Item 0 is', shoplist[0])
print('Item 1 is', shoplist[1])
print('Item 2 is', shoplist[2])
print('Item 3 is', shoplist[3])
print('Item -1 is', shoplist[-1])
print('Item -2 is', shoplist[-2])
print('Character 0 is', name[0])

# 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[:])

thingsRoam Academy Contact: +92-308-1222240 academy.thingsroam.com

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

thingsRoam Academy Contact: +92-308-1222240 academy.thingsroam.com

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

# I purchased the first item, so I remove it from the list


del shoplist[0]

print('shoplist is', shoplist)


print('mylist is', mylist)
# Notice that both shoplist and mylist both print
# the same list without the 'apple' confirming that
# they point to the same object

print('Copy by making a full slice')


# Make a copy by doing a full slice
mylist = shoplist[:]
# Remove first item
del mylist[0]

print('shoplist is', shoplist)


print('mylist is', mylist)
# Notice that now the two lists are different

thingsRoam Academy Contact: +92-308-1222240 academy.thingsroam.com

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']

More About Strings


We have already discussed strings in detail earlier. What more can there be to know?
Well, did you know that strings are also objects and have methods which do
everything from checking part of a string to stripping spaces?

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:

# This is a string object


name = 'Swaroop'

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))

thingsRoam Academy Contact: +92-308-1222240 academy.thingsroam.com

Email: [email protected]
Module # 2C (Part-2) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez

Output:
$ python ds_str_methods.py

Yes, the string starts with "Swa"


Yes, it contains the string "a"
Yes, it contains the string "war"
Brazil_*_Russia_*_India_*_China

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.

 Use import to load a library module into a program’s memory.


 Then refer to things from the module as module_name.thing_name.
o Python uses . (dot) to mean “part of”.
 Using math, one of the modules in the standard library:

Example:

import math

print('pi is', math.pi)


print('cos(pi) is', math.cos(math.pi))

Output:

pi is 3.141592653589793
cos(pi) is -1.0

thingsRoam Academy Contact: +92-308-1222240 academy.thingsroam.com

Email: [email protected]
Module # 2C (Part-2) Prepared by:
Data Structure and Modules in Python M. Wasiq Pervez

The from..import statement


Example
from math import sqrt
print("Square root of 16 is", sqrt(16))

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:

 Print the ceiling of 3.456 (should be 4)


 Print the square root of 9216 (should be 96.0)
 Calculate and print the area of a circle whose radius is 2 (should be
12.566370614359172

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.

thingsRoam Academy Contact: +92-308-1222240 academy.thingsroam.com

Email: [email protected]

You might also like